From cc9602048e4bf77db9bdccd633c81a0d2c3c1f73 Mon Sep 17 00:00:00 2001 From: Melloware Date: Fri, 15 Mar 2024 13:43:08 -0400 Subject: [PATCH] Fix #6047:InputNumber remove onKeyPress (#6158) --- components/lib/inputnumber/InputNumber.js | 54 ++++++--------------- components/lib/inputnumber/inputnumber.d.ts | 5 -- 2 files changed, 15 insertions(+), 44 deletions(-) diff --git a/components/lib/inputnumber/InputNumber.js b/components/lib/inputnumber/InputNumber.js index 3bd88f7796..8ea484a107 100644 --- a/components/lib/inputnumber/InputNumber.js +++ b/components/lib/inputnumber/InputNumber.js @@ -322,6 +322,12 @@ export const InputNumber = React.memo( return; } + if (event.shiftKey || event.altKey) { + isSpecialChar.current = true; + + return; + } + if (props.onKeyDown) { props.onKeyDown(event); @@ -333,21 +339,11 @@ export const InputNumber = React.memo( lastValue.current = event.target.value; - if (event.shiftKey || event.altKey) { - isSpecialChar.current = true; - - return; - } - let selectionStart = event.target.selectionStart; let selectionEnd = event.target.selectionEnd; let inputValue = event.target.value; let newValueStr = null; - if (event.altKey) { - event.preventDefault(); - } - switch (event.code) { //up case 'ArrowUp': @@ -379,6 +375,7 @@ export const InputNumber = React.memo( //enter and tab case 'Tab': + case 'NumpadEnter': case 'Enter': newValueStr = validateValue(parseValue(inputValue)); inputRef.current.value = formatValue(newValueStr); @@ -494,37 +491,17 @@ export const InputNumber = React.memo( break; default: - break; - } - }; - - const onInputKeyUp = (event) => { - if (props.disabled || props.readOnly) { - return; - } - - if (props.onKeyUp) { - props.onKeyUp(event); - - // do not continue if the user defined event wants to prevent - if (event.defaultPrevented) { - return; - } - } - - const code = event.which || event.keyCode; + event.preventDefault(); - if (code !== 13) { - // to submit a form - event.preventDefault(); - } + let char = event.key; + const _isDecimalSign = isDecimalSign(char); + const _isMinusSign = isMinusSign(char); - const char = String.fromCharCode(code); - const _isDecimalSign = isDecimalSign(char); - const _isMinusSign = isMinusSign(char); + if (((event.code.startsWith('Digit') || event.code.startsWith('Numpad')) && Number(char) >= 0 && Number(char) <= 9) || _isMinusSign || _isDecimalSign) { + insert(event, char, { isDecimalSign: _isDecimalSign, isMinusSign: _isMinusSign }); + } - if ((48 <= code && code <= 57) || _isMinusSign || _isDecimalSign) { - insert(event, char, { isDecimalSign: _isDecimalSign, isMinusSign: _isMinusSign }); + break; } }; @@ -1137,7 +1114,6 @@ export const InputNumber = React.memo( name={props.name} autoFocus={props.autoFocus} onKeyDown={onInputKeyDown} - onKeyPress={onInputKeyUp} onInput={onInput} onClick={onInputClick} onPointerDown={onInputPointerDown} diff --git a/components/lib/inputnumber/inputnumber.d.ts b/components/lib/inputnumber/inputnumber.d.ts index ef19e8b7a8..a9e2330e55 100644 --- a/components/lib/inputnumber/inputnumber.d.ts +++ b/components/lib/inputnumber/inputnumber.d.ts @@ -312,11 +312,6 @@ export interface InputNumberProps extends Omit} event - Browser event */ onKeyDown?(event: React.KeyboardEvent): void; - /** - * Callback to invoke when the key released. - * @param {React.KeyboardEvent} event - Browser event - */ - onKeyUp?(event: React.KeyboardEvent): void; /** * Used to get the child elements of the component. * @readonly