Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Typeahead): guard Enter/Escape/arrow keys against IME composition (#4860)

Ojas Mutreja1135101e588b
diff --git a/.changeset/typeahead-ime-composition-enter.md b/.changeset/typeahead-ime-composition-enter.md
new file mode 100644
index 0000000000..25ff83f2be
--- /dev/null
+++ b/.changeset/typeahead-ime-composition-enter.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `BaseTypeahead` (and everything built on it — `Typeahead`, `Tokenizer`, `PowerSearch`'s content-search field) no longer misinterprets the Enter keydown that commits an IME composition (Korean/Japanese/Chinese input) as "accept the highlighted suggestion". Previously that keydown both selected the highlighted result and cleared the input, so the still-composing syllable landed in the freshly-cleared field and became its own spurious second selection on the next Enter. Also guarded the Enter-to-save handler in `PowerSearchEditPopover`, which had the same gap when typing a CJK filter value.
+
+@HelloOjasMutreja
diff --git a/packages/core/src/PowerSearch/PowerSearchEditPopover.test.tsx b/packages/core/src/PowerSearch/PowerSearchEditPopover.test.tsx
index a0db192e4d..072b48fe81 100644
--- a/packages/core/src/PowerSearch/PowerSearchEditPopover.test.tsx
+++ b/packages/core/src/PowerSearch/PowerSearchEditPopover.test.tsx
@@ -283,4 +283,46 @@ describe('PowerSearch', () => {
// onSave should NOT be called because the event was already consumed (defaultPrevented)
expect(onSave).not.toHaveBeenCalled();
});
+
+ it('does not save/close on a composing Enter while typing a filter value (#4828)', () => {
+ const onSave = vi.fn();
+ const onCancel = vi.fn();
+
+ function StringValueHarness() {
+ const internalConfig = useInternalConfig(testConfig);
+ return (
+ <PowerSearchEditPopover
+ config={internalConfig}
+ filter={{
+ field: 'status',
+ operator: 'is',
+ value: {type: 'string', value: '한국어'},
+ }}
+ mode="edit"
+ onSave={onSave}
+ onCancel={onCancel}
+ />
+ );
+ }
+
+ const {container} = render(<StringValueHarness />);
+ const input = container.querySelector('input');
+ expect(input).not.toBeNull();
+
+ // Same composing-Enter signal as BaseTypeahead's guard: isComposing
+ // (modern) or legacy keyCode 229. An IME commits its composition on
+ // Enter too, so this keydown must not also close/save the popover.
+ // handleKeyDown is bound on an ancestor container div, so the keydown
+ // reaches it the same way it would from any real input inside — same
+ // dispatch mechanism the sibling defaultPrevented test above uses.
+ fireEvent.keyDown(input!, {key: 'Enter', isComposing: true});
+ expect(onSave).not.toHaveBeenCalled();
+
+ fireEvent.keyDown(input!, {key: 'Enter', keyCode: 229});
+ expect(onSave).not.toHaveBeenCalled();
+
+ // A real, non-composing Enter still saves normally.
+ fireEvent.keyDown(input!, {key: 'Enter'});
+ expect(onSave).toHaveBeenCalledTimes(1);
+ });
});
diff --git a/packages/core/src/PowerSearch/PowerSearchEditPopover.tsx b/packages/core/src/PowerSearch/PowerSearchEditPopover.tsx
index 92e7af97f0..60c3c0a00f 100644
--- a/packages/core/src/PowerSearch/PowerSearchEditPopover.tsx
+++ b/packages/core/src/PowerSearch/PowerSearchEditPopover.tsx
@@ -20,6 +20,7 @@ import {HStack, VStack} from '../Stack';
import {Icon} from '../Icon';
import {TreeList, type TreeListItemData} from '../TreeList';
import {useTranslator} from '../i18n';
+import {isImeKeyEvent} from '../hooks/useFocusTrap';
import {spacingVars, typeScaleVars} from '../theme/tokens.stylex';
import {PowerSearchValueEditor} from './PowerSearchValueEditor';
import {resolveOperatorLabel} from './resolveOperatorLabel';
@@ -702,6 +703,12 @@ export function PowerSearchEditPopover({
// Handle Enter to save, Escape to cancel
const handleKeyDown = useCallback(
(e: React.KeyboardEvent) => {
+ // Don't treat an IME composition-commit Enter as save-to-close --
+ // typing a CJK filter value and pressing Enter to confirm the
+ // composition would otherwise close the popover mid-composition.
+ if (isImeKeyEvent(e.nativeEvent)) {
+ return;
+ }
if (e.key === 'Enter' && !isSaveDisabled && !e.defaultPrevented) {
e.preventDefault();
handleSave();
diff --git a/packages/core/src/Typeahead/BaseTypeahead.tsx b/packages/core/src/Typeahead/BaseTypeahead.tsx
index d9f5aa53d8..c8b0fecd4c 100644
--- a/packages/core/src/Typeahead/BaseTypeahead.tsx
+++ b/packages/core/src/Typeahead/BaseTypeahead.tsx
@@ -30,6 +30,7 @@ import * as stylex from '@stylexjs/stylex';
import type {StyleXStyles} from '@stylexjs/stylex';
import {usePopover} from '../Popover/usePopover';
import {useAnnounce} from '../hooks/useAnnounce';
+import {isImeKeyEvent} from '../hooks/useFocusTrap';
import {TypeaheadItem} from './TypeaheadItem';
import {Icon} from '../Icon';
import {
@@ -633,6 +634,17 @@ export const BaseTypeahead = function BaseTypeahead<T extends SearchableItem>({
return;
}
+ // An IME candidate window uses Enter to commit the composition and
+ // Escape/ArrowUp/ArrowDown/Home/End to navigate its own candidates.
+ // Without this guard, a composing Enter both fires handleSelect AND
+ // clears the input via handleSelect's setQuery(''), so the IME's
+ // subsequent compositionend then writes the still-pending syllable
+ // into the freshly-cleared field -- producing a second, spurious
+ // selection on the next real Enter.
+ if (isImeKeyEvent(e.nativeEvent)) {
+ return;
+ }
+
if (!popover.isOpen) {
if (e.key === 'ArrowDown' && (hasEntriesOnFocus || query.length > 0)) {
e.preventDefault();
diff --git a/packages/core/src/Typeahead/Typeahead.test.tsx b/packages/core/src/Typeahead/Typeahead.test.tsx
index 459ca58a07..4b52bbe85c 100644
--- a/packages/core/src/Typeahead/Typeahead.test.tsx
+++ b/packages/core/src/Typeahead/Typeahead.test.tsx
@@ -198,6 +198,45 @@ describe('BaseTypeahead', () => {
});
});
+ describe('IME composition guard (#4828)', () => {
+ it('does not select the highlighted result on a composing Enter', async () => {
+ const onChange = vi.fn();
+ render(
+ <BaseTypeahead
+ searchSource={fruitSource}
+ value={null}
+ onChange={onChange}
+ debounceMs={0}
+ />,
+ );
+ const input = screen.getByRole('combobox');
+ fireEvent.change(input, {target: {value: 'App'}});
+ await waitFor(() => {
+ expect(input).toHaveAttribute('aria-expanded', 'true');
+ });
+
+ // The browser fires this composing keydown for the Enter that commits
+ // an IME candidate (isComposing: true, or legacy keyCode 229) before
+ // compositionend writes the pending syllable into the input. Without
+ // the guard this both selects the highlighted result AND clears the
+ // input via handleSelect, so the syllable that compositionend then
+ // writes lands in an emptied field instead of being part of the word.
+ fireEvent.keyDown(input, {key: 'Enter', isComposing: true});
+ expect(onChange).not.toHaveBeenCalled();
+ expect(input).toHaveValue('App');
+
+ fireEvent.keyDown(input, {key: 'Enter', keyCode: 229});
+ expect(onChange).not.toHaveBeenCalled();
+ expect(input).toHaveValue('App');
+
+ // A real, non-composing Enter still selects normally.
+ fireEvent.keyDown(input, {key: 'Enter'});
+ expect(onChange).toHaveBeenCalledWith(
+ expect.objectContaining({label: 'Apple'}),
+ );
+ });
+ });
+
it('disables input when isDisabled', () => {
render(
<BaseTypeahead
@@ -1038,11 +1077,16 @@ describe('Typeahead disabledMessage', () => {
});
});
-
describe('Typeahead statusVariant forwarding', () => {
it('defaults to attached (status renders with data-variant="attached")', () => {
const {container} = render(
- <Typeahead label="Fruit" searchSource={fruitSource} value={null} onChange={() => {}} status={{type: 'error', message: 'Required'}} />,
+ <Typeahead
+ label="Fruit"
+ searchSource={fruitSource}
+ value={null}
+ onChange={() => {}}
+ status={{type: 'error', message: 'Required'}}
+ />,
);
expect(container.querySelector('.astryx-field-status')).toHaveAttribute(
'data-variant',
@@ -1052,7 +1096,14 @@ describe('Typeahead statusVariant forwarding', () => {
it('forwards statusVariant="detached" to the underlying Field status', () => {
const {container} = render(
- <Typeahead label="Fruit" searchSource={fruitSource} value={null} onChange={() => {}} status={{type: 'error', message: 'Required'}} statusVariant="detached" />,
+ <Typeahead
+ label="Fruit"
+ searchSource={fruitSource}
+ value={null}
+ onChange={() => {}}
+ status={{type: 'error', message: 'Required'}}
+ statusVariant="detached"
+ />,
);
expect(container.querySelector('.astryx-field-status')).toHaveAttribute(
'data-variant',