From 62199fc5989ce37fe8459c99d46120902ff0a192 Mon Sep 17 00:00:00 2001 From: Julien BIANCHI Date: Mon, 19 Oct 2026 00:27:38 +0200 Subject: [PATCH] fix: leave Enter and the arrows to the IME while the search box is composing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While an input method editor composes a word (Japanese, Chinese, Korean), the arrows move through its candidates and Enter confirms the word. The browser still delivers those keydowns to the page, flagged `isComposing`, and the search dialog acted on them: the arrows moved the active result, and Enter opened it. With a provisional query that matches nothing, the options fall back to the suggested searches, so the Enter that confirmed the word replaced it with "shipping a side project"; when picking a recipient for a remote, it picked whichever developer the half-typed name matched. Ignore the keys while composing, as the issue command, the mention textarea and the conversation thread already do, so the IME keeps them. The test calls the component with mocked hooks, like the composer tests, and drives the input's handler: composing Enter and ArrowDown leave the value and the active option alone and never prevent the default, while the same keys without composition still move the selection and pick the suggestion. 🤖 Generated with Claude Code --- resources/js/components/search-dialog.tsx | 2 +- tests/Unit/SearchDialogComposition.test.tsx | 99 +++++++++++++++++++++ tests/draft-images.config.ts | 1 + tests/tsconfig.json | 1 + 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/SearchDialogComposition.test.tsx diff --git a/resources/js/components/search-dialog.tsx b/resources/js/components/search-dialog.tsx index 5661b73fed..9319c8ca22 100644 --- a/resources/js/components/search-dialog.tsx +++ b/resources/js/components/search-dialog.tsx @@ -143,7 +143,7 @@ export function SearchDialog({ }; const onKeyDown = (event: KeyboardEvent) => { - if (options.length === 0) { + if (event.nativeEvent.isComposing || options.length === 0) { return; } diff --git a/tests/Unit/SearchDialogComposition.test.tsx b/tests/Unit/SearchDialogComposition.test.tsx new file mode 100644 index 0000000000..5c91d84891 --- /dev/null +++ b/tests/Unit/SearchDialogComposition.test.tsx @@ -0,0 +1,99 @@ +/// +import assert from 'node:assert/strict'; +import * as React from 'react'; +import type { ReactElement, ReactNode } from 'react'; +import { test, vi } from 'vite-plus/test'; + +let state: unknown[] = []; +let cursor = 0; +const visit = vi.fn(); + +vi.doMock('react', () => ({ + ...React, + useEffect: () => {}, + useSyncExternalStore: (_subscribe: unknown, snapshot: () => unknown) => + snapshot(), + useState: (initial: unknown) => { + const index = cursor++; + if (!(index in state)) { + state[index] = typeof initial === 'function' ? initial() : initial; + } + return [ + state[index], + (value: unknown) => { + state[index] = + typeof value === 'function' ? value(state[index]) : value; + }, + ]; + }, +})); +vi.doMock('@inertiajs/react', async () => ({ + ...(await vi.importActual('@inertiajs/react')), + router: { visit }, +})); + +const { SearchDialog } = + await import('../../resources/js/components/search-dialog'); + +type Element = ReactElement>; + +function elements(node: ReactNode): Element[] { + if (!React.isValidElement(node)) { + return []; + } + const element = node as Element; + return [ + element, + ...React.Children.toArray(element.props.children as ReactNode).flatMap( + elements, + ), + ]; +} + +function input() { + cursor = 0; + const dialog = SearchDialog({ open: true, onOpenChange: () => {} }); + return elements(dialog).find( + (element) => element.props['data-test'] === 'search-input', + )!; +} + +function keyDown(key: string, isComposing: boolean) { + const event = { + key, + nativeEvent: { isComposing }, + preventDefault: vi.fn(), + }; + (input().props.onKeyDown as (event: unknown) => void)(event); + return event; +} + +test('leaves Enter and the arrows to the IME while composing', () => { + state = []; + assert.equal(input().props.value, ''); + assert.equal( + input().props['aria-activedescendant'], + 'search-suggestion-0', + ); + + const composedEnter = keyDown('Enter', true); + assert.equal(composedEnter.preventDefault.mock.calls.length, 0); + assert.equal(input().props.value, ''); + + const composedArrow = keyDown('ArrowDown', true); + assert.equal(composedArrow.preventDefault.mock.calls.length, 0); + assert.equal( + input().props['aria-activedescendant'], + 'search-suggestion-0', + ); + + keyDown('ArrowDown', false); + assert.equal( + input().props['aria-activedescendant'], + 'search-suggestion-1', + ); + const enter = keyDown('Enter', false); + assert.equal(enter.preventDefault.mock.calls.length, 1); + assert.equal(input().props.value, 'debugging in production'); + assert.equal(visit.mock.calls.length, 0); +}); diff --git a/tests/draft-images.config.ts b/tests/draft-images.config.ts index 81c1aaadb7..481d657046 100644 --- a/tests/draft-images.config.ts +++ b/tests/draft-images.config.ts @@ -16,6 +16,7 @@ export default defineConfig({ 'tests/Unit/EmojiSearch.test.tsx', 'tests/Unit/EmojiShortcodes.test.tsx', 'tests/Unit/QuoteReply.test.tsx', + 'tests/Unit/SearchDialogComposition.test.tsx', ], }, }); diff --git a/tests/tsconfig.json b/tests/tsconfig.json index 6603e1ca73..75337d9f0f 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -8,6 +8,7 @@ "Unit/**/*.test.ts", "Unit/DraftStash.test.tsx", "Unit/ComposerDraft.test.tsx", + "Unit/SearchDialogComposition.test.tsx", "../resources/js/types/*.d.ts", "draft-images.config.ts" ]