Fix invalid value being propagated on change event for number input (#44195)

This commit is contained in:
Nathan Silveira 2024-02-01 12:49:12 -03:00 committed by GitHub
parent 861cc7cc02
commit 862383e1eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 1 deletions

View File

@ -0,0 +1,5 @@
Significance: patch
Type: fix
Comment: Fix invalid value being propagated on change event for number input

View File

@ -19,6 +19,8 @@ type Props = {
onKeyDown?: ( event: React.KeyboardEvent< HTMLInputElement > ) => void;
};
const NOT_NUMBERS_OR_SEPARATORS_REGEX = /[^0-9,.]/g;
export const useNumberInputProps = ( {
value,
onChange,
@ -56,7 +58,9 @@ export const useNumberInputProps = ( {
}
},
onChange( newValue: string ) {
const sanitizeValue = parseNumber( newValue );
const sanitizeValue = parseNumber(
newValue.replace( NOT_NUMBERS_OR_SEPARATORS_REGEX, '' )
);
onChange( sanitizeValue );
},
};