This commit is contained in:
Jonathan Labreuille
2016-04-26 12:43:48 +02:00
parent 256bca8ed9
commit 9684c88651

View File

@@ -13,14 +13,22 @@ function(
getInitialState: function() { getInitialState: function() {
return { return {
items: [], items: [],
initialized: false select2: false
}; };
}, },
componentWillMount: function() { componentWillMount: function() {
this.loadCachedItems(); this.loadCachedItems();
}, },
allowMultipleValues: function() {
return (this.props.field.multiple === true);
},
isSelect2Initialized: function() {
return (this.state.select2 === true);
},
componentDidMount: function() { componentDidMount: function() {
if(this.allowMultipleValues()) {
this.setupSelect2(); this.setupSelect2();
}
}, },
componentDidUpdate: function(prevProps, prevState) { componentDidUpdate: function(prevProps, prevState) {
if( if(
@@ -33,22 +41,17 @@ function(
} }
}, },
componentWillUnmount: function() { componentWillUnmount: function() {
if( if(this.allowMultipleValues()) {
!this.props.field.multiple this.destroySelect2();
|| this.state.initialized === false
|| this.refs.select === undefined
) {
return;
} }
},
destroySelect2: function() {
if(this.isSelect2Initialized()) {
jQuery('#'+this.refs.select.id).select2('destroy'); jQuery('#'+this.refs.select.id).select2('destroy');
}
}, },
setupSelect2: function() { setupSelect2: function() {
if( if(this.isSelect2Initialized()) {
!this.props.field.multiple
|| this.state.initialized === true
|| this.refs.select === undefined
) {
return; return;
} }
@@ -80,7 +83,7 @@ function(
select2.on('change', this.handleChange); select2.on('change', this.handleChange);
this.setState({ initialized: true }); this.setState({ select2: true });
}, },
getSelectedValues: function() { getSelectedValues: function() {
if(this.props.field['selected'] !== undefined) { if(this.props.field['selected'] !== undefined) {