insert the pasted content after the cursor when pasting numbers or amounts

This commit is contained in:
MaysWind
2025-11-23 01:44:40 +08:00
parent ed8c5c96ac
commit 10b9c09192

View File

@@ -160,12 +160,40 @@ export function useCommonNumberInputBase(props: CommonNumberInputProps, maxDecim
return;
}
const value = parseNumber(text);
const parsedText = parseNumber(text);
if (Number.isNaN(parsedText) || !Number.isFinite(parsedText)) {
e.preventDefault();
return;
}
const inputEl = e.target as HTMLInputElement;
const start = inputEl.selectionStart ?? 0;
const end = inputEl.selectionEnd ?? 0;
const fullText = currentValue.value.slice(0, start) + text + currentValue.value.slice(end);
const value = parseNumber(fullText);
const textualValue = formatNumber(value);
const decimalSeparator = getCurrentDecimalSeparator();
const hasDecimalSeparator = text.indexOf(decimalSeparator) >= 0;
const hasDecimalSeparator = fullText.indexOf(decimalSeparator) >= 0;
const pastedAmount = getValidFormattedValue(value, textualValue, hasDecimalSeparator);
let newPos: number = start + text.length;
currentValue.value = getValidFormattedValue(value, textualValue, hasDecimalSeparator);
if (pastedAmount.length !== fullText.length) {
if (newPos > pastedAmount.length) {
newPos = pastedAmount.length;
} else {
newPos = start;
}
}
if (window.requestAnimationFrame) {
window.requestAnimationFrame(() => {
inputEl.selectionStart = inputEl.selectionEnd = newPos;
});
}
currentValue.value = pastedAmount;
e.preventDefault();
}