-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateArrayField.js
More file actions
136 lines (121 loc) · 3.8 KB
/
DateArrayField.js
File metadata and controls
136 lines (121 loc) · 3.8 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* global Pikaday */
var _ = require('underscore'),
React = require('react'),
moment = require('moment');
var lastId = 0;
function newItem(value) {
lastId++;
return { key: 'i' + lastId, value: value, picker: null };
}
module.exports = {
// set default properties
getDefaultProps: function() {
return {
format: 'YYYY-MM-DD',
pickers: []
};
},
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)
});
}
},
componentWillUpdate: function() {
this.props.value.forEach(function (val, i) {
// Destroy each of our datepickers
if (this.props.pickers[i]) {
this.props.pickers[i].destroy();
}
}, this);
},
componentDidUpdate: function() {
this.props.value.forEach(function (val, i) {
var dateInput = this.getDOMNode().getElementsByClassName('datepicker_' + this.state.values[i].key)[0];
// Add a date picker to each updated field
this.props.pickers[i] = new Pikaday({
field: dateInput,
format: this.props.format,
onSelect: function(date) {//eslint-disable-line no-unused-vars
if (this.props.onChange && this.props.pickers[i].toString() !== val.value) {
this.props.value[i] = this.props.pickers[i].toString();
this.props.onChange(this.props.pickers[i].toString());
}
}.bind(this)
});
}, this);
},
componentDidMount: function() {
this.props.value.forEach(function (val, i) {
var dateInput = this.getDOMNode().getElementsByClassName('datepicker_' + this.state.values[i].key)[0];
if (this.props.pickers[i]) this.props.pickers[i].destroy();
this.props.pickers[i] = new Pikaday({
field: dateInput,
format: this.props.format,
onSelect: function(date) {//eslint-disable-line no-unused-vars
if (this.props.onChange && this.props.pickers[i].toString() !== val.value) {
this.props.value[i] = this.props.pickers[i].toString();
this.props.onChange(this.props.pickers[i].toString());
}
}.bind(this)
});
}, this);
},
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
});
},
handleBlur: function(e) {//eslint-disable-line no-unused-vars
if (this.state.value === this.props.value) return;
this.picker.setMoment(moment(this.state.value, this.props.format));
},
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 datepicker_' + i.key} 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 date</button>
</div>
);
}
};