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 #3172

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
10 changes: 10 additions & 0 deletions src/tag-input/__tests__/vitest-tag-input.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ describe('TagInput Component', () => {
const wrapper3 = render(<TagInput readonly={false}></TagInput>);
const container3 = wrapper3.container.querySelector('.t-input');
expect(container3.querySelector(`.${'t-is-readonly'}`)).toBeFalsy();
// readonly = false backspace able
const onRemoveFn = vi.fn();
const wrapper4 = getTagInputValueMount(TagInput, { readonly: false }, { onRemove: onRemoveFn });
fireEvent.keyDown(wrapper4.container.querySelector('input'), { key: 'Backspace', code: 'Backspace', charCode: 8 });
expect(onRemoveFn).toHaveBeenCalled();
// readonly = false backspace disable
const onRemoveFnUn = vi.fn();
const wrapper5 = getTagInputValueMount(TagInput, { readonly: true }, { onRemove: onRemoveFnUn });
fireEvent.keyDown(wrapper5.container.querySelector('input'), { key: 'Backspace', code: 'Backspace', charCode: 8 });
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 @@ -57,7 +57,7 @@ export default function useTagList(props: TagInputProps) {
// 按下回退键,删除标签
const onInputBackspaceKeyDown = (value: InputValue, context: { e: KeyboardEvent<HTMLInputElement> }) => {
const { e } = context;
if (!tagValue || !tagValue.length) return;
if (!tagValue || !tagValue.length || readonly) return;
// 回车键删除,输入框值为空时,才允许 Backspace 删除标签
if (!value && ['Backspace', 'NumpadDelete'].includes(e.key)) {
const index = tagValue.length - 1;
Expand Down
Loading