Skip to content

fix(ui): resolve TextArea wrapper imports and add tests#1923

Open
Harshithk951 wants to merge 1 commit into
Karanjot786:mainfrom
Harshithk951:fix-textarea-imports-and-tests
Open

fix(ui): resolve TextArea wrapper imports and add tests#1923
Harshithk951 wants to merge 1 commit into
Karanjot786:mainfrom
Harshithk951:fix-textarea-imports-and-tests

Conversation

@Harshithk951

@Harshithk951 Harshithk951 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Description

This PR fixes missing imports (useState, useInput, KeyEvent) in the functional TextArea component wrapper inside packages/ui/src/components/TextArea.ts and changes its outer element from text to box to ensure children layout elements are reconciled properly. It also adds comprehensive unit tests.

Related Issue

Closes #1921

Which package(s)?

@termuijs/ui

Type of Change

  • 🐛 Bug fix (type:bug)
  • ✨ Feature (type:feature)
  • 📝 Docs (type:docs)
  • 🧪 Tests (type:testing)
  • ♻️ Refactor (type:refactor)
  • 🎨 Design / UX (type:design)
  • ♿ Accessibility (type:accessibility)
  • ⚡ Performance (type:performance)
  • 🔧 DevOps / CI (type:devops)
  • 🔒 Security (type:security)

Checklist

  • ⭐ You starred the repo. The needs-star check blocks your merge otherwise.
  • Tests pass locally: bun vitest run
  • Build passes: bun run build
  • Typecheck passes: bun run typecheck
  • You read CONTRIBUTING.md.
  • Your PR title follows type: short description.
  • Widget state mutators call markDirty() (if your change affects rendering).
  • No new any types without an inline comment explaining why.
  • No unrelated refactors bundled into this PR.

GSSoC 2026 Participation

  • You are a GSSoC 2026 contributor.
  • Your GSSoC profile: https://gssoc.girlscript.org/profile/Harshithk951

Screenshots / Recordings (UI changes)

N/A

Notes for the Reviewer

None

Summary by CodeRabbit

  • Bug Fixes

    • Improved the text area’s interactive behavior so typing, deleting, and inserting new lines update the displayed content correctly.
    • Refined cursor navigation handling for a smoother editing experience.
  • Tests

    • Added coverage for text input, backspace, line breaks, and caret movement to help prevent regressions.

@Harshithk951 Harshithk951 requested a review from Karanjot786 as a code owner July 1, 2026 15:43
@github-actions github-actions Bot added area:ui @termuijs/ui type:testing +10 pts. Tests. type:bug +10 pts. Bug fix. labels Jul 1, 2026
@coderabbitai

coderabbitai Bot commented Jul 1, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Fixes missing imports (useState, useInput, KeyEvent) in TextArea.ts and changes the root element type from 'text' to 'box'. Adds a new Vitest test suite for TextArea covering initial render, typing/backspace, and newline/navigation behaviors.

Changes

TextArea Fix and Tests

Layer / File(s) Summary
Fix missing imports and root element type
packages/ui/src/components/TextArea.ts
Adds useState, useInput imports from @termuijs/jsx and KeyEvent type from @termuijs/core; changes root createElement type from 'text' to 'box'.
Add TextArea test suite
packages/ui/src/components/TextArea.test.tsx
New Vitest tests verify initial value rendering, typing/backspace with onChange assertions, and newline insertion with caret navigation.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested labels: quality:clean, level:beginner

Suggested reviewers: Karanjot786

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise, follows the required format, and accurately summarizes the TextArea fix and tests.
Description check ✅ Passed The PR description matches the template and fills the required sections with clear package, issue, change type, and checklist details.
Linked Issues check ✅ Passed The changes satisfy #1921 by adding TextArea tests and fixing the missing imports that caused the ReferenceError.
Out of Scope Changes check ✅ Passed The additional wrapper element change is consistent with the stated TextArea fix and does not appear unrelated to the issue.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/ui/src/components/TextArea.ts (1)

112-122: 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift

Per-row element type flips between text and row for the same key as the cursor moves.

