Skip to content

Commit df9452a

Browse files
dhj-ltdesign-bot
andauthored
fix(input): prevent onEnter event during IME composition (#5862)
* fix(input): prevent onEnter event during IME composition - Add isComposition check in useInputEventHandler to prevent Enter key event during Chinese input method composition - Pass isComposition state from useInput to useInputEventHandler - Add test case to verify IME composition behavior - Fixes Table filter component issue where Enter key triggers both IME commit and filter confirmation * chore: stash changelog [ci skip] --------- Co-authored-by: tdesign-bot <[email protected]>
1 parent b75f08e commit df9452a

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

packages/components/input/__tests__/input.test.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,32 @@ describe('Input Component', () => {
394394
expect(onEnterFn1.mock.calls[0][1].e.type).toBe('keydown');
395395
});
396396

397+
it('events.enter should not trigger during IME composition', async () => {
398+
const onEnterFn = vi.fn();
399+
const wrapper = mount(<Input value="text" onEnter={onEnterFn}></Input>);
400+
const input = wrapper.find('input');
401+
402+
// 模拟中文输入法开始
403+
input.trigger('compositionstart');
404+
await wrapper.vm.$nextTick();
405+
406+
// 在输入法激活状态下按回车键,不应该触发 onEnter 事件
407+
input.trigger('keydown.enter');
408+
await wrapper.vm.$nextTick();
409+
expect(onEnterFn).not.toHaveBeenCalled();
410+
411+
// 模拟中文输入法结束
412+
input.trigger('compositionend');
413+
await wrapper.vm.$nextTick();
414+
415+
// 输入法结束后按回车键,应该正常触发 onEnter 事件
416+
input.trigger('keydown.enter');
417+
await wrapper.vm.$nextTick();
418+
expect(onEnterFn).toHaveBeenCalled(1);
419+
expect(onEnterFn.mock.calls[0][0]).toBe('text');
420+
expect(onEnterFn.mock.calls[0][1].e.type).toBe('keydown');
421+
});
422+
397423
it('events.focus works fine', async () => {
398424
const onFocusFn = vi.fn();
399425
const wrapper = mount(<Input onFocus={onFocusFn}></Input>);

packages/components/input/hooks/useInputEventHandler.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ import { Ref } from 'vue';
22
import { TdInputProps } from './../type';
33
import { getOutputValue } from './useInput';
44

5-
export function useInputEventHandler(props: TdInputProps, isHover: Ref<Boolean>) {
5+
export function useInputEventHandler(props: TdInputProps, isHover: Ref<Boolean>, isComposition?: Ref<boolean>) {
66
const handleKeydown = (e: KeyboardEvent) => {
77
if (props.disabled) return;
88
const { code } = e;
99
const tmpValue = getOutputValue((e.currentTarget as HTMLInputElement).value, props.type);
1010
if (/enter/i.test(code) || /enter/i.test(e.key)) {
11-
props.onEnter?.(tmpValue, { e });
11+
// 修复中文输入法回车键冲突:在中文输入法激活时不触发onEnter事件
12+
if (!isComposition?.value) {
13+
props.onEnter?.(tmpValue, { e });
14+
}
1215
} else {
1316
props.onKeydown?.(tmpValue, { e });
1417
}

packages/components/input/input.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export default defineComponent({
7070

7171
const { inputPreRef } = useInputWidth(props, inputRef, innerValue);
7272

73-
const inputEventHandler = useInputEventHandler(props, isHover);
73+
const inputEventHandler = useInputEventHandler(props, isHover, isComposition);
7474

7575
const tPlaceholder = computed(() => props.placeholder ?? globalConfig.value.placeholder);
7676
const inputAttrs = computed(() => {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
pr_number: 5862
3+
contributor: dhj-l
4+
---
5+
6+
- fix(Input): 修复中文输入法激活时不触发 `onEnter` 事件的问题 @dhj-l ([#5862](https://github.com/Tencent/tdesign-vue-next/pull/5862))

0 commit comments

Comments
 (0)