Allows reinitializing Select2 upon component rerender

This commit is contained in:
Vlad
2018-01-31 17:53:17 -05:00
parent b88dec06d9
commit 63000c48c4

View File

@@ -27,14 +27,20 @@ define([
}
},
componentDidUpdate: function (prevProps) {
if (
(this.props.item !== undefined && prevProps.item !== undefined)
if ((this.props.item !== undefined && prevProps.item !== undefined)
&& (this.props.item.id !== prevProps.item.id)
) {
jQuery(`#${this.refs.select.id}`)
.val(this.getSelectedValues())
.trigger('change');
}
if (this.isSelect2Initialized() &&
(this.getFieldId(this.props) !== this.getFieldId(prevProps)) &&
this.props.field.resetSelect2OnUpdate !== undefined
) {
this.resetSelect2();
}
},
componentWillUnmount: function () {
if (this.allowMultipleValues() || this.props.field.forceSelect2) {
@@ -45,6 +51,10 @@ define([
const props = data || this.props;
return props.field.id || props.field.name;
},
resetSelect2: function () {
this.destroySelect2();
this.setupSelect2();
},
destroySelect2: function () {
if (this.isSelect2Initialized()) {
jQuery(`#${this.refs.select.id}`).select2('destroy');