Skip to content

Commit f647cad

Browse files
committed
Fix setValue incorrectly assuming there is a change when there is none
When using the component with multiple set to true and there are no option selected, the setValue call will incorrectly assume there is a change (and trigger a change event) because jquery val method returns null instead of an empty array. This can trigger an infinite loop if you have a controlled component where the state of the host component is synchronized with the selected elements (hosting component will receive the change, update its state and then send the updated state = [] to the component) Fix consist in checking if the select is in multiple mode and if so, ensuring we return an empty array in case there is no option selected.
1 parent 8acc158 commit f647cad

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

src/components/Select2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export default class Select2 extends Component {
6767
}
6868

6969
setValue(value) {
70-
const elVal = this.el.val();
70+
const elVal = this.props.multiple ? this.el.val() || [] : this.el.val();
7171
if (!shallowEqualFuzzy(elVal, value)) {
7272
this.el.val(value).trigger('change');
7373
}

0 commit comments

Comments
 (0)