diff --git a/components/lib/inputnumber/InputNumber.js b/components/lib/inputnumber/InputNumber.js index 53546e2872..98514e155c 100644 --- a/components/lib/inputnumber/InputNumber.js +++ b/components/lib/inputnumber/InputNumber.js @@ -318,12 +318,44 @@ export const InputNumber = React.memo( isSpecialChar.current = false; }; + const onInputAndroidKey = (event) => { + if (!DomHandler.isAndroid() || 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; + + if (code !== 13) { + // to submit a form + event.preventDefault(); + } + + const char = String.fromCharCode(code); + const _isDecimalSign = isDecimalSign(char); + const _isMinusSign = isMinusSign(char); + + if ((48 <= code && code <= 57) || _isMinusSign || _isDecimalSign) { + insert(event, char, { isDecimalSign: _isDecimalSign, isMinusSign: _isMinusSign }); + } else { + updateValue(event, event.target.value, null, 'delete-single'); + } + }; + const onInputKeyDown = (event) => { if (props.disabled || props.readOnly) { return; } - if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) { + if (event.altKey || event.ctrlKey || event.metaKey) { isSpecialChar.current = true; return; @@ -340,6 +372,11 @@ export const InputNumber = React.memo( lastValue.current = event.target.value; + // Android is handled specially in onInputAndroidKey + if (DomHandler.isAndroid()) { + return; + } + let selectionStart = event.target.selectionStart; let selectionEnd = event.target.selectionEnd; let inputValue = event.target.value; @@ -430,7 +467,6 @@ export const InputNumber = React.memo( newValueStr = deleteRange(inputValue, selectionStart, selectionEnd); updateValue(event, newValueStr, null, 'delete-range'); } - break; // del @@ -1123,6 +1159,7 @@ export const InputNumber = React.memo( name={props.name} autoFocus={props.autoFocus} onKeyDown={onInputKeyDown} + onKeyPress={onInputAndroidKey} onInput={onInput} onClick={onInputClick} onPointerDown={onInputPointerDown}