mirror of
https://github.com/youzan/vant.git
synced 2026-01-12 07:01:45 +08:00
fix(Field): correctly limit maxlength when pasting multiple emojis (#13713)
This commit is contained in:
@@ -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('');
|
||||
}
|
||||
|
||||
@@ -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😀😀😀');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user