diff --git a/.changeset/35996-ime-enter-fields.md b/.changeset/35996-ime-enter-fields.md new file mode 100644 index 00000000000..9dd9795f45d --- /dev/null +++ b/.changeset/35996-ime-enter-fields.md @@ -0,0 +1,12 @@ +--- +'@object-ui/components': patch +'@object-ui/fields': patch +--- + +Clause-②: yes (widening) +Metadata-protocol: none +Skills-docs: none — no skill teaches the input-method key guard (zero hits for `isImeKey` or `isComposing` under `skills/`); the field behaviour a skill describes is unchanged outside a composition + +An Enter that picks a word in a Chinese, Japanese or Korean input method no longer acts in three more field widgets: a line grid's cell no longer moves to the next line or appends one (its arrows no longer move either), the date picker no longer commits the half-typed date, and the record picker's page-jump box no longer jumps and clears itself. Safari's 229 Enter raised just after the composition ends is treated the same way; the next Enter does what it always did. + +`@object-ui/components` now exports `isImeKey(event)`, the one "does the input method own this key?" predicate. The list grid and every `@object-ui/fields` widget that reads its own keys ask it, so the copy `@object-ui/fields` kept is gone. diff --git a/ui/packages/components/src/index.ts b/ui/packages/components/src/index.ts index 5c2136cf868..b9ad916b6c4 100644 --- a/ui/packages/components/src/index.ts +++ b/ui/packages/components/src/index.ts @@ -41,6 +41,10 @@ export { renderChildren, renderNodeSlot, isEmptyNodeSlot } from './lib/utils'; export { cva } from 'class-variance-authority'; export { getLazyIcon, isLucideIconName, LazyIcon, toKebabIconName } from './lib/lazy-icon'; +// The one "does the input method own this key?" predicate (objectstack#35996): +// the list grid and the field widgets of `@object-ui/fields` both ask it. +export { isImeKey } from './lib/ime-key'; + // The inline Markdown subset `element:text` draws (objectui#12778). Exported // because app-shell's marketplace README renderer draws its inline text with // the same one, so both share the raw-HTML skip and the link-scheme refusal. diff --git a/ui/packages/components/src/lib/ime-key.ts b/ui/packages/components/src/lib/ime-key.ts new file mode 100644 index 00000000000..3dbe4609aff --- /dev/null +++ b/ui/packages/components/src/lib/ime-key.ts @@ -0,0 +1,24 @@ +/** + * ObjectUI + * Copyright (c) 2024-present ObjectStack Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/** + * A key the input method owns: pressed while a composition is open + * (`isComposing`, keyCode 229), or the 229 a browser reports for the key that + * ends one (Safari raises the candidate-picking Enter AFTER `compositionend`, + * with `isComposing` already false and keyCode 229). Enter, Esc, Tab, the + * comma, the arrows and Backspace pressed then pick, type or move a candidate + * inside the input method; a control that reads those keys itself must leave + * them alone and act on the next key after the composition ends. + * + * The ONE definition of this question (objectstack#35996). The list grid + * (`data-table`) and every widget in `@object-ui/fields` that reads its own + * keys ask it, so a fix to it reaches all of them at once. + */ +export function isImeKey(e: { nativeEvent: KeyboardEvent; keyCode?: number }): boolean { + return e.nativeEvent.isComposing === true || e.nativeEvent.keyCode === 229 || e.keyCode === 229; +} diff --git a/ui/packages/components/src/renderers/complex/dataTableCellSelection.ts b/ui/packages/components/src/renderers/complex/dataTableCellSelection.ts index 411fc59201c..6a65216279a 100644 --- a/ui/packages/components/src/renderers/complex/dataTableCellSelection.ts +++ b/ui/packages/components/src/renderers/complex/dataTableCellSelection.ts @@ -14,15 +14,12 @@ import type { TableColumn } from '@object-ui/types'; /** - * A key the input method owns: pressed while a composition is open - * (`isComposing`), or the 229 a browser reports for the key that ends one - * (Safari raises the candidate-picking Enter after `compositionend`, as 229). - * Enter, Esc, Tab and the arrows pressed then pick or move a candidate; the - * table must not commit, cancel or move on them. + * The input-method key guard the table asks before it commits, cancels or + * moves on Enter, Esc, Tab or an arrow. Defined once in `../../lib/ime-key` + * (objectstack#35996) and passed through here so the table keeps importing + * its key helpers from one module. */ -export function isImeKey(e: { nativeEvent: KeyboardEvent; keyCode?: number }): boolean { - return e.nativeEvent.isComposing === true || e.nativeEvent.keyCode === 229 || e.keyCode === 229; -} +export { isImeKey } from '../../lib/ime-key'; /** * The attribute a link wears when a single click on it still follows it in a diff --git a/ui/packages/fields/src/widgets/GridField.tsx b/ui/packages/fields/src/widgets/GridField.tsx index 1963691eb69..8d70ff153fe 100644 --- a/ui/packages/fields/src/widgets/GridField.tsx +++ b/ui/packages/fields/src/widgets/GridField.tsx @@ -1161,6 +1161,10 @@ function GridFieldBody({ const onCellKeyDown = useCallback( (e: React.KeyboardEvent, rowIdx: number, colIdx: number) => { + // objectstack#35996 — Enter and the arrows pressed while an input + // method composes pick or move its candidate: the grid neither moves + // nor appends a line on them. + if (isImeKey(e)) return; if (e.key === 'Enter' && rowIdx === rows.length - 1 && allowAdd && !isList) { e.preventDefault(); addRow(colIdx); diff --git a/ui/packages/fields/src/widgets/RecordPickerDialog.tsx b/ui/packages/fields/src/widgets/RecordPickerDialog.tsx index e0d7fb95c4b..bd58ecdcc65 100644 --- a/ui/packages/fields/src/widgets/RecordPickerDialog.tsx +++ b/ui/packages/fields/src/widgets/RecordPickerDialog.tsx @@ -50,6 +50,7 @@ import { useRecordQuery } from './useRecordQuery.js'; import { isRecordReference, type LookupFilterEntry } from './lookupRecordReference.js'; import { objectResolvesSearch } from './searchOffer.js'; import { PHONE_TOUCH_HEIGHT } from './touchTarget.js'; +import { isImeKey } from './imeKey.js'; // The one place a lookup column's display value is decided — shared with the // inline dropdown in LookupField so a single `lookupColumns` declaration // cannot render two different ways (objectui#5492). @@ -974,7 +975,9 @@ export function RecordPickerDialog({ // Page jump handler const handlePageJump = useCallback( (e: React.KeyboardEvent) => { - if (e.key !== 'Enter') return; + // objectstack#35996 — an Enter that picks an input-method candidate + // neither jumps nor clears the box under the composition. + if (e.key !== 'Enter' || isImeKey(e)) return; const page = parseInt(pageJumpValue, 10); if (!isNaN(page) && page >= 1 && page <= totalPages) { setCurrentPage(page); diff --git a/ui/packages/fields/src/widgets/date-picker/DatePickerField.tsx b/ui/packages/fields/src/widgets/date-picker/DatePickerField.tsx index 77364b1d104..90074f05017 100644 --- a/ui/packages/fields/src/widgets/date-picker/DatePickerField.tsx +++ b/ui/packages/fields/src/widgets/date-picker/DatePickerField.tsx @@ -44,6 +44,7 @@ import { fromLocalDay, } from './localeDateText.js'; import { useFieldTranslation } from '../useFieldTranslation.js'; +import { isImeKey } from '../imeKey.js'; /** * The `date` and date-time controls of the form field widgets @@ -266,6 +267,9 @@ function TemporalPicker({ domProps.onBlur?.(e); }} onKeyDown={(e) => { + // objectstack#35996 — an Enter that picks an input-method candidate + // does not commit the half-typed text. + if (isImeKey(e)) return; if (e.key === 'Enter') { // Commit, and keep a bare Enter from submitting the form with // the text not yet read. diff --git a/ui/packages/fields/src/widgets/imeComposition-35996.test.tsx b/ui/packages/fields/src/widgets/imeComposition-35996.test.tsx new file mode 100644 index 00000000000..f4d61fb6220 --- /dev/null +++ b/ui/packages/fields/src/widgets/imeComposition-35996.test.tsx @@ -0,0 +1,199 @@ +/** + * ObjectUI + * Copyright (c) 2024-present ObjectStack Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/** + * An Enter the input method owns does only what the input method does with it + * (objectstack#35996), in the three widgets objectstack#35797 left out: + * + * - `GridField`: a cell's Enter moves to the next line, and on the last line + * appends one; the composed text then landed in the wrong line. + * - `DatePickerField`: Enter commits the typed date text. + * - `RecordPickerDialog`: Enter in the page-jump box jumps and clears it. + * + * Each widget is driven through the two sequences browsers really send + * (Chromium / Firefox: the picking keydown inside the composition, with + * `isComposing: true` and keyCode 229; Safari: the 229 keydown after + * `compositionend`), and each case ends with the control: a plain Enter does + * what the widget always did, so a guard that swallowed every Enter goes red. + * + * The predicate itself is pinned once, in `imeComposition-35797.test.tsx`; + * `./imeKey` is the pass-through of the one definition on the + * `@object-ui/components` entry, which the identity case below pins. + */ + +import * as React from 'react'; +import { describe, it, expect, vi, afterEach } from 'vitest'; +import { render, screen, cleanup, waitFor, fireEvent } from '@testing-library/react'; +import '@testing-library/jest-dom'; + +import { isImeKey as componentsIsImeKey } from '@object-ui/components'; +import { isImeKey } from './imeKey'; +import { GridField } from './GridField'; +import { DatePickerField } from './date-picker/DatePickerField'; +import { RecordPickerDialog } from './RecordPickerDialog'; +import { getCellRenderer } from '../index'; + +/** The keydown that picks a candidate while the composition is open. */ +const COMPOSING_ENTER = { key: 'Enter', code: 'Enter', keyCode: 229, which: 229, isComposing: true } as const; +/** Safari's candidate-picking Enter, raised after `compositionend`. */ +const SAFARI_POST_COMPOSITION_ENTER = { key: 'Enter', code: 'Enter', keyCode: 229, which: 229, isComposing: false } as const; +/** An Enter the input method does not own. */ +const PLAIN_ENTER = { key: 'Enter', code: 'Enter', keyCode: 13, which: 13 } as const; + +type Composer = (el: HTMLElement, text: string) => void; +const type = (el: HTMLElement, text: string) => fireEvent.change(el, { target: { value: text } }); + +/** Chromium / Firefox: the picking keydown arrives inside the composition. */ +const composeChromium: Composer = (el, text) => { + fireEvent.compositionStart(el, { data: '' }); + type(el, text); + fireEvent.keyDown(el, COMPOSING_ENTER); + fireEvent.compositionEnd(el, { data: text }); +}; + +/** Safari: the composition has ended when the picking keydown arrives. */ +const composeSafari: Composer = (el, text) => { + fireEvent.compositionStart(el, { data: '' }); + type(el, text); + fireEvent.compositionEnd(el, { data: text }); + fireEvent.keyDown(el, SAFARI_POST_COMPOSITION_ENTER); +}; + +const BROWSERS = [['Chromium', composeChromium], ['Safari', composeSafari]] as const; + +afterEach(() => { + cleanup(); + vi.restoreAllMocks(); +}); + +describe('one IME-key predicate in the repository (objectstack#35996)', () => { + it('the fields widgets ask the very function the components entry exports', () => { + expect(isImeKey).toBe(componentsIsImeKey); + }); +}); + +describe('GridField — an IME Enter in a cell neither moves nor appends a line (objectstack#35996)', () => { + const FIELD = { name: 'lines', type: 'grid', columns: [{ name: 'desc', type: 'text', label: 'Desc' }] } as never; + + function mountGrid() { + const onChange = vi.fn(); + const Host = () => { + const [value, setValue] = React.useState[]>([{ desc: 'a' }, { desc: 'b' }]); + return ( + []) => { + onChange(v); + setValue(v); + }} + field={FIELD} + /> + ); + }; + const { container } = render(); + const cell = (row: number) => container.querySelector(`[data-cell="${row}-0"]`) as HTMLInputElement | null; + return { cell, onChange }; + } + + for (const [browser, compose] of BROWSERS) { + it(`${browser}: the candidate-picking Enter keeps focus on its line; the next Enter moves down`, () => { + const { cell } = mountGrid(); + cell(0)!.focus(); + compose(cell(0)!, '你'); + expect(document.activeElement).toBe(cell(0)); + expect(cell(0)!.value).toBe('你'); + + fireEvent.keyDown(cell(0)!, PLAIN_ENTER); + expect(document.activeElement).toBe(cell(1)); + }); + + it(`${browser}: on the last line the candidate-picking Enter appends no line; the next Enter does`, async () => { + const { cell, onChange } = mountGrid(); + cell(1)!.focus(); + compose(cell(1)!, '好'); + // The composed text is the one write; no third line. + expect(onChange.mock.calls.every(([rows]) => (rows as unknown[]).length === 2)).toBe(true); + expect(cell(2)).toBeNull(); + expect(document.activeElement).toBe(cell(1)); + + fireEvent.keyDown(cell(1)!, PLAIN_ENTER); + expect(onChange).toHaveBeenLastCalledWith([expect.anything(), expect.anything(), expect.anything()]); + await waitFor(() => expect(document.activeElement).toBe(cell(2))); + }); + } +}); + +describe('DatePickerField — an IME Enter does not commit the typed text (objectstack#35996)', () => { + const FIELD = { name: 'close_date', type: 'date' } as never; + + for (const [browser, compose] of BROWSERS) { + it(`${browser}: the candidate-picking Enter commits nothing; the next Enter commits the date`, () => { + const onChange = vi.fn(); + render(); + const input = screen.getByRole('textbox') as HTMLInputElement; + input.focus(); + compose(input, '10/16/2026'); + expect(onChange).not.toHaveBeenCalled(); + expect(input).not.toHaveAttribute('aria-invalid', 'true'); + expect(input.value).toBe('10/16/2026'); + + fireEvent.keyDown(input, PLAIN_ENTER); + expect(onChange).toHaveBeenLastCalledWith('2026-10-16'); + }); + } +}); + +describe('RecordPickerDialog — an IME Enter in the page-jump box neither jumps nor clears it (objectstack#35996)', () => { + const USERS = Array.from({ length: 12 }, (_, i) => { + const n = String(i + 1).padStart(2, '0'); + return { id: `u${n}`, name: `User ${n}` }; + }); + const FIELDS = { name: { type: 'text', label: 'Name' } }; + + function mountPicker() { + const dataSource = { + find: vi.fn(async (_o: string, params?: { $skip?: number; $top?: number }) => { + const skip = params?.$skip ?? 0; + const top = params?.$top ?? USERS.length; + return { data: USERS.slice(skip, skip + top), total: USERS.length }; + }), + findOne: vi.fn(async (_o: string, id: string) => USERS.find((r) => r.id === id) ?? null), + getObjectSchema: vi.fn(async () => ({ name: 'sys_user', fields: FIELDS })), + } as any; + render( + {}} + dataSource={dataSource} + objectName="sys_user" + columns={['name']} + fieldsMeta={FIELDS} + cellRenderer={getCellRenderer} + onSelect={() => {}} + />, + ); + } + const rows = () => document.querySelectorAll('[data-testid^="record-row-"]'); + + for (const [browser, compose] of BROWSERS) { + it(`${browser}: the candidate-picking Enter stays on page 1 and keeps the text; the next Enter jumps`, async () => { + mountPicker(); + await waitFor(() => expect(rows()).toHaveLength(10)); + const jump = screen.getByTestId('record-picker-page-jump') as HTMLInputElement; + jump.focus(); + compose(jump, '2'); + expect(jump.value).toBe('2'); + expect(jump).toHaveAttribute('placeholder', '1'); + + fireEvent.keyDown(jump, PLAIN_ENTER); + await waitFor(() => expect(rows()).toHaveLength(2)); + expect(jump).toHaveAttribute('placeholder', '2'); + expect(jump.value).toBe(''); + }); + } +}); diff --git a/ui/packages/fields/src/widgets/imeKey.ts b/ui/packages/fields/src/widgets/imeKey.ts index 56977fae4cd..fdb902bf613 100644 --- a/ui/packages/fields/src/widgets/imeKey.ts +++ b/ui/packages/fields/src/widgets/imeKey.ts @@ -7,26 +7,10 @@ */ /** - * A key the input method owns: pressed while a composition is open - * (`isComposing`, keyCode 229), or the 229 a browser reports for the key that - * ends one (Safari raises the candidate-picking Enter AFTER `compositionend`, - * with `isComposing` already false and keyCode 229). Enter, the comma, the - * arrows and Backspace pressed then pick, type or move a candidate inside the - * input method; a widget that reads those keys itself must leave them alone - * and act on the next key after the composition ends. - * - * The one predicate every widget in this package that reads its own keys asks - * (`TagsField`, and the search boxes of `LookupField` and `PeoplePicker`), so - * they cannot drift into three answers. It is package-internal on purpose: - * nothing re-exports it from this package's entry. - * - * ⚠️ It has one twin outside this package: `isImeKey` in - * `@object-ui/components` (`renderers/complex/dataTableCellSelection.ts`), - * the list grid's own key guard. That one is not on the components entry, - * and importing it here would add an export to a published entry, so this is - * a second definition of the same question. A change to one is a change to - * both; nothing checks that the two still agree. + * The input-method key guard every widget in this package that reads its own + * keys asks. It is NOT defined here: the one definition is `isImeKey` on the + * `@object-ui/components` entry (objectstack#35996), which the list grid asks + * too. This module only passes it through so the widgets keep one local + * import path; it is not re-exported from this package's entry. */ -export function isImeKey(e: { nativeEvent: KeyboardEvent; keyCode?: number }): boolean { - return e.nativeEvent.isComposing === true || e.nativeEvent.keyCode === 229 || e.keyCode === 229; -} +export { isImeKey } from '@object-ui/components';