Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tag-input): prevent backspace event in readonly mode and add test #3376

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading