Allows reinitializing Select2 upon component rerender
This commit is contained in:
@@ -27,14 +27,20 @@ define([
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
componentDidUpdate: function (prevProps) {
|
componentDidUpdate: function (prevProps) {
|
||||||
if (
|
if ((this.props.item !== undefined && prevProps.item !== undefined)
|
||||||
(this.props.item !== undefined && prevProps.item !== undefined)
|
|
||||||
&& (this.props.item.id !== prevProps.item.id)
|
&& (this.props.item.id !== prevProps.item.id)
|
||||||
) {
|
) {
|
||||||
jQuery(`#${this.refs.select.id}`)
|
jQuery(`#${this.refs.select.id}`)
|
||||||
.val(this.getSelectedValues())
|
.val(this.getSelectedValues())
|
||||||
.trigger('change');
|
.trigger('change');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.isSelect2Initialized() &&
|
||||||
|
(this.getFieldId(this.props) !== this.getFieldId(prevProps)) &&
|
||||||
|
this.props.field.resetSelect2OnUpdate !== undefined
|
||||||
|
) {
|
||||||
|
this.resetSelect2();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
componentWillUnmount: function () {
|
componentWillUnmount: function () {
|
||||||
if (this.allowMultipleValues() || this.props.field.forceSelect2) {
|
if (this.allowMultipleValues() || this.props.field.forceSelect2) {
|
||||||
@@ -45,6 +51,10 @@ define([
|
|||||||
const props = data || this.props;
|
const props = data || this.props;
|
||||||
return props.field.id || props.field.name;
|
return props.field.id || props.field.name;
|
||||||
},
|
},
|
||||||
|
resetSelect2: function () {
|
||||||
|
this.destroySelect2();
|
||||||
|
this.setupSelect2();
|
||||||
|
},
|
||||||
destroySelect2: function () {
|
destroySelect2: function () {
|
||||||
if (this.isSelect2Initialized()) {
|
if (this.isSelect2Initialized()) {
|
||||||
jQuery(`#${this.refs.select.id}`).select2('destroy');
|
jQuery(`#${this.refs.select.id}`).select2('destroy');
|
||||||
|
Reference in New Issue
Block a user