Commit f2817dfd authored by afc163's avatar afc163

refactor TagSelect state, close #161

parent 7815eecd
...@@ -15,19 +15,17 @@ const TagSelectOption = ({ children, checked, onChange, value }) => ( ...@@ -15,19 +15,17 @@ const TagSelectOption = ({ children, checked, onChange, value }) => (
{children} {children}
</CheckableTag> </CheckableTag>
); );
TagSelectOption.defaultProps = {
displayName: 'TagSelectOption', TagSelectOption.isTagSelectOption = true;
};
class TagSelect extends Component { class TagSelect extends Component {
static defaultProps = { static defaultProps = {
initialValue: [], defaultValue: [],
}; };
state = { state = {
checkedAll: false,
expand: false, expand: false,
checkedTags: this.props.initialValue || [], checkedTags: this.props.defaultValue || [],
}; };
onSelectAll = (checked) => { onSelectAll = (checked) => {
...@@ -37,10 +35,7 @@ class TagSelect extends Component { ...@@ -37,10 +35,7 @@ class TagSelect extends Component {
checkedTags = this.getAllTags(); checkedTags = this.getAllTags();
} }
this.setState({ this.setState({ checkedTags });
checkedAll: checked,
checkedTags,
});
if (onChange) { if (onChange) {
onChange(checkedTags); onChange(checkedTags);
...@@ -50,7 +45,7 @@ class TagSelect extends Component { ...@@ -50,7 +45,7 @@ class TagSelect extends Component {
getAllTags() { getAllTags() {
const { children } = this.props; const { children } = this.props;
const checkedTags = children const checkedTags = children
.filter(child => child.props.displayName === 'TagSelectOption') .filter(child => this.isTagSelectOption(child))
.map(child => child.props.value); .map(child => child.props.value);
return checkedTags; return checkedTags;
} }
...@@ -66,12 +61,7 @@ class TagSelect extends Component { ...@@ -66,12 +61,7 @@ class TagSelect extends Component {
checkedTags.splice(index, 1); checkedTags.splice(index, 1);
} }
const tags = this.getAllTags(); this.setState({ checkedTags });
this.setState({
checkedAll: tags.length === checkedTags.length,
checkedTags,
});
if (onChange) { if (onChange) {
onChange(checkedTags); onChange(checkedTags);
...@@ -84,10 +74,18 @@ class TagSelect extends Component { ...@@ -84,10 +74,18 @@ class TagSelect extends Component {
}); });
} }
isTagSelectOption = (node) => {
return node && node.type && (
node.type.isTagSelectOption || node.type.displayName === 'TagSelectOption'
);
}
render() { render() {
const { checkedTags, checkedAll, expand } = this.state; const { checkedTags, expand } = this.state;
const { children, className, style, expandable } = this.props; const { children, className, style, expandable } = this.props;
const checkedAll = this.getAllTags().length === checkedTags.length;
const cls = classNames(styles.tagSelect, className, { const cls = classNames(styles.tagSelect, className, {
[styles.hasExpandTag]: expandable, [styles.hasExpandTag]: expandable,
[styles.expanded]: expand, [styles.expanded]: expand,
...@@ -103,16 +101,22 @@ class TagSelect extends Component { ...@@ -103,16 +101,22 @@ class TagSelect extends Component {
全部 全部
</CheckableTag> </CheckableTag>
{ {
checkedTags && children.filter(child => child.props.displayName === 'TagSelectOption').map(child => React.cloneElement(child, { checkedTags && children
key: `tag-select-${child.props.value}`, .map((child) => {
checked: checkedTags.indexOf(child.props.value) > -1, if (this.isTagSelectOption(child)) {
onChange: this.handleTagChange, return React.cloneElement(child, {
})) key: `tag-select-${child.props.value}`,
checked: checkedTags.indexOf(child.props.value) > -1,
onChange: this.handleTagChange,
});
}
return child;
})
} }
{ {
expandable && ( expandable && (
<a className={styles.trigger} onClick={this.handleExpand}> <a className={styles.trigger} onClick={this.handleExpand}>
{ expand ? 'ζ”Άθ΅·' : '展开'} <Icon type={expand ? 'up' : 'down'} /> {expand ? 'ζ”Άθ΅·' : '展开'} <Icon type={expand ? 'up' : 'down'} />
</a> </a>
) )
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment