From 79b60ea25882a97030a4d81245f8712ce2e5726a Mon Sep 17 00:00:00 2001 From: Flavien DELANGLE Date: Mon, 17 Jun 2024 12:35:36 +0200 Subject: [PATCH 1/4] [fields] Do not apply digit editing when pressing `Space` (#13510) --- .../tests/editing.TimeField.test.tsx | 7 +++ .../hooks/useField/useField.utils.ts | 57 +++++++++++++++++++ 2 files changed, 64 insertions(+) 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 3b389d300ae0..c92ec6a3ec85 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 @@ -330,6 +330,13 @@ describe(' - Editing', () => { keyStrokes: [{ value: '02:12 p', expected: '02:12 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 4cae569c6700..391e3420f4cd 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 @@ -117,6 +117,63 @@ export const cleanLeadingZeros = ( valueStr: string, size: number, ) => { + const today = utils.date(undefined); + const formattedZero = utils.formatByString( + utils.setSeconds(today, 0), + FORMAT_SECONDS_NO_LEADING_ZEROS, + ); + + if (formattedZero === '0') { + return NON_LOCALIZED_DIGITS; + } + + return Array.from({ length: 10 }).map((_, index) => + utils.formatByString(utils.setSeconds(today, index), FORMAT_SECONDS_NO_LEADING_ZEROS), + ); +}; + +export const removeLocalizedDigits = (valueStr: string, localizedDigits: string[]) => { + if (localizedDigits[0] === '0') { + return valueStr; + } + + const digits: string[] = []; + let currentFormattedDigit = ''; + for (let i = 0; i < valueStr.length; i += 1) { + currentFormattedDigit += valueStr[i]; + const matchingDigitIndex = localizedDigits.indexOf(currentFormattedDigit); + if (matchingDigitIndex > -1) { + digits.push(matchingDigitIndex.toString()); + currentFormattedDigit = ''; + } + } + + return digits.join(''); +}; + +export const applyLocalizedDigits = (valueStr: string, localizedDigits: string[]) => { + if (localizedDigits[0] === '0') { + return valueStr; + } + + return valueStr + .split('') + .map((char) => localizedDigits[Number(char)]) + .join(''); +}; + +export const isStringNumber = (valueStr: string, localizedDigits: string[]) => { + const nonLocalizedValueStr = removeLocalizedDigits(valueStr, localizedDigits); + // `Number(' ')` returns `0` even if ' ' is not a valid number. + return nonLocalizedValueStr !== ' ' && !Number.isNaN(Number(nonLocalizedValueStr)); +}; + +/** + * Remove the leading zeroes to a digit section value. + * E.g.: `03` => `3` + * Warning: Should only be called with non-localized digits. Call `removeLocalizedDigits` with your value if needed. + */ +export const cleanLeadingZeros = (valueStr: string, size: number) => { let cleanValueStr = valueStr; // Remove the leading zeros From 3f161d4ad644a8efebe8a1aa68e1c51d63dfb2ae Mon Sep 17 00:00:00 2001 From: Flavien DELANGLE Date: Mon, 17 Jun 2024 12:38:08 +0200 Subject: [PATCH 2/4] Update useField.utils.ts Signed-off-by: Flavien DELANGLE --- .../hooks/useField/useField.utils.ts | 57 ------------------- 1 file changed, 57 deletions(-) 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 391e3420f4cd..4cae569c6700 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 @@ -117,63 +117,6 @@ export const cleanLeadingZeros = ( valueStr: string, size: number, ) => { - const today = utils.date(undefined); - const formattedZero = utils.formatByString( - utils.setSeconds(today, 0), - FORMAT_SECONDS_NO_LEADING_ZEROS, - ); - - if (formattedZero === '0') { - return NON_LOCALIZED_DIGITS; - } - - return Array.from({ length: 10 }).map((_, index) => - utils.formatByString(utils.setSeconds(today, index), FORMAT_SECONDS_NO_LEADING_ZEROS), - ); -}; - -export const removeLocalizedDigits = (valueStr: string, localizedDigits: string[]) => { - if (localizedDigits[0] === '0') { - return valueStr; - } - - const digits: string[] = []; - let currentFormattedDigit = ''; - for (let i = 0; i < valueStr.length; i += 1) { - currentFormattedDigit += valueStr[i]; - const matchingDigitIndex = localizedDigits.indexOf(currentFormattedDigit); - if (matchingDigitIndex > -1) { - digits.push(matchingDigitIndex.toString()); - currentFormattedDigit = ''; - } - } - - return digits.join(''); -}; - -export const applyLocalizedDigits = (valueStr: string, localizedDigits: string[]) => { - if (localizedDigits[0] === '0') { - return valueStr; - } - - return valueStr - .split('') - .map((char) => localizedDigits[Number(char)]) - .join(''); -}; - -export const isStringNumber = (valueStr: string, localizedDigits: string[]) => { - const nonLocalizedValueStr = removeLocalizedDigits(valueStr, localizedDigits); - // `Number(' ')` returns `0` even if ' ' is not a valid number. - return nonLocalizedValueStr !== ' ' && !Number.isNaN(Number(nonLocalizedValueStr)); -}; - -/** - * Remove the leading zeroes to a digit section value. - * E.g.: `03` => `3` - * Warning: Should only be called with non-localized digits. Call `removeLocalizedDigits` with your value if needed. - */ -export const cleanLeadingZeros = (valueStr: string, size: number) => { let cleanValueStr = valueStr; // Remove the leading zeros From 8cdf8d239aea2f807220a002a8c1dba9b31dea75 Mon Sep 17 00:00:00 2001 From: Flavien DELANGLE Date: Mon, 17 Jun 2024 12:39:26 +0200 Subject: [PATCH 3/4] Update useFieldCharacterEditing.ts Signed-off-by: Flavien DELANGLE --- .../src/internals/hooks/useField/useFieldCharacterEditing.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/x-date-pickers/src/internals/hooks/useField/useFieldCharacterEditing.ts b/packages/x-date-pickers/src/internals/hooks/useField/useFieldCharacterEditing.ts index 65de36d9c8c5..f47121530915 100644 --- a/packages/x-date-pickers/src/internals/hooks/useField/useFieldCharacterEditing.ts +++ b/packages/x-date-pickers/src/internals/hooks/useField/useFieldCharacterEditing.ts @@ -384,7 +384,7 @@ export const useFieldCharacterEditing = ({ const applyCharacterEditing = useEventCallback((params: ApplyCharacterEditingParams) => { const activeSection = sections[params.sectionIndex]; - const isNumericEditing = !Number.isNaN(Number(params.keyPressed)); + const isNumericEditing = params.keyPressed !== ' ' && !Number.isNaN(Number(params.keyPressed)); const response = isNumericEditing ? applyNumericEditing(params) : applyLetterEditing(params); if (response == null) { setTempAndroidValueStr(null); From d2d3656e24e7c5e92c2be39024d4309829cc8e14 Mon Sep 17 00:00:00 2001 From: Lukas Date: Mon, 17 Jun 2024 19:19:47 +0300 Subject: [PATCH 4/4] Bump `caniuse-lite` to a newer version that seems to work --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index 3454c2ff1606..f67a91c98d8f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4873,9 +4873,9 @@ camelize@^1.0.0: integrity sha512-dU+Tx2fsypxTgtLoE36npi3UqcjSSMNYfkqgmoEhtZrraP5VWq0K7FkWVTYa8eMPtnU/G2txVsfdCJTn9uzpuQ== caniuse-lite@^1.0.30001406, caniuse-lite@^1.0.30001538, caniuse-lite@^1.0.30001565: - version "1.0.30001576" - resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001576.tgz#893be772cf8ee6056d6c1e2d07df365b9ec0a5c4" - integrity sha512-ff5BdakGe2P3SQsMsiqmt1Lc8221NR1VzHj5jXN5vBny9A6fpze94HiVV/n7XRosOlsShJcvMv5mdnpjOGCEgg== + version "1.0.30001587" + resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001587.tgz#a0bce920155fa56a1885a69c74e1163fc34b4881" + integrity sha512-HMFNotUmLXn71BQxg8cijvqxnIAofforZOwGsxyXJ0qugTdspUF4sPSJ2vhgprHCB996tIDzEq1ubumPDV8ULA== chai-dom@^1.12.0: version "1.12.0"