mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
fix(Field): incorrect cursor position when using formatter (#11348)
This commit is contained in:
parent
74d77a6c12
commit
b27a3cedfe
@ -291,7 +291,9 @@ export default defineComponent({
|
|||||||
value: string,
|
value: string,
|
||||||
trigger: FieldFormatTrigger = 'onChange'
|
trigger: FieldFormatTrigger = 'onChange'
|
||||||
) => {
|
) => {
|
||||||
|
const originalValue = value;
|
||||||
value = limitValueLength(value);
|
value = limitValueLength(value);
|
||||||
|
const isExceedLimit = value !== originalValue;
|
||||||
|
|
||||||
if (props.type === 'number' || props.type === 'digit') {
|
if (props.type === 'number' || props.type === 'digit') {
|
||||||
const isNumber = props.type === 'number';
|
const isNumber = props.type === 'number';
|
||||||
@ -303,10 +305,18 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (inputRef.value && inputRef.value.value !== value) {
|
if (inputRef.value && inputRef.value.value !== value) {
|
||||||
const { selectionStart, selectionEnd } = inputRef.value;
|
// When the value length exceeds maxlength and the input is focused,
|
||||||
inputRef.value.value = value;
|
// correct the cursor position to be consistent with the native behavior.
|
||||||
if (state.focused) {
|
// https://github.com/youzan/vant/issues/11289
|
||||||
inputRef.value.setSelectionRange(selectionStart, selectionEnd);
|
if (state.focused && isExceedLimit) {
|
||||||
|
const { selectionStart, selectionEnd } = inputRef.value;
|
||||||
|
inputRef.value.value = value;
|
||||||
|
inputRef.value.setSelectionRange(
|
||||||
|
selectionStart! - 1,
|
||||||
|
selectionEnd! - 1
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
inputRef.value.value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user