Skip to content

Commit

Permalink
fix:InputNumber: Value pasted by the user is not entered when suffix …
Browse files Browse the repository at this point in the history
…and decimals are used (#5646)

* fix:InputNumber: Value pasted by the user is not entered when suffix and decimals are used

* fix:code format
  • Loading branch information
kl-nevermore authored Dec 26, 2023
1 parent 27042f0 commit 9d14e8b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions components/lib/inputnumber/InputNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,10 @@ export const InputNumber = React.memo(
}
};

const replaceSuffix = (value) => {
return value ? value.replace(_suffix.current, '').trim().replace(/\s/g, '').replace(_currency.current, '') : value;
};

const insertText = (value, text, start, end) => {
let textSplit = text === '.' ? text : text.split('.');

Expand All @@ -673,7 +677,7 @@ export const InputNumber = React.memo(

_decimal.current.lastIndex = 0;

return decimalCharIndex > 0 ? value.slice(0, start) + formatValue(text) + value.slice(end) : value || formatValue(text);
return decimalCharIndex > 0 ? value.slice(0, start) + formatValue(text) + replaceSuffix(value).slice(end) : value || formatValue(text);
} else if (end - start === value.length) {
return formatValue(text);
} else if (start === 0) {
Expand All @@ -683,7 +687,11 @@ export const InputNumber = React.memo(
} else if (end === value.length) {
return value.slice(0, start) + text;
} else {
return value.slice(0, start) + text + value.slice(end);
const selectionValue = value.slice(start, end);
// Fix: if the suffix starts with a space, the input will be cleared after pasting
const space = /\s$/.test(selectionValue) ? ' ' : '';

return value.slice(0, start) + text + space + value.slice(end);
}
};

Expand Down Expand Up @@ -949,7 +957,9 @@ export const InputNumber = React.memo(

_decimal.current.lastIndex = 0;

return decimalCharIndex !== -1 ? replaceDecimalSeparator(val1).split(_decimal.current)[0] + val2.slice(decimalCharIndex) : val1;
const newVal1 = replaceDecimalSeparator(val1).split(_decimal.current)[0].replace(_suffix.current, '').trim();

return decimalCharIndex !== -1 ? newVal1 + val2.slice(decimalCharIndex) : val1;
}

return val1;
Expand All @@ -960,7 +970,7 @@ export const InputNumber = React.memo(
const valueSplit = value.split(_decimal.current);

if (valueSplit.length === 2) {
return valueSplit[1].replace(_suffix.current, '').trim().replace(/\s/g, '').replace(_currency.current, '').length;
return replaceSuffix(valueSplit[1]).length;
}
}

Expand Down
2 changes: 1 addition & 1 deletion components/lib/utils/ObjectUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ export default class ObjectUtils {
}

static isLetter(char) {
return char && (char.toUpperCase() != char.toLowerCase() || char.codePointAt(0) > 127);
return /^[a-zA-Z\u00C0-\u017F]$/.test(char);
}

/**
Expand Down

0 comments on commit 9d14e8b

Please sign in to comment.