From bcc0da4b0e4a4f64d64586b956738b1d0e0931d1 Mon Sep 17 00:00:00 2001 From: Flavien DELANGLE Date: Mon, 17 Jun 2024 12:35:36 +0200 Subject: [PATCH] [fields] Do not apply digit editing when pressing `Space` (#13510) --- .../src/TimeField/tests/editing.TimeField.test.tsx | 7 +++++++ .../src/internals/hooks/useField/useField.utils.ts | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/x-date-pickers/src/TimeField/tests/editing.TimeField.test.tsx b/packages/x-date-pickers/src/TimeField/tests/editing.TimeField.test.tsx index bd0311e210bb7..d9736f41ad511 100644 --- a/packages/x-date-pickers/src/TimeField/tests/editing.TimeField.test.tsx +++ b/packages/x-date-pickers/src/TimeField/tests/editing.TimeField.test.tsx @@ -621,6 +621,13 @@ describe(' - Editing', () => { keyStrokes: [{ value: 'p', expected: 'PM' }], }); }); + + it('should not edit when pressing the Space key', () => { + testFieldChange({ + format: adapter.formats.hours24h, + keyStrokes: [{ value: ' ', expected: 'hh' }], + }); + }); }); describeAdapters( diff --git a/packages/x-date-pickers/src/internals/hooks/useField/useField.utils.ts b/packages/x-date-pickers/src/internals/hooks/useField/useField.utils.ts index 34b84f4fa72db..1d5719f585e80 100644 --- a/packages/x-date-pickers/src/internals/hooks/useField/useField.utils.ts +++ b/packages/x-date-pickers/src/internals/hooks/useField/useField.utils.ts @@ -169,7 +169,8 @@ export const applyLocalizedDigits = (valueStr: string, localizedDigits: string[] export const isStringNumber = (valueStr: string, localizedDigits: string[]) => { const nonLocalizedValueStr = removeLocalizedDigits(valueStr, localizedDigits); - return !Number.isNaN(Number(nonLocalizedValueStr)); + // `Number(' ')` returns `0` even if ' ' is not a valid number. + return nonLocalizedValueStr !== ' ' && !Number.isNaN(Number(nonLocalizedValueStr)); }; /**