Max Red - beautify code and set default selected indices to an empty array [skip ci]

This commit is contained in:
ivan 2017-01-13 10:33:17 -03:00
parent 6fc460d4b6
commit 139a474693
2 changed files with 6 additions and 7 deletions

View File

@ -11,7 +11,7 @@ class ToggleList extends React.Component {
}; };
state = { state = {
selected: [1, 3] selected: []
}; };
render() { render() {
@ -35,26 +35,25 @@ class ToggleList extends React.Component {
let classes = { let classes = {
'toggle-list__item': true, 'toggle-list__item': true,
'toggle-list__first-item': (index === 0), 'toggle-list__first-item': (index === 0),
'toggle-list__selected': (_.includes(this.state.selected, index)) 'toggle-list__selected': _.includes(this.state.selected, index)
}; };
return classNames(classes); return classNames(classes);
} }
selectItem(index) { selectItem(index) {
let actual = _.clone(this.state.selected); let newSelected = _.clone(this.state.selected);
_.includes(this.state.selected, index) ? _.remove(actual, t => t == index) : actual.push(index); _.includes(this.state.selected, index) ? _.remove(newSelected, _index => _index == index) : newSelected.push(index);
console.log(actual);
this.setState({ this.setState({
selected: actual selected: newSelected
}); });
if (this.props.onChange) { if (this.props.onChange) {
this.props.onChange({ this.props.onChange({
target: { target: {
value: actual value: newSelected
} }
}); });
} }