Skip to content

Commit

Permalink
fix(tag-input): prevent backspace event in readonly mode and add test (
Browse files Browse the repository at this point in the history
  • Loading branch information
RSS1102 authored Nov 1, 2024
1 parent cadfdaa commit 37e7fb7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/tag-input/__tests__/vitest-tag-input.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('TagInput Component', () => {
expect(wrapper.attributes('placeholder')).toBe('This is TagInput placeholder');
});

it('props.readonly works fine', () => {
it('props.readonly works fine', async () => {
// readonly default value is false
const wrapper1 = mount({
render() {
Expand All @@ -225,13 +225,26 @@ describe('TagInput Component', () => {
},
}).find('.t-input');
expect(wrapper2.classes('t-is-readonly')).toBeTruthy();

// readonly = false
const wrapper3 = mount({
render() {
return <TagInput readonly={false}></TagInput>;
},
}).find('.t-input');
expect(wrapper3.classes('t-is-readonly')).toBeFalsy();
// readonly = false and able backspace
const onRemoveFnOn = vi.fn();
const wrapper4 = getTagInputValueMount(TagInput, { readonly: false }, { remove: onRemoveFnOn });
wrapper4.find('input').trigger('keydown.backspace');
await wrapper4.vm.$nextTick();
expect(onRemoveFnOn).toHaveBeenCalled();
// readonly = true and prevent backspace
const onRemoveFnUn = vi.fn();
const wrapper5 = getTagInputValueMount(TagInput, { readonly: true }, { remove: onRemoveFnUn });
wrapper5.find('input').trigger('keydown.backspace');
await wrapper5.vm.$nextTick();
expect(onRemoveFnUn).not.toHaveBeenCalled();
});

it('props.readonly: readonly TagInput does not need clearIcon', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/tag-input/useTagList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default function useTagList(props: TdTagInputProps, context: SetupContext
// 按下回退键,删除标签
const onInputBackspaceKeyDown = (value: InputValue, p: { e: KeyboardEvent }) => {
const { e } = p;
if (!tagValue.value || !tagValue.value.length) return;
if (!tagValue.value || !tagValue.value.length || readonly.value) return;
// 回车键删除,输入框值为空时,才允许 Backspace 删除标签
const isDelete = /(Backspace|NumpadDelete)/.test(e.code) || /(Backspace|NumpadDelete)/.test(e.key);
if (!value && isDelete) {
Expand Down

0 comments on commit 37e7fb7

Please sign in to comment.