Skip to content

Commit 3530934

Browse files
committed
fix(studio): leave NumberField IME confirmation to the input method
1 parent 725f069 commit 3530934

3 files changed

Lines changed: 66 additions & 1 deletion

File tree

‎docs/studio/canvas.mdx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ For exact values, use **Design**. Its sections depend on what you selected:
4141

4242
Make one kind of change at a time, play it, then decide on the next.
4343

44+
When typing an exact numeric value, press **Enter** to apply it. If you use an input method, the Enter that confirms
45+
its text finishes that input first; press Enter again to apply the value.
46+
4447
<Warning>
4548
**Auto-record manual edits as keyframes** is on by default, so moving something at one frame can quietly become an
4649
animation. Turn it off in the timeline toolbar for a fix that should hold for the whole clip.

‎packages/studio/src/components/ui/NumberField.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,13 @@ export function NumberField({
9999
aria-invalid={invalid || undefined}
100100
className={cn(fieldText, "tabular-nums")}
101101
onKeyDown={(event) => {
102-
if (event.key !== "Enter") return;
102+
// Older WebKit clears isComposing before confirmation, but leaves keyCode 229.
103+
if (
104+
event.key !== "Enter" ||
105+
event.nativeEvent.isComposing ||
106+
event.nativeEvent.keyCode === 229
107+
)
108+
return;
103109
event.preventDefault();
104110
commitFromText();
105111
}}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// @vitest-environment happy-dom
2+
import React, { act } from "react";
3+
import { afterEach, describe, expect, it, vi } from "vitest";
4+
import { NumberField } from "./NumberField";
5+
import { cleanupMounted, mountHost } from "./mountHost.testHelpers";
6+
7+
afterEach(cleanupMounted);
8+
9+
describe("NumberField composition", () => {
10+
it.each([
11+
{ name: "an active composition", isComposing: true, keyCode: 13 },
12+
{ name: "Safari's composition confirmation", isComposing: false, keyCode: 229 },
13+
])("leaves Enter to $name before committing on a later Enter", ({ isComposing, keyCode }) => {
14+
const commits: number[] = [];
15+
const onTrack = vi.fn();
16+
const host = mountHost(
17+
<NumberField
18+
label="Size"
19+
value={24}
20+
onCommit={(next) => commits.push(next)}
21+
onTrack={onTrack}
22+
/>,
23+
);
24+
const input = host.querySelector("input")!;
25+
act(() => {
26+
const setValue = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!;
27+
setValue.call(input, "42");
28+
input.dispatchEvent(new Event("input", { bubbles: true }));
29+
});
30+
const confirmingEnter = new KeyboardEvent("keydown", {
31+
key: "Enter",
32+
keyCode,
33+
which: keyCode,
34+
isComposing,
35+
bubbles: true,
36+
cancelable: true,
37+
});
38+
39+
act(() => void input.dispatchEvent(confirmingEnter));
40+
41+
expect(confirmingEnter.defaultPrevented).toBe(false);
42+
expect(input.value).toBe("42");
43+
expect(commits).toEqual([]);
44+
expect(onTrack).not.toHaveBeenCalled();
45+
46+
act(
47+
() =>
48+
void input.dispatchEvent(
49+
new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true }),
50+
),
51+
);
52+
53+
expect(commits).toEqual([42]);
54+
expect(onTrack).toHaveBeenCalledTimes(1);
55+
});
56+
});

0 commit comments

Comments
 (0)