Skip to content

Commit

Permalink
fix(web): controlled behavior with selectAllLabel
Browse files Browse the repository at this point in the history
  • Loading branch information
bietkul committed Jul 14, 2021
1 parent a60f4b8 commit 6c69bb6
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions packages/web/src/components/list/MultiList.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ class MultiList extends Component {
// value gets handled on the initial load and
// consecutive loads
const { currentValue } = this.state;
const value = Object.keys(currentValue).filter(item => currentValue[item]);
const value = Object.keys(currentValue).filter(
item => currentValue[item],
);
if (value.length) this.setValue(value, true);
},
);
Expand All @@ -133,7 +135,9 @@ class MultiList extends Component {
// value gets handled on the initial load and
// consecutive loads
const { currentValue } = this.state;
const value = Object.keys(currentValue).filter(item => currentValue[item]);
const value = Object.keys(currentValue).filter(
item => currentValue[item],
);
if (value.length) this.setValue(value, true);
},
);
Expand Down Expand Up @@ -169,7 +173,6 @@ class MultiList extends Component {
selectedValue = [selectAllLabel];
}
}

if (this.props.value !== prevProps.value) {
this.setValue(this.props.value, true);
} else if (
Expand Down Expand Up @@ -277,7 +280,6 @@ class MultiList extends Component {
const { selectAllLabel } = props;
let { currentValue } = this.state;
let finalValues = null;

if (
selectAllLabel
&& ((Array.isArray(value) && value.includes(selectAllLabel))
Expand Down Expand Up @@ -407,7 +409,7 @@ class MultiList extends Component {
});
} else {
this.state = {
...this.state || {},
...(this.state || {}),
options: [],
};
}
Expand Down Expand Up @@ -465,11 +467,36 @@ class MultiList extends Component {
if (isEvent(e)) {
currentValue = e.target.value;
}
const { value, onChange } = this.props;
const { value, onChange, selectAllLabel } = this.props;
if (value === undefined) {
this.setValue(currentValue);
} else if (onChange) {
onChange(parseValueArray(this.props.value, currentValue));
let valueToSet = parseValueArray(value, currentValue);
if (selectAllLabel) {
// Remove selectAllLabel if any other option is selected
if (currentValue === selectAllLabel) {
if (
value === selectAllLabel
|| (Array.isArray(value) && value.includes(selectAllLabel))
) {
valueToSet = [];
} else {
const options = [];
this.state.options.forEach((item) => {
options.unshift(item.key);
});
options.unshift(selectAllLabel);
valueToSet = options;
}
} else if (
Array.isArray(valueToSet)
&& valueToSet.length > 1
&& valueToSet.includes(selectAllLabel)
) {
valueToSet = valueToSet.filter(val => val !== selectAllLabel);
}
}
onChange(valueToSet);
}
};

Expand Down

0 comments on commit 6c69bb6

Please sign in to comment.