An open source design system that's fully customizable and agent ready
TypeScript
fix(core): guard IME composition in editable command-key handlers (#4908)
nynexman446435010990783f
diff --git a/.changeset/ime-composition-editable-fields.md b/.changeset/ime-composition-editable-fields.mdnew file mode 100644index 0000000000..4e6c00cae6--- /dev/null+++ b/.changeset/ime-composition-editable-fields.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] The editable text fields in `Selector`, `MultiSelector`, `Typeahead`, `DateInput`, `DateTimeInput`, `TimeInput`, and `NumberInput` no longer misinterpret the keydown that commits or cancels an IME composition (Korean/Japanese/Chinese input) as a command. Previously a composing Enter would select/toggle the highlighted option or commit a typed date, a composing Escape would exit `Typeahead`'s edit mode, and a composing arrow would step a time or number value — all before the composition finished. Each field now lets the IME finish first, matching the guard already in place for `BaseTypeahead` and the Chat composer.++@cixzhangdiff --git a/packages/core/src/DateInput/DateInput.test.tsx b/packages/core/src/DateInput/DateInput.test.tsxindex 060cb1cbc4..a6cfed9470 100644--- a/packages/core/src/DateInput/DateInput.test.tsx+++ b/packages/core/src/DateInput/DateInput.test.tsx@@ -536,6 +536,27 @@ describe('DateInput', () => {expect(onChange).toHaveBeenCalledWith('2026-03-15');});+ it('does not commit on a composing Enter (IME)', () => {+ const onChange = vi.fn();+ render(<DateInput label="Date" onChange={onChange} />);++ const input = screen.getByRole('combobox');+ fireEvent.change(input, {target: {value: '03/15/2026'}});+ onChange.mockClear();++ // The composing keydown (isComposing / legacy keyCode 229) that commits an+ // IME candidate fires before compositionend; it must not be read as+ // "commit the typed date".+ fireEvent.keyDown(input, {key: 'Enter', isComposing: true});+ expect(onChange).not.toHaveBeenCalled();+ fireEvent.keyDown(input, {key: 'Enter', keyCode: 229});+ expect(onChange).not.toHaveBeenCalled();++ // A real, non-composing Enter still commits.+ fireEvent.keyDown(input, {key: 'Enter'});+ expect(onChange).toHaveBeenCalledWith('2026-03-15');+ });+// --- Arrow-down opens calendar popover ---// Note: Tests involving popover rendering (show/hide with calendar)diff --git a/packages/core/src/DateInput/DateInput.tsx b/packages/core/src/DateInput/DateInput.tsxindex d1b0ca4e2d..2ec5fc9eeb 100644--- a/packages/core/src/DateInput/DateInput.tsx+++ b/packages/core/src/DateInput/DateInput.tsx@@ -60,7 +60,7 @@ import {useCalendarConstraints} from '../Calendar/hooks';import {useInputStatusIcon} from '../hooks/useInputStatusIcon';import {usePopover} from '../Popover';import {useTooltip} from '../Tooltip';-import {getInputARIA, parseDateInput} from '../utils';+import {getInputARIA, isImeKeyEvent, parseDateInput} from '../utils';import {plainDateFromISO,plainDateToISO,@@ -627,6 +627,14 @@ export function DateInput({// Handle keyboard events on inputconst handleInputKeyDown = useCallback((e: React.KeyboardEvent) => {+ // An in-progress IME composition uses Enter to commit the candidate and+ // Escape to cancel it; that composing keydown fires before+ // compositionend, so without this guard a Korean/Japanese/Chinese user+ // committing a syllable with Enter would instead commit the pending date+ // (or Escape would close the calendar mid-composition). See utils/ime.ts.+ if (isImeKeyEvent(e.nativeEvent)) {+ return;+ }if (e.key === 'Escape' && popover.isOpen) {e.preventDefault();popover.hide();diff --git a/packages/core/src/DateTimeInput/DateTimeInput.test.tsx b/packages/core/src/DateTimeInput/DateTimeInput.test.tsxindex 513681c5fb..1af565f1a7 100644--- a/packages/core/src/DateTimeInput/DateTimeInput.test.tsx+++ b/packages/core/src/DateTimeInput/DateTimeInput.test.tsx@@ -97,6 +97,48 @@ describe('DateTimeInput', () => {expect(screen.getByLabelText('Meeting time')).toBeInTheDocument();});+ it('does not commit the date on a composing Enter (IME)', () => {+ const onChange = vi.fn();+ render(<DateTimeInput label="Meeting" onChange={onChange} />);+ const dateInput = screen.getByRole('combobox');+ fireEvent.change(dateInput, {target: {value: '03/15/2026'}});+ onChange.mockClear();++ // The composing keydown (isComposing / legacy keyCode 229) that commits an+ // IME candidate must not be read as "commit the typed date".+ fireEvent.keyDown(dateInput, {key: 'Enter', isComposing: true});+ expect(onChange).not.toHaveBeenCalled();+ fireEvent.keyDown(dateInput, {key: 'Enter', keyCode: 229});+ expect(onChange).not.toHaveBeenCalled();++ // A real, non-composing Enter still commits.+ fireEvent.keyDown(dateInput, {key: 'Enter'});+ expect(onChange).toHaveBeenCalled();+ });++ it('does not step the time on a composing ArrowUp (IME)', () => {+ const onChange = vi.fn();+ render(+ <DateTimeInput+ label="Meeting"+ value={'2026-03-15T14:30' as ISODateTimeString}+ onChange={onChange}+ />,+ );+ const timeInput = screen.getByLabelText('Meeting time');++ // An IME candidate window navigates with the arrows; a composing ArrowUp+ // must not step the time value.+ fireEvent.keyDown(timeInput, {key: 'ArrowUp', isComposing: true});+ expect(onChange).not.toHaveBeenCalled();+ fireEvent.keyDown(timeInput, {key: 'ArrowUp', keyCode: 229});+ expect(onChange).not.toHaveBeenCalled();++ // A real, non-composing ArrowUp still steps the time by one minute.+ fireEvent.keyDown(timeInput, {key: 'ArrowUp'});+ expect(onChange).toHaveBeenCalledWith('2026-03-15T14:31');+ });+it('displays formatted date in date input when value is provided', () => {render(<DateTimeInputdiff --git a/packages/core/src/DateTimeInput/DateTimeInput.tsx b/packages/core/src/DateTimeInput/DateTimeInput.tsxindex 34eea3faf7..d899ca5ece 100644--- a/packages/core/src/DateTimeInput/DateTimeInput.tsx+++ b/packages/core/src/DateTimeInput/DateTimeInput.tsx@@ -69,6 +69,7 @@ import {formatISOTime,isTimeInRange,adjustTime,+ isImeKeyEvent,mergeProps,mergeRefs,isFocusDetached,@@ -742,6 +743,13 @@ export function DateTimeInput({const handleDateKeyDown = useCallback((e: React.KeyboardEvent) => {+ // Guard the composing keydown (fires before compositionend): an IME uses+ // Enter to commit the candidate and Escape to cancel it, so without this+ // a CJK user committing a syllable with Enter would commit the pending+ // date instead. See utils/ime.ts.+ if (isImeKeyEvent(e.nativeEvent)) {+ return;+ }if (e.key === 'Escape' && popover.isOpen) {e.preventDefault();popover.hide();@@ -835,6 +843,13 @@ export function DateTimeInput({const handleTimeKeyDown = useCallback((e: KeyboardEvent<HTMLInputElement>) => {+ // ArrowUp/ArrowDown step the time and preventDefault; an IME candidate+ // window uses those same arrows to navigate candidates, so guard the+ // composing keydown (fires before compositionend) to avoid stealing them+ // mid-composition. See utils/ime.ts.+ if (isImeKeyEvent(e.nativeEvent)) {+ return;+ }if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {e.preventDefault();diff --git a/packages/core/src/MultiSelector/MultiSelector.test.tsx b/packages/core/src/MultiSelector/MultiSelector.test.tsxindex 73d11bbd26..f4a5547fce 100644--- a/packages/core/src/MultiSelector/MultiSelector.test.tsx+++ b/packages/core/src/MultiSelector/MultiSelector.test.tsx@@ -415,6 +415,37 @@ describe('MultiSelector', () => {expect(onChange).toHaveBeenCalledWith(['Apple']);});+ it('does not toggle the highlighted option on a composing Enter (IME)', async () => {+ const user = userEvent.setup();+ const onChange = vi.fn();+ render(+ <MultiSelector+ label="Fruit"+ options={defaultOptions}+ value={[]}+ onChange={onChange}+ hasSearch+ />,+ );+ await user.click(screen.getByRole('button', {name: 'Fruit'}));+ const search = screen.getByRole('combobox', h);+ // Filter to Banana and highlight it so an unguarded Enter would toggle it.+ await user.type(search, 'ban');+ await user.keyboard('{ArrowDown}');+ expect(search).toHaveAttribute('aria-activedescendant');++ // The composing keydown (isComposing / legacy keyCode 229) that commits an+ // IME candidate must not be read as "toggle the highlighted option".+ fireEvent.keyDown(search, {key: 'Enter', isComposing: true});+ expect(onChange).not.toHaveBeenCalled();+ fireEvent.keyDown(search, {key: 'Enter', keyCode: 229});+ expect(onChange).not.toHaveBeenCalled();++ // A real, non-composing Enter still toggles the highlighted option.+ fireEvent.keyDown(search, {key: 'Enter'});+ expect(onChange).toHaveBeenCalledWith(['Banana']);+ });+it('toggles the correct item when selected items are sorted to top', async () => {const user = userEvent.setup();const onChange = vi.fn();diff --git a/packages/core/src/MultiSelector/MultiSelector.tsx b/packages/core/src/MultiSelector/MultiSelector.tsxindex 5b93789db2..10b16b1589 100644--- a/packages/core/src/MultiSelector/MultiSelector.tsx+++ b/packages/core/src/MultiSelector/MultiSelector.tsx@@ -71,7 +71,7 @@ import {getSelectableOptions,} from '../Selector/utils';import {useMultiCombobox} from './hooks';-import {getInputARIA, mergeProps} from '../utils';+import {getInputARIA, isImeKeyEvent, mergeProps} from '../utils';import {useAnnounce} from '../hooks/useAnnounce';import type {BaseProps} from '../BaseProps';import type {SizeValue} from '../utils/types';@@ -1182,6 +1182,15 @@ export function MultiSelector<T extends MultiSelectorOptionType>({}}}onKeyDown={e => {+ // An in-progress IME composition uses these same keys (Enter to+ // commit the candidate, Escape/Arrows to navigate the candidate+ // window); the composing keydown fires before compositionend, so+ // without this guard a Korean/Japanese/Chinese user committing a+ // syllable with Enter would instead toggle the highlighted option.+ // See utils/ime.ts.+ if (isImeKeyEvent(e.nativeEvent)) {+ return;+ }// Arrow keys navigate options; Enter toggles; Escape closes.// Space and Home/End are left to the input (type a space / move// the caret) per the APG editable combobox; PageUp/PageDown arediff --git a/packages/core/src/NumberInput/NumberInput.test.tsx b/packages/core/src/NumberInput/NumberInput.test.tsxindex 7c9a9ab538..534d2b3547 100644--- a/packages/core/src/NumberInput/NumberInput.test.tsx+++ b/packages/core/src/NumberInput/NumberInput.test.tsx@@ -1340,6 +1340,24 @@ describe('NumberInput stepping', () => {expect(onChange).toHaveBeenLastCalledWith(4);});+ it('does not step or commit on a composing keydown (IME)', () => {+ const onChange = vi.fn();+ render(<NumberInput label="Amount" value={5} onChange={onChange} />);+ const input = screen.getByRole('spinbutton');++ // The field is type="text", so an IME composes into it. Enter commits the+ // candidate and the arrows walk the candidate window; neither should reach+ // the stepping or commit paths. Both signals a browser may report.+ fireEvent.keyDown(input, {key: 'ArrowUp', isComposing: true});+ fireEvent.keyDown(input, {key: 'ArrowDown', keyCode: 229});+ fireEvent.keyDown(input, {key: 'Enter', isComposing: true});+ expect(onChange).not.toHaveBeenCalled();++ // A real, non-composing ArrowUp still steps.+ fireEvent.keyDown(input, {key: 'ArrowUp'});+ expect(onChange).toHaveBeenLastCalledWith(6);+ });+it('lets onKeyDown cancel keyboard stepping', () => {const onChange = vi.fn();const onKeyDown = vi.fn((event: React.KeyboardEvent<HTMLInputElement>) =>diff --git a/packages/core/src/NumberInput/NumberInput.tsx b/packages/core/src/NumberInput/NumberInput.tsxindex 70375ceaa3..5027af0b6b 100644--- a/packages/core/src/NumberInput/NumberInput.tsx+++ b/packages/core/src/NumberInput/NumberInput.tsx@@ -169,7 +169,7 @@ export type {InputStatus as NumberInputStatus,InputStatusType as NumberInputStatusType,} from '../Field';-import {mergeProps, mergeRefs} from '../utils';+import {isImeKeyEvent, mergeProps, mergeRefs} from '../utils';import type {BaseProps} from '../BaseProps';import type {SizeValue} from '../utils/types';import {themeProps} from '../utils/themeProps';@@ -746,6 +746,14 @@ export function NumberInput({// Handle keyboard eventsconst handleKeyDown = useCallback((e: KeyboardEvent<HTMLInputElement>) => {+ // The field is type="text" for formatted display, so an IME can compose+ // into it: Enter commits the candidate and the arrows walk the candidate+ // window. The composing keydown fires before compositionend, so without+ // this guard those keystrokes would commit or step the value instead.+ // See utils/ime.ts.+ if (isImeKeyEvent(e.nativeEvent)) {+ return;+ }const hasModifier = e.altKey || e.ctrlKey || e.metaKey || e.shiftKey;if (!hasModifier && (e.key === 'ArrowUp' || e.key === 'ArrowDown')) {onKeyDown?.(e);diff --git a/packages/core/src/Selector/Selector.test.tsx b/packages/core/src/Selector/Selector.test.tsxindex 59ce85b5eb..7901830477 100644--- a/packages/core/src/Selector/Selector.test.tsx+++ b/packages/core/src/Selector/Selector.test.tsx@@ -1000,6 +1000,39 @@ describe('Selector', () => {).toBeInTheDocument();});+ it('does not select the highlighted option on a composing Enter (IME)', async () => {+ const user = userEvent.setup();+ const onChange = vi.fn();+ render(+ <Selector+ label="Fruit"+ options={OPTIONS}+ onChange={onChange}+ hasSearch+ />,+ );+ await user.click(screen.getByRole('button', {name: 'Fruit'}));+ const search = screen.getByRole('combobox', h);+ // Filter to Banana and highlight it so an unguarded Enter would commit a+ // selection.+ await user.type(search, 'ban');+ await user.keyboard('{ArrowDown}');+ expect(search).toHaveAttribute('aria-activedescendant');++ // The browser fires this composing keydown for the Enter that commits an+ // IME candidate (isComposing: true, or the legacy keyCode 229) before+ // compositionend writes the syllable. It must NOT be read as "select the+ // highlighted option".+ fireEvent.keyDown(search, {key: 'Enter', isComposing: true});+ expect(onChange).not.toHaveBeenCalled();+ fireEvent.keyDown(search, {key: 'Enter', keyCode: 229});+ expect(onChange).not.toHaveBeenCalled();++ // A real, non-composing Enter still selects the highlighted option.+ fireEvent.keyDown(search, {key: 'Enter'});+ expect(onChange).toHaveBeenCalledWith('Banana');+ });+describe('result announcements', () => {it('announces the match count politely while searching', async () => {const user = userEvent.setup();diff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsxindex 564de668f9..b6c0fc7458 100644--- a/packages/core/src/Selector/Selector.tsx+++ b/packages/core/src/Selector/Selector.tsx@@ -72,7 +72,7 @@ import {import {useCombobox, useSelectedItemOffset} from './hooks';import {useTypeahead} from '../hooks/useTypeahead';import {SelectorOption} from './SelectorOption';-import {getInputARIA, mergeProps} from '../utils';+import {getInputARIA, isImeKeyEvent, mergeProps} from '../utils';import {useSize} from '../SizeContext/SizeContext';import type {BaseProps} from '../BaseProps';import type {SizeValue} from '../utils/types';@@ -1055,6 +1055,15 @@ export function Selector<T extends SelectorOptionType>(}}}onKeyDown={e => {+ // An in-progress IME composition uses these same keys (Enter to+ // commit the candidate, Escape/Arrows to navigate the candidate+ // window); the composing keydown fires before compositionend, so+ // without this guard a Korean/Japanese/Chinese user committing a+ // syllable with Enter would instead select the highlighted option.+ // See utils/ime.ts.+ if (isImeKeyEvent(e.nativeEvent)) {+ return;+ }// Arrow keys navigate options; Enter selects; Escape closes.// Home/End are left to the input for caret movement (APG editable// combobox); PageUp/PageDown are the sanctioned substitute fordiff --git a/packages/core/src/TimeInput/TimeInput.test.tsx b/packages/core/src/TimeInput/TimeInput.test.tsxindex 06e44000e6..f79bf0be63 100644--- a/packages/core/src/TimeInput/TimeInput.test.tsx+++ b/packages/core/src/TimeInput/TimeInput.test.tsx@@ -42,6 +42,29 @@ describe('TimeInput', () => {expect(screen.getByPlaceholderText('Pick a time')).toBeInTheDocument();});+ it('does not step the time on a composing ArrowUp/ArrowDown (IME)', () => {+ const onChange = vi.fn();+ render(+ <TimeInput+ label="Time"+ value={'14:30' as ISOTimeString}+ onChange={onChange}+ />,+ );+ const input = screen.getByLabelText('Time');++ // An IME candidate window navigates with the arrows; a composing keydown+ // (isComposing / legacy keyCode 229) must not step the time value.+ fireEvent.keyDown(input, {key: 'ArrowUp', isComposing: true});+ expect(onChange).not.toHaveBeenCalled();+ fireEvent.keyDown(input, {key: 'ArrowDown', keyCode: 229});+ expect(onChange).not.toHaveBeenCalled();++ // A real, non-composing ArrowUp still steps the time by one minute.+ fireEvent.keyDown(input, {key: 'ArrowUp'});+ expect(onChange).toHaveBeenCalledWith('14:31');+ });+it('displays formatted time in 12h format', () => {render(<TimeInputdiff --git a/packages/core/src/TimeInput/TimeInput.tsx b/packages/core/src/TimeInput/TimeInput.tsxindex bc9cbe4e98..107af1f280 100644--- a/packages/core/src/TimeInput/TimeInput.tsx+++ b/packages/core/src/TimeInput/TimeInput.tsx@@ -55,6 +55,7 @@ import {formatDisplayTime24h,formatISOTime,adjustTime,+ isImeKeyEvent,isTimeInRange,mergeProps,mergeRefs,@@ -530,6 +531,13 @@ export function TimeInput({// Handle keyboard navigation on inputconst handleInputKeyDown = useCallback((e: KeyboardEvent<HTMLInputElement>) => {+ // ArrowUp/ArrowDown step the time and preventDefault; an IME candidate+ // window uses those same arrows to navigate candidates, so guard the+ // composing keydown (fires before compositionend) to avoid stealing them+ // mid-composition. See utils/ime.ts.+ if (isImeKeyEvent(e.nativeEvent)) {+ return;+ }// Arrow-key adjustment mutates the value; block it while showing a// disabled reason (the input keeps focusability via aria-disabled).if (isDisabled) {diff --git a/packages/core/src/Typeahead/Typeahead.test.tsx b/packages/core/src/Typeahead/Typeahead.test.tsxindex 6e53840e7c..e37f349de1 100644--- a/packages/core/src/Typeahead/Typeahead.test.tsx+++ b/packages/core/src/Typeahead/Typeahead.test.tsx@@ -796,6 +796,41 @@ describe('Typeahead edit mode', () => {// onChange should not have been called — value restoredexpect(onChange).not.toHaveBeenCalled();});++ it('does not exit edit mode on a composing Escape (IME)', async () => {+ const onChange = vi.fn();+ render(+ <Typeahead+ label="Fruit"+ searchSource={fruitSource}+ value={fruits[0]}+ onChange={onChange}+ />,+ );++ // Enter edit mode by clicking the token; the input uncollapses and+ // rejoins the Tab order (tabindex is cleared).+ const tokenText = screen.getByText(fruits[0].label);+ fireEvent.click(tokenText.closest('div')!);+ await act(async () => {+ await new Promise(r => requestAnimationFrame(r));+ });+ const input = screen.getByRole('combobox');+ expect(input).not.toHaveAttribute('tabindex', '-1');++ // BaseTypeahead invokes this external handler before its own IME guard, so+ // a composing Escape (isComposing / legacy keyCode 229) — which an IME uses+ // to cancel the pending candidate — must not exit edit mode here.+ fireEvent.keyDown(input, {key: 'Escape', isComposing: true});+ expect(screen.getByRole('combobox')).not.toHaveAttribute('tabindex', '-1');+ fireEvent.keyDown(input, {key: 'Escape', keyCode: 229});+ expect(screen.getByRole('combobox')).not.toHaveAttribute('tabindex', '-1');++ // A real, non-composing Escape still exits edit mode: the token is+ // restored and the collapsed input drops back out of the Tab order.+ fireEvent.keyDown(input, {key: 'Escape'});+ expect(screen.getByRole('combobox')).toHaveAttribute('tabindex', '-1');+ });});describe('Typeahead collapsed input tab order', () => {diff --git a/packages/core/src/Typeahead/Typeahead.tsx b/packages/core/src/Typeahead/Typeahead.tsxindex a1ab712f28..072e6be0ba 100644--- a/packages/core/src/Typeahead/Typeahead.tsx+++ b/packages/core/src/Typeahead/Typeahead.tsx@@ -45,7 +45,7 @@ import {VisuallyHidden} from '../VisuallyHidden';import {spacingVars, sizeVars} from '../theme/tokens.stylex';import {groupStyles} from '../InputGroup/groupStyles';import {useInputGroup} from '../InputGroup/InputGroupContext';-import {getInputARIA, mergeProps, mergeRefs} from '../utils';+import {getInputARIA, isImeKeyEvent, mergeProps, mergeRefs} from '../utils';import type {BaseProps} from '../BaseProps';import type {SizeValue} from '../utils/types';import type {SearchableItem, SearchSource} from './types';@@ -369,6 +369,15 @@ export function Typeahead<T extends SearchableItem>({// Handle Escape during edit mode — restore tokenconst handleKeyDown = useCallback((e: React.KeyboardEvent<HTMLInputElement>) => {+ // BaseTypeahead invokes this external handler *before* its own IME+ // guard, so we must guard here too: an IME candidate window uses Escape+ // to cancel the pending composition, and that composing Escape fires+ // before compositionend. Without this, a Korean/Japanese/Chinese user+ // cancelling a candidate would instead exit edit mode and blur the+ // field. See utils/ime.ts.+ if (isImeKeyEvent(e.nativeEvent)) {+ return;+ }if (e.key === 'Escape' && editingValue) {e.preventDefault();setIsEditing(false);