Skip to content

Commit 9ee83e6

Browse files
[field] Improve the field controlled edition (#17816)
1 parent f75bef8 commit 9ee83e6

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

packages/x-date-pickers/src/internals/hooks/useField/useFieldCharacterEditing.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,13 @@ export const useFieldCharacterEditing = <TValue extends PickerValidValue>({
200200
};
201201

202202
const applyNumericEditing: CharacterEditingApplier = (params) => {
203-
const getNewSectionValue = (
204-
queryValue: string,
203+
const getNewSectionValue = ({
204+
queryValue,
205+
skipIfBelowMinimum,
206+
section,
207+
}: {
208+
queryValue: string;
209+
skipIfBelowMinimum: boolean;
205210
section: Pick<
206211
FieldSection,
207212
| 'format'
@@ -210,8 +215,8 @@ export const useFieldCharacterEditing = <TValue extends PickerValidValue>({
210215
| 'hasLeadingZerosInFormat'
211216
| 'hasLeadingZerosInInput'
212217
| 'maxLength'
213-
>,
214-
): ReturnType<QueryApplier<TValue>> => {
218+
>;
219+
}): ReturnType<QueryApplier<TValue>> => {
215220
const cleanQueryValue = removeLocalizedDigits(queryValue, localizedDigits);
216221
const queryValueNumber = Number(cleanQueryValue);
217222
const sectionBoundaries = sectionsValueBoundaries[section.type]({
@@ -227,7 +232,7 @@ export const useFieldCharacterEditing = <TValue extends PickerValidValue>({
227232
// If the user types `0` on a month section,
228233
// It is below the minimum, but we want to store the `0` in the query,
229234
// So that when he pressed `1`, it will store `01` and move to the next section.
230-
if (queryValueNumber < sectionBoundaries.minimum) {
235+
if (skipIfBelowMinimum && queryValueNumber < sectionBoundaries.minimum) {
231236
return { saveQuery: true };
232237
}
233238

@@ -254,7 +259,11 @@ export const useFieldCharacterEditing = <TValue extends PickerValidValue>({
254259
activeSection.contentType === 'digit' ||
255260
activeSection.contentType === 'digit-with-letter'
256261
) {
257-
return getNewSectionValue(queryValue, activeSection);
262+
return getNewSectionValue({
263+
queryValue,
264+
skipIfBelowMinimum: false,
265+
section: activeSection,
266+
});
258267
}
259268

260269
// When editing a letter-format month and the user presses a digit,
@@ -267,13 +276,17 @@ export const useFieldCharacterEditing = <TValue extends PickerValidValue>({
267276
'MM',
268277
);
269278

270-
const response = getNewSectionValue(queryValue, {
271-
type: activeSection.type,
272-
format: 'MM',
273-
hasLeadingZerosInFormat,
274-
hasLeadingZerosInInput: true,
275-
contentType: 'digit',
276-
maxLength: 2,
279+
const response = getNewSectionValue({
280+
queryValue,
281+
skipIfBelowMinimum: true,
282+
section: {
283+
type: activeSection.type,
284+
format: 'MM',
285+
hasLeadingZerosInFormat,
286+
hasLeadingZerosInInput: true,
287+
contentType: 'digit',
288+
maxLength: 2,
289+
},
277290
});
278291

279292
if (isQueryResponseWithoutValue(response)) {
@@ -296,7 +309,11 @@ export const useFieldCharacterEditing = <TValue extends PickerValidValue>({
296309
// When editing a letter-format weekDay and the user presses a digit,
297310
// We can support the numeric editing by returning the nth day in the week day array.
298311
if (activeSection.type === 'weekDay') {
299-
const response = getNewSectionValue(queryValue, activeSection);
312+
const response = getNewSectionValue({
313+
queryValue,
314+
skipIfBelowMinimum: true,
315+
section: activeSection,
316+
});
300317
if (isQueryResponseWithoutValue(response)) {
301318
return response;
302319
}

packages/x-date-pickers/src/internals/hooks/useField/useFieldState.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,13 @@ export const useFieldState = <
373373
}
374374

375375
/**
376-
* If all the sections are filled but the date is invalid,
376+
* If all the sections are filled but the date is invalid and the previous date is valid or null,
377377
* Then we publish an invalid date.
378378
*/
379-
if (newActiveDateSections.every((sectionBis) => sectionBis.value !== '')) {
379+
if (
380+
newActiveDateSections.every((sectionBis) => sectionBis.value !== '') &&
381+
(activeDate == null || utils.isValid(activeDate))
382+
) {
380383
setSectionUpdateToApplyOnNextInvalidDate(newSectionValue);
381384
return publishValue(fieldValueManager.updateDateInValue(value, section, newActiveDate));
382385
}

0 commit comments

Comments
 (0)