-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathStaticDatePicker.tsx
332 lines (323 loc) · 11.9 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
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { StaticDatePickerProps } from './StaticDatePicker.types';
import { DatePickerViewRenderers, useDatePickerDefaultizedProps } from '../DatePicker/shared';
import { renderDateViewCalendar } from '../dateViewRenderers';
import { useStaticPicker } from '../internals/hooks/useStaticPicker';
import { validateDate } from '../validation';
import { DateView } from '../models';
import { singleItemValueManager } from '../internals/utils/valueManagers';
type StaticDatePickerComponent = ((
props: StaticDatePickerProps & React.RefAttributes<HTMLDivElement>,
) => React.JSX.Element) & { propTypes?: any };
/**
* Demos:
*
* - [DatePicker](https://mui.com/x/react-date-pickers/date-picker/)
* - [Validation](https://mui.com/x/react-date-pickers/validation/)
*
* API:
*
* - [StaticDatePicker API](https://mui.com/x/api/date-pickers/static-date-picker/)
*/
const StaticDatePicker = React.forwardRef(function StaticDatePicker(
inProps: StaticDatePickerProps,
ref: React.Ref<HTMLDivElement>,
) {
const defaultizedProps = useDatePickerDefaultizedProps<StaticDatePickerProps>(
inProps,
'MuiStaticDatePicker',
);
const displayStaticWrapperAs = defaultizedProps.displayStaticWrapperAs ?? 'mobile';
const viewRenderers: DatePickerViewRenderers<DateView> = {
day: renderDateViewCalendar,
month: renderDateViewCalendar,
year: renderDateViewCalendar,
...defaultizedProps.viewRenderers,
};
// Props with the default values specific to the static variant
const props = {
...defaultizedProps,
viewRenderers,
displayStaticWrapperAs,
yearsPerRow: defaultizedProps.yearsPerRow ?? (displayStaticWrapperAs === 'mobile' ? 3 : 4),
slotProps: {
...defaultizedProps.slotProps,
toolbar: {
hidden: displayStaticWrapperAs === 'desktop',
...defaultizedProps.slotProps?.toolbar,
},
},
};
const { renderPicker } = useStaticPicker<DateView, typeof props>({
ref,
props,
valueManager: singleItemValueManager,
valueType: 'date',
validator: validateDate,
});
return renderPicker();
}) as StaticDatePickerComponent;
StaticDatePicker.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the TypeScript types and run "pnpm proptypes" |
// ----------------------------------------------------------------------
/**
* If `true`, the main element is focused during the first mount.
* This main element is:
* - the element chosen by the visible view if any (i.e: the selected day on the `day` view).
* - the `input` element if there is a field rendered.
*/
autoFocus: PropTypes.bool,
className: PropTypes.string,
/**
* Formats the day of week displayed in the calendar header.
* @param {PickerValidDate} date The date of the day of week provided by the adapter.
* @returns {string} The name to display.
* @default (date: PickerValidDate) => adapter.format(date, 'weekdayShort').charAt(0).toUpperCase()
*/
dayOfWeekFormatter: PropTypes.func,
/**
* The default value.
* Used when the component is not controlled.
*/
defaultValue: PropTypes.object,
/**
* If `true`, the component is disabled.
* When disabled, the value cannot be changed and no interaction is possible.
* @default false
*/
disabled: PropTypes.bool,
/**
* If `true`, disable values after the current date for date components, time for time components and both for date time components.
* @default false
*/
disableFuture: PropTypes.bool,
/**
* If `true`, today's date is rendering without highlighting with circle.
* @default false
*/
disableHighlightToday: PropTypes.bool,
/**
* If `true`, disable values before the current date for date components, time for time components and both for date time components.
* @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']),
/**
* If `true`, the week number will be display in the calendar.
*/
displayWeekNumber: PropTypes.bool,
/**
* The day view will show as many weeks as needed after the end of the current month to match this value.
* Put it to 6 to have a fixed number of weeks in Gregorian calendars
*/
fixedWeekNumber: PropTypes.number,
/**
* If `true`, calls `renderLoading` instead of rendering the day calendar.
* Can be used to preload information and show it in calendar.
* @default false
*/
loading: PropTypes.bool,
/**
* Locale for components texts.
* Allows overriding texts coming from `LocalizationProvider` and `theme`.
*/
localeText: PropTypes.object,
/**
* Maximal selectable date.
* @default 2099-12-31
*/
maxDate: PropTypes.object,
/**
* Minimal selectable date.
* @default 1900-01-01
*/
minDate: PropTypes.object,
/**
* Months rendered per row.
* @default 3
*/
monthsPerRow: PropTypes.oneOf([3, 4]),
/**
* Callback fired when the value is accepted.
* @template TValue The value type. It will be the same type as `value` or `null`. It can be in `[start, end]` format in case of range value.
* @template TError The validation error type. It will be either `string` or a `null`. It can be in `[start, end]` format in case of range value.
* @param {TValue} value The value that was just accepted.
* @param {FieldChangeHandlerContext<TError>} context The context containing the validation result of the current value.
*/
onAccept: PropTypes.func,
/**
* Callback fired when the value changes.
* @template TValue The value type. It will be the same type as `value` or `null`. It can be in `[start, end]` format in case of range value.
* @template TError The validation error type. It will be either `string` or a `null`. It can be in `[start, end]` format in case of range value.
* @param {TValue} value The new value.
* @param {FieldChangeHandlerContext<TError>} context The context containing the validation result of the current value.
*/
onChange: PropTypes.func,
/**
* Callback fired when component requests to be closed.
* Can be fired when selecting (by default on `desktop` mode) or clearing a value.
* @deprecated Please avoid using as it will be removed in next major version.
*/
onClose: PropTypes.func,
/**
* Callback fired when the error associated with the current value changes.
* When a validation error is detected, the `error` parameter contains a non-null value.
* This can be used to render an appropriate form error.
* @template TError The validation error type. It will be either `string` or a `null`. It can be in `[start, end]` format in case of range value.
* @template TValue The value type. It will be the same type as `value` or `null`. It can be in `[start, end]` format in case of range value.
* @param {TError} error The reason why the current value is not valid.
* @param {TValue} value The value associated with the error.
*/
onError: PropTypes.func,
/**
* Callback fired on month change.
* @param {PickerValidDate} month The new month.
*/
onMonthChange: PropTypes.func,
/**
* Callback fired on view change.
* @template TView
* @param {TView} view The new view.
*/
onViewChange: PropTypes.func,
/**
* Callback fired on year change.
* @param {PickerValidDate} year The new year.
*/
onYearChange: PropTypes.func,
/**
* The default visible view.
* Used when the component view is not controlled.
* Must be a valid option from `views` list.
*/
openTo: PropTypes.oneOf(['day', 'month', 'year']),
/**
* Force rendering in particular orientation.
*/
orientation: PropTypes.oneOf(['landscape', 'portrait']),
/**
* If `true`, the component is read-only.
* When read-only, the value cannot be changed but the user can interact with the interface.
* @default false
*/
readOnly: PropTypes.bool,
/**
* If `true`, disable heavy animations.
* @default `@media(prefers-reduced-motion: reduce)` || `navigator.userAgent` matches Android <10 or iOS <13
*/
reduceAnimations: PropTypes.bool,
/**
* The date used to generate the new value when both `value` and `defaultValue` are empty.
* @default The closest valid date-time using the validation props, except callbacks like `shouldDisable<...>`.
*/
referenceDate: PropTypes.object,
/**
* Component displaying when passed `loading` true.
* @returns {React.ReactNode} The node to render when loading.
* @default () => <span>...</span>
*/
renderLoading: PropTypes.func,
/**
* Disable specific date.
*
* Warning: This function can be called multiple times (for example when rendering date calendar, checking if focus can be moved to a certain date, etc.). Expensive computations can impact performance.
*
* @param {PickerValidDate} day The date to test.
* @returns {boolean} If `true` the date will be disabled.
*/
shouldDisableDate: PropTypes.func,
/**
* Disable specific month.
* @param {PickerValidDate} month The month to test.
* @returns {boolean} If `true`, the month will be disabled.
*/
shouldDisableMonth: PropTypes.func,
/**
* Disable specific year.
* @param {PickerValidDate} year The year to test.
* @returns {boolean} If `true`, the year will be disabled.
*/
shouldDisableYear: PropTypes.func,
/**
* If `true`, days outside the current month are rendered:
*
* - if `fixedWeekNumber` is defined, renders days to have the weeks requested.
*
* - if `fixedWeekNumber` is not defined, renders day to fill the first and last week of the current month.
*
* - ignored if `calendars` equals more than `1` on range pickers.
* @default false
*/
showDaysOutsideCurrentMonth: PropTypes.bool,
/**
* The props used for each component slot.
* @default {}
*/
slotProps: PropTypes.object,
/**
* Overridable component slots.
* @default {}
*/
slots: PropTypes.object,
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
/**
* Choose which timezone to use for the value.
* Example: "default", "system", "UTC", "America/New_York".
* If you pass values from other timezones to some props, they will be converted to this timezone before being used.
* @see See the {@link https://mui.com/x/react-date-pickers/timezone/ timezones documentation} for more details.
* @default The timezone of the `value` or `defaultValue` prop is defined, 'default' otherwise.
*/
timezone: PropTypes.string,
/**
* The selected value.
* Used when the component is controlled.
*/
value: PropTypes.object,
/**
* The visible view.
* Used when the component view is controlled.
* Must be a valid option from `views` list.
*/
view: PropTypes.oneOf(['day', 'month', 'year']),
/**
* Define custom view renderers for each section.
* If `null`, the section will only have field editing.
* If `undefined`, internally defined view will be used.
*/
viewRenderers: PropTypes.shape({
day: PropTypes.func,
month: PropTypes.func,
year: PropTypes.func,
}),
/**
* Available views.
*/
views: PropTypes.arrayOf(PropTypes.oneOf(['day', 'month', 'year']).isRequired),
/**
* Years are displayed in ascending (chronological) order by default.
* If `desc`, years are displayed in descending order.
* @default 'asc'
*/
yearsOrder: PropTypes.oneOf(['asc', 'desc']),
/**
* Years rendered per row.
* @default `4` when `displayStaticWrapperAs === 'desktop'`, `3` otherwise.
*/
yearsPerRow: PropTypes.oneOf([3, 4]),
} as any;
export { StaticDatePicker };