xonecas.io
70160
Ce

cel-tui / commit eea7005

feat(core): implement TextInput — editing, cursor, scroll, painting

- Text editing module: insert, delete (backward/forward), cursor movement
  (left/right/up/down/home/end), vertical navigation across lines
- TextInput painting: word-wrap, scroll offset, cursor display (inverted),
  placeholder rendering when empty
- Framework-managed cursor and scroll state (WeakMap per props)
- Input routing: focused TextInput consumes editing keys, modifier combos
  bubble up to onKeyPress handlers, submitKey fires onSubmit
- Unfocused TextInput does not consume keys
- 29 new tests (text-edit: 20, integration: 9), 170 total all passing

xonecas2026-04-03 eea700538193241cfef4a9719d4afb49dd86e220 parent 1e1ec1d

5 files changed

packages/core/src/cel.ts+98−11
@@ -8,9 +8,21 @@ import {
88 findKeyPressHandler,
99 collectFocusable,
1010 } from "./hit-test.js";
11-import { parseKey, isEditingKey } from "./keys.js";
11+import { parseKey, isEditingKey, normalizeKey } from "./keys.js";
1212 import { layout, type LayoutNode } from "./layout.js";
13-import { paint } from "./paint.js";
13+import {
14+ paint,
15+ getTextInputCursor,
16+ setTextInputCursor,
17+ setTextInputScroll,
18+} from "./paint.js";
19+import {
20+ insertChar,
21+ deleteBackward,
22+ deleteForward,
23+ moveCursor,
24+ type EditState,
25+} from "./text-edit.js";
1426 import type { Terminal } from "./terminal.js";
1527
1628 type RenderFn = () => Node | Node[];
@@ -131,18 +143,74 @@ function handleMouseEvent(event: MouseEvent): void {
131143 }
132144
133145 function handleKeyEvent(key: string): void {
134- // Tab / Shift+Tab — focus traversal (handled by app via onFocus/onBlur)
135- // Escape — unfocus (handled by app)
136- // Enter on focused clickable — find and fire onClick
146+ // Find the focused TextInput (if any) to route editing keys
147+ const focusedInput = findFocusedTextInput();
148+
149+ if (focusedInput) {
150+ const props = focusedInput.node
151+ .props as import("@cel-tui/types").TextInputProps;
152+
153+ // Check submitKey
154+ const submitKey = normalizeKey(props.submitKey ?? "enter");
155+ if (key === submitKey && props.onSubmit) {
156+ props.onSubmit();
157+ cel.render();
158+ return;
159+ }
137160
138- // For now, route key events through the layout tree:
139- // Walk all layers top-to-bottom, find onKeyPress handlers
161+ // Editing keys are consumed by TextInput
162+ if (isEditingKey(key)) {
163+ const cursor = getTextInputCursor(props);
164+ const editState: EditState = { value: props.value, cursor };
165+ let newState: EditState | null = null;
166+
167+ switch (key) {
168+ case "backspace":
169+ newState = deleteBackward(editState);
170+ break;
171+ case "delete":
172+ newState = deleteForward(editState);
173+ break;
174+ case "left":
175+ case "right":
176+ case "up":
177+ case "down":
178+ case "home":
179+ case "end":
180+ newState = moveCursor(
181+ editState,
182+ key as "left" | "right" | "up" | "down" | "home" | "end",
183+ focusedInput.rect.width,
184+ );
185+ break;
186+ case "enter":
187+ newState = insertChar(editState, "\n");
188+ break;
189+ case "tab":
190+ newState = insertChar(editState, "\t");
191+ break;
192+ default:
193+ // Single printable character
194+ if (key.length === 1) {
195+ newState = insertChar(editState, key);
196+ }
197+ break;
198+ }
199+
200+ if (newState && newState !== editState) {
201+ setTextInputCursor(props, newState.cursor);
202+ if (newState.value !== editState.value) {
203+ props.onChange(newState.value);
204+ }
205+ cel.render();
206+ return;
207+ }
208+ }
209+ }
210+
211+ // Key not consumed by TextInput — bubble through layers
140212 for (let i = currentLayouts.length - 1; i >= 0; i--) {
141213 const layoutRoot = currentLayouts[i]!;
142-
143- // Find the first onKeyPress handler in the tree (root acts as global)
144- // In a full implementation, this would start from the focused element
145- // and bubble up. For now, walk from root.
146214 const path = [layoutRoot];
147215 const handler = findKeyPressHandler(path);
148216 if (handler) {
@@ -153,6 +221,25 @@ function handleKeyEvent(key: string): void {
153221 }
154222 }
155223
224+function findFocusedTextInput(): LayoutNode | null {
225+ for (let i = currentLayouts.length - 1; i >= 0; i--) {
226+ const found = findFocusedInTree(currentLayouts[i]!);
227+ if (found) return found;
228+ }
229+ return null;
230+}
231+
232+function findFocusedInTree(ln: LayoutNode): LayoutNode | null {
233+ if (ln.node.type === "textinput" && ln.node.props.focused) {
234+ return ln;
235+ }
236+ for (const child of ln.children) {
237+ const found = findFocusedInTree(child);
238+ if (found) return found;
239+ }
240+ return null;
241+}
242+
156243 // --- Public API ---
157244
158245 /**
packages/core/src/paint.ts+126−2
@@ -1,4 +1,4 @@
1-import type { Color } from "@cel-tui/types";
1+import type { Color, TextInputProps } from "@cel-tui/types";
22 import type { Cell } from "./cell-buffer.js";
33 import { CellBuffer } from "./cell-buffer.js";
44 import type { LayoutNode, Rect } from "./layout.js";
@@ -25,7 +25,7 @@ function paintLayoutNode(ln: LayoutNode, buf: CellBuffer): void {
2525 paintText(node.content, node.props, rect, buf);
2626 break;
2727 case "textinput":
28- // TODO: implement in phase 7
28+ paintTextInput(node.props, rect, buf);
2929 break;
3030 case "vstack":
3131 case "hstack":
@@ -110,6 +110,130 @@ function paintText(
110110 }
111111 }
112112
113+function paintTextInput(
114+ props: TextInputProps,
115+ rect: Rect,
116+ buf: CellBuffer,
117+): void {
118+ const { x, y, width: w, height: h } = rect;
119+ if (w <= 0 || h <= 0) return;
120+
121+ const value = props.value;
122+ const showPlaceholder = value.length === 0 && props.placeholder;
123+
124+ if (showPlaceholder && props.placeholder) {
125+ // Paint placeholder text
126+ paintText(props.placeholder.content, props.placeholder.props, rect, buf);
127+ return;
128+ }
129+
130+ // Word-wrap value (always on for TextInput)
131+ const lines: string[] = [];
132+ for (const rawLine of value.split("\n")) {
133+ if (rawLine.length <= w) {
134+ lines.push(rawLine);
135+ } else {
136+ wrapLine(rawLine, w, lines);
137+ }
138+ }
139+
140+ // Framework-managed scroll: use stored scroll offset
141+ const scrollOffset = getTextInputScroll(props);
142+
143+ // Paint visible lines
144+ for (let row = 0; row < h; row++) {
145+ const lineIdx = scrollOffset + row;
146+ if (lineIdx >= lines.length) break;
147+ const line = lines[lineIdx]!;
148+ for (let col = 0; col < line.length && col < w; col++) {
149+ buf.set(x + col, y + row, makeCell(line[col]!, props));
150+ }
151+ }
152+
153+ // Paint cursor if focused
154+ if (props.focused) {
155+ const cursorOffset = getTextInputCursor(props);
156+ const pos = offsetToWrappedPos(value, cursorOffset, w);
157+ const screenRow = pos.line - scrollOffset;
158+ if (screenRow >= 0 && screenRow < h && pos.col < w) {
159+ const existing = buf.get(x + pos.col, y + screenRow);
160+ // Invert colors for cursor visibility
161+ buf.set(x + pos.col, y + screenRow, {
162+ char: existing.char === " " && !existing.bgColor ? " " : existing.char,
163+ fgColor: existing.bgColor ?? "black",
164+ bgColor: existing.fgColor ?? "white",
165+ bold: existing.bold,
166+ italic: existing.italic,
167+ underline: existing.underline,
168+ });
169+ }
170+ }
171+}
172+
173+/**
174+ * Map a cursor offset in the raw value to a (line, col) position
175+ * in the word-wrapped output.
176+ */
177+function offsetToWrappedPos(
178+ value: string,
179+ cursor: number,
180+ width: number,
181+): { line: number; col: number } {
182+ const rawLines = value.split("\n");
183+ let offset = 0;
184+ let wrappedLine = 0;
185+
186+ for (const rawLine of rawLines) {
187+ if (cursor <= offset + rawLine.length) {
188+ // Cursor is in this raw line
189+ const colInRaw = cursor - offset;
190+ if (width <= 0) return { line: wrappedLine, col: colInRaw };
191+ const extraLines = Math.floor(colInRaw / width);
192+ return { line: wrappedLine + extraLines, col: colInRaw % width };
193+ }
194+ // Count wrapped lines for this raw line
195+ if (rawLine.length <= width || width <= 0) {
196+ wrappedLine += 1;
197+ } else {
198+ wrappedLine += Math.ceil(rawLine.length / width);
199+ }
200+ offset += rawLine.length + 1; // +1 for \n
201+ }
202+
203+ return { line: wrappedLine, col: 0 };
204+}
205+
206+// --- TextInput framework-managed state ---
207+
208+const textInputCursors = new WeakMap<TextInputProps, number>();
209+const textInputScrolls = new WeakMap<TextInputProps, number>();
210+
211+/** Get the cursor offset for a TextInput (framework-managed). */
212+export function getTextInputCursor(props: TextInputProps): number {
213+ return textInputCursors.get(props) ?? props.value.length;
214+}
215+
216+/** Set the cursor offset for a TextInput. */
217+export function setTextInputCursor(
218+ props: TextInputProps,
219+ cursor: number,
220+): void {
221+ textInputCursors.set(props, cursor);
222+}
223+
224+/** Get the scroll offset for a TextInput (framework-managed). */
225+export function getTextInputScroll(props: TextInputProps): number {
226+ return textInputScrolls.get(props) ?? 0;
227+}
228+
229+/** Set the scroll offset for a TextInput. */
230+export function setTextInputScroll(
231+ props: TextInputProps,
232+ scroll: number,
233+): void {
234+ textInputScrolls.set(props, scroll);
235+}
236+
113237 /**
114238 * Simple word-wrap: break a line into multiple lines at word boundaries.
115239 */
packages/core/src/text-edit.test.ts+134−0
@@ -0,0 +1,134 @@
1+import { describe, expect, test } from "bun:test";
2+import {
3+ insertChar,
4+ deleteBackward,
5+ deleteForward,
6+ moveCursor,
7+ type EditState,
8+} from "./text-edit.js";
9+
10+function state(value: string, cursor: number): EditState {
11+ return { value, cursor };
12+}
13+
14+describe("text editing", () => {
15+ describe("insertChar", () => {
16+ test("inserts at start", () => {
17+ const result = insertChar(state("hello", 0), "X");
18+ expect(result.value).toBe("Xhello");
19+ expect(result.cursor).toBe(1);
20+ });
21+
22+ test("inserts at end", () => {
23+ const result = insertChar(state("hello", 5), "!");
24+ expect(result.value).toBe("hello!");
25+ expect(result.cursor).toBe(6);
26+ });
27+
28+ test("inserts in middle", () => {
29+ const result = insertChar(state("helo", 3), "l");
30+ expect(result.value).toBe("hello");
31+ expect(result.cursor).toBe(4);
32+ });
33+
34+ test("inserts newline", () => {
35+ const result = insertChar(state("ab", 1), "\n");
36+ expect(result.value).toBe("a\nb");
37+ expect(result.cursor).toBe(2);
38+ });
39+
40+ test("inserts tab", () => {
41+ const result = insertChar(state("ab", 1), "\t");
42+ expect(result.value).toBe("a\tb");
43+ expect(result.cursor).toBe(2);
44+ });
45+ });
46+
47+ describe("deleteBackward", () => {
48+ test("deletes character before cursor", () => {
49+ const result = deleteBackward(state("hello", 3));
50+ expect(result.value).toBe("helo");
51+ expect(result.cursor).toBe(2);
52+ });
53+
54+ test("no-op at start of string", () => {
55+ const result = deleteBackward(state("hello", 0));
56+ expect(result.value).toBe("hello");
57+ expect(result.cursor).toBe(0);
58+ });
59+
60+ test("deletes last character", () => {
61+ const result = deleteBackward(state("a", 1));
62+ expect(result.value).toBe("");
63+ expect(result.cursor).toBe(0);
64+ });
65+ });
66+
67+ describe("deleteForward", () => {
68+ test("deletes character after cursor", () => {
69+ const result = deleteForward(state("hello", 2));
70+ expect(result.value).toBe("helo");
71+ expect(result.cursor).toBe(2);
72+ });
73+
74+ test("no-op at end of string", () => {
75+ const result = deleteForward(state("hello", 5));
76+ expect(result.value).toBe("hello");
77+ expect(result.cursor).toBe(5);
78+ });
79+ });
80+
81+ describe("moveCursor", () => {
82+ test("left moves back one", () => {
83+ const result = moveCursor(state("hello", 3), "left");
84+ expect(result.cursor).toBe(2);
85+ });
86+
87+ test("left at start stays at 0", () => {
88+ const result = moveCursor(state("hello", 0), "left");
89+ expect(result.cursor).toBe(0);
90+ });
91+
92+ test("right moves forward one", () => {
93+ const result = moveCursor(state("hello", 3), "right");
94+ expect(result.cursor).toBe(4);
95+ });
96+
97+ test("right at end stays at end", () => {
98+ const result = moveCursor(state("hello", 5), "right");
99+ expect(result.cursor).toBe(5);
100+ });
101+
102+ test("home moves to start", () => {
103+ const result = moveCursor(state("hello", 3), "home");
104+ expect(result.cursor).toBe(0);
105+ });
106+
107+ test("end moves to end", () => {
108+ const result = moveCursor(state("hello", 2), "end");
109+ expect(result.cursor).toBe(5);
110+ });
111+
112+ test("up moves to previous line", () => {
113+ const result = moveCursor(state("abc\ndef", 5), "up", 10);
114+ // Cursor at "e" (offset 5), up should go to offset 1 ("b")
115+ expect(result.cursor).toBe(1);
116+ });
117+
118+ test("down moves to next line", () => {
119+ const result = moveCursor(state("abc\ndef", 1), "down", 10);
120+ // Cursor at "b" (offset 1), down should go to offset 5 ("e")
121+ expect(result.cursor).toBe(5);
122+ });
123+
124+ test("up at first line stays at start", () => {
125+ const result = moveCursor(state("hello", 3), "up", 10);
126+ expect(result.cursor).toBe(0);
127+ });
128+
129+ test("down at last line stays at end", () => {
130+ const result = moveCursor(state("hello", 3), "down", 10);
131+ expect(result.cursor).toBe(5);
132+ });
133+ });
134+});
packages/core/src/text-edit.ts+164−0
@@ -0,0 +1,164 @@
1+/**
2+ * Framework-managed editing state for TextInput.
3+ */
4+export interface EditState {
5+ /** Current text value. */
6+ value: string;
7+ /** Cursor offset in the string (0 = before first char). */
8+ cursor: number;
9+}
10+
11+/**
12+ * Insert a character (or string) at the cursor position.
13+ */
14+export function insertChar(state: EditState, char: string): EditState {
15+ const { value, cursor } = state;
16+ return {
17+ value: value.slice(0, cursor) + char + value.slice(cursor),
18+ cursor: cursor + char.length,
19+ };
20+}
21+
22+/**
23+ * Delete the character before the cursor (Backspace).
24+ */
25+export function deleteBackward(state: EditState): EditState {
26+ const { value, cursor } = state;
27+ if (cursor === 0) return state;
28+ return {
29+ value: value.slice(0, cursor - 1) + value.slice(cursor),
30+ cursor: cursor - 1,
31+ };
32+}
33+
34+/**
35+ * Delete the character after the cursor (Delete key).
36+ */
37+export function deleteForward(state: EditState): EditState {
38+ const { value, cursor } = state;
39+ if (cursor >= value.length) return state;
40+ return {
41+ value: value.slice(0, cursor) + value.slice(cursor + 1),
42+ cursor,
43+ };
44+}
45+
46+/**
47+ * Move the cursor in the given direction.
48+ *
49+ * @param state - Current edit state.
50+ * @param direction - Movement direction.
51+ * @param width - Available width (for up/down line navigation).
52+ */
53+export function moveCursor(
54+ state: EditState,
55+ direction: "left" | "right" | "up" | "down" | "home" | "end",
56+ width?: number,
57+): EditState {
58+ const { value, cursor } = state;
59+
60+ switch (direction) {
61+ case "left":
62+ return { value, cursor: Math.max(0, cursor - 1) };
63+ case "right":
64+ return { value, cursor: Math.min(value.length, cursor + 1) };
65+ case "home":
66+ return { value, cursor: 0 };
67+ case "end":
68+ return { value, cursor: value.length };
69+ case "up":
70+ case "down":
71+ return moveVertical(state, direction, width ?? 80);
72+ }
73+}
74+
75+/**
76+ * Vertical cursor movement — maps cursor offset to line/column,
77+ * moves up or down a line, then maps back to offset.
78+ */
79+function moveVertical(
80+ state: EditState,
81+ direction: "up" | "down",
82+ width: number,
83+): EditState {
84+ const { value, cursor } = state;
85+ const lines = value.split("\n");
86+
87+ // Find which line and column the cursor is on
88+ let offset = 0;
89+ let cursorLine = 0;
90+ let cursorCol = 0;
91+
92+ for (let i = 0; i < lines.length; i++) {
93+ const lineLen = lines[i]!.length;
94+ if (offset + lineLen >= cursor && cursorLine === 0 && offset <= cursor) {
95+ cursorLine = i;
96+ cursorCol = cursor - offset;
97+ }
98+ offset += lineLen + 1; // +1 for \n
99+ }
100+
101+ // Re-scan to get correct line (the above loop has a bug for first line)
102+ offset = 0;
103+ for (let i = 0; i < lines.length; i++) {
104+ const lineLen = lines[i]!.length;
105+ if (cursor >= offset && cursor <= offset + lineLen) {
106+ cursorLine = i;
107+ cursorCol = cursor - offset;
108+ break;
109+ }
110+ offset += lineLen + 1;
111+ }
112+
113+ // Move line
114+ let targetLine = cursorLine;
115+ if (direction === "up") {
116+ targetLine = Math.max(0, cursorLine - 1);
117+ } else {
118+ targetLine = Math.min(lines.length - 1, cursorLine + 1);
119+ }
120+
121+ if (targetLine === cursorLine) {
122+ // Can't move — go to start (up) or end (down)
123+ return {
124+ value,
125+ cursor: direction === "up" ? 0 : value.length,
126+ };
127+ }
128+
129+ // Map column to new line, clamping to line length
130+ const targetLineLen = lines[targetLine]!.length;
131+ const targetCol = Math.min(cursorCol, targetLineLen);
132+
133+ // Compute new offset
134+ let newOffset = 0;
135+ for (let i = 0; i < targetLine; i++) {
136+ newOffset += lines[i]!.length + 1;
137+ }
138+ newOffset += targetCol;
139+
140+ return { value, cursor: newOffset };
141+}
142+
143+/**
144+ * Map a cursor offset to (line, column) in the raw text (split by \n).
145+ * Used for painting the cursor.
146+ */
147+export function cursorToLineCol(
148+ value: string,
149+ cursor: number,
150+): { line: number; col: number } {
151+ const lines = value.split("\n");
152+ let offset = 0;
153+
154+ for (let i = 0; i < lines.length; i++) {
155+ const lineLen = lines[i]!.length;
156+ if (cursor <= offset + lineLen) {
157+ return { line: i, col: cursor - offset };
158+ }
159+ offset += lineLen + 1;
160+ }
161+
162+ // Cursor at very end
163+ return { line: lines.length - 1, col: lines[lines.length - 1]!.length };
164+}
packages/core/src/text-input.test.ts+236−0
@@ -0,0 +1,236 @@
1+import { afterEach, describe, expect, test } from "bun:test";
2+import { cel } from "./cel.js";
3+import { MockTerminal } from "./terminal.js";
4+import { VStack } from "./primitives/stacks.js";
5+import { Text } from "./primitives/text.js";
6+import { TextInput } from "./primitives/text-input.js";
7+
8+describe("TextInput integration", () => {
9+ let term: MockTerminal;
10+
11+ afterEach(() => {
12+ cel.stop();
13+ });
14+
15+ function setup(columns = 20, rows = 5): MockTerminal {
16+ term = new MockTerminal(columns, rows);
17+ cel.init(term);
18+ return term;
19+ }
20+
21+ async function waitForRender(): Promise<void> {
22+ await new Promise((resolve) => setTimeout(resolve, 10));
23+ }
24+
25+ test("renders value text", async () => {
26+ const term = setup(20, 3);
27+ cel.viewport(() =>
28+ VStack({}, [TextInput({ value: "hello", onChange: () => {} })]),
29+ );
30+ await waitForRender();
31+ expect(term.output).toContain("hello");
32+ });
33+
34+ test("renders placeholder when value is empty", async () => {
35+ const term = setup(20, 3);
36+ cel.viewport(() =>
37+ VStack({}, [
38+ TextInput({
39+ value: "",
40+ onChange: () => {},
41+ placeholder: Text("type here...", { fgColor: "brightBlack" }),
42+ }),
43+ ]),
44+ );
45+ await waitForRender();
46+ expect(term.output).toContain("type here...");
47+ });
48+
49+ test("typing fires onChange with new value", async () => {
50+ const term = setup(20, 3);
51+ let value = "hi";
52+ cel.viewport(() =>
53+ VStack({}, [
54+ TextInput({
55+ value,
56+ focused: true,
57+ onChange: (v) => {
58+ value = v;
59+ },
60+ }),
61+ ]),
62+ );
63+ await waitForRender();
64+
65+ // Type "!"
66+ term.sendInput("!");
67+ await waitForRender();
68+
69+ expect(value).toBe("hi!");
70+ });
71+
72+ test("backspace deletes character", async () => {
73+ const term = setup(20, 3);
74+ let value = "abc";
75+ cel.viewport(() =>
76+ VStack({}, [
77+ TextInput({
78+ value,
79+ focused: true,
80+ onChange: (v) => {
81+ value = v;
82+ },
83+ }),
84+ ]),
85+ );
86+ await waitForRender();
87+
88+ // Press backspace
89+ term.sendInput("\x7f");
90+ await waitForRender();
91+
92+ expect(value).toBe("ab");
93+ });
94+
95+ test("enter inserts newline by default", async () => {
96+ const term = setup(20, 3);
97+ let value = "ab";
98+ cel.viewport(() =>
99+ VStack({}, [
100+ TextInput({
101+ value,
102+ focused: true,
103+ onChange: (v) => {
104+ value = v;
105+ },
106+ }),
107+ ]),
108+ );
109+ await waitForRender();
110+
111+ // Press enter
112+ term.sendInput("\r");
113+ await waitForRender();
114+
115+ expect(value).toBe("ab\n");
116+ });
117+
118+ test("submitKey fires onSubmit instead of inserting", async () => {
119+ const term = setup(20, 3);
120+ let submitted = false;
121+ let value = "hello";
122+ cel.viewport(() =>
123+ VStack({}, [
124+ TextInput({
125+ value,
126+ focused: true,
127+ onChange: (v) => {
128+ value = v;
129+ },
130+ onSubmit: () => {
131+ submitted = true;
132+ },
133+ submitKey: "enter",
134+ }),
135+ ]),
136+ );
137+ await waitForRender();
138+
139+ term.sendInput("\r");
140+ await waitForRender();
141+
142+ expect(submitted).toBe(true);
143+ expect(value).toBe("hello"); // Value should not change
144+ });
145+
146+ test("ctrl+enter as submitKey with enter for newlines", async () => {
147+ const term = setup(20, 3);
148+ let submitted = false;
149+ let value = "hi";
150+ cel.viewport(() =>
151+ VStack({}, [
152+ TextInput({
153+ value,
154+ focused: true,
155+ onChange: (v) => {
156+ value = v;
157+ },
158+ onSubmit: () => {
159+ submitted = true;
160+ },
161+ submitKey: "ctrl+enter",
162+ }),
163+ ]),
164+ );
165+ await waitForRender();
166+
167+ // Enter should insert newline (not submit)
168+ term.sendInput("\r");
169+ await waitForRender();
170+ expect(value).toBe("hi\n");
171+ expect(submitted).toBe(false);
172+ });
173+
174+ test("modifier keys bubble up (not consumed by TextInput)", async () => {
175+ const term = setup(20, 3);
176+ let receivedKey = "";
177+ let value = "text";
178+ cel.viewport(() =>
179+ VStack(
180+ {
181+ onKeyPress: (key) => {
182+ receivedKey = key;
183+ },
184+ },
185+ [
186+ TextInput({
187+ value,
188+ focused: true,
189+ onChange: (v) => {
190+ value = v;
191+ },
192+ }),
193+ ],
194+ ),
195+ );
196+ await waitForRender();
197+
198+ // Ctrl+S should bubble up
199+ term.sendInput("\x13");
200+ await waitForRender();
201+
202+ expect(receivedKey).toBe("ctrl+s");
203+ expect(value).toBe("text"); // Value unchanged
204+ });
205+
206+ test("unfocused TextInput does not consume keys", async () => {
207+ const term = setup(20, 3);
208+ let receivedKey = "";
209+ let value = "text";
210+ cel.viewport(() =>
211+ VStack(
212+ {
213+ onKeyPress: (key) => {
214+ receivedKey = key;
215+ },
216+ },
217+ [
218+ TextInput({
219+ value,
220+ focused: false,
221+ onChange: (v) => {
222+ value = v;
223+ },
224+ }),
225+ ],
226+ ),
227+ );
228+ await waitForRender();
229+
230+ term.sendInput("a");
231+ await waitForRender();
232+
233+ expect(value).toBe("text"); // Not consumed
234+ expect(receivedKey).toBe("a"); // Bubbled up
235+ });
236+});