Skip to content

Commit

Permalink
[fields] Do not apply digit editing when pressing Space (#13510)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle authored and web-flow committed Jun 17, 2024
1 parent 2492f85 commit 79b60ea
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,13 @@ describe('<TimeField /> - 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,63 @@ export const cleanLeadingZeros = <TDate>(
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
Expand Down

0 comments on commit 79b60ea

Please sign in to comment.