-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
Expand file tree
/
Copy pathDatePicker.js
More file actions
321 lines (301 loc) · 8.99 KB
/
DatePicker.js
File metadata and controls
321 lines (301 loc) · 8.99 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import React, {Component, PropTypes} from 'react';
import {dateTimeFormat, formatIso, isEqualDate} from './dateUtils';
import DatePickerDialog from './DatePickerDialog';
import TextField from '../TextField';
class DatePicker extends Component {
static propTypes = {
/**
* Constructor for date formatting for the specified `locale`.
* The constructor must follow this specification: ECMAScript Internationalization API 1.0 (ECMA-402).
* `Intl.DateTimeFormat` is supported by most modern browsers, see http://caniuse.com/#search=intl,
* otherwise https://github.com/andyearnshaw/Intl.js is a good polyfill.
*
* By default, a built-in `DateTimeFormat` is used which supports the 'en-US' `locale`.
*/
DateTimeFormat: PropTypes.func,
/**
* If true, automatically accept and close the picker on select a date.
*/
autoOk: PropTypes.bool,
/**
* Override the default text of the 'Cancel' button.
*/
cancelLabel: PropTypes.node,
/**
* The css class name of the root element.
*/
className: PropTypes.string,
/**
* Used to control how the Date Picker will be displayed when the input field is focused.
* `dialog` (default) displays the DatePicker as a dialog with a modal.
* `inline` displays the DatePicker below the input field (similar to auto complete).
*/
container: PropTypes.oneOf(['dialog', 'inline']),
/**
* This is the initial date value of the component.
* If either `value` or `valueLink` is provided they will override this
* prop with `value` taking precedence.
*/
defaultDate: PropTypes.object,
/**
* Override the inline-styles of DatePickerDialog's Container element.
*/
dialogContainerStyle: PropTypes.object,
/**
* Disables the year selection in the date picker.
*/
disableYearSelection: PropTypes.bool,
/**
* Disables the DatePicker.
*/
disabled: PropTypes.bool,
/**
* Used to change the first day of week. It varies from
* Saturday to Monday between different locales.
* The allowed range is 0 (Sunday) to 6 (Saturday).
* The default is `1`, Monday, as per ISO 8601.
*/
firstDayOfWeek: PropTypes.number,
/**
* This function is called to format the date displayed in the input field, and should return a string.
* By default if no `locale` and `DateTimeFormat` is provided date objects are formatted to ISO 8601 YYYY-MM-DD.
*
* @param {object} date Date object to be formatted.
* @returns {any} The formatted date.
*/
formatDate: PropTypes.func,
/**
* Locale used for formatting the `DatePicker` date strings. Other than for 'en-US', you
* must provide a `DateTimeFormat` that supports the chosen `locale`.
*/
locale: PropTypes.string,
/**
* The ending of a range of valid dates. The range includes the endDate.
* The default value is current date + 100 years.
*/
maxDate: PropTypes.object,
/**
* The beginning of a range of valid dates. The range includes the startDate.
* The default value is current date - 100 years.
*/
minDate: PropTypes.object,
/**
* Tells the component to display the picker in portrait or landscape mode.
*/
mode: PropTypes.oneOf(['portrait', 'landscape']),
/**
* Override the default text of the 'OK' button.
*/
okLabel: PropTypes.node,
/**
* Callback function that is fired when the date value changes.
*
* @param {null} null Since there is no particular event associated with the change,
* the first argument will always be null.
* @param {object} date The new date.
*/
onChange: PropTypes.func,
/**
* Callback function that is fired when the Date Picker's dialog is dismissed.
*/
onDismiss: PropTypes.func,
/**
* Callback function that is fired when the Date Picker's `TextField` gains focus.
*/
onFocus: PropTypes.func,
/**
* Callback function that is fired when the Date Picker's dialog is shown.
*/
onShow: PropTypes.func,
/**
* Callback function that is fired when a touch tap event occurs on the Date Picker's `TextField`.
*
* @param {object} event TouchTap event targeting the `TextField`.
*/
onTouchTap: PropTypes.func,
/**
* Callback function used to determine if a day's entry should be disabled on the calendar.
*
* @param {object} day Date object of a day.
* @returns {boolean} Indicates whether the day should be disabled.
*/
shouldDisableDate: PropTypes.func,
/**
* Override the inline-styles of the root element.
*/
style: PropTypes.object,
/**
* Override the inline-styles of DatePicker's TextField element.
*/
textFieldStyle: PropTypes.object,
/**
* Sets the date for the Date Picker programmatically.
*/
value: PropTypes.object,
};
static defaultProps = {
autoOk: false,
container: 'dialog',
disabled: false,
disableYearSelection: false,
firstDayOfWeek: 1,
style: {},
};
static contextTypes = {
muiTheme: PropTypes.object.isRequired,
};
state = {
date: undefined,
};
componentWillMount() {
this.setState({
date: this.isControlled() ? this.getControlledDate() : this.props.defaultDate,
});
}
componentWillReceiveProps(nextProps) {
if (this.isControlled()) {
const newDate = this.getControlledDate(nextProps);
if (!isEqualDate(this.state.date, newDate)) {
this.setState({
date: newDate,
});
}
}
}
getDate() {
return this.state.date;
}
/**
* Open the date-picker dialog programmatically from a parent.
*/
openDialog() {
/**
* if the date is not selected then set it to new date
* (get the current system date while doing so)
* else set it to the currently selected date
*/
if (this.state.date !== undefined) {
this.setState({
dialogDate: this.getDate(),
}, this.refs.dialogWindow.show);
} else {
this.setState({
dialogDate: new Date(),
}, this.refs.dialogWindow.show);
}
}
/**
* Alias for `openDialog()` for an api consistent with TextField.
*/
focus() {
this.openDialog();
}
handleAccept = (date) => {
if (!this.isControlled()) {
this.setState({
date: date,
});
}
if (this.props.onChange) {
this.props.onChange(null, date);
}
};
handleFocus = (event) => {
event.target.blur();
if (this.props.onFocus) {
this.props.onFocus(event);
}
};
handleTouchTap = (event) => {
if (this.props.onTouchTap) {
this.props.onTouchTap(event);
}
if (!this.props.disabled) {
setTimeout(() => {
this.openDialog();
}, 0);
}
};
isControlled() {
return this.props.hasOwnProperty('value');
}
getControlledDate(props = this.props) {
if (props.value instanceof Date) {
return props.value;
}
}
formatDate = (date) => {
if (this.props.locale) {
const DateTimeFormat = this.props.DateTimeFormat || dateTimeFormat;
return new DateTimeFormat(this.props.locale, {
day: 'numeric',
month: 'numeric',
year: 'numeric',
}).format(date);
} else {
return formatIso(date);
}
};
render() {
const {
DateTimeFormat,
autoOk,
cancelLabel,
className,
container,
defaultDate, // eslint-disable-line no-unused-vars
dialogContainerStyle,
disableYearSelection,
firstDayOfWeek,
formatDate: formatDateProp,
locale,
maxDate,
minDate,
mode,
okLabel,
onDismiss,
onFocus, // eslint-disable-line no-unused-vars
onShow,
onTouchTap, // eslint-disable-line no-unused-vars
shouldDisableDate,
style,
textFieldStyle,
...other,
} = this.props;
const {prepareStyles} = this.context.muiTheme;
const formatDate = formatDateProp || this.formatDate;
return (
<div className={className} style={prepareStyles(Object.assign({}, style))}>
<TextField
{...other}
onFocus={this.handleFocus}
onTouchTap={this.handleTouchTap}
ref="input"
style={textFieldStyle}
value={this.state.date ? formatDate(this.state.date) : ''}
/>
<DatePickerDialog
DateTimeFormat={DateTimeFormat}
autoOk={autoOk}
cancelLabel={cancelLabel}
container={container}
containerStyle={dialogContainerStyle}
disableYearSelection={disableYearSelection}
firstDayOfWeek={firstDayOfWeek}
initialDate={this.state.dialogDate}
locale={locale}
maxDate={maxDate}
minDate={minDate}
mode={mode}
okLabel={okLabel}
onAccept={this.handleAccept}
onShow={onShow}
onDismiss={onDismiss}
ref="dialogWindow"
shouldDisableDate={shouldDisableDate}
/>
</div>
);
}
}
export default DatePicker;