xonecas.io
70160
Ce

cel-tui / commit 747a5d7

fix(core): persist TextInput cursor position across re-renders

TextInput state (cursor, scroll) was keyed on the props object via
WeakMap, but props are recreated each frame. Now keyed on the
onChange function reference, which is a stable identity the app
provides.

Also fixes character case preservation: printable characters typed
into TextInput now use the raw input data instead of the lowercased
parseKey result, so 'X' stays 'X' not 'x'.

xonecas2026-04-04 747a5d70badea3c2bc1569c5b7bb4f14697d507a parent 2f123dc

4 files changed

TODO.md+1−1
@@ -76,7 +76,7 @@ What's implemented, what's missing, and what needs hardening. Reference `spec.md
7676 - ✅ `onSubmit` fires on `submitKey`
7777 - ✅ Placeholder rendering
7878 - ✅ Cursor display (inverted colors)
79-- 🔧 Cursor state uses `WeakMap` keyed on props — works for single render but may lose cursor position across re-renders since props objects are recreated each frame
79+- ✅ Cursor state keyed on `onChange` function reference for stable identity across re-renders
8080 - ❌ Framework-managed scroll (auto-scroll to keep cursor visible)
8181 - ❌ Mouse wheel scroll inside TextInput
8282
packages/core/src/cel.ts+6−4
@@ -91,7 +91,7 @@ function handleInput(data: string): void {
9191
9292 // Keyboard input
9393 const key = parseKey(data);
94- handleKeyEvent(key);
94+ handleKeyEvent(key, data);
9595 }
9696
9797 interface MouseEvent {
@@ -228,7 +228,7 @@ function findClickFocusTarget(path: LayoutNode[]): LayoutNode | null {
228228 return null;
229229 }
230230
231-function handleKeyEvent(key: string): void {
231+function handleKeyEvent(key: string, rawData?: string): void {
232232 // --- Focus traversal keys ---
233233
234234 // Tab / Shift+Tab: cycle through focusable elements
@@ -332,8 +332,10 @@ function handleKeyEvent(key: string): void {
332332 newState = insertChar(editState, "\t");
333333 break;
334334 default:
335- // Single printable character
336- if (key.length === 1) {
335+ // Single printable character — use raw data to preserve case
336+ if (key.length === 1 && rawData && rawData.length === 1) {
337+ newState = insertChar(editState, rawData);
338+ } else if (key.length === 1) {
337339 newState = insertChar(editState, key);
338340 }
339341 break;
packages/core/src/paint.ts+13−6
@@ -432,12 +432,19 @@ export function setContainerScroll(
432432 containerScrolls.set(props, scroll);
433433 }
434434
435-const textInputCursors = new WeakMap<TextInputProps, number>();
436-const textInputScrolls = new WeakMap<TextInputProps, number>();
435+/**
436+ * TextInput state is keyed on the `onChange` function reference, which is
437+ * a stable identity across re-renders (the app provides the same closure).
438+ * This avoids losing cursor/scroll position when props objects are recreated
439+ * each frame.
440+ */
441+type OnChangeFn = (value: string) => void;
442+const textInputCursors = new WeakMap<OnChangeFn, number>();
443+const textInputScrolls = new WeakMap<OnChangeFn, number>();
437444
438445 /** Get the cursor offset for a TextInput (framework-managed). */
439446 export function getTextInputCursor(props: TextInputProps): number {
440- return textInputCursors.get(props) ?? props.value.length;
447+ return textInputCursors.get(props.onChange) ?? props.value.length;
441448 }
442449
443450 /** Set the cursor offset for a TextInput. */
@@ -445,12 +452,12 @@ export function setTextInputCursor(
445452 props: TextInputProps,
446453 cursor: number,
447454 ): void {
448- textInputCursors.set(props, cursor);
455+ textInputCursors.set(props.onChange, cursor);
449456 }
450457
451458 /** Get the scroll offset for a TextInput (framework-managed). */
452459 export function getTextInputScroll(props: TextInputProps): number {
453- return textInputScrolls.get(props) ?? 0;
460+ return textInputScrolls.get(props.onChange) ?? 0;
454461 }
455462
456463 /** Set the scroll offset for a TextInput. */
@@ -458,7 +465,7 @@ export function setTextInputScroll(
458465 props: TextInputProps,
459466 scroll: number,
460467 ): void {
461- textInputScrolls.set(props, scroll);
468+ textInputScrolls.set(props.onChange, scroll);
462469 }
463470
464471 /**
packages/core/src/text-input.test.ts+34−0
@@ -203,6 +203,40 @@ describe("TextInput integration", () => {
203203 expect(value).toBe("text"); // Value unchanged
204204 });
205205
206+ test("cursor position persists across re-renders", async () => {
207+ const term = setup(20, 3);
208+ let value = "abcdef";
209+ const onChange = (v: string) => {
210+ value = v;
211+ cel.render();
212+ };
213+
214+ cel.viewport(() =>
215+ VStack({}, [
216+ TextInput({
217+ value,
218+ focused: true,
219+ onChange,
220+ }),
221+ ]),
222+ );
223+ await waitForRender();
224+
225+ // Move cursor left 3 times (from end of "abcdef")
226+ term.sendInput("\x1b[D"); // left
227+ await waitForRender();
228+ term.sendInput("\x1b[D"); // left
229+ await waitForRender();
230+ term.sendInput("\x1b[D"); // left
231+ await waitForRender();
232+
233+ // Now type "X" — should insert at position 3 (after "abc")
234+ term.sendInput("X");
235+ await waitForRender();
236+
237+ expect(value).toBe("abcXdef");
238+ });
239+
206240 test("unfocused TextInput does not consume keys", async () => {
207241 const term = setup(20, 3);
208242 let receivedKey = "";