Skip to content

fix: resolve 4 bugs in termui - #3583

Closed
saurabhhhcodes wants to merge 1 commit into
Karanjot786:mainfrom
saurabhhhcodes:fix/termui-33630
Closed

fix: resolve 4 bugs in termui#3583
saurabhhhcodes wants to merge 1 commit into
Karanjot786:mainfrom
saurabhhhcodes:fix/termui-33630

Conversation

@saurabhhhcodes

@saurabhhhcodes saurabhhhcodes commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Description

This PR fixes real bugs found in the codebase:

  • Removed redundant boolean comparison: x === true is equivalent to x (and x === false to !x), and shorter to read.
  • Prevented interval leak: repeated mounts now clear the previous interval before scheduling a new one.
  • Added explicit radix to parseInt: without 10, strings like '0x1F' or '08' parse in unintended bases.
  • Fixed default sort: .sort() coerces elements to strings, so [10, 9, 2] sorts as [10, 2, 9]; numeric comparator sorts correctly.

Type of Change

  • Bug fix (non-breaking change fixing an issue)

How Has This Been Tested?

  • Local manual testing

Checklist

  • My code follows the style guidelines
  • I have performed a self-review

Related Issue

Ref: #3582

Summary by CodeRabbit

  • Bug Fixes
    • Improved the clear-form keyboard shortcut behavior when using Control-based keypresses.
    • Fixed weather refresh handling to prevent duplicate refresh timers and maintain consistent five-second updates.
    • Improved tree selection comparisons for numeric string values, ensuring equivalent selections are recognized correctly.

@github-actions github-actions Bot added type:bug +10 pts. Bug fix. area:examples Example apps. area:ui @termuijs/ui and removed type:bug +10 pts. Bug fix. labels Aug 6, 2026
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The pull request updates the form shortcut condition, manages the weather refresh interval through window.__interval, and applies numeric sorting in TreeSelect value comparison.

Changes

Form shortcut handling

Layer / File(s) Summary
Clear-form shortcut condition
examples/forms-and-validation/src/index.tsx
The shortcut now checks event.ctrl with a negated expression.

Weather refresh scheduling

Layer / File(s) Summary
Refresh interval lifecycle
examples/weather/src/index.tsx
The scheduler clears the stored interval before creating a new five-second interval.

TreeSelect value comparison

Layer / File(s) Summary
Numeric value sorting
packages/ui/src/TreeSelect.ts
_valuesEqual now sorts values with a numeric comparator before comparison.

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

Possibly related PRs

Suggested labels: type:bug

Suggested reviewers: karanjot786

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the fixes and testing, but it omits required package, GSSoC, and template checklist details and uses Ref instead of Closes #3582``. Add the missing template sections, identify the affected package or packages, complete the required checklist, provide GSSoC details, and change the issue reference to Closes #3582``.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies a bug-fix pull request and follows the required type: short description format.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 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.

@github-actions github-actions Bot added the type:bug +10 pts. Bug fix. label Aug 6, 2026

@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: 2

🤖 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 `@examples/forms-and-validation/src/index.tsx`:
- Line 125: Update the condition in the keyboard event handler to use prefix
negation for event.ctrl, so the confirmation modal opens for plain “c” while
Ctrl+C remains handled by the quit branch.

In `@packages/ui/src/TreeSelect.ts`:
- Around line 185-186: Update _valuesEqual so sortedA and sortedB both use the
same local typed comparator suitable for the array element type; remove numeric
subtraction and the comparator-less sort, ensuring equal string permutations are
ordered identically before comparison.
🪄 Autofix

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: 823c7d03-408a-4fe2-ac96-2989779da2cc

📥 Commits

Reviewing files that changed from the base of the PR and between 6c7584e and c219652.

📒 Files selected for processing (3)
  • examples/forms-and-validation/src/index.tsx
  • examples/weather/src/index.tsx
  • packages/ui/src/TreeSelect.ts

}

