-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathStaticDatePicker.tsx
380 lines (371 loc) · 11.3 KB
/
StaticDatePicker.tsx
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import * as React from 'react';
import PropTypes from 'prop-types';
import { BaseDatePickerProps, useDatePickerDefaultizedProps } from '../DatePicker/shared';
import DatePickerToolbar from '../DatePicker/DatePickerToolbar';
import StaticWrapper, { StaticWrapperProps } from '../internal/pickers/wrappers/StaticWrapper';
import Picker from '../internal/pickers/Picker/Picker';
import { MuiPickersAdapter } from '../internal/pickers/hooks/useUtils';
import { useDateValidation } from '../internal/pickers/hooks/useValidation';
import { parsePickerInputValue } from '../internal/pickers/date-utils';
import { usePickerState, PickerStateValueManager } from '../internal/pickers/hooks/usePickerState';
const valueManager: PickerStateValueManager<unknown, unknown> = {
emptyValue: null,
parseInput: parsePickerInputValue,
areValuesEqual: (utils: MuiPickersAdapter, a: unknown, b: unknown) => utils.isEqual(a, b),
};
export interface StaticDatePickerProps<TDate = unknown> extends BaseDatePickerProps<TDate> {
/**
* Force static wrapper inner components to be rendered in mobile or desktop mode.
* @default 'mobile'
*/
displayStaticWrapperAs?: StaticWrapperProps['displayStaticWrapperAs'];
}
type StaticDatePickerComponent = (<TDate>(
props: StaticDatePickerProps<TDate> & React.RefAttributes<HTMLDivElement>,
) => JSX.Element) & { propTypes?: any };
/**
*
* Demos:
*
* - [Date Picker](https://material-ui.com/components/date-picker/)
*
* API:
*
* - [StaticDatePicker API](https://material-ui.com/api/static-date-picker/)
*/
const StaticDatePicker = React.forwardRef(function StaticDatePicker<TDate>(
inProps: StaticDatePickerProps<TDate>,
ref: React.Ref<HTMLDivElement>,
) {
// TODO: TDate needs to be instantiated at every usage.
const props = useDatePickerDefaultizedProps(
inProps as StaticDatePickerProps<unknown>,
'MuiStaticDatePicker',
);
const validationError = useDateValidation(props) !== null;
const { pickerProps, inputProps } = usePickerState(props, valueManager);
// Note that we are passing down all the value without spread.
// It saves us >1kb gzip and make any prop available automatically on any level down.
const {
ToolbarComponent = DatePickerToolbar,
value,
onChange,
displayStaticWrapperAs = 'mobile',
...other
} = props;
const AllDateInputProps = { ...inputProps, ...other, ref, validationError };
return (
<StaticWrapper displayStaticWrapperAs={displayStaticWrapperAs}>
<Picker
{...pickerProps}
toolbarTitle={props.label || props.toolbarTitle}
ToolbarComponent={ToolbarComponent}
DateInputProps={AllDateInputProps}
{...other}
/>
</StaticWrapper>
);
}) as StaticDatePickerComponent;
StaticDatePicker.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* Regular expression to detect "accepted" symbols.
* @default /\dap/gi
*/
acceptRegex: PropTypes.instanceOf(RegExp),
/**
* Enables keyboard listener for moving between days in calendar.
* Defaults to `true` unless the `ClockPicker` is used inside a `Static*` picker component.
*/
allowKeyboardControl: PropTypes.bool,
/**
* If `true`, `onChange` is fired on click even if the same date is selected.
* @default false
*/
allowSameDateSelection: PropTypes.bool,
/**
* className applied to the root component.
*/
className: PropTypes.string,
/**
* The components used for each slot.
* Either a string to use a HTML element or a component.
* @default {}
*/
components: PropTypes.shape({
LeftArrowButton: PropTypes.elementType,
LeftArrowIcon: PropTypes.elementType,
RightArrowButton: PropTypes.elementType,
RightArrowIcon: PropTypes.elementType,
SwitchViewButton: PropTypes.elementType,
SwitchViewIcon: PropTypes.elementType,
}),
/**
* The props used for each slot inside.
* @default {}
*/
componentsProps: PropTypes.object,
/**
* Default calendar month displayed when `value={null}`.
*/
defaultCalendarMonth: PropTypes.any,
/**
* If `true` the popup or dialog will immediately close after submitting full date.
* @default `true` for Desktop, `false` for Mobile (based on the chosen wrapper and `desktopModeMediaQuery` prop).
*/
disableCloseOnSelect: PropTypes.bool,
/**
* If `true`, the picker and text field are disabled.
*/
disabled: PropTypes.bool,
/**
* @default false
*/
disableFuture: PropTypes.bool,
/**
* If `true`, todays date is rendering without highlighting with circle.
* @default false
*/
disableHighlightToday: PropTypes.bool,
/**
* Disable mask on the keyboard, this should be used rarely. Consider passing proper mask for your format.
* @default false
*/
disableMaskedInput: PropTypes.bool,
/**
* Do not render open picker button (renders only text field with validation).
* @default false
*/
disableOpenPicker: PropTypes.bool,
/**
* @default false
*/
disablePast: PropTypes.bool,
/**
* Force static wrapper inner components to be rendered in mobile or desktop mode.
* @default 'mobile'
*/
displayStaticWrapperAs: PropTypes.oneOf(['desktop', 'mobile']),
/**
* Get aria-label text for control that opens picker dialog. Aria-label text must include selected date. @DateIOType
* @default (value, utils) => `Choose date, selected date is ${utils.format(utils.date(value), 'fullDate')}`
*/
getOpenDialogAriaText: PropTypes.func,
/**
* Get aria-label text for switching between views button.
*/
getViewSwitchingButtonText: PropTypes.func,
/**
* @ignore
*/
ignoreInvalidInputs: PropTypes.bool,
/**
* Props to pass to keyboard input adornment.
*/
InputAdornmentProps: PropTypes.object,
/**
* Format string.
*/
inputFormat: PropTypes.string,
/**
* @ignore
*/
InputProps: PropTypes.object,
/**
* Pass a ref to the `input` element.
*/
inputRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({
current: PropTypes.object,
}),
]),
/**
* @ignore
*/
key: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/**
* @ignore
*/
label: PropTypes.node,
/**
* Left arrow icon aria-label text.
*/
leftArrowButtonText: PropTypes.string,
/**
* If `true` renders `LoadingComponent` in calendar instead of calendar view.
* Can be used to preload information and show it in calendar.
* @default false
*/
loading: PropTypes.bool,
/**
* Custom mask. Can be used to override generate from format. (e.g. `__/__/____ __:__` or `__/__/____ __:__ _M`).
*/
mask: PropTypes.string,
/**
* @ignore
*/
maxDate: PropTypes.oneOfType([
PropTypes.any,
PropTypes.instanceOf(Date),
PropTypes.number,
PropTypes.string,
]),
/**
* @ignore
*/
minDate: PropTypes.oneOfType([
PropTypes.any,
PropTypes.instanceOf(Date),
PropTypes.number,
PropTypes.string,
]),
/**
* Callback fired when date is accepted @DateIOType.
*/
onAccept: PropTypes.func,
/**
* Callback fired when the value (the selected date) changes @DateIOType.
*/
onChange: PropTypes.func.isRequired,
/**
* Callback fired when the popup requests to be closed.
* Use in controlled mode (see open).
*/
onClose: PropTypes.func,
/**
* Callback that fired when input value or new `value` prop validation returns **new** validation error (or value is valid after error).
* In case of validation error detected `reason` prop return non-null value and `TextField` must be displayed in `error` state.
* This can be used to render appropriate form error.
*
* [Read the guide](https://next.material-ui-pickers.dev/guides/forms) about form integration and error displaying.
* @DateIOType
*/
onError: PropTypes.func,
/**
* Callback firing on month change. @DateIOType
*/
onMonthChange: PropTypes.func,
/**
* Callback fired when the popup requests to be opened.
* Use in controlled mode (see open).
*/
onOpen: PropTypes.func,
/**
* Callback fired on view change.
*/
onViewChange: PropTypes.func,
/**
* Callback firing on year change @DateIOType.
*/
onYearChange: PropTypes.func,
/**
* Control the popup or dialog open state.
*/
open: PropTypes.bool,
/**
* Props to pass to keyboard adornment button.
*/
OpenPickerButtonProps: PropTypes.object,
/**
* Icon displaying for open picker button.
*/
openPickerIcon: PropTypes.node,
/**
* First view to show.
*/
openTo: PropTypes.oneOf(['day', 'month', 'year']),
/**
* Force rendering in particular orientation.
*/
orientation: PropTypes.oneOf(['landscape', 'portrait']),
/**
* Make picker read only.
*/
readOnly: PropTypes.bool,
/**
* Disable heavy animations.
* @default typeof navigator !== 'undefined' && /(android)/i.test(navigator.userAgent)
*/
reduceAnimations: PropTypes.bool,
/**
* Custom renderer for day. Check the [PickersDay](https://material-ui.com/api/pickers-day/) component.
*/
renderDay: PropTypes.func,
/**
* The `renderInput` prop allows you to customize the rendered input.
* The `props` argument of this render prop contains props of [TextField](https://material-ui.com/api/text-field/#textfield-api) that you need to forward.
* Pay specific attention to the `ref` and `inputProps` keys.
* @example ```jsx
* renderInput={props => <TextField {...props} />}
* ````
*/
renderInput: PropTypes.func.isRequired,
/**
* Component displaying when passed `loading` true.
* @default () => <span data-mui-test="loading-progress">...</span>
*/
renderLoading: PropTypes.func,
/**
* Custom formatter to be passed into Rifm component.
*/
rifmFormatter: PropTypes.func,
/**
* Right arrow icon aria-label text.
*/
rightArrowButtonText: PropTypes.string,
/**
* Disable specific date. @DateIOType
*/
shouldDisableDate: PropTypes.func,
/**
* Disable specific years dynamically.
* Works like `shouldDisableDate` but for year selection view @DateIOType.
*/
shouldDisableYear: PropTypes.func,
/**
* If `true`, days that have `outsideCurrentMonth={true}` are displayed.
* @default false
*/
showDaysOutsideCurrentMonth: PropTypes.bool,
/**
* If `true`, show the toolbar even in desktop mode.
*/
showToolbar: PropTypes.bool,
/**
* Component that will replace default toolbar renderer.
* @default DatePickerToolbar
*/
ToolbarComponent: PropTypes.elementType,
/**
* Date format, that is displaying in toolbar.
*/
toolbarFormat: PropTypes.string,
/**
* Mobile picker date value placeholder, displaying if `value` === `null`.
* @default "–"
*/
toolbarPlaceholder: PropTypes.node,
/**
* Mobile picker title, displaying in the toolbar.
* @default "SELECT DATE"
*/
toolbarTitle: PropTypes.node,
/**
* The value of the picker.
*/
value: PropTypes.oneOfType([
PropTypes.any,
PropTypes.instanceOf(Date),
PropTypes.number,
PropTypes.string,
]),
/**
* Array of views to show.
*/
views: PropTypes.arrayOf(PropTypes.oneOf(['day', 'month', 'year']).isRequired),
} as any;
export default StaticDatePicker;