|
|
|
@ -19,13 +19,6 @@ TagSelectOption.defaultProps = { |
|
|
|
displayName: 'TagSelectOption', |
|
|
|
}; |
|
|
|
|
|
|
|
const TagSelectExpand = ({ children }) => ( |
|
|
|
<div>{children}</div> |
|
|
|
); |
|
|
|
TagSelectExpand.defaultProps = { |
|
|
|
displayName: 'TagSelectExpand', |
|
|
|
}; |
|
|
|
|
|
|
|
class TagSelect extends PureComponent { |
|
|
|
static defaultProps = { |
|
|
|
initialValue: [], |
|
|
|
@ -40,18 +33,13 @@ class TagSelect extends PureComponent { |
|
|
|
onSelectAll = (checked) => { |
|
|
|
const { onChange } = this.props; |
|
|
|
let checkedTags = []; |
|
|
|
let expanded = this.state.expand; |
|
|
|
|
|
|
|
if (checked) { |
|
|
|
const tags = this.getAllTags(); |
|
|
|
checkedTags = tags.list; |
|
|
|
expanded = tags.expand; |
|
|
|
checkedTags = this.getAllTags(); |
|
|
|
} |
|
|
|
|
|
|
|
this.setState({ |
|
|
|
checkedAll: checked, |
|
|
|
checkedTags, |
|
|
|
expand: expanded, |
|
|
|
}); |
|
|
|
|
|
|
|
if (onChange) { |
|
|
|
@ -60,21 +48,11 @@ class TagSelect extends PureComponent { |
|
|
|
} |
|
|
|
|
|
|
|
getAllTags() { |
|
|
|
let { expand } = this.state; |
|
|
|
const { children } = this.props; |
|
|
|
|
|
|
|
let checkedTags = children.filter(child => child.props.displayName === 'TagSelectOption').map(child => child.props.value); |
|
|
|
const expandChild = children.filter(child => child.props.displayName === 'TagSelectExpand')[0]; |
|
|
|
if (expandChild) { |
|
|
|
checkedTags = checkedTags.concat( |
|
|
|
expandChild.props.children.map(child => child.props.value) |
|
|
|
); |
|
|
|
expand = true; |
|
|
|
} |
|
|
|
return { |
|
|
|
list: checkedTags, |
|
|
|
expand, |
|
|
|
}; |
|
|
|
const checkedTags = children |
|
|
|
.filter(child => child.props.displayName === 'TagSelectOption') |
|
|
|
.map(child => child.props.value); |
|
|
|
return checkedTags; |
|
|
|
} |
|
|
|
|
|
|
|
handleTagChange = (value, checked) => { |
|
|
|
@ -90,13 +68,8 @@ class TagSelect extends PureComponent { |
|
|
|
|
|
|
|
const tags = this.getAllTags(); |
|
|
|
|
|
|
|
let checkedAll = false; |
|
|
|
if (tags.list.length === checkedTags.length) { |
|
|
|
checkedAll = true; |
|
|
|
} |
|
|
|
|
|
|
|
this.setState({ |
|
|
|
checkedAll, |
|
|
|
checkedAll: tags.length === checkedTags.length, |
|
|
|
checkedTags, |
|
|
|
}); |
|
|
|
|
|
|
|
@ -113,50 +86,34 @@ class TagSelect extends PureComponent { |
|
|
|
|
|
|
|
render() { |
|
|
|
const { checkedTags, checkedAll, expand } = this.state; |
|
|
|
const { children, className, style } = this.props; |
|
|
|
|
|
|
|
const expandNode = children.filter(child => child.props.displayName === 'TagSelectExpand')[0]; |
|
|
|
const { children, className, style, expandable } = this.props; |
|
|
|
|
|
|
|
const cls = classNames(styles.tagSelect, className, { |
|
|
|
[styles.expandTag]: expandNode, |
|
|
|
[styles.hasExpandTag]: expandable, |
|
|
|
[styles.expanded]: expand, |
|
|
|
}); |
|
|
|
|
|
|
|
return ( |
|
|
|
<div className={cls} style={style}> |
|
|
|
<div> |
|
|
|
<CheckableTag |
|
|
|
checked={checkedAll} |
|
|
|
key="tag-select-__all__" |
|
|
|
onChange={this.onSelectAll} |
|
|
|
> |
|
|
|
全部 |
|
|
|
</CheckableTag> |
|
|
|
{ |
|
|
|
children.filter(child => child.props.displayName === 'TagSelectOption').map(child => React.cloneElement(child, { |
|
|
|
key: `tag-select-${child.props.value}`, |
|
|
|
checked: checkedTags.indexOf(child.props.value) > -1, |
|
|
|
onChange: this.handleTagChange, |
|
|
|
})) |
|
|
|
} |
|
|
|
{ |
|
|
|
expandNode && ( |
|
|
|
<a className={styles.trigger} onClick={this.handleExpand}> |
|
|
|
{ expand ? '收起' : '展开'} <Icon type={expand ? 'up' : 'down'} /> |
|
|
|
</a> |
|
|
|
) |
|
|
|
} |
|
|
|
</div> |
|
|
|
<CheckableTag |
|
|
|
checked={checkedAll} |
|
|
|
key="tag-select-__all__" |
|
|
|
onChange={this.onSelectAll} |
|
|
|
> |
|
|
|
全部 |
|
|
|
</CheckableTag> |
|
|
|
{ |
|
|
|
children.filter(child => child.props.displayName === 'TagSelectOption').map(child => React.cloneElement(child, { |
|
|
|
key: `tag-select-${child.props.value}`, |
|
|
|
checked: checkedTags.indexOf(child.props.value) > -1, |
|
|
|
onChange: this.handleTagChange, |
|
|
|
})) |
|
|
|
} |
|
|
|
{ |
|
|
|
expandNode && ( |
|
|
|
<div className={expand ? styles.expand : styles.fold}> |
|
|
|
{ |
|
|
|
expandNode.props.children.map(child => React.cloneElement(child, { |
|
|
|
key: `tag-select-${child.props.value}`, |
|
|
|
checked: checkedTags.indexOf(child.props.value) > -1, |
|
|
|
onChange: this.handleTagChange, |
|
|
|
})) |
|
|
|
} |
|
|
|
</div> |
|
|
|
expandable && ( |
|
|
|
<a className={styles.trigger} onClick={this.handleExpand}> |
|
|
|
{ expand ? '收起' : '展开'} <Icon type={expand ? 'up' : 'down'} /> |
|
|
|
</a> |
|
|
|
) |
|
|
|
} |
|
|
|
</div> |
|
|
|
@ -165,6 +122,5 @@ class TagSelect extends PureComponent { |
|
|
|
} |
|
|
|
|
|
|
|
TagSelect.Option = TagSelectOption; |
|
|
|
TagSelect.Expand = TagSelectExpand; |
|
|
|
|
|
|
|
export default TagSelect; |
|
|
|
|