-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathDateInput.js
More file actions
288 lines (264 loc) · 7.74 KB
/
DateInput.js
File metadata and controls
288 lines (264 loc) · 7.74 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import moment from 'moment';
import FormElement from './FormElement';
import Input from './Input';
import Icon from './Icon';
import Datepicker from './Datepicker';
import { uuid, isElInChildren, registerStyle } from './util';
export default class DateInput extends Component {
constructor(props) {
super();
this.state = {
id: `form-element-${uuid()}`,
opened: (props.defaultOpened || false),
};
this.onDateIconClick = this.onDateIconClick.bind(this);
this.onInputKeyDown = this.onInputKeyDown.bind(this);
this.onInputChange = this.onInputChange.bind(this);
this.onInputBlur = this.onInputBlur.bind(this);
this.onDatepickerSelect = this.onDatepickerSelect.bind(this);
this.onDatepickerBlur = this.onDatepickerBlur.bind(this);
this.onDatepickerClose = this.onDatepickerClose.bind(this);
registerStyle('dateinput', [
[
'.slds-has-error .slds-datepicker .slds-select',
'{ border: 1px solid #d8dde6; box-shadow: none; }',
],
]);
}
componentDidUpdate(prevProps, prevState) {
if (this.props.onValueChange && prevState.value !== this.state.value) {
this.props.onValueChange(this.state.value, prevState.value);
}
}
onDateIconClick() {
setTimeout(() => {
this.showDatepicker();
}, 10);
}
onInputKeyDown(e) {
if (e.keyCode === 13) { // return key
e.preventDefault();
e.stopPropagation();
this.setValueFromInput(e.target.value);
if (this.props.onComplete) {
setTimeout(() => {
this.props.onComplete();
}, 10);
}
} else if (e.keyCode === 40) { // down key
this.showDatepicker();
e.preventDefault();
e.stopPropagation();
}
if (this.props.onKeyDown) {
this.props.onKeyDown(e);
}
}
onInputChange(e) {
const inputValue = e.target.value;
this.setState({ inputValue });
if (this.props.onChange) {
this.props.onChange(e, inputValue);
}
}
onInputBlur(e) {
this.setValueFromInput(e.target.value);
setTimeout(() => {
if (!this.isFocusedInComponent()) {
if (this.props.onBlur) {
this.props.onBlur();
}
if (this.props.onComplete) {
this.props.onComplete();
}
}
}, 10);
}
onDatepickerSelect(dvalue) {
const value = moment(dvalue).format(this.getValueFormat());
this.setState({ value, inputValue: undefined });
setTimeout(() => {
this.setState({ opened: false });
const inputEl = this.input;
if (inputEl) {
inputEl.focus();
inputEl.select();
}
if (this.props.onComplete) {
this.props.onComplete();
}
}, 200);
}
onDatepickerBlur() {
this.setState({ opened: false });
setTimeout(() => {
if (!this.isFocusedInComponent()) {
if (this.props.onBlur) {
this.props.onBlur();
}
if (this.props.onComplete) {
this.props.onComplete();
}
}
}, 10);
}
onDatepickerClose() {
this.setState({ opened: false });
const inputEl = this.input;
if (inputEl) {
inputEl.focus();
inputEl.select();
}
}
getValueFormat() {
return this.props.includeTime ? 'YYYY-MM-DDTHH:mm:ss.SSSZ' : 'YYYY-MM-DD';
}
getInputValueFormat() {
return this.props.dateFormat || (this.props.includeTime ? 'L HH:mm' : 'L');
}
setValueFromInput(inputValue) {
let value = this.state.value;
if (!inputValue) {
value = '';
} else {
value = moment(inputValue, this.getInputValueFormat());
if (value.isValid()) {
value = value.format(this.getValueFormat());
} else {
value = '';
}
}
this.setState({ value, inputValue: undefined });
}
isFocusedInComponent() {
const rootEl = this.node;
const targetEl = document.activeElement;
return isElInChildren(rootEl, targetEl);
}
showDatepicker() {
let value = this.state.value;
if (typeof this.state.inputValue !== 'undefined') {
value = moment(this.state.inputValue, this.getInputValueFormat());
if (value.isValid()) {
value = value.format(this.getValueFormat());
} else {
value = this.state.value;
}
}
this.setState({ opened: true, value });
}
renderInput({ inputValue, ...props }) {
const pprops = props;
delete pprops.onValueChange;
return (
<div className='slds-input-has-icon slds-input-has-icon--right'>
<Input
inputRef={node => (this.input = node)}
value={ inputValue }
{ ...props }
onKeyDown={ this.onInputKeyDown }
onChange={ this.onInputChange }
onBlur={ this.onInputBlur }
/>
<span
tabIndex={ -1 }
style={ props.disabled ? undefined : { cursor: 'pointer' } }
onClick={ props.disabled ? undefined : this.onDateIconClick }
>
<Icon icon='event' className='slds-input__icon' />
</span>
</div>
);
}
renderDropdown(dateValue, minDate, maxDate, extensionRenderer) {
const datepickerClassNames = classnames(
'slds-dropdown',
`slds-dropdown--${this.props.menuAlign}`
);
return (
this.state.opened ?
<Datepicker
className={ datepickerClassNames }
selectedDate={ dateValue }
autoFocus
minDate={minDate}
maxDate={maxDate}
extensionRenderer={ extensionRenderer }
onSelect={ this.onDatepickerSelect }
onBlur={ this.onDatepickerBlur }
onClose={ this.onDatepickerClose }
/> : <div />
);
}
render() {
const id = this.props.id || this.state.id;
const {
totalCols, cols, label, required, error,
defaultValue, value, menuAlign,
minDate, maxDate,
extensionRenderer,
...props
} = this.props;
const dateValue =
typeof value !== 'undefined' ? value :
typeof this.state.value !== 'undefined' ? this.state.value :
defaultValue;
const mvalue = moment(dateValue, this.getValueFormat());
const inputValue =
typeof this.state.inputValue !== 'undefined' ?
this.state.inputValue :
typeof dateValue !== 'undefined' && mvalue.isValid() ?
mvalue.format(this.getInputValueFormat()) :
undefined;
const dropdown = this.renderDropdown(
mvalue.isValid() ? mvalue.format('YYYY-MM-DD') : undefined,
minDate,
maxDate,
extensionRenderer,
);
const formElemProps = { id, totalCols, cols, label, required, error, dropdown };
delete props.dateFormat;
delete props.defaultOpened;
delete props.includeTime;
delete props.onComplete;
return (
<FormElement
formElementRef={ node => (this.node = node) }
{ ...formElemProps }
style={ menuAlign === 'right' ? { position: 'absolute', right: null } : {} }
>
{ this.renderInput({ id, inputValue, ...props }) }
</FormElement>
);
}
}
const MENU_ALIGN = ['left', 'right'];
DateInput.propTypes = {
id: PropTypes.string,
label: PropTypes.string,
required: PropTypes.bool,
error: FormElement.propTypes.error,
totalCols: PropTypes.number,
cols: PropTypes.number,
value: PropTypes.string,
defaultValue: PropTypes.string,
defaultOpened: PropTypes.bool,
dateFormat: PropTypes.string,
includeTime: PropTypes.bool,
onKeyDown: PropTypes.func,
onBlur: PropTypes.func,
onChange: PropTypes.func,
onValueChange: PropTypes.func,
onComplete: PropTypes.func,
menuAlign: PropTypes.oneOf(MENU_ALIGN),
minDate: PropTypes.string,
maxDate: PropTypes.string,
extensionRenderer: PropTypes.func,
};
DateInput.defaultProps = {
menuAlign: 'left',
};
DateInput.isFormElement = true;