Skip to content

fix(company-input-v2): keep Use row on refocus, purge stale free-text (v4.x) - #291

Merged
smarcet merged 10 commits into
v4.xfrom
fix/company-input-v2-use-row-persistence-v4
Jul 15, 2026
Merged

fix(company-input-v2): keep Use row on refocus, purge stale free-text (v4.x)#291
smarcet merged 10 commits into
v4.xfrom
fix/company-input-v2-use-row-persistence-v4

Conversation

@gcutrini

Copy link
Copy Markdown
Contributor

ref: https://app.clickup.com/t/86b9rnr4r

Follow-up to #290 addressing two dropdown regressions found during local verification.

Bugs fixed

1. Use "<typed>" row disappeared after committing free-text and refocusing — the alreadyListed check treated the just-committed {id: 0, name} entry as a real match and suppressed the Use row. Also affected passive refocus (click back with no typing) because MUI resets params.inputValue to empty when the display value matches the selection.

2. Stale free-text option flashed briefly on refocus after a new commit — the previously-committed {id: 0} option lingered in the options list until the debounced API response replaced it. Refocus in that window showed the stale entry.

Changes

Fixes (2 commits):

  • filterOptions now only counts real (id > 0) companies as "already listed" for the Use row check, and falls back to the committed free-text's name when params.inputValue is empty.
  • Effect purges any leftover {id: 0} options synchronously the moment normalizedValue changes, before firing the API request.

Refactors on top (8 commits, no behavior change):

  • Rename findExistingByNamefindExistingCompany (mirrors isExistingCompany).
  • Extract namesMatch — dedupes the X.trim().toLowerCase() comparison across three sites.
  • Extract shouldOfferUseRow — unit-testable predicate for the Use row.
  • Extract resolveTypedCompany — dedupes onBlur/onChange typed-string resolution.
  • Simplify onBlur currentName via getOptionName.
  • Extract getUseRowText — formalises the active-typing + committed-free-text fallback.
  • Extract resolveCommittedCompany, use fireChange in onChange (removes prop/handler name shadow, dedupes with the rest of the component).
  • Extract findCanonicalUpgrade, simplify effect's options builder.

Naming convention

The extracted helpers follow a consistent verb-prefix convention already in the file: is* predicates, find* returns match or null, resolve* transforms input to a Company (always returns), get* computed getter, should* boolean decision.

Tests

34/34 green. Each fix commit ships with an integration test that fails without the fix (verified by reverting each in isolation). The shouldOfferUseRow extraction adds four unit tests including the id:0-doesnt-suppress-Use case that pins the regression.

gcutrini added 10 commits July 15, 2026 12:09
…exists

The Use "<typed>" row disappeared after the user committed a free-text
entry and re-focused the field, because filterOptions treated its own
prior {id:0,name} option as "already listed".

Two fixes to filterOptions:
- alreadyListed only counts real (id > 0) companies, not free-text.
- When params.inputValue is empty (passive refocus with no typing) and
  the current value is a committed free-text, fall back to its name so
  the Use row still appears.

Adds a regression test for the retype-same-text case.
After the user commits a free-text value via blur and starts a new
query, the previously-committed {id: 0} option lingered in the options
list until the debounced API response replaced them. In the meantime a
refocus would briefly show the stale entry ("ti" flashing while the
user was typing "tip").

The effect now purges any leftover free-text options synchronously the
moment normalizedValue or inputValue changes, before firing the request.

Adds a regression test that catches the stale entry mid-request.
…Company

Mirrors the isExistingCompany predicate — pair reads more naturally
as "predicate + finder for the same shape". Pure rename.
The X.trim().toLowerCase() comparison for company names appeared in
three places (findExistingCompany, filterOptions, onBlur). Extracting
a single namesMatch helper removes the duplication and gives the
intent a name.
Moves the Use-row eligibility predicate out of the filterOptions
callback for unit-testability. filterOptions now composes cleanly with
the (later) getUseRowText helper.

Adds four unit tests, including the id:0-doesnt-suppress-Use case that
pins the recently-fixed regression.
Dedupes the findExistingCompany(opts, X) || { id: 0, name: X } pattern
that appeared in both onBlur and onChange. Both sites are answering the
same question — turn a typed string into either the canonical existing
company or a fresh free-text entry — and should agree on the answer.
…Name

The ternary derivation exactly duplicates what getOptionName already
does (handles string, company object, other → "").
Formalises the two-source resolution (active typing + committed free-text
fallback) that filterOptions already needed. Reduces filterOptions to
its two-line essence.
…ireChange in onChange

The onChange handler's three-branch normalization (string, synthetic
Use row, pass-through) is now a single helper call. The handler also
uses the existing fireChange helper instead of hand-building the
onChange envelope; matches the rest of the component and removes the
onChange prop/handler name shadow.
… effect

The effect callback now reads as three coherent steps: reset options,
try to upgrade a stale free-text to its canonical version, done. The
"is this a free-text? then look up the canonical" logic is now a named
helper (findCanonicalUpgrade) instead of an inline conditional.

Also collapses the six-line newOptions builder into one composed array.
@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3baec08e-c1ca-4631-8718-d55bfcf8ae4a

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/company-input-v2-use-row-persistence-v4

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.

@gcutrini
gcutrini requested a review from smarcet July 15, 2026 15:24

@smarcet smarcet left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@smarcet
smarcet merged commit cf90947 into v4.x Jul 15, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants