Skip to content

Commit 37e7fb7

Browse files
authored
fix(tag-input): prevent backspace event in readonly mode and add test (#3376)
1 parent cadfdaa commit 37e7fb7

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/tag-input/__tests__/vitest-tag-input.test.jsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ describe('TagInput Component', () => {
210210
expect(wrapper.attributes('placeholder')).toBe('This is TagInput placeholder');
211211
});
212212

213-
it('props.readonly works fine', () => {
213+
it('props.readonly works fine', async () => {
214214
// readonly default value is false
215215
const wrapper1 = mount({
216216
render() {
@@ -225,13 +225,26 @@ describe('TagInput Component', () => {
225225
},
226226
}).find('.t-input');
227227
expect(wrapper2.classes('t-is-readonly')).toBeTruthy();
228+
228229
// readonly = false
229230
const wrapper3 = mount({
230231
render() {
231232
return <TagInput readonly={false}></TagInput>;
232233
},
233234
}).find('.t-input');
234235
expect(wrapper3.classes('t-is-readonly')).toBeFalsy();
236+
// readonly = false and able backspace
237+
const onRemoveFnOn = vi.fn();
238+
const wrapper4 = getTagInputValueMount(TagInput, { readonly: false }, { remove: onRemoveFnOn });
239+
wrapper4.find('input').trigger('keydown.backspace');
240+
await wrapper4.vm.$nextTick();
241+
expect(onRemoveFnOn).toHaveBeenCalled();
242+
// readonly = true and prevent backspace
243+
const onRemoveFnUn = vi.fn();
244+
const wrapper5 = getTagInputValueMount(TagInput, { readonly: true }, { remove: onRemoveFnUn });
245+
wrapper5.find('input').trigger('keydown.backspace');
246+
await wrapper5.vm.$nextTick();
247+
expect(onRemoveFnUn).not.toHaveBeenCalled();
235248
});
236249

237250
it('props.readonly: readonly TagInput does not need clearIcon', async () => {

src/tag-input/useTagList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export default function useTagList(props: TdTagInputProps, context: SetupContext
7070
// 按下回退键,删除标签
7171
const onInputBackspaceKeyDown = (value: InputValue, p: { e: KeyboardEvent }) => {
7272
const { e } = p;
73-
if (!tagValue.value || !tagValue.value.length) return;
73+
if (!tagValue.value || !tagValue.value.length || readonly.value) return;
7474
// 回车键删除,输入框值为空时,才允许 Backspace 删除标签
7575
const isDelete = /(Backspace|NumpadDelete)/.test(e.code) || /(Backspace|NumpadDelete)/.test(e.key);
7676
if (!value && isDelete) {

0 commit comments

Comments
 (0)