gridcn2.0.0
Types and hooks

isComposingKey

Tell a key of an IME composition from a plain key, so your Enter and Escape handlers ignore keys that confirm composed text.

An IME (input method editor) is the tool for typing Chinese, Japanese, or Korean. While it composes text, a key such as Enter confirms the text and is not a command. isComposingKey tells the two apart. The function does not depend on the grid.

pnpm dlx shadcn@latest add @gridcn/is-composing-key

The item installs to components/gridcn/is-composing-key.ts.

isComposingKey

function isComposingKey(
  event: React.KeyboardEvent | Pick<KeyboardEvent, "isComposing" | "keyCode">,
): boolean;

The function returns true while a key belongs to an IME composition. Gate your own Enter and Escape handlers with it:

import { isComposingKey } from "@/components/gridcn/is-composing-key";

export function NameField({ onEnter }: { onEnter: () => void }) {
  return (
    <input
      onKeyDown={(event) => {
        if (isComposingKey(event)) return;
        if (event.key === "Enter") onEnter();
      }}
    />
  );
}

While an IME composes, Enter confirms the composed text and onEnter does not run.

Arguments

ArgumentTypeMeaning
eventReact keyboard event, or { isComposing, keyCode }The keydown to test.

Behavior

  • If isComposing is true or keyCode is 229, the result is true.
  • Safari confirms a composition with isComposing set to false and keyCode set to 229. A test of isComposing alone misses that key.
  • For a React event, the function reads nativeEvent.
  • The data-grid-sdk module of the core exports the same function. The grid keyboard, useCellEditor, and the text, number, URL, date, and long text editors use it.
  • useDebouncedInput applies the same test in its onKeyDown. The hook carries its own copy, so the use-debounce item needs no is-composing-key item.

On this page