Skip to content

Commit

Permalink
feat(prop-types): 添加props的参数类型校验
Browse files Browse the repository at this point in the history
  • Loading branch information
beth committed May 19, 2018
1 parent f270360 commit 970cce7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 24 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"react-scripts": "1.1.4"
},
"dependencies": {
"classnames": "^2.2.5"
"classnames": "^2.2.5",
"prop-types": "^15.6.1"
}
}
11 changes: 6 additions & 5 deletions src/component/Head/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { PropTypes, Component } from 'react';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import './index.css';

class Head extends Component {
// static propTypes = {
// title: PropTypes.string,
// clearHandle: PropTypes.func,
// }
static propTypes = {
title: PropTypes.string,
clearHandle: PropTypes.func,
}

static defaultProps = {
title: '我是默认名字'
Expand Down
11 changes: 6 additions & 5 deletions src/component/Menu/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import React, { PropTypes, Component } from 'react';
import React, { Component } from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import Head from '../Head';
import Tree from '../Tree';
import TreeNode from '../Tree/treeNode';
import './index.css';

class Menu extends Component {
// static propTypes = {
// title: PropTypes.string,
// data: PropTypes.ary,
// }
static propTypes = {
title: PropTypes.string,
data: PropTypes.ary,
}

constructor(props) {
super();
Expand Down
48 changes: 42 additions & 6 deletions src/component/Tree/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
import React, { PropTypes, Component } from 'react';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import './index.css';

export const contextTypes = {
bTree: PropTypes.shape({
root: PropTypes.object,
checkable: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.node,
]),
filterTreeNode: PropTypes.func,
renderTreeNode: PropTypes.func,

isKeyChecked: PropTypes.func,

onNodeClick: PropTypes.func,
onNodeExpand: PropTypes.func,
onNodeSelect: PropTypes.func,
onBatchNodeCheck: PropTypes.func,
onCheckConductFinished: PropTypes.func,
})
}

class Tree extends Component {
// static propTypes = {
// className: PropTypes.string,
Expand All @@ -10,6 +31,8 @@ class Tree extends Component {
// children: PropTypes.any
// }

static childContextTypes = contextTypes;

constructor(props) {
super();
this.state = {
Expand All @@ -28,12 +51,16 @@ class Tree extends Component {
}

getChildContext() {
const {} = this.props;
const {
checkable, filterTreeNode
} = this.props;

return {
tree: {
bTree: {
// root: this,
checkable,

filterTreeNode,
renderTreeNode: this.renderTreeNode,
isKeyChecked: this.isKeyChecked,

Expand All @@ -46,8 +73,18 @@ class Tree extends Component {
}
}

onNodeClick = () => {}
onNodeSelect = () => {}
onNodeClick = (e, treeNode) => {
console.log('onNodeSelect');
const { onClick } = this.props;
if (onClick) {
onClick(e, treeNode);
}
}

onNodeSelect = (e, treeNode) => {
console.log('onNodeSelect');
}

onNodeContextMenu = () => {}
onBatchNodeCheck = () => {}
onCheckConductFinished = () => {}
Expand Down Expand Up @@ -78,7 +115,6 @@ class Tree extends Component {
}

render() {
console.log(React.Children, '===');
return (
<div
className={classNames({
Expand Down
15 changes: 8 additions & 7 deletions src/component/Tree/treeNode.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import React, { PropTypes, Component } from 'react';
import React, { Component } from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import {
toArray,
getNodeChildren,
} from './utils';
import './index.css';

class TreeNode extends Component {
// static propTypes = {
// currentIndex: PropTypes.string,
// className: PropTypes.string,
// title: PropTypes.any,
// children: PropTypes.any
// }
static propTypes = {
currentIndex: PropTypes.string,
className: PropTypes.string,
title: PropTypes.any,
children: PropTypes.any
}

renderCheckbox = () => {
return (
Expand Down

0 comments on commit 970cce7

Please sign in to comment.