fixed multiple select component

This commit is contained in:
Jonathan Labreuille
2015-10-09 14:48:54 +02:00
parent be2c35c13e
commit ed9407a890
7 changed files with 102 additions and 65 deletions

View File

@ -4,7 +4,8 @@ define([
'form/fields/textarea.jsx',
'form/fields/select.jsx',
'form/fields/radio.jsx',
'form/fields/checkbox.jsx'
'form/fields/checkbox.jsx',
'form/fields/selection.jsx'
],
function(
React,
@ -12,7 +13,8 @@ function(
FormFieldTextarea,
FormFieldSelect,
FormFieldRadio,
FormFieldCheckbox
FormFieldCheckbox,
FormFieldSelection
) {
var FormField = React.createClass({
renderField: function(data, inline = false) {
@ -26,29 +28,33 @@ function(
var field = false;
if(data.field['field'] !== undefined) {
field = data.field.field;
} else{
switch(data.field.type) {
case 'text':
field = (<FormFieldText {...data} />);
break;
data.field = jQuery.merge(data.field, data.field.field);
}
case 'textarea':
field = (<FormFieldTextarea {...data} />);
break;
switch(data.field.type) {
case 'text':
field = (<FormFieldText {...data} />);
break;
case 'select':
field = (<FormFieldSelect {...data} />);
break;
case 'textarea':
field = (<FormFieldTextarea {...data} />);
break;
case 'radio':
field = (<FormFieldRadio {...data} />);
break;
case 'select':
field = (<FormFieldSelect {...data} />);
break;
case 'checkbox':
field = (<FormFieldCheckbox {...data} />);
break;
}
case 'radio':
field = (<FormFieldRadio {...data} />);
break;
case 'checkbox':
field = (<FormFieldCheckbox {...data} />);
break;
case 'selection':
field = (<FormFieldSelection {...data} />);
break;
}
if(inline === true) {
@ -75,7 +81,8 @@ function(
return this.renderField({
index: index,
field: subfield,
item: this.props.item
item: this.props.item,
onValueChange: this.props.onValueChange || false
});
}.bind(this));
} else {

View File

@ -1,6 +1,7 @@
define([
'react',
'jquery'
'jquery',
'select2'
],
function(
React,
@ -9,43 +10,45 @@ function(
var Selection = React.createClass({
getInitialState: function() {
return {
loading: false,
items: [],
selected: []
items: []
}
},
componentWillMount: function() {
this.loadCachedItems();
},
componentDidMount: function() {
if(this.props.select2) {
jQuery('#'+this.props.id).select2({
width: '25em'
});
}
jQuery('#'+this.props.field.id).select2()
.on('change', this.handleChange);
},
componentDidUpdate: function() {
jQuery('#'+this.props.field.id).select2(
'val',
(this.props.item[this.props.field.name])
);
},
loadCachedItems: function() {
if(typeof(window['mailpoet_'+this.props.endpoint]) !== 'undefined') {
var items = window['mailpoet_'+this.props.endpoint];
if(typeof(window['mailpoet_'+this.props.field.endpoint]) !== 'undefined') {
var items = window['mailpoet_'+this.props.field.endpoint];
this.setState({
items: items
});
}
},
handleChange: function() {
this.setState({
selected: jQuery('#'+this.props.id).val()
return this.props.onValueChange({
target: {
value: jQuery('#'+this.props.field.id).select2('val'),
name: this.props.field.name
}
});
},
getSelected: function() {
return this.state.selected;
},
render: function() {
var options = this.state.items.map(function(item, index) {
return (
<option
key={ 'action-' + index }
value={ item.id }>
key={ item.id }
value={ item.id }
>
{ item.name }
</option>
);
@ -54,12 +57,12 @@ function(
return (
<select
ref="selection"
id={ this.props.id || 'mailpoet_field_selection'}
placeholder={ this.props.placeholder }
multiple={ this.props.multiple }
>
{ options }
</select>
id={ this.props.field.id || 'mailpoet_field_selection'}
placeholder={ this.props.field.placeholder }
multiple={ this.props.field.multiple }
onChange={ this.handleChange }
defaultValue={ this.props.item[this.props.field.name] }
>{ options }</select>
);
}
});

View File

@ -27,17 +27,14 @@ define(
type: 'text'
},
{
name: 'list',
name: 'segments',
label: 'Lists',
tip: "The subscriber list that will be used for this campaign.",
field: (
<Selection
placeholder="Select a list"
id="mailpoet_segments"
endpoint="segments"
multiple={ true }
select2={ true } />
)
type: 'selection',
placeholder: "Select a list",
id: "mailpoet_segments",
endpoint: "segments",
multiple: true
},
{
name: 'sender',