Skip to content

Commit

Permalink
Fix #4875: InputNumber minus sign with currency (#4904)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware authored Sep 7, 2023
1 parent c8df483 commit 137d0c7
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions components/lib/inputnumber/InputNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,9 @@ export const InputNumber = React.memo(

if (selectionStart === selectionEnd) {
const deleteChar = inputValue.charAt(selectionStart - 1);
const { decimalCharIndex, decimalCharIndexWithoutPrefix } = getDecimalCharIndexes(inputValue);

if (isNumeralChar(deleteChar)) {
const { decimalCharIndex, decimalCharIndexWithoutPrefix } = getDecimalCharIndexes(inputValue);
const decimalLength = getDecimalLength(inputValue);

if (_group.current.test(deleteChar)) {
Expand All @@ -402,6 +402,12 @@ export const InputNumber = React.memo(
} else {
newValueStr = inputValue.slice(0, selectionStart - 1) + inputValue.slice(selectionStart);
}
} else if (_currency.current.test(deleteChar)) {
const { minusCharIndex, currencyCharIndex } = getCharIndexes(inputValue);

if (minusCharIndex === currencyCharIndex - 1) {
newValueStr = inputValue.slice(0, minusCharIndex) + inputValue.slice(selectionStart);
}
}

updateValue(event, newValueStr, null, 'delete-single');
Expand Down Expand Up @@ -473,18 +479,16 @@ export const InputNumber = React.memo(
}
}

const key = event.key;
const code = event.which || event.keyCode;

if (key !== 'Enter') {
if (code !== 13) {
// to submit a form
event.preventDefault();
}

const _isDecimalSign = isDecimalSign(key);
const _isMinusSign = isMinusSign(key);

const code = event.which || event.keyCode;
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 });
Expand Down Expand Up @@ -583,10 +587,12 @@ export const InputNumber = React.memo(
let newValueStr;

if (sign.isMinusSign) {
if (selectionStart === 0) {
const isNewMinusSign = minusCharIndex === -1;

if (isNewMinusSign && (selectionStart === 0 || selectionStart === currencyCharIndex + 1)) {
newValueStr = inputValue;

if (minusCharIndex === -1 || selectionEnd !== 0) {
if (isNewMinusSign || selectionEnd !== 0) {
newValueStr = insertText(inputValue, text, 0, selectionEnd);
}

Expand Down Expand Up @@ -1037,7 +1043,7 @@ export const InputNumber = React.memo(
name={props.name}
autoFocus={props.autoFocus}
onKeyDown={onInputKeyDown}
onKeyUp={onInputKeyUp}
onKeyPress={onInputKeyUp}
onInput={onInput}
onClick={onInputClick}
onBlur={onInputBlur}
Expand Down

0 comments on commit 137d0c7

Please sign in to comment.