fix(Field): correctly limit maxlength when pasting multiple emojis (#13713)
Some checks failed
CodeQL / Analyze (javascript) (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / test (push) Has been cancelled
CI / build (push) Has been cancelled
Issue Close Require / issue-close-require (push) Has been cancelled

This commit is contained in:
inottn
2025-12-14 20:04:33 +08:00
committed by GitHub
parent 1ea0e9cc8e
commit db53e8d408
2 changed files with 21 additions and 1 deletions

View File

@@ -308,10 +308,11 @@ export default defineComponent({
}
// Remove redundant interpolated values,
// make it consistent with the native input maxlength behavior.
const selectionEnd = inputRef.value?.selectionEnd;
let selectionEnd = inputRef.value?.selectionEnd;
if (state.focused && selectionEnd) {
const valueArr = [...value];
const exceededLength = valueArr.length - +maxlength;
selectionEnd = getStringLength(value.slice(0, selectionEnd));
valueArr.splice(selectionEnd - exceededLength, exceededLength);
return valueArr.join('');
}

View File

@@ -644,3 +644,22 @@ test('should update selection range correctly when using formatter with emoji',
expect(input.element.selectionEnd).toEqual(4);
});
test('should limit maxlength correctly when pasting multiple emojis', async () => {
const wrapper = mount(Field, {
props: {
maxlength: 4,
modelValue: '',
},
});
const input = wrapper.find('input');
await input.trigger('focus');
input.element.value = '1😀😀😀😀';
input.element.selectionEnd = 9;
input.trigger('input');
expect(wrapper.emitted('update:modelValue')[0][0]).toEqual('1😀😀😀');
expect(input.element.value).toEqual('1😀😀😀');
});