Skip to content

Commit

Permalink
fix(Select): select all doesn't work under control mode
Browse files Browse the repository at this point in the history
  • Loading branch information
youluna committed Mar 14, 2019
1 parent d19f167 commit a1519d2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docs/config-provider/demo/error-boundary.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const fallbackUI = (props) => {
return <span style={{color: 'red'}}>{error.toString()}</span>;
};

export default class App extends React.Component {
class App extends React.Component {
state = {
throwError: false
};
Expand Down
22 changes: 22 additions & 0 deletions docs/select/demo/multiple.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,33 @@ function handleChange(value) {
console.log(value);
}


class Demo extends React.Component{
constructor(props) {
super(props)
this.state = {
value: []
}
}

handleChange = (value) => {
this.setState({value})
}

render() {
return (
<Select hasSelectAll value={this.state.value} mode="multiple" onChange={this.handleChange} dataSource={dataSource} style={{width: 200}} />)
}
}


ReactDOM.render(
<div>
<Select mode="multiple" onChange={handleChange} dataSource={dataSource} style={{width: 200}} />
&nbsp;&nbsp;&nbsp;&nbsp;
<Select hasSelectAll mode="multiple" onChange={handleChange} dataSource={dataSource} style={{width: 200}} />
&nbsp;&nbsp;&nbsp;&nbsp;
<Demo />
</div>
, mountNode);
````
46 changes: 26 additions & 20 deletions src/select/base.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ export default class Base extends React.Component {

componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
this.updateSelectAllYet(nextProps.value);

this.setState({
value: nextProps.value,
});
Expand Down Expand Up @@ -305,29 +307,33 @@ export default class Base extends React.Component {
}
}

updateSelectAllYet(value) {
// multiple mode
// is current state select all or not
this.selectAllYet = false;
if (Array.isArray(value)) {
const selectAllValues = this.dataStore
.getEnableDS()
.map(item => item.value);

if (selectAllValues.length <= value.length) {
this.selectAllYet = true;

selectAllValues.forEach(val => {
if (value.indexOf(val) === -1) {
this.selectAllYet = false;
return;
}
});
}
}
}

handleChange(value, ...args) {
this.updateSelectAllYet(value);

// 非受控模式清空内部数据
if (!('value' in this.props)) {
// multiple mode
// is current state select all or not
this.selectAllYet = false;
if (Array.isArray(value)) {
const selectAllValues = this.dataStore
.getEnableDS()
.map(item => item.value);

if (selectAllValues.length <= value.length) {
this.selectAllYet = true;

selectAllValues.forEach(val => {
if (value.indexOf(val) === -1) {
this.selectAllYet = false;
return;
}
});
}
}

this.setState({
value: value,
});
Expand Down

0 comments on commit a1519d2

Please sign in to comment.