fix: input-number 光标漂移问题

This commit is contained in:
allenve 2023-10-20 17:35:09 +08:00
parent aabc616051
commit 9f1941f433

View File

@ -317,8 +317,37 @@ export default class NumberControl extends React.Component<
}
onChange(resultValue);
setTimeout(() => {
this.changeCursorPos(+resultValue);
}, 0);
}
// 取真实用户输入的值去改变光标的位置
@autobind
changeCursorPos(value: number) {
if (isNaN(value)) {
return;
}
const {kilobitSeparator, prefix} = this.props;
let pos = `${value}`.length;
if (prefix) {
pos += prefix.length;
}
if (kilobitSeparator) {
// 处理有千分符的情况 123,456,789
const ksLen = Math.floor((`${Math.abs(value)}`.length - 1) / 3);
if (ksLen > 0) {
pos += ksLen;
}
}
if (this.input) {
this.input.setSelectionRange?.(pos, pos);
}
}
filterNum(value: number | string | undefined): number | undefined;
filterNum(
value: number | string | undefined,