Skip to content

fix: resolve 4 bugs in termui - #3565

Open
saurabhhhcodes wants to merge 1 commit into
Karanjot786:mainfrom
saurabhhhcodes:fix/termui-10137
Open

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

Conversation

@saurabhhhcodes

@saurabhhhcodes saurabhhhcodes commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Description

This PR fixes real bugs found in the codebase:

  • Added Number.EPSILON to Math.round: prevents floating-point drift (e.g. 1.005 * 100 rounding to 100 instead of 101).
  • 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.
  • Added explicit radix to parseInt: without 10, strings like '0x1F' or '08' parse in unintended bases.

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: #3564

Summary by CodeRabbit

  • Bug Fixes
    • Improved keyboard tab selection in showcase and widget gallery examples for more reliable numeric key handling.
    • Smoothed switch knob animation positioning to prevent minor rounding inaccuracies.
    • Fixed TreeSelect comparisons so selected numeric-looking values are ordered and matched consistently.

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

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The changes make tab-key parsing explicitly base 10, add Number.EPSILON before switch-position rounding, and use numeric ordering when comparing selected tree values.

Changes

Numeric handling fixes

Layer / File(s) Summary
Explicit decimal tab parsing
examples/showcase/src/index.tsx, examples/widget-gallery/src/index.ts
Both tab handlers call parseInt with radix 10.
UI numeric rounding and sorting
packages/ui/src/Switch.ts, packages/ui/src/TreeSelect.ts
Switch position rounding adds Number.EPSILON. Tree selection comparison sorts values numerically.

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

Possibly related PRs

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 sections and does not use the required Closes # issue-link format. Add the missing template sections, identify the affected packages, complete the required checklist and GSSoC fields, and replace Ref: #3564 with `Closes `#3564.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the four bug fixes 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 area:examples Example apps. area:ui @termuijs/ui labels Aug 5, 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: 1

🤖 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/TreeSelect.ts`:
- Around line 185-186: Update the sortedA and sortedB sort calls to use a typed
numeric comparator that converts each string element to a number before
subtraction. Ensure both arrays use numeric ordering so equivalent values such
as “2” and “10” compare consistently under strict TypeScript checking.
🪄 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: f6523553-2b9f-459c-a5f7-fd903e330638

📥 Commits

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

📒 Files selected for processing (4)
  • examples/showcase/src/index.tsx
  • examples/widget-gallery/src/index.ts
  • packages/ui/src/Switch.ts
  • packages/ui/src/TreeSelect.ts

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 | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n -C 4 'function _valuesEqual|sortedA|sortedB' packages/ui/src/TreeSelect.ts

while IFS= read -r file; do
  echo "== $file =="
  rg -n -C 2 '"strict"|"noImplicitAny"|"extends"|"include"' "$file" || true
done < <(fd -a 'tsconfig.*\.json$' .)

Repository: Karanjot786/TermUI

Length of output: 600


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== TreeSelect relevant section =="
sed -n '170,192p' packages/ui/src/TreeSelect.ts

echo
echo "== TypeScript config files =="
git ls-files '*tsconfig*.json' | while IFS= read -r file; do
  echo "-- $file --"
  sed -n '1,220p' "$file"
done

echo
echo "== tsconfig strict flags references =="
rg -n '"strict"|"extends"|"include"|"compilerOptions"' $(git ls-files '*tsconfig*.json') || true

echo
echo "== Read-only behavior probe for _valuesEqual with ['2','10'] and ['10','2'] =="
node - <<'JS'
function valuesEqual(a, b) {
  const sortedA = [...a].sort((a, b) => a - b);
  const sortedB = [...b].sort();
  if (a.length !== b.length) return false;
  for (let i = 0; i < sortedA.length; i++) {
    if (sortedA[i] !== sortedB[i]) return false;
  }
  return true;
}
console.log(JSON.stringify({
  inputA: ['2', '10'],
  inputB: ['10', '2'],
  equal: valuesEqual(['2', '10'], ['10', '2'])
}));
JS

Repository: Karanjot786/TermUI

Length of output: 26059


Use a typed numeric comparator for both array sorts.

packages/ui/tsconfig.json enables strict, so subtracting the string[] elements in sortedA.sort((a, b) => a - b) is a type error. sortedB.sort() also does default lexicographic sorting, so ['2', '10'] and ['10', '2'] are treated as unequal.

Proposed fix
-    const sortedA = [...a].sort((a, b) => a - b);
-    const sortedB = [...b].sort();
+    const compareNumeric = (left: string, right: string): number =>
+        Number(left) - Number(right);
+    const sortedA = [...a].sort(compareNumeric);
+    const sortedB = [...b].sort(compareNumeric);
📝 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 compareNumeric = (left: string, right: string): number =>
Number(left) - Number(right);
const sortedA = [...a].sort(compareNumeric);
const sortedB = [...b].sort(compareNumeric);
🧰 Tools
🪛 GitHub Actions: CI / 0_build-and-test.txt

[error] 185-185: TypeScript TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type. The DTS build failed while running 'tsup'.


[error] 185-185: TypeScript TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type. The DTS build failed while running 'tsup'.

🪛 GitHub Actions: CI / build-and-test

[error] 185-185: TypeScript DTS build failed: the left-hand side of an arithmetic operation must be any, number, bigint, or an enum type (TS2362), and the right-hand side must satisfy the same requirement (TS2363). Failed command: bun run build (tsup).

🤖 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 the sortedA and
sortedB sort calls to use a typed numeric comparator that converts each string
element to a number before subtraction. Ensure both arrays use numeric ordering
so equivalent values such as “2” and “10” compare consistently under strict
TypeScript checking.

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