Skip to content

fix: do not trigger onSelect for already selected values on token split - #1265

Closed
mokshupadhyay wants to merge 2 commits into
react-component:masterfrom
mokshupadhyay:fix/token-split-onselect
Closed

mokshupadhyay wants to merge 2 commits into
react-component:masterfrom
mokshupadhyay:fix/token-split-onselect

Conversation

@mokshupadhyay

@mokshupadhyay mokshupadhyay commented Nov 28, 2026 •

Copy link
Copy Markdown

With mode="tags" and tokenSeparators, typing or pasting a list calls onSelect for values that are not part of the input at all:

<Select mode="tags" defaultValue={['a', 'b']} tokenSeparators={[',']} onSelect={fn} />
// type `2,3,`

onSelect gets "a", "b", "2" and "3", although a and b were not typed and were not selected again (onChange reports the right list, ['a', 'b', '2', '3']).

onInternalSearchSplit ran triggerSelect over the whole merged list ([...rawValues, ...patchValues]). It now runs it once for each unique token of the current input only (patchValues). A token that is typed again, even if it is already selected (for example 1 in 1,2,), still triggers onSelect, in line with how Enter and clicking the same option behave. (This is the narrower behaviour suggested in review; my first version also silenced already selected tokens that were typed.)

Tests (tests/Tags.test.tsx):

  • "should not trigger onSelect for unrelated selected values when separating tokens": fails on master (it receives the extra "a" and "b").
  • "should trigger onSelect once for each token of the input, even an already selected one": passes on both master and this branch (it pins the behaviour that is preserved).

The full suite (468 tests) and tsc pass; eslint has no errors and prettier --check is clean.

An AI coding assistant (Claude Code) helped me find and fix this; I reviewed the change and the test.

Summary by CodeRabbit

  • 问题修复
    • 标签模式下使用分隔符输入多个值时,已选值会保留在变更结果中;选择回调仅针对本次输入的 token 触发,重复 token 会去重处理。

@vercel

vercel Bot commented Nov 28, 2026

Copy link
Copy Markdown

@mokshupadhyay is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

分隔输入生成的值仍会合并并触发变更回调。选择回调现在只针对本次输入中去重后的 token 触发。新增测试验证选择回调和变更回调的值。

Changes

分隔输入的选择回调

层 / 文件 摘要
筛选分隔值并验证回调
src/Select.tsx, tests/Tags.test.tsx
onInternalSearchSplit 对本次输入中去重后的 token 触发 onSelect。测试验证已有值与新值合并进入 onChange,以及重复 token 只触发一次选择回调。

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

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: zombiej, afc163

Merge Risk: Minimal · up to ddb60

The change stops onSelect from firing again for unrelated values that were already selected when tokens are split in tags mode. Tests cover the new behavior. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题 “fix: do not trigger onSelect for already selected values on token split” 与变更内容一致。变更将 onInternalSearchSplit 中的 onSelect 调用限制为本次输入中的 token,已选值不再触发 onSelect。标题描述了主要修复点,并使用了 fix 前缀。
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

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

兔子听见逗号响,
新 token 排队进篮筐。
重复脚印合成一行,
旧值新值并肩入账。
变更回调报声清亮,
选择回调只点本次的名。

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7dbfcfb9-0701-4384-866f-d60fce76f332
Commits

Reviewing files that changed from the base of the PR and between 6d72419 and ddb6018.

Files selected for processing: 2

  • src/Select.tsx
  • tests/Tags.test.tsx

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@mokshupadhyay

Copy link
Copy Markdown
Author

Hi @zombieJ, the CI workflows on this PR are waiting for approval to run (first contribution to this repo). Could you approve them when you have a moment, and take a look at the change? Happy to adjust anything. Thanks!

@codecov

codecov Bot commented Dec 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.46%. Comparing base (f21e0bd) to head (6d72419).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1265   +/-   ##
=======================================
  Coverage   99.46%   99.46%           
=======================================
  Files          31       31           
  Lines        1304     1305    +1     
  Branches      454      477   +23     
=======================================
+ Hits         1297     1298    +1     
  Misses          7        7           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yoyo837

yoyo837 commented Dec 2, 2026

Copy link
Copy Markdown
Member

onSelect can fire even when the value is unchanged. The single-select regression test expects it on every selection of the same option. In tags mode, submitting an existing tag with Enter still fires it, while this PR suppresses it when the same tag is submitted with a separator.

Could we limit the fix to emitting once per valid token from the current input? That would avoid re-emitting unrelated selected values and preserve the callback for explicitly re-entered tags.

… values

Re-selecting an already selected tag by typing it is still reported, while unrelated selected values are no longer reported again.
@mokshupadhyay

Copy link
Copy Markdown
Author

Thanks, that makes sense. I changed it as you suggested in ddb6018: onSelect now fires once for each unique token of the current input (patchValues), so a tag that is typed again, even if already selected, still triggers it. What no longer happens is re-emitting the other values that were already selected but were not part of the input (on master, typing 2,3, with defaultValue={["a", "b"]} calls onSelect for a and b too).

The tests now cover both sides: the unrelated-values test fails on master, and the "typed again still fires" test passes on both. Full suite: 468 passing. I also rewrote the PR description to match.

@yoyo837

yoyo837 commented Dec 2, 2026

Copy link
Copy Markdown
Member

Thanks for the update. I need to correct my earlier suggestion: each tokenized selection should fire onSelect once per distinct value in the full resulting selection, including previously selected values, even if the selection is unchanged.

For example, with ['a', 'b'] already selected, entering 2,2,3, should fire four separate calls for a, b, 2, and 3. Iterating over newRawValues preserves that behavior and deduplicates within each operation.

@mokshupadhyay

Copy link
Copy Markdown
Author

Thanks for taking the time to think it through, and for the correction. With that rule the original behaviour is already right: on master, newRawValues is the de-duplicated union of the current selection and the typed tokens, and onSelect fires once per value in it (so ["a", "b"] plus 2,2,3, gives a, b, 2, 3). There is nothing left to change, so I am closing this PR. Sorry for the noise.

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