The cursor row returns a bare createElement('text', { key: r }, ...) while every other row returns createElement('row', { key: r, ... }, createElement('text', { width: ... }, ...)). Since cursor.row changes as the user navigates, the element type bound to a given key switches between text and row across re-renders — the same class of type-mismatch reconciliation issue this PR fixes at the root level (textbox). This risks remounts/misalignment of sibling rows as the cursor moves.

Additionally, the cursor row's text omits the width prop that the non-cursor branch sets (width: lineStr.length || 1), and its content embeds raw ANSI escape sequences (\x1b[7m...\x1b[27m) inline in the string. If width/layout is derived from raw string length, the non-printing escape bytes will inflate the computed width relative to the sibling branch, producing misaligned columns specifically on the cursor's line.

Suggested direction
-                if (r === cursor.row) {
-                    const before = lineStr.slice(0, cursor.col);
-                    const char = cursor.col < lineStr.length ? lineStr[cursor.col] : ' ';
-                    const after = cursor.col < lineStr.length ? lineStr.slice(cursor.col + 1) : '';
-                    
-                    return createElement('text', { key: r }, before + '\x1b[7m' + char + '\x1b[27m' + after);
-                }
-                return createElement('row', { key: r, gap: 0, width: '100%', height: 1 }, createElement('text', { width: lineStr.length || 1 }, lineStr || ' '));
+                if (r === cursor.row) {
+                    const before = lineStr.slice(0, cursor.col);
+                    const char = cursor.col < lineStr.length ? lineStr[cursor.col] : ' ';
+                    const after = cursor.col < lineStr.length ? lineStr.slice(cursor.col + 1) : '';
+                    const visibleLen = (before + char + after).length;
+                    return createElement('row', { key: r, gap: 0, width: '100%', height: 1 },
+                        createElement('text', { width: visibleLen || 1 }, before + '\x1b[7m' + char + '\x1b[27m' + after));
+                }
+                return createElement('row', { key: r, gap: 0, width: '100%', height: 1 }, createElement('text', { width: lineStr.length || 1 }, lineStr || ' '));
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/ui/src/components/TextArea.ts` around lines 112 - 122, The row
rendering in TextArea is using the same key for two different element types,
switching between createElement('text', ...) for the cursor row and
createElement('row', ...) for non-cursor rows. Update the TextArea render path
so the keyed row always uses a consistent wrapper type (for example, keep the
same row structure and vary only the inner text content/styling) in the cursor
and non-cursor branches. Also make the cursor branch match the non-cursor
branch’s width/layout props and avoid embedding raw ANSI escape sequences
directly in the rendered string so cursor movement does not trigger remounts or
width misalignment.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/ui/src/components/TextArea.test.tsx`:
- Around line 25-34: The TextArea test is asserting substrings that pass even
when no real navigation or split behavior occurs. Update the TextArea test to
move the cursor to the intended split point before pressing enter, using the
existing render/fireKey/getOutput flow in TextArea.test.tsx and the TextArea
cursor handling in TextArea.ts, then assert the rendered output reflects the
actual split at that position rather than matching characters already present in
the original value.

---

Outside diff comments:
In `@packages/ui/src/components/TextArea.ts`:
- Around line 112-122: The row rendering in TextArea is using the same key for
two different element types, switching between createElement('text', ...) for
the cursor row and createElement('row', ...) for non-cursor rows. Update the
TextArea render path so the keyed row always uses a consistent wrapper type (for
example, keep the same row structure and vary only the inner text
content/styling) in the cursor and non-cursor branches. Also make the cursor
branch match the non-cursor branch’s width/layout props and avoid embedding raw
ANSI escape sequences directly in the rendered string so cursor movement does
not trigger remounts or width misalignment.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 92ae0a19-664f-4705-9743-d4b200b29c3d

📥 Commits

Reviewing files that changed from the base of the PR and between 85e8d36 and 6c9de4e.

📒 Files selected for processing (2)
  • packages/ui/src/components/TextArea.test.tsx
  • packages/ui/src/components/TextArea.ts

Comment thread packages/ui/src/components/TextArea.test.tsx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ui @termuijs/ui type:bug +10 pts. Bug fix. type:testing +10 pts. Tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Unit Tests for TextArea in packages/ui/src/components/TextArea.ts

1 participant