Skip to content

Fix: password visibility toggle doesn't work due to disconnected reff - #3416

Open
Alexx3890 wants to merge 2 commits into
Karanjot786:mainfrom
Alexx3890:patch-1
Open

Fix: password visibility toggle doesn't work due to disconnected reff#3416
Alexx3890 wants to merge 2 commits into
Karanjot786:mainfrom
Alexx3890:patch-1

Conversation

@Alexx3890

@Alexx3890 Alexx3890 commented Aug 3, 2026

Copy link
Copy Markdown

Description

Fixes the password visibility toggle in the examples/auth-flow login example by connecting the parent passwordRef to the PasswordInput instance and keeping the visibility label synchronized with the widget state. It also removes duplicated toggle and submit logic by introducing shared helper functions.

Related Issue

Closes #3415

Which package(s)?

examples/auth-flow

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](https://chatgpt.com/c/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.

Note: Only check the test/build/typecheck boxes if you've actually run those commands successfully.

GSSoC 2026 Participation

  • No

Screenshots / Recordings (UI changes)

N/A

Notes for the Reviewer

This PR fixes the password visibility toggle in the examples/auth-flow example by ensuring the parent component and PasswordInputJSX share the same PasswordInput instance. It also synchronizes the visibility label with the widget state and consolidates duplicated toggle and submit logic for easier maintenance.

Summary by CodeRabbit

  • Bug Fixes

    • Improved password field behavior and focus handling during authentication.
    • Ensured Enter/Return consistently submits login forms.
    • Improved password visibility toggling across supported interactions.
    • Preserved reliable logout behavior on protected screens.
    • Improved consistency when entering, submitting, and managing login credentials.
  • Refactor

    • Simplified and standardized authentication screen behavior for a more consistent experience.


## Summary

Fixes the password visibility toggle in the `auth-flow` example. Previously, pressing `v` or `Enter` while the **Toggle** control was focused had no effect, and the `[Show]/[Hide]` label never updated.

## Root cause

`LoginScreen` created a `passwordRef` with `useRef`, but never passed it to `PasswordInputJSX`. Instead, `PasswordInputJSX` created its own internal ref and used it to instantiate the `PasswordInput` widget.

As a result, `LoginScreen`'s `passwordRef.current` always remained `null`, so calls to `toggleVisibility()` were no-ops.

Additionally, the visibility label was derived from `passwordRef.current?.showText` during render. Since mutating a ref does not trigger a re-render, the label could become out of sync with the widget's visibility state.

## Fix

* Pass the parent-owned `passwordRef` to `PasswordInputJSX` via a `widgetRef` prop instead of creating a separate internal ref.
* Track password visibility using component state (`isVisible`) and synchronize it with the widget after calling `toggleVisibility()`.
* Extract the password toggle logic into a shared `togglePasswordVisibility()` helper.
* Reuse a single `submit()` handler for both the `enter` and `return` key bindings to avoid duplicated logic.
Copilot AI review requested due to automatic review settings August 3, 2026 16:36
@Alexx3890
Alexx3890 requested a review from Karanjot786 as a code owner August 3, 2026 16:36
@github-actions github-actions Bot added area:examples Example apps. type:bug +10 pts. Bug fix. labels Aug 3, 2026
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: e7e3991d-a37a-4e93-a56f-f51ecc792cee

📥 Commits

Reviewing files that changed from the base of the PR and between 6d97edc and bb6c948.

📒 Files selected for processing (1)
  • examples/auth-flow/src/index.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • examples/auth-flow/src/index.tsx

📝 Walkthrough

Walkthrough

The auth flow connects LoginScreen to the password widget through a shared reference. It centralizes password visibility toggling and submit handling, reuses them for keyboard actions, and adds explicit input guards.

Changes

Authentication flow

Layer / File(s) Summary
Input handling updates
examples/auth-flow/src/index.tsx
Unused imports are removed. Component declarations and text-input key handling gain explicit guards and return types.
Password visibility and login actions
examples/auth-flow/src/index.tsx
PasswordInputJSX receives widgetRef and forwards input events. LoginScreen tracks visibility and reuses toggle and submit handlers. Login rendering passes passwordRef to the password widget.
Protected-screen wiring
examples/auth-flow/src/index.tsx
Protected-screen keymap, authentication selection, and render wiring are reformatted. Logout and error-boundary behavior remain unchanged.

Estimated code review effort: 3 (Moderate) | ~15–30 minutes

Possibly related PRs

  • Karanjot786/TermUI#2040: Both changes update password visibility toggling and related login keyboard or submit behavior.

Suggested reviewers: karanjot786

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the password visibility toggle bug, despite a minor typo in “reff”.
Description check ✅ Passed The description includes the required sections, linked issue, package, change type, checklist, and reviewer notes.
Linked Issues check ✅ Passed The changes address issue [#3415] by connecting the parent ref to the PasswordInput instance and synchronizing visibility state.
Out of Scope Changes check ✅ Passed The changes remain within the authentication example and support the linked fix through shared toggle, submit, and visibility logic.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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.

@github-actions github-actions 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.

🎉 Thanks for your first PR to TermUI, @ankit3890.

Before your PR merges:

  1. Star the repo. Required. The star-check job blocks your merge otherwise.
  2. ✅ All checks green: build, test, typecheck.
  3. 🏷 PR title follows type: short description. Example: fix: handle empty list.
  4. 🔗 Link your closing issue in the description.

GSSoC 2026 points come from labels after merge:

  • gssoc:approved. +50 base points.
  • level:beginner / intermediate / advanced / critical. +20 / +35 / +55 / +80.
  • quality:clean / exceptional. x 1.2 / x 1.5.
  • type:*. Stackable bonus.

Your reviewer responds within 48 hours. Ping @Karanjot786 on Discord for urgent help.

🚀 Welcome to the cohort.

@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 `@examples/auth-flow/src/index.tsx`:
- Line 100: Update PasswordInputJSX to return the existing PasswordInput type
instead of casting widgetRef.current to any. Preserve the current ref behavior,
and add an inline comment only if the TermUI JSX type system requires a
remaining workaround.
🪄 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: ff2d6b36-733a-4d6f-890d-f3cfe18d79cc

📥 Commits

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

📒 Files selected for processing (1)
  • examples/auth-flow/src/index.tsx

Comment thread examples/auth-flow/src/index.tsx Outdated

Copilot AI 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.

Pull request overview

This PR fixes the non-functional password visibility toggle in the examples/auth-flow login example by wiring the parent passwordRef into the PasswordInput instance and consolidating duplicated toggle/submit key handling.

Changes:

  • Connects LoginScreen’s passwordRef to PasswordInputJSX via a widgetRef prop so parent actions reach the actual widget.
  • Adds local isVisible state to re-render and keep the toggle label updated when visibility is toggled.
  • Refactors Enter/Return handling into a shared submit() helper and centralizes visibility toggling logic.
Suppressed comments (2)

examples/auth-flow/src/index.tsx:197

  • To keep the "[Show]/[Hide]" label synchronized with the PasswordInput widget (including Alt+V toggles while the password field is focused), pass the visibility-change callback from LoginScreen into PasswordInputJSX.
                    value={password}
                    onChange={setPassword}
                    placeholder="Enter password..."
                    isFocused={focusedIndex === 1}
                    widgetRef={passwordRef}

examples/auth-flow/src/index.tsx:98

  • When PasswordInputJSX forwards key events to PasswordInput.handleKey(), Alt+V can toggle visibility inside the widget without updating the LoginScreen label. Invoke the visibility callback after handling Alt+V so the UI stays in sync.
    useInput((_key: string, event: KeyEvent) => {
        if (!isFocused || !widgetRef.current) return;
        widgetRef.current.handleKey(event);
    });

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread examples/auth-flow/src/index.tsx Outdated
Comment on lines +69 to +81
function PasswordInputJSX({
value,
onChange,
placeholder,
isFocused,
widgetRef,
}: {
value: string;
onChange: (val: string) => void;
placeholder?: string;
isFocused: boolean;
widgetRef: { current: PasswordInput | null };
}) {

@Alexx3890 Alexx3890 left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Addressed the CodeRabbit suggestion — removed as any from both
TextInputJSX and PasswordInputJSX, returning the concrete
TextInput / PasswordInput types instead, since ref.current /
widgetRef.current are already narrowed to non-null at that point.
Added a short inline comment explaining why these return a widget
instance rather than a standard JSX.Element (TermUI's JSX runtime
accepts widget instances directly as render output).

Also restored the key && guard in TextInputJSX's default case
that had been dropped in an earlier revision — without it, a
falsy/empty key for a non-printable keystroke could throw on
key.length.

One open item: I haven't run pnpm typecheck against this yet to
confirm TermUI's JSX pragma doesn't require these to satisfy
JSX.Element at the call site. If it does, we'll need a narrower
cast (e.g. as unknown as JSX.Element) instead of a bare return —
will confirm before merge.

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

Labels

area:examples Example apps. type:bug +10 pts. Bug fix.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug : Password visibility toggle is non-functional due to disconnected passwordRef

2 participants