From 9223ce08302d4edad5f1bfe93a46024c9a0e14ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E6=9C=88=E7=9A=84=E5=B0=BE=E5=B7=B4?= Date: Mon, 22 Jan 2018 14:34:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9TagSelect=E4=B8=BA=E5=8F=97?= =?UTF-8?q?=E6=8E=A7=E7=BB=84=E4=BB=B6=20(#763)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修改TagSekect组件通过value控制选中项 * 修改TagSelect组件说明文档 * TagSelect组件添加defaultValue的支持 * 在文档中添加defaultValue说明 * 完善tagSelect * 规范TagSelect中state命名,保持统一 * 修改接收到下一个props时更新state的逻辑判断 * 封装onChange方法,减少重复的逻辑代码 * 避免直接修改state --- src/components/TagSelect/index.d.ts | 3 +- src/components/TagSelect/index.js | 60 +++++++++++++++-------------- src/components/TagSelect/index.md | 4 +- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/components/TagSelect/index.d.ts b/src/components/TagSelect/index.d.ts index 632e556a..e9decee1 100644 --- a/src/components/TagSelect/index.d.ts +++ b/src/components/TagSelect/index.d.ts @@ -2,10 +2,11 @@ import * as React from 'react'; export interface TagSelectProps { onChange?: (value: Array) => void; expandable?: boolean; + value?: Array| Array; style?: React.CSSProperties; } export interface TagSelectOptionProps { - value: string; + value: string| number; style?: React.CSSProperties; } diff --git a/src/components/TagSelect/index.js b/src/components/TagSelect/index.js index 5ce15693..40c38bc7 100644 --- a/src/components/TagSelect/index.js +++ b/src/components/TagSelect/index.js @@ -21,21 +21,30 @@ TagSelectOption.isTagSelectOption = true; class TagSelect extends Component { state = { expand: false, - checkedTags: this.props.defaultValue || [], + value: this.props.value || this.props.defaultValue || [], }; + componentWillReceiveProps(nextProps) { + if ('value' in nextProps) { + this.setState({ value: nextProps.value }); + } + } - onSelectAll = (checked) => { + onChange = (value) => { const { onChange } = this.props; + if (!('value' in this.props)) { + this.setState({ value }); + } + if (onChange) { + onChange(value); + } + } + + onSelectAll = (checked) => { let checkedTags = []; if (checked) { checkedTags = this.getAllTags(); } - - this.setState({ checkedTags }); - - if (onChange) { - onChange(checkedTags); - } + this.onChange(checkedTags); } getAllTags() { @@ -48,8 +57,7 @@ class TagSelect extends Component { } handleTagChange = (value, checked) => { - const { onChange } = this.props; - const { checkedTags } = this.state; + const checkedTags = [...this.state.value]; const index = checkedTags.indexOf(value); if (checked && index === -1) { @@ -57,12 +65,7 @@ class TagSelect extends Component { } else if (!checked && index > -1) { checkedTags.splice(index, 1); } - - this.setState({ checkedTags }); - - if (onChange) { - onChange(checkedTags); - } + this.onChange(checkedTags); } handleExpand = () => { @@ -78,10 +81,10 @@ class TagSelect extends Component { } render() { - const { checkedTags, expand } = this.state; + const { value, expand } = this.state; const { children, className, style, expandable } = this.props; - const checkedAll = this.getAllTags().length === checkedTags.length; + const checkedAll = this.getAllTags().length === value.length; const cls = classNames(styles.tagSelect, className, { [styles.hasExpandTag]: expandable, @@ -97,16 +100,17 @@ class TagSelect extends Component { 全部 { - checkedTags && React.Children.map(children, (child) => { - if (this.isTagSelectOption(child)) { - return React.cloneElement(child, { - key: `tag-select-${child.props.value}`, - checked: checkedTags.indexOf(child.props.value) > -1, - onChange: this.handleTagChange, - }); - } - return child; - }) + value && React.Children.map(children, (child) => { + if (this.isTagSelectOption(child)) { + return React.cloneElement(child, { + key: `tag-select-${child.props.value}`, + value: child.props.value, + checked: value.indexOf(child.props.value) > -1, + onChange: this.handleTagChange, + }); + } + return child; + }) } { expandable && ( diff --git a/src/components/TagSelect/index.md b/src/components/TagSelect/index.md index 6b39e4f5..608219cd 100644 --- a/src/components/TagSelect/index.md +++ b/src/components/TagSelect/index.md @@ -15,6 +15,8 @@ order: 13 | 参数 | 说明 | 类型 | 默认值 | |----------|------------------------------------------|-------------|-------| +| value |选中的项 |string[] \| number[] | | +| defaultValue |默认选中的项 |string[] \| number[] | | | onChange | 标签选择的回调函数 | Function(checkedTags) | | | expandable | 是否展示 `展开/收起` 按钮 | Boolean | false | @@ -23,5 +25,5 @@ order: 13 | 参数 | 说明 | 类型 | 默认值 | |----------|------------------------------------------|-------------|-------| -| value | TagSelect的值 | Function(checkedTags) | - | +| value | TagSelect的值 | string\| number | - | | children | tag的内容 | string \| ReactNode | - | -- GitLab