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

Merged
merged 2 commits 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
18 changes: 17 additions & 1 deletion src/tag-input/__tests__/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,28 @@ describe('TagInput', () => {
expect(input.element.getAttribute('placeholder')).toBe('请输入');
});

it(':readonly', () => {
it(':readonly', async () => {
const value = ref('123');
const tags = ref(['Vue', 'React', 'Vue', 'React', 'Vue', 'React', 'Vue', 'React']);

const wrapper = mount(() => <TagInput readonly inputValue={value.value} />);
const input = wrapper.find('.t-input__inner');
value.value = '123123';
expect(input.element.value).toBe('123');

// readonly = false and able backspace
const onRemoveFnOn = vi.fn();
const wrapper1 = mount(() => <TagInput v-model={tags.value} onRemove={onRemoveFnOn} />);
wrapper1.find('input').trigger('keydown.backspace');
await wrapper1.vm.$nextTick();
expect(onRemoveFnOn).toHaveBeenCalled();

// readonly = true and prevent backspace
const onRemoveFnUn = vi.fn();
const wrapper2 = mount(() => <TagInput readonly v-model={tags.value} />);
wrapper2.find('input').trigger('keydown.backspace');
await wrapper2.vm.$nextTick();
expect(onRemoveFnUn).not.toHaveBeenCalled();
});

it(':size', () => {
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 @@ -60,7 +60,7 @@ export default function useTagList(props: TagInputProps) {
// 按下回退键,删除标签
const onInputBackspaceKeyDown = (value: InputValue, context: { e: KeyboardEvent }) => {
const { e } = context;
if (!tagValue.value || !tagValue.value.length || e.key === 'Process') return;
if (!tagValue.value || !tagValue.value.length || e.key === 'Process' || isReadonly.value) return;
// 回车键删除,输入框值为空时,才允许 Backspace 删除标签
const isDelete = /(Backspace|NumpadDelete)/i.test(e.code) || /(Backspace|NumpadDelete)/i.test(e.key);
if (!value && isDelete) {
Expand Down
Loading