-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayField.js
More file actions
83 lines (72 loc) · 2.09 KB
/
ArrayField.js
File metadata and controls
83 lines (72 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var _ = require('underscore'),
React = require('react');
var lastId = 0;
function newItem(value) {
lastId = lastId + 1;
return { key: 'i' + lastId, value: value };
}
module.exports = {
getInitialState: function() {
return {
values: this.props.value.map(newItem)
};
},
componentWillReceiveProps: function(nextProps) {
if (nextProps.value.join('|') !== _.pluck(this.state.values, 'value').join('|')) {
this.setState({
values: nextProps.value.map(newItem)
});
}
},
addItem: function() {
var newValues = this.state.values.concat(newItem(''));
this.setState({
values: newValues
});
this.valueChanged(_.pluck(newValues, 'value'));
},
removeItem: function(i) {
var newValues = _.without(this.state.values, i);
this.setState({
values: newValues
});
this.valueChanged(_.pluck(newValues, 'value'));
},
updateItem: function(i, event) {
var updatedValues = this.state.values;
var updateIndex = updatedValues.indexOf(i);
updatedValues[updateIndex].value = this.cleanInput ? this.cleanInput(event.target.value) : event.target.value;
this.setState({
values: updatedValues
});
this.valueChanged(_.pluck(updatedValues, 'value'));
},
valueChanged: function(values) {
this.props.onChange({
path: this.props.path,
value: values
});
},
renderItem: function(i) {
/* eslint-disable no-script-url */
return (
<div key={i.key} className='field-item'>
<a href="javascript:;" className='field-item-button btn-cancel' onClick={this.removeItem.bind(this, i)}>×</a>
<input ref={'input_' + i.key} className='form-control multi' type='text' name={this.props.path} value={i.value} onChange={this.updateItem.bind(this, i)} autoComplete='off' />
</div>
);
/* eslint-enable */
},
renderField: function () {
return (
<div>
{this.state.values.map(this.renderItem)}
<button type="button" className='btn btn-xs btn-default' onClick={this.addItem}>Add item</button>
</div>
);
},
// Override shouldCollapse to check for array length
shouldCollapse: function () {
return this.props.collapse && !this.props.value.length;
}
};