forked from mui/mui-x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SingleInputDateRangeField.tsx
298 lines (287 loc) · 9.67 KB
/
SingleInputDateRangeField.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
import * as React from 'react';
import PropTypes from 'prop-types';
import MuiTextField from '@mui/material/TextField';
import { useThemeProps } from '@mui/material/styles';
import { useSlotProps } from '@mui/base/utils';
import { SingleInputDateRangeFieldProps } from './SingleInputDateRangeField.types';
import { useSingleInputDateRangeField } from './useSingleInputDateRangeField';
type DateRangeFieldComponent = (<TDate>(
props: SingleInputDateRangeFieldProps<TDate> & React.RefAttributes<HTMLInputElement>,
) => JSX.Element) & { propTypes?: any; fieldType?: string };
const SingleInputDateRangeField = React.forwardRef(function SingleInputDateRangeField<TDate>(
inProps: SingleInputDateRangeFieldProps<TDate>,
ref: React.Ref<HTMLInputElement>,
) {
const themeProps = useThemeProps({
props: inProps,
name: 'MuiSingleInputDateRangeField',
});
const { slots, slotProps, components, componentsProps, InputProps, inputProps, ...other } =
themeProps;
const ownerState = themeProps;
const TextField = slots?.textField ?? components?.TextField ?? MuiTextField;
const { inputRef: externalInputRef, ...textFieldProps }: SingleInputDateRangeFieldProps<TDate> =
useSlotProps({
elementType: TextField,
externalSlotProps: slotProps?.textField ?? componentsProps?.textField,
externalForwardedProps: other,
ownerState,
});
// TODO: Remove when mui/material-ui#35088 will be merged
textFieldProps.inputProps = { ...textFieldProps.inputProps, ...inputProps };
textFieldProps.InputProps = { ...textFieldProps.InputProps, ...InputProps };
const {
ref: inputRef,
onPaste,
inputMode,
readOnly,
...fieldProps
} = useSingleInputDateRangeField<TDate, typeof textFieldProps>({
props: textFieldProps,
inputRef: externalInputRef,
});
return (
<TextField
ref={ref}
{...fieldProps}
inputProps={{ ...fieldProps.inputProps, ref: inputRef, onPaste, inputMode, readOnly }}
/>
);
}) as DateRangeFieldComponent;
SingleInputDateRangeField.fieldType = 'single-input';
SingleInputDateRangeField.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* If `true`, the `input` element is focused during the first mount.
* @default false
*/
autoFocus: PropTypes.bool,
className: PropTypes.string,
/**
* The color of the component.
* It supports both default and custom theme colors, which can be added as shown in the
* [palette customization guide](https://mui.com/material-ui/customization/palette/#adding-new-colors).
* @default 'primary'
*/
color: PropTypes.oneOf(['error', 'info', 'primary', 'secondary', 'success', 'warning']),
/**
* Overridable components.
* @default {}
* @deprecated Please use `slots`.
*/
components: PropTypes.object,
/**
* The props used for each component slot.
* @default {}
* @deprecated Please use `slotProps`.
*/
componentsProps: PropTypes.object,
/**
* The default value. Use when the component is not controlled.
*/
defaultValue: PropTypes.arrayOf(PropTypes.any),
/**
* If `true`, the component is disabled.
* @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`, disable values before the current date for date components, time for time components and both for date time components.
* @default false
*/
disablePast: PropTypes.bool,
/**
* If `true`, the component is displayed in focused state.
*/
focused: PropTypes.bool,
/**
* Format of the date when rendered in the input(s).
*/
format: PropTypes.string,
/**
* Props applied to the [`FormHelperText`](/material-ui/api/form-helper-text/) element.
*/
FormHelperTextProps: PropTypes.object,
/**
* If `true`, the input will take up the full width of its container.
* @default false
*/
fullWidth: PropTypes.bool,
/**
* The helper text content.
*/
helperText: PropTypes.node,
/**
* If `true`, the label is hidden.
* This is used to increase density for a `FilledInput`.
* Be sure to add `aria-label` to the `input` element.
* @default false
*/
hiddenLabel: PropTypes.bool,
/**
* The id of the `input` element.
* Use this prop to make `label` and `helperText` accessible for screen readers.
*/
id: PropTypes.string,
/**
* Props applied to the [`InputLabel`](/material-ui/api/input-label/) element.
* Pointer events like `onClick` are enabled if and only if `shrink` is `true`.
*/
InputLabelProps: PropTypes.object,
/**
* [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
*/
inputProps: PropTypes.object,
/**
* Props applied to the Input element.
* It will be a [`FilledInput`](/material-ui/api/filled-input/),
* [`OutlinedInput`](/material-ui/api/outlined-input/) or [`Input`](/material-ui/api/input/)
* component depending on the `variant` prop value.
*/
InputProps: PropTypes.object,
/**
* Pass a ref to the `input` element.
*/
inputRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({
current: PropTypes.any.isRequired,
}),
]),
/**
* The label content.
*/
label: PropTypes.node,
/**
* If `dense` or `normal`, will adjust vertical spacing of this and contained components.
* @default 'none'
*/
margin: PropTypes.oneOf(['dense', 'none', 'normal']),
/**
* Maximal selectable date.
*/
maxDate: PropTypes.any,
/**
* Minimal selectable date.
*/
minDate: PropTypes.any,
/**
* Name attribute of the `input` element.
*/
name: PropTypes.string,
onBlur: PropTypes.func,
/**
* Callback fired when the value changes.
* @template TValue The value type. Will be either the same type as `value` or `null`. Can be in `[start, end]` format in case of range value.
* @template TError The validation error type. Will be either `string` or a `null`. 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 the error associated to the current value changes.
* @template TValue The value type. Will be either the same type as `value` or `null`. Can be in `[start, end]` format in case of range value.
* @template TError The validation error type. Will be either `string` or a `null`. Can be in `[start, end]` format in case of range value.
* @param {TError} error The new error.
* @param {TValue} value The value associated to the error.
*/
onError: PropTypes.func,
onFocus: PropTypes.func,
/**
* Callback fired when the selected sections change.
* @param {FieldSelectedSections} newValue The new selected sections.
*/
onSelectedSectionsChange: PropTypes.func,
/**
* It prevents the user from changing the value of the field
* (not from interacting with the field).
* @default false
*/
readOnly: PropTypes.bool,
/**
* If `true`, the label is displayed as required and the `input` element is required.
* @default false
*/
required: PropTypes.bool,
/**
* The currently selected sections.
* This prop accept four formats:
* 1. If a number is provided, the section at this index will be selected.
* 2. If an object with a `startIndex` and `endIndex` properties are provided, the sections between those two indexes will be selected.
* 3. If a string of type `FieldSectionType` is provided, the first section with that name will be selected.
* 4. If `null` is provided, no section will be selected
* If not provided, the selected sections will be handled internally.
*/
selectedSections: PropTypes.oneOfType([
PropTypes.oneOf([
'all',
'day',
'hours',
'meridiem',
'minutes',
'month',
'seconds',
'weekDay',
'year',
]),
PropTypes.number,
PropTypes.shape({
endIndex: PropTypes.number.isRequired,
startIndex: PropTypes.number.isRequired,
}),
]),
/**
* Disable specific date.
* @template TDate
* @param {TDate} day The date to test.
* @param {string} position The date to test, 'start' or 'end'.
* @returns {boolean} Returns `true` if the date should be disabled.
*/
shouldDisableDate: PropTypes.func,
/**
* The size of the component.
*/
size: PropTypes.oneOf(['medium', 'small']),
/**
* The props used for each component slot.
* @default {}
*/
slotProps: PropTypes.object,
/**
* Overridable component slots.
* @default {}
*/
slots: PropTypes.object,
style: 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,
]),
/**
* The ref object used to imperatively interact with the field.
*/
unstableFieldRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
/**
* The selected value.
* Used when the component is controlled.
*/
value: PropTypes.arrayOf(PropTypes.any),
/**
* The variant to use.
* @default 'outlined'
*/
variant: PropTypes.oneOf(['filled', 'outlined', 'standard']),
} as any;
export { SingleInputDateRangeField };