@mui/x-date-pickers 8.0.0-beta.0 → 8.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +77 -0
- package/DateCalendar/DayCalendar.d.ts +3 -7
- package/DateCalendar/DayCalendar.js +16 -8
- package/DateCalendar/index.d.ts +0 -1
- package/DateField/useDateField.d.ts +1 -1
- package/DateField/useDateField.js +2 -16
- package/DatePicker/DatePicker.js +1 -0
- package/DateTimeField/useDateTimeField.d.ts +1 -1
- package/DateTimeField/useDateTimeField.js +2 -16
- package/DateTimePicker/DateTimePicker.js +1 -0
- package/DesktopDatePicker/DesktopDatePicker.js +1 -0
- package/DesktopDateTimePicker/DesktopDateTimePicker.js +1 -0
- package/DesktopTimePicker/DesktopTimePicker.js +1 -0
- package/MobileDatePicker/MobileDatePicker.js +1 -0
- package/MobileDateTimePicker/MobileDateTimePicker.js +1 -0
- package/MobileTimePicker/MobileTimePicker.js +1 -0
- package/PickersDay/PickersDay.d.ts +1 -72
- package/PickersDay/PickersDay.js +30 -28
- package/PickersDay/PickersDay.types.d.ts +114 -0
- package/PickersDay/PickersDay.types.js +5 -0
- package/PickersDay/index.d.ts +1 -1
- package/PickersDay/usePickerDayOwnerState.d.ts +14 -0
- package/PickersDay/usePickerDayOwnerState.js +40 -0
- package/TimeField/useTimeField.d.ts +1 -1
- package/TimeField/useTimeField.js +2 -16
- package/TimePicker/TimePicker.js +1 -0
- package/esm/DateCalendar/DayCalendar.d.ts +3 -7
- package/esm/DateCalendar/DayCalendar.js +16 -8
- package/esm/DateCalendar/index.d.ts +0 -1
- package/esm/DateField/useDateField.d.ts +1 -1
- package/esm/DateField/useDateField.js +3 -17
- package/esm/DatePicker/DatePicker.js +1 -0
- package/esm/DateTimeField/useDateTimeField.d.ts +1 -1
- package/esm/DateTimeField/useDateTimeField.js +3 -17
- package/esm/DateTimePicker/DateTimePicker.js +1 -0
- package/esm/DesktopDatePicker/DesktopDatePicker.js +1 -0
- package/esm/DesktopDateTimePicker/DesktopDateTimePicker.js +1 -0
- package/esm/DesktopTimePicker/DesktopTimePicker.js +1 -0
- package/esm/MobileDatePicker/MobileDatePicker.js +1 -0
- package/esm/MobileDateTimePicker/MobileDateTimePicker.js +1 -0
- package/esm/MobileTimePicker/MobileTimePicker.js +1 -0
- package/esm/PickersDay/PickersDay.d.ts +1 -72
- package/esm/PickersDay/PickersDay.js +30 -28
- package/esm/PickersDay/PickersDay.types.d.ts +114 -0
- package/esm/PickersDay/PickersDay.types.js +1 -0
- package/esm/PickersDay/index.d.ts +1 -1
- package/esm/PickersDay/usePickerDayOwnerState.d.ts +14 -0
- package/esm/PickersDay/usePickerDayOwnerState.js +32 -0
- package/esm/TimeField/useTimeField.d.ts +1 -1
- package/esm/TimeField/useTimeField.js +3 -17
- package/esm/TimePicker/TimePicker.js +1 -0
- package/esm/hooks/useSplitFieldProps.d.ts +1 -1
- package/esm/index.js +1 -1
- package/esm/internals/components/PickerFieldUI.d.ts +5 -5
- package/esm/internals/hooks/useField/buildSectionsFromFormat.d.ts +2 -2
- package/esm/internals/hooks/useField/buildSectionsFromFormat.js +9 -9
- package/esm/internals/hooks/useField/index.d.ts +1 -1
- package/esm/internals/hooks/useField/useField.d.ts +2 -4
- package/esm/internals/hooks/useField/useField.js +5 -229
- package/esm/internals/hooks/useField/useField.types.d.ts +48 -68
- package/esm/internals/hooks/useField/useField.utils.d.ts +2 -5
- package/esm/internals/hooks/useField/useField.utils.js +7 -92
- package/esm/internals/hooks/useField/useFieldCharacterEditing.d.ts +19 -26
- package/esm/internals/hooks/useField/useFieldCharacterEditing.js +42 -60
- package/esm/internals/hooks/useField/useFieldRootHandleKeyDown.d.ts +16 -0
- package/esm/internals/hooks/useField/useFieldRootHandleKeyDown.js +204 -0
- package/esm/internals/hooks/useField/useFieldState.d.ts +23 -13
- package/esm/internals/hooks/useField/useFieldState.js +103 -30
- package/esm/internals/hooks/useField/useFieldV6TextField.d.ts +3 -3
- package/esm/internals/hooks/useField/useFieldV6TextField.js +202 -135
- package/esm/internals/hooks/useField/useFieldV7TextField.d.ts +3 -2
- package/esm/internals/hooks/useField/useFieldV7TextField.js +217 -153
- package/esm/internals/hooks/usePicker/usePicker.types.d.ts +1 -0
- package/esm/internals/index.d.ts +2 -1
- package/esm/internals/index.js +1 -0
- package/esm/managers/useDateManager.js +3 -3
- package/esm/managers/useDateTimeManager.js +3 -3
- package/esm/managers/useTimeManager.js +3 -3
- package/esm/models/fields.d.ts +2 -2
- package/esm/models/manager.d.ts +3 -2
- package/hooks/useSplitFieldProps.d.ts +1 -1
- package/index.js +1 -1
- package/internals/components/PickerFieldUI.d.ts +5 -5
- package/internals/hooks/useField/buildSectionsFromFormat.d.ts +2 -2
- package/internals/hooks/useField/buildSectionsFromFormat.js +9 -9
- package/internals/hooks/useField/index.d.ts +1 -1
- package/internals/hooks/useField/useField.d.ts +2 -4
- package/internals/hooks/useField/useField.js +5 -231
- package/internals/hooks/useField/useField.types.d.ts +48 -68
- package/internals/hooks/useField/useField.utils.d.ts +2 -5
- package/internals/hooks/useField/useField.utils.js +8 -94
- package/internals/hooks/useField/useFieldCharacterEditing.d.ts +19 -26
- package/internals/hooks/useField/useFieldCharacterEditing.js +41 -59
- package/internals/hooks/useField/useFieldRootHandleKeyDown.d.ts +16 -0
- package/internals/hooks/useField/useFieldRootHandleKeyDown.js +210 -0
- package/internals/hooks/useField/useFieldState.d.ts +23 -13
- package/internals/hooks/useField/useFieldState.js +102 -29
- package/internals/hooks/useField/useFieldV6TextField.d.ts +3 -3
- package/internals/hooks/useField/useFieldV6TextField.js +202 -135
- package/internals/hooks/useField/useFieldV7TextField.d.ts +3 -2
- package/internals/hooks/useField/useFieldV7TextField.js +218 -154
- package/internals/hooks/usePicker/usePicker.types.d.ts +1 -0
- package/internals/index.d.ts +2 -1
- package/internals/index.js +7 -0
- package/managers/useDateManager.js +3 -3
- package/managers/useDateTimeManager.js +3 -3
- package/managers/useTimeManager.js +3 -3
- package/models/fields.d.ts +2 -2
- package/models/manager.d.ts +3 -2
- package/modern/DateCalendar/DayCalendar.d.ts +3 -7
- package/modern/DateCalendar/DayCalendar.js +16 -8
- package/modern/DateCalendar/index.d.ts +0 -1
- package/modern/DateField/useDateField.d.ts +1 -1
- package/modern/DateField/useDateField.js +3 -17
- package/modern/DatePicker/DatePicker.js +1 -0
- package/modern/DateTimeField/useDateTimeField.d.ts +1 -1
- package/modern/DateTimeField/useDateTimeField.js +3 -17
- package/modern/DateTimePicker/DateTimePicker.js +1 -0
- package/modern/DesktopDatePicker/DesktopDatePicker.js +1 -0
- package/modern/DesktopDateTimePicker/DesktopDateTimePicker.js +1 -0
- package/modern/DesktopTimePicker/DesktopTimePicker.js +1 -0
- package/modern/MobileDatePicker/MobileDatePicker.js +1 -0
- package/modern/MobileDateTimePicker/MobileDateTimePicker.js +1 -0
- package/modern/MobileTimePicker/MobileTimePicker.js +1 -0
- package/modern/PickersDay/PickersDay.d.ts +1 -72
- package/modern/PickersDay/PickersDay.js +30 -28
- package/modern/PickersDay/PickersDay.types.d.ts +114 -0
- package/modern/PickersDay/PickersDay.types.js +1 -0
- package/modern/PickersDay/index.d.ts +1 -1
- package/modern/PickersDay/usePickerDayOwnerState.d.ts +14 -0
- package/modern/PickersDay/usePickerDayOwnerState.js +32 -0
- package/modern/TimeField/useTimeField.d.ts +1 -1
- package/modern/TimeField/useTimeField.js +3 -17
- package/modern/TimePicker/TimePicker.js +1 -0
- package/modern/hooks/useSplitFieldProps.d.ts +1 -1
- package/modern/index.js +1 -1
- package/modern/internals/components/PickerFieldUI.d.ts +5 -5
- package/modern/internals/hooks/useField/buildSectionsFromFormat.d.ts +2 -2
- package/modern/internals/hooks/useField/buildSectionsFromFormat.js +9 -9
- package/modern/internals/hooks/useField/index.d.ts +1 -1
- package/modern/internals/hooks/useField/useField.d.ts +2 -4
- package/modern/internals/hooks/useField/useField.js +5 -229
- package/modern/internals/hooks/useField/useField.types.d.ts +48 -68
- package/modern/internals/hooks/useField/useField.utils.d.ts +2 -5
- package/modern/internals/hooks/useField/useField.utils.js +7 -92
- package/modern/internals/hooks/useField/useFieldCharacterEditing.d.ts +19 -26
- package/modern/internals/hooks/useField/useFieldCharacterEditing.js +42 -60
- package/modern/internals/hooks/useField/useFieldRootHandleKeyDown.d.ts +16 -0
- package/modern/internals/hooks/useField/useFieldRootHandleKeyDown.js +204 -0
- package/modern/internals/hooks/useField/useFieldState.d.ts +23 -13
- package/modern/internals/hooks/useField/useFieldState.js +103 -30
- package/modern/internals/hooks/useField/useFieldV6TextField.d.ts +3 -3
- package/modern/internals/hooks/useField/useFieldV6TextField.js +202 -135
- package/modern/internals/hooks/useField/useFieldV7TextField.d.ts +3 -2
- package/modern/internals/hooks/useField/useFieldV7TextField.js +217 -153
- package/modern/internals/hooks/usePicker/usePicker.types.d.ts +1 -0
- package/modern/internals/index.d.ts +2 -1
- package/modern/internals/index.js +1 -0
- package/modern/managers/useDateManager.js +3 -3
- package/modern/managers/useDateTimeManager.js +3 -3
- package/modern/managers/useTimeManager.js +3 -3
- package/modern/models/fields.d.ts +2 -2
- package/modern/models/manager.d.ts +3 -2
- package/package.json +1 -1
- package/tsconfig.build.tsbuildinfo +1 -1
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import useEventCallback from '@mui/utils/useEventCallback';
|
|
2
|
+
import { useUtils } from "../useUtils.js";
|
|
3
|
+
import { cleanDigitSectionValue, getLetterEditingOptions, removeLocalizedDigits } from "./useField.utils.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns the `onKeyDown` handler to pass to the root element of the field.
|
|
7
|
+
*/
|
|
8
|
+
export function useFieldRootHandleKeyDown(parameters) {
|
|
9
|
+
const utils = useUtils();
|
|
10
|
+
const {
|
|
11
|
+
manager: {
|
|
12
|
+
internal_fieldValueManager: fieldValueManager
|
|
13
|
+
},
|
|
14
|
+
internalPropsWithDefaults: {
|
|
15
|
+
minutesStep,
|
|
16
|
+
disabled,
|
|
17
|
+
readOnly
|
|
18
|
+
},
|
|
19
|
+
stateResponse: {
|
|
20
|
+
// States and derived states
|
|
21
|
+
state,
|
|
22
|
+
value,
|
|
23
|
+
activeSectionIndex,
|
|
24
|
+
parsedSelectedSections,
|
|
25
|
+
sectionsValueBoundaries,
|
|
26
|
+
localizedDigits,
|
|
27
|
+
timezone,
|
|
28
|
+
sectionOrder,
|
|
29
|
+
// Methods to update the states
|
|
30
|
+
clearValue,
|
|
31
|
+
clearActiveSection,
|
|
32
|
+
setSelectedSections,
|
|
33
|
+
updateSectionValue
|
|
34
|
+
}
|
|
35
|
+
} = parameters;
|
|
36
|
+
return useEventCallback(event => {
|
|
37
|
+
if (disabled) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
// eslint-disable-next-line default-case
|
|
41
|
+
switch (true) {
|
|
42
|
+
// Select all
|
|
43
|
+
case (event.ctrlKey || event.metaKey) && String.fromCharCode(event.keyCode) === 'A' && !event.shiftKey && !event.altKey:
|
|
44
|
+
{
|
|
45
|
+
// prevent default to make sure that the next line "select all" while updating
|
|
46
|
+
// the internal state at the same time.
|
|
47
|
+
event.preventDefault();
|
|
48
|
+
setSelectedSections('all');
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Move selection to next section
|
|
53
|
+
case event.key === 'ArrowRight':
|
|
54
|
+
{
|
|
55
|
+
event.preventDefault();
|
|
56
|
+
if (parsedSelectedSections == null) {
|
|
57
|
+
setSelectedSections(sectionOrder.startIndex);
|
|
58
|
+
} else if (parsedSelectedSections === 'all') {
|
|
59
|
+
setSelectedSections(sectionOrder.endIndex);
|
|
60
|
+
} else {
|
|
61
|
+
const nextSectionIndex = sectionOrder.neighbors[parsedSelectedSections].rightIndex;
|
|
62
|
+
if (nextSectionIndex !== null) {
|
|
63
|
+
setSelectedSections(nextSectionIndex);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Move selection to previous section
|
|
70
|
+
case event.key === 'ArrowLeft':
|
|
71
|
+
{
|
|
72
|
+
event.preventDefault();
|
|
73
|
+
if (parsedSelectedSections == null) {
|
|
74
|
+
setSelectedSections(sectionOrder.endIndex);
|
|
75
|
+
} else if (parsedSelectedSections === 'all') {
|
|
76
|
+
setSelectedSections(sectionOrder.startIndex);
|
|
77
|
+
} else {
|
|
78
|
+
const nextSectionIndex = sectionOrder.neighbors[parsedSelectedSections].leftIndex;
|
|
79
|
+
if (nextSectionIndex !== null) {
|
|
80
|
+
setSelectedSections(nextSectionIndex);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Reset the value of the selected section
|
|
87
|
+
case event.key === 'Delete':
|
|
88
|
+
{
|
|
89
|
+
event.preventDefault();
|
|
90
|
+
if (readOnly) {
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
if (parsedSelectedSections == null || parsedSelectedSections === 'all') {
|
|
94
|
+
clearValue();
|
|
95
|
+
} else {
|
|
96
|
+
clearActiveSection();
|
|
97
|
+
}
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Increment / decrement the selected section value
|
|
102
|
+
case ['ArrowUp', 'ArrowDown', 'Home', 'End', 'PageUp', 'PageDown'].includes(event.key):
|
|
103
|
+
{
|
|
104
|
+
event.preventDefault();
|
|
105
|
+
if (readOnly || activeSectionIndex == null) {
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// if all sections are selected, mark the currently editing one as selected
|
|
110
|
+
if (parsedSelectedSections === 'all') {
|
|
111
|
+
setSelectedSections(activeSectionIndex);
|
|
112
|
+
}
|
|
113
|
+
const activeSection = state.sections[activeSectionIndex];
|
|
114
|
+
const newSectionValue = adjustSectionValue(utils, timezone, activeSection, event.key, sectionsValueBoundaries, localizedDigits, fieldValueManager.getDateFromSection(value, activeSection), {
|
|
115
|
+
minutesStep
|
|
116
|
+
});
|
|
117
|
+
updateSectionValue({
|
|
118
|
+
section: activeSection,
|
|
119
|
+
newSectionValue,
|
|
120
|
+
shouldGoToNextSection: false
|
|
121
|
+
});
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
function getDeltaFromKeyCode(keyCode) {
|
|
128
|
+
switch (keyCode) {
|
|
129
|
+
case 'ArrowUp':
|
|
130
|
+
return 1;
|
|
131
|
+
case 'ArrowDown':
|
|
132
|
+
return -1;
|
|
133
|
+
case 'PageUp':
|
|
134
|
+
return 5;
|
|
135
|
+
case 'PageDown':
|
|
136
|
+
return -5;
|
|
137
|
+
default:
|
|
138
|
+
return 0;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function adjustSectionValue(utils, timezone, section, keyCode, sectionsValueBoundaries, localizedDigits, activeDate, stepsAttributes) {
|
|
142
|
+
const delta = getDeltaFromKeyCode(keyCode);
|
|
143
|
+
const isStart = keyCode === 'Home';
|
|
144
|
+
const isEnd = keyCode === 'End';
|
|
145
|
+
const shouldSetAbsolute = section.value === '' || isStart || isEnd;
|
|
146
|
+
const adjustDigitSection = () => {
|
|
147
|
+
const sectionBoundaries = sectionsValueBoundaries[section.type]({
|
|
148
|
+
currentDate: activeDate,
|
|
149
|
+
format: section.format,
|
|
150
|
+
contentType: section.contentType
|
|
151
|
+
});
|
|
152
|
+
const getCleanValue = value => cleanDigitSectionValue(utils, value, sectionBoundaries, localizedDigits, section);
|
|
153
|
+
const step = section.type === 'minutes' && stepsAttributes?.minutesStep ? stepsAttributes.minutesStep : 1;
|
|
154
|
+
let newSectionValueNumber;
|
|
155
|
+
if (shouldSetAbsolute) {
|
|
156
|
+
if (section.type === 'year' && !isEnd && !isStart) {
|
|
157
|
+
return utils.formatByString(utils.date(undefined, timezone), section.format);
|
|
158
|
+
}
|
|
159
|
+
if (delta > 0 || isStart) {
|
|
160
|
+
newSectionValueNumber = sectionBoundaries.minimum;
|
|
161
|
+
} else {
|
|
162
|
+
newSectionValueNumber = sectionBoundaries.maximum;
|
|
163
|
+
}
|
|
164
|
+
} else {
|
|
165
|
+
const currentSectionValue = parseInt(removeLocalizedDigits(section.value, localizedDigits), 10);
|
|
166
|
+
newSectionValueNumber = currentSectionValue + delta * step;
|
|
167
|
+
}
|
|
168
|
+
if (newSectionValueNumber % step !== 0) {
|
|
169
|
+
if (delta < 0 || isStart) {
|
|
170
|
+
newSectionValueNumber += step - (step + newSectionValueNumber) % step; // for JS -3 % 5 = -3 (should be 2)
|
|
171
|
+
}
|
|
172
|
+
if (delta > 0 || isEnd) {
|
|
173
|
+
newSectionValueNumber -= newSectionValueNumber % step;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (newSectionValueNumber > sectionBoundaries.maximum) {
|
|
177
|
+
return getCleanValue(sectionBoundaries.minimum + (newSectionValueNumber - sectionBoundaries.maximum - 1) % (sectionBoundaries.maximum - sectionBoundaries.minimum + 1));
|
|
178
|
+
}
|
|
179
|
+
if (newSectionValueNumber < sectionBoundaries.minimum) {
|
|
180
|
+
return getCleanValue(sectionBoundaries.maximum - (sectionBoundaries.minimum - newSectionValueNumber - 1) % (sectionBoundaries.maximum - sectionBoundaries.minimum + 1));
|
|
181
|
+
}
|
|
182
|
+
return getCleanValue(newSectionValueNumber);
|
|
183
|
+
};
|
|
184
|
+
const adjustLetterSection = () => {
|
|
185
|
+
const options = getLetterEditingOptions(utils, timezone, section.type, section.format);
|
|
186
|
+
if (options.length === 0) {
|
|
187
|
+
return section.value;
|
|
188
|
+
}
|
|
189
|
+
if (shouldSetAbsolute) {
|
|
190
|
+
if (delta > 0 || isStart) {
|
|
191
|
+
return options[0];
|
|
192
|
+
}
|
|
193
|
+
return options[options.length - 1];
|
|
194
|
+
}
|
|
195
|
+
const currentOptionIndex = options.indexOf(section.value);
|
|
196
|
+
const newOptionIndex = (currentOptionIndex + delta) % options.length;
|
|
197
|
+
const clampedIndex = (newOptionIndex + options.length) % options.length;
|
|
198
|
+
return options[clampedIndex];
|
|
199
|
+
};
|
|
200
|
+
if (section.contentType === 'digit' || section.contentType === 'digit-with-letter') {
|
|
201
|
+
return adjustDigitSection();
|
|
202
|
+
}
|
|
203
|
+
return adjustLetterSection();
|
|
204
|
+
}
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
import { UseFieldInternalProps,
|
|
2
|
-
import { FieldSelectedSections, PickersTimezone, InferFieldSection } from "../../../models/index.js";
|
|
1
|
+
import { UseFieldInternalProps, UseFieldState, FieldParsedSelectedSections, FieldSectionsValueBoundaries, SectionOrdering, UseFieldForwardedProps, CharacterEditingQuery } from "./useField.types.js";
|
|
2
|
+
import { FieldSelectedSections, PickersTimezone, InferFieldSection, PickerManager } from "../../../models/index.js";
|
|
3
3
|
import { PickerValidValue } from "../../models/index.js";
|
|
4
|
-
export
|
|
4
|
+
export declare const useFieldState: <TValue extends PickerValidValue, TEnableAccessibleFieldDOMStructure extends boolean, TError, TValidationProps extends {}, TForwardedProps extends UseFieldForwardedProps<TEnableAccessibleFieldDOMStructure>>(parameters: UseFieldStateParameters<TValue, TEnableAccessibleFieldDOMStructure, TError, TValidationProps, TForwardedProps>) => UseFieldStateReturnValue<TValue>;
|
|
5
|
+
interface UseFieldStateParameters<TValue extends PickerValidValue, TEnableAccessibleFieldDOMStructure extends boolean, TError, TValidationProps extends {}, TForwardedProps extends UseFieldForwardedProps<TEnableAccessibleFieldDOMStructure>> {
|
|
6
|
+
manager: PickerManager<TValue, TEnableAccessibleFieldDOMStructure, TError, TValidationProps, any>;
|
|
7
|
+
internalPropsWithDefaults: UseFieldInternalProps<TValue, TEnableAccessibleFieldDOMStructure, TError> & TValidationProps;
|
|
8
|
+
forwardedProps: TForwardedProps;
|
|
9
|
+
}
|
|
10
|
+
export interface UpdateSectionValueParameters<TValue extends PickerValidValue> {
|
|
5
11
|
/**
|
|
6
12
|
* The section on which we want to apply the new value.
|
|
7
13
|
*/
|
|
@@ -15,20 +21,24 @@ export interface UpdateSectionValueParams<TValue extends PickerValidValue> {
|
|
|
15
21
|
*/
|
|
16
22
|
shouldGoToNextSection: boolean;
|
|
17
23
|
}
|
|
18
|
-
export interface
|
|
19
|
-
state: UseFieldState<TValue>;
|
|
20
|
-
value: TValue;
|
|
24
|
+
export interface UseFieldStateReturnValue<TValue extends PickerValidValue> {
|
|
21
25
|
activeSectionIndex: number | null;
|
|
26
|
+
areAllSectionsEmpty: boolean;
|
|
27
|
+
error: boolean;
|
|
28
|
+
localizedDigits: string[];
|
|
22
29
|
parsedSelectedSections: FieldParsedSelectedSections;
|
|
23
|
-
|
|
30
|
+
sectionOrder: SectionOrdering;
|
|
31
|
+
sectionsValueBoundaries: FieldSectionsValueBoundaries;
|
|
32
|
+
state: UseFieldState<TValue>;
|
|
33
|
+
timezone: PickersTimezone;
|
|
34
|
+
value: TValue;
|
|
24
35
|
clearValue: () => void;
|
|
25
36
|
clearActiveSection: () => void;
|
|
26
|
-
|
|
27
|
-
|
|
37
|
+
setCharacterQuery: (characterQuery: CharacterEditingQuery | null) => void;
|
|
38
|
+
setSelectedSections: (sections: FieldSelectedSections) => void;
|
|
28
39
|
setTempAndroidValueStr: (tempAndroidValueStr: string | null) => void;
|
|
29
|
-
|
|
40
|
+
updateSectionValue: (parameters: UpdateSectionValueParameters<TValue>) => void;
|
|
41
|
+
updateValueFromValueStr: (valueStr: string) => void;
|
|
30
42
|
getSectionsFromValue: (value: TValue, fallbackSections?: InferFieldSection<TValue>[] | null) => InferFieldSection<TValue>[];
|
|
31
|
-
localizedDigits: string[];
|
|
32
|
-
timezone: PickersTimezone;
|
|
33
43
|
}
|
|
34
|
-
export
|
|
44
|
+
export {};
|
|
@@ -2,25 +2,30 @@ import _extends from "@babel/runtime/helpers/esm/extends";
|
|
|
2
2
|
import * as React from 'react';
|
|
3
3
|
import useControlled from '@mui/utils/useControlled';
|
|
4
4
|
import useTimeout from '@mui/utils/useTimeout';
|
|
5
|
+
import useEventCallback from '@mui/utils/useEventCallback';
|
|
5
6
|
import { useRtl } from '@mui/system/RtlProvider';
|
|
6
7
|
import { usePickerTranslations } from "../../../hooks/usePickerTranslations.js";
|
|
7
8
|
import { useUtils, useLocalizationContext } from "../useUtils.js";
|
|
8
|
-
import { mergeDateIntoReferenceDate, getSectionsBoundaries, validateSections, getDateFromDateSections, parseSelectedSections, getLocalizedDigits } from "./useField.utils.js";
|
|
9
|
+
import { mergeDateIntoReferenceDate, getSectionsBoundaries, validateSections, getDateFromDateSections, parseSelectedSections, getLocalizedDigits, getSectionOrder } from "./useField.utils.js";
|
|
9
10
|
import { buildSectionsFromFormat } from "./buildSectionsFromFormat.js";
|
|
11
|
+
import { useValidation } from "../../../validation/index.js";
|
|
10
12
|
import { useControlledValue } from "../useControlledValue.js";
|
|
11
13
|
import { getSectionTypeGranularity } from "../../utils/getDefaultReferenceDate.js";
|
|
12
|
-
|
|
14
|
+
const QUERY_LIFE_DURATION_MS = 5000;
|
|
15
|
+
export const useFieldState = parameters => {
|
|
13
16
|
const utils = useUtils();
|
|
14
17
|
const translations = usePickerTranslations();
|
|
15
18
|
const adapter = useLocalizationContext();
|
|
16
19
|
const isRtl = useRtl();
|
|
17
20
|
const {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
manager: {
|
|
22
|
+
validator,
|
|
23
|
+
valueType,
|
|
24
|
+
internal_valueManager: valueManager,
|
|
25
|
+
internal_fieldValueManager: fieldValueManager
|
|
26
|
+
},
|
|
27
|
+
internalPropsWithDefaults,
|
|
28
|
+
internalPropsWithDefaults: {
|
|
24
29
|
value: valueProp,
|
|
25
30
|
defaultValue,
|
|
26
31
|
referenceDate: referenceDateProp,
|
|
@@ -32,8 +37,11 @@ export const useFieldState = params => {
|
|
|
32
37
|
shouldRespectLeadingZeros = false,
|
|
33
38
|
timezone: timezoneProp,
|
|
34
39
|
enableAccessibleFieldDOMStructure = true
|
|
40
|
+
},
|
|
41
|
+
forwardedProps: {
|
|
42
|
+
error: errorProp
|
|
35
43
|
}
|
|
36
|
-
} =
|
|
44
|
+
} = parameters;
|
|
37
45
|
const {
|
|
38
46
|
value,
|
|
39
47
|
handleValueChange,
|
|
@@ -51,6 +59,23 @@ export const useFieldState = params => {
|
|
|
51
59
|
React.useEffect(() => {
|
|
52
60
|
valueRef.current = value;
|
|
53
61
|
}, [value]);
|
|
62
|
+
const {
|
|
63
|
+
hasValidationError
|
|
64
|
+
} = useValidation({
|
|
65
|
+
props: internalPropsWithDefaults,
|
|
66
|
+
validator,
|
|
67
|
+
timezone,
|
|
68
|
+
value,
|
|
69
|
+
onError: internalPropsWithDefaults.onError
|
|
70
|
+
});
|
|
71
|
+
const error = React.useMemo(() => {
|
|
72
|
+
// only override when `error` is undefined.
|
|
73
|
+
// in case of multi input fields, the `error` value is provided externally and will always be defined.
|
|
74
|
+
if (errorProp !== undefined) {
|
|
75
|
+
return errorProp;
|
|
76
|
+
}
|
|
77
|
+
return hasValidationError;
|
|
78
|
+
}, [hasValidationError, errorProp]);
|
|
54
79
|
const localizedDigits = React.useMemo(() => getLocalizedDigits(utils), [utils]);
|
|
55
80
|
const sectionsValueBoundaries = React.useMemo(() => getSectionsBoundaries(utils, localizedDigits, timezone), [utils, localizedDigits, timezone]);
|
|
56
81
|
const getSectionsFromValue = React.useCallback(valueToAnalyze => fieldValueManager.getSectionsFromValue(valueToAnalyze, date => buildSectionsFromFormat({
|
|
@@ -75,14 +100,15 @@ export const useFieldState = params => {
|
|
|
75
100
|
isRtl,
|
|
76
101
|
locale: utils.locale
|
|
77
102
|
},
|
|
78
|
-
tempValueStrAndroid: null
|
|
103
|
+
tempValueStrAndroid: null,
|
|
104
|
+
characterQuery: null
|
|
79
105
|
};
|
|
80
106
|
const granularity = getSectionTypeGranularity(sections);
|
|
81
107
|
const referenceValue = valueManager.getInitialReferenceValue({
|
|
82
108
|
referenceDate: referenceDateProp,
|
|
83
109
|
value,
|
|
84
110
|
utils,
|
|
85
|
-
props:
|
|
111
|
+
props: internalPropsWithDefaults,
|
|
86
112
|
granularity,
|
|
87
113
|
timezone
|
|
88
114
|
});
|
|
@@ -102,13 +128,15 @@ export const useFieldState = params => {
|
|
|
102
128
|
};
|
|
103
129
|
const parsedSelectedSections = React.useMemo(() => parseSelectedSections(selectedSections, state.sections), [selectedSections, state.sections]);
|
|
104
130
|
const activeSectionIndex = parsedSelectedSections === 'all' ? 0 : parsedSelectedSections;
|
|
131
|
+
const sectionOrder = React.useMemo(() => getSectionOrder(state.sections, isRtl && !enableAccessibleFieldDOMStructure), [state.sections, isRtl, enableAccessibleFieldDOMStructure]);
|
|
132
|
+
const areAllSectionsEmpty = React.useMemo(() => state.sections.every(section => section.value === ''), [state.sections]);
|
|
105
133
|
const publishValue = newValue => {
|
|
106
134
|
const context = {
|
|
107
135
|
validationError: validator({
|
|
108
136
|
adapter,
|
|
109
137
|
value: newValue,
|
|
110
138
|
timezone,
|
|
111
|
-
props:
|
|
139
|
+
props: internalPropsWithDefaults
|
|
112
140
|
})
|
|
113
141
|
};
|
|
114
142
|
handleValueChange(newValue, context);
|
|
@@ -122,7 +150,7 @@ export const useFieldState = params => {
|
|
|
122
150
|
return newSections;
|
|
123
151
|
};
|
|
124
152
|
const sectionToUpdateOnNextInvalidDateRef = React.useRef(null);
|
|
125
|
-
const
|
|
153
|
+
const updateSectionValueOnNextInvalidDateTimeout = useTimeout();
|
|
126
154
|
const setSectionUpdateToApplyOnNextInvalidDate = newSectionValue => {
|
|
127
155
|
if (activeSectionIndex == null) {
|
|
128
156
|
return;
|
|
@@ -131,7 +159,7 @@ export const useFieldState = params => {
|
|
|
131
159
|
sectionIndex: activeSectionIndex,
|
|
132
160
|
value: newSectionValue
|
|
133
161
|
};
|
|
134
|
-
|
|
162
|
+
updateSectionValueOnNextInvalidDateTimeout.start(0, () => {
|
|
135
163
|
sectionToUpdateOnNextInvalidDateRef.current = null;
|
|
136
164
|
});
|
|
137
165
|
};
|
|
@@ -141,9 +169,13 @@ export const useFieldState = params => {
|
|
|
141
169
|
sections: prevState.sections.map(section => _extends({}, section, {
|
|
142
170
|
value: ''
|
|
143
171
|
})),
|
|
144
|
-
tempValueStrAndroid: null
|
|
172
|
+
tempValueStrAndroid: null,
|
|
173
|
+
characterQuery: null
|
|
145
174
|
}));
|
|
146
175
|
} else {
|
|
176
|
+
setState(prevState => _extends({}, prevState, {
|
|
177
|
+
characterQuery: null
|
|
178
|
+
}));
|
|
147
179
|
publishValue(valueManager.emptyValue);
|
|
148
180
|
}
|
|
149
181
|
};
|
|
@@ -159,9 +191,13 @@ export const useFieldState = params => {
|
|
|
159
191
|
if (fieldValueManager.getDateFromSection(value, activeSection) === null) {
|
|
160
192
|
setState(prevState => _extends({}, prevState, {
|
|
161
193
|
sections: setSectionValue(activeSectionIndex, ''),
|
|
162
|
-
tempValueStrAndroid: null
|
|
194
|
+
tempValueStrAndroid: null,
|
|
195
|
+
characterQuery: null
|
|
163
196
|
}));
|
|
164
197
|
} else {
|
|
198
|
+
setState(prevState => _extends({}, prevState, {
|
|
199
|
+
characterQuery: null
|
|
200
|
+
}));
|
|
165
201
|
publishValue(fieldValueManager.updateDateInValue(value, activeSection, null));
|
|
166
202
|
}
|
|
167
203
|
};
|
|
@@ -187,14 +223,14 @@ export const useFieldState = params => {
|
|
|
187
223
|
const newValue = fieldValueManager.parseValueStr(valueStr, state.referenceValue, parseDateStr);
|
|
188
224
|
publishValue(newValue);
|
|
189
225
|
};
|
|
190
|
-
const
|
|
226
|
+
const cleanActiveDateSectionsIfValueNullTimeout = useTimeout();
|
|
191
227
|
const updateSectionValue = ({
|
|
192
228
|
section,
|
|
193
229
|
newSectionValue,
|
|
194
230
|
shouldGoToNextSection
|
|
195
231
|
}) => {
|
|
196
|
-
|
|
197
|
-
|
|
232
|
+
updateSectionValueOnNextInvalidDateTimeout.clear();
|
|
233
|
+
cleanActiveDateSectionsIfValueNullTimeout.clear();
|
|
198
234
|
const activeDate = fieldValueManager.getDateFromSection(value, section);
|
|
199
235
|
|
|
200
236
|
/**
|
|
@@ -219,7 +255,7 @@ export const useFieldState = params => {
|
|
|
219
255
|
if (utils.isValid(newActiveDate)) {
|
|
220
256
|
const mergedDate = mergeDateIntoReferenceDate(utils, newActiveDate, newActiveDateSections, fieldValueManager.getDateFromSection(state.referenceValue, section), true);
|
|
221
257
|
if (activeDate == null) {
|
|
222
|
-
|
|
258
|
+
cleanActiveDateSectionsIfValueNullTimeout.start(0, () => {
|
|
223
259
|
if (valueRef.current === value) {
|
|
224
260
|
setState(prevState => _extends({}, prevState, {
|
|
225
261
|
sections: fieldValueManager.clearDateSections(state.sections, section),
|
|
@@ -258,9 +294,14 @@ export const useFieldState = params => {
|
|
|
258
294
|
tempValueStrAndroid: null
|
|
259
295
|
}));
|
|
260
296
|
};
|
|
261
|
-
const setTempAndroidValueStr = tempValueStrAndroid => setState(
|
|
297
|
+
const setTempAndroidValueStr = tempValueStrAndroid => setState(prevState => _extends({}, prevState, {
|
|
262
298
|
tempValueStrAndroid
|
|
263
299
|
}));
|
|
300
|
+
const setCharacterQuery = useEventCallback(newCharacterQuery => {
|
|
301
|
+
setState(prevState => _extends({}, prevState, {
|
|
302
|
+
characterQuery: newCharacterQuery
|
|
303
|
+
}));
|
|
304
|
+
});
|
|
264
305
|
|
|
265
306
|
// If `prop.value` changes, we update the state to reflect the new value
|
|
266
307
|
if (value !== state.lastExternalValue) {
|
|
@@ -292,28 +333,60 @@ export const useFieldState = params => {
|
|
|
292
333
|
locale: utils.locale
|
|
293
334
|
},
|
|
294
335
|
sections,
|
|
295
|
-
tempValueStrAndroid: null
|
|
336
|
+
tempValueStrAndroid: null,
|
|
337
|
+
characterQuery: null
|
|
296
338
|
}));
|
|
297
339
|
}
|
|
340
|
+
if (state.characterQuery != null && !error && activeSectionIndex == null) {
|
|
341
|
+
setCharacterQuery(null);
|
|
342
|
+
}
|
|
343
|
+
if (state.characterQuery != null && state.sections[state.characterQuery.sectionIndex]?.type !== state.characterQuery.sectionType) {
|
|
344
|
+
setCharacterQuery(null);
|
|
345
|
+
}
|
|
298
346
|
React.useEffect(() => {
|
|
299
347
|
if (sectionToUpdateOnNextInvalidDateRef.current != null) {
|
|
300
348
|
sectionToUpdateOnNextInvalidDateRef.current = null;
|
|
301
349
|
}
|
|
302
350
|
});
|
|
351
|
+
const cleanCharacterQueryTimeout = useTimeout();
|
|
352
|
+
React.useEffect(() => {
|
|
353
|
+
if (state.characterQuery != null) {
|
|
354
|
+
cleanCharacterQueryTimeout.start(QUERY_LIFE_DURATION_MS, () => setCharacterQuery(null));
|
|
355
|
+
}
|
|
356
|
+
return () => {};
|
|
357
|
+
}, [state.characterQuery, setCharacterQuery, cleanCharacterQueryTimeout]);
|
|
358
|
+
|
|
359
|
+
// If `tempValueStrAndroid` is still defined for some section when running `useEffect`,
|
|
360
|
+
// Then `onChange` has only been called once, which means the user pressed `Backspace` to reset the section.
|
|
361
|
+
// This causes a small flickering on Android,
|
|
362
|
+
// But we can't use `useEnhancedEffect` which is always called before the second `onChange` call and then would cause false positives.
|
|
363
|
+
React.useEffect(() => {
|
|
364
|
+
if (state.tempValueStrAndroid != null && activeSectionIndex != null) {
|
|
365
|
+
clearActiveSection();
|
|
366
|
+
}
|
|
367
|
+
}, [state.sections]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
368
|
+
|
|
303
369
|
return {
|
|
304
|
-
|
|
305
|
-
value,
|
|
370
|
+
// States and derived states
|
|
306
371
|
activeSectionIndex,
|
|
372
|
+
areAllSectionsEmpty,
|
|
373
|
+
error,
|
|
374
|
+
localizedDigits,
|
|
307
375
|
parsedSelectedSections,
|
|
308
|
-
|
|
376
|
+
sectionOrder,
|
|
377
|
+
sectionsValueBoundaries,
|
|
378
|
+
state,
|
|
379
|
+
timezone,
|
|
380
|
+
value,
|
|
381
|
+
// Methods to update the states
|
|
309
382
|
clearValue,
|
|
310
383
|
clearActiveSection,
|
|
384
|
+
setCharacterQuery,
|
|
385
|
+
setSelectedSections,
|
|
386
|
+
setTempAndroidValueStr,
|
|
311
387
|
updateSectionValue,
|
|
312
388
|
updateValueFromValueStr,
|
|
313
|
-
|
|
314
|
-
getSectionsFromValue
|
|
315
|
-
sectionsValueBoundaries,
|
|
316
|
-
localizedDigits,
|
|
317
|
-
timezone
|
|
389
|
+
// Utilities methods
|
|
390
|
+
getSectionsFromValue
|
|
318
391
|
};
|
|
319
392
|
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UseFieldParameters, UseFieldProps, UseFieldReturnValue } from "./useField.types.js";
|
|
2
2
|
import { InferFieldSection } from "../../../models/index.js";
|
|
3
3
|
import { PickerValidValue } from "../../models/index.js";
|
|
4
|
+
export declare const addPositionPropertiesToSections: <TValue extends PickerValidValue>(sections: InferFieldSection<TValue>[], localizedDigits: string[], isRtl: boolean) => FieldSectionWithPositions<TValue>[];
|
|
5
|
+
export declare const useFieldV6TextField: <TValue extends PickerValidValue, TError, TValidationProps extends {}, TProps extends UseFieldProps<false>>(parameters: UseFieldParameters<TValue, false, TError, TValidationProps, TProps>) => UseFieldReturnValue<false, TProps>;
|
|
4
6
|
type FieldSectionWithPositions<TValue extends PickerValidValue> = InferFieldSection<TValue> & {
|
|
5
7
|
/**
|
|
6
8
|
* Start index of the section in the format
|
|
@@ -21,6 +23,4 @@ type FieldSectionWithPositions<TValue extends PickerValidValue> = InferFieldSect
|
|
|
21
23
|
*/
|
|
22
24
|
endInInput: number;
|
|
23
25
|
};
|
|
24
|
-
export declare const addPositionPropertiesToSections: <TValue extends PickerValidValue>(sections: InferFieldSection<TValue>[], localizedDigits: string[], isRtl: boolean) => FieldSectionWithPositions<TValue>[];
|
|
25
|
-
export declare const useFieldV6TextField: UseFieldTextField<false>;
|
|
26
26
|
export {};
|