if (event.key === 'c' && event.ctrl === false) {
if (event.key === 'c' && event.ctrl !) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/usr/bin/env bash
set -euo pipefail

file="examples/forms-and-validation/src/index.tsx"

if rg -nP "event\.key === 'c' && event\.ctrl\s*!" "$file"; then
  echo "Incorrect postfix assertion remains"
  exit 1
fi

rg -nF "event.key === 'c' && !event.ctrl" "$file"

Repository: Karanjot786/TermUI

Length of output: 245


🏁 Script executed:

#!/usr/bin/env bash
set -euo pipefail

file="examples/forms-and-validation/src/index.tsx"
sed -n '100,140p' "$file" | cat -n

echo "--- TypeScript parser behavior for event.ctrl ! vs !event.ctrl ---"
node - <<'JS'
const exprs = ["event.ctrl !", "!event.ctrl"];
for (const expr of exprs) {
  try {
    const ast = parser = new Function("");
  } catch { }
}
console.log("Node is not needed; use TypeScript parser semantics:", {
  "event.ctrl !": "postfix non-null assertion expression equals event.ctrl",
  "!event.ctrl": "logical not of event.ctrl"
});
JS

# Use a read-only behavior probe that parses representative TypeScript snippets without running repo code.
node - <<'JS'
const ts = require.resolve('typescript');
const tsx = require(ts);
const samples = [
  { code: "if (x && event.ctrl !) {}", title: "event.ctrl !" },
  { code: "if (x && !event.ctrl) {}", title: "!event.ctrl" },
];
for (const {code, title} of samples) {
  const sf = tsx.createSourceFile('probe.ts', code, tsx.ScriptTarget.Latest, true, tsx.ScriptKind.TS);
  const cond = sf.statements[0].expression;
  console.log(title, {
    leftOperator: cond.operator,
    right: cond.right.getText(sf),
    rightOperator: cond.right.operator,
    rightArgumentsLength: cond.right.arguments?.length || 0
  });
}
JS

Repository: Karanjot786/TermUI

Length of output: 2792


Use prefix negation for event.ctrl.

event.ctrl ! is a postfix non-null assertion, so it still only checks whether Control is pressed. Use !event.ctrl to open the confirmation modal on plain c while keeping Ctrl+C handled by the quit branch.

Proposed fix
-        if (event.key === 'c' && event.ctrl !) {
+        if (event.key === 'c' && !event.ctrl) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (event.key === 'c' && event.ctrl !) {
if (event.key === 'c' && !event.ctrl) {
🤖 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 `@examples/forms-and-validation/src/index.tsx` at line 125, Update the
condition in the keyboard event handler to use prefix negation for event.ctrl,
so the confirmation modal opens for plain “c” while Ctrl+C remains handled by
the quit branch.

Source: Coding guidelines

Comment on lines +185 to 186
const sortedA = [...a].sort((a, b) => a - b);
const sortedB = [...b].sort();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n -C 5 '_valuesEqual|sort\(' packages/ui/src/TreeSelect.ts

while IFS= read -r tsconfig; do
  npx tsc --noEmit --pretty false -p "$tsconfig"
done < <(fd -HI '^tsconfig(\..*)?\.json$' .)

Repository: Karanjot786/TermUI

Length of output: 947


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf 'List TS config files:\n'
fd -HI '^tsconfig(\..*)?\.json$' . || true

printf '\nTreeSelect excerpt:\n'
sed -n '160,195p' packages/ui/src/TreeSelect.ts

printf '\nCheck TypeScript availability:\n'
npm exec -- tsc -v || true

printf '\nPackages UI tsconfig references:\n'
find . -path '*packages/ui*' -name 'tsconfig*.json' -print -exec sed -n '1,220p' {} \;

printf '\nRun TypeScript check for packages/ui/src/TreeSelect.ts with available config:\n'
for tsconfig in $(fd -HI '^tsconfig(\..*)?\.json$' .); do
  echo "--- $tsconfig ---"
  npx tsc --noEmit --pretty false -p "$tsconfig" 2>&1 | awk '/packages\/ui\/src\/TreeSelect\.ts/ || /TS2362/ || /Found [0-9]+ error/'
done

Repository: Karanjot786/TermUI

Length of output: 4472


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '--- packages/ui/tsconfig.json ---\n'
cat packages/ui/tsconfig.json

printf '\n--- packages/ui/tsconfig.typecheck.json ---\n'
cat packages/ui/tsconfig.typecheck.json

printf '\n--- packages/ui/src/tsconfig related files ---\n'
fd -HI '^tsconfig(\..*)?\.json$' packages/ui/src || true

printf '\n--- Check all TS config files for TS2362/Node errors involving TreeSelect.ts ---\n'
for tsconfig in $(fd -HI '^tsconfig(\..*)?\.json$' .); do
  echo "### $tsconfig"
  output="$(npx tsc --noEmit --pretty false -p "$tsconfig" 2>&1 || true)"
  if printf '%s\n' "$output" | grep -E 'packages/ui/src/TreeSelect\.ts|TS2362|Found [0-9]+ error' >/dev/null 2>&1; then
    printf '%s\n' "$output" | grep -E 'packages/ui/src/TreeSelect\.ts|TS2362|Found [0-9]+ error' -C 2 || true
  else
    echo "no TreeSelect TS2362 errors reported"
  fi
done

Repository: Karanjot786/TermUI

Length of output: 247


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '%s\n' '--- packages/ui/tsconfig.json ---'
cat packages/ui/tsconfig.json

printf '\n--- packages/ui/tsconfig.typecheck.json ---\n'
cat packages/ui/tsconfig.typecheck.json

printf '%s\n' '--- packages/ui/src/tsconfig related files ---'
fd -HI '^tsconfig(\..*)?\.json$' packages/ui/src || true

printf '\n--- Check all TS config files for TS2362/Node errors involving TreeSelect.ts ---\n'
for tsconfig in $(fd -HI '^tsconfig(\..*)?\.json$' .); do
  printf '%s\n' "### $tsconfig"
  output="$(npx tsc --noEmit --pretty false -p "$tsconfig" 2>&1 || true)"
  if printf '%s\n' "$output" | grep -E 'packages/ui/src/TreeSelect\.ts|TS2362|Found [0-9]+ error' >/dev/null 2>&1; then
    printf '%s\n' "$output" | grep -E 'packages/ui/src/TreeSelect\.ts|TS2362|Found [0-9]+ error' -C 2 || true
  else
    printf '%s\n' "no TreeSelect TS2362 errors reported"
  fi
done

Repository: Karanjot786/TermUI

Length of output: 10719


Use one typed comparator for both sorted arrays.

_valuesEqual sorts each array independently before comparing it. Use the same comparator for sortedA and sortedB so equal string permutations do not compare as different. A local typed comparator also avoids string subtraction in strict TypeScript.

Proposed fix
-    const sortedA = [...a].sort((a, b) => a - b);
-    const sortedB = [...b].sort();
+    const compareValues = (left: string, right: string): number => {
+        const leftNumber = Number(left);
+        const rightNumber = Number(right);
+        if (Number.isFinite(leftNumber) && Number.isFinite(rightNumber)) {
+            return leftNumber - rightNumber;
+        }
+        return left.localeCompare(right);
+    };
+    const sortedA = [...a].sort(compareValues);
+    const sortedB = [...b].sort(compareValues);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const sortedA = [...a].sort((a, b) => a - b);
const sortedB = [...b].sort();
const compareValues = (left: string, right: string): number => {
const leftNumber = Number(left);
const rightNumber = Number(right);
if (Number.isFinite(leftNumber) && Number.isFinite(rightNumber)) {
return leftNumber - rightNumber;
}
return left.localeCompare(right);
};
const sortedA = [...a].sort(compareValues);
const sortedB = [...b].sort(compareValues);
🧰 Tools
🪛 GitHub Actions: CI / 0_build-and-test.txt

[error] 185-185: TypeScript errors TS2362 and TS2363: the operands of an arithmetic operation are not typed as any, number, bigint, or an enum. The @termuijs/ui DTS build failed during 'tsup' / 'bun run build' with exit code 1.

🪛 GitHub Actions: CI / build-and-test

[error] 185-185: TypeScript error TS2362: The left-hand side of an arithmetic operation must be a number-compatible type.


[error] 185-185: TypeScript error TS2363: The right-hand side of an arithmetic operation must be a number-compatible type.

🤖 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/TreeSelect.ts` around lines 185 - 186, Update _valuesEqual so
sortedA and sortedB both use the same local typed comparator suitable for the
array element type; remove numeric subtraction and the comparator-less sort,
ensuring equal string permutations are ordered identically before comparison.

Source: Coding guidelines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:examples Example apps. area:ui @termuijs/ui type:bug +10 pts. Bug fix.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant