Skip to content

fix: depend on rawValues directly in OptionList selection callbacks - #1266

Open
mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/option-list-stale-rawvalues
Open

mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/option-list-stale-rawvalues

Conversation

@mokshupadhyay

@mokshupadhyay mokshupadhyay commented Dec 2, 2026 •

Copy link
Copy Markdown

OptionList memoizes isSelected and isAriaSelected (they decide whether an option is shown as selected) with the dependency [...rawValues].toString(). Two different selections can have the same string:

  • the number 1 and the string '1' (both "1"), and
  • ['a,b', 'c'] and ['a', 'b,c'] (both "a,b,c").

When the selection changes between such values, the dependency does not change, so the callback keeps the old rawValues and the list shows the previous selection.

Result of the new tests, from the original source (the option list opened with virtual={false}):

change before after
value={1} to value="1" the "Number 1" option keeps rc-select-item-option-selected (stale) only "String 1" is selected
value={['a,b', 'c']} to value={['a', 'b,c']} the old options keep the selected class (stale) the new options are selected

The fix lists rawValues itself in the dependencies of isSelected and isAriaSelected. rawValues is already memoized in Select (a new Set is created only when the selected values change) and is already a dependency of the select context, so the callbacks are not recreated more often than their consumers.

Tests: added two tests in tests/OptionList.test.tsx; both fail on the original source and pass now. npm test passes (24 suites, 468 tests), npm run tsc is clean, and npm run lint reports 0 errors (the changed files go from 7 warnings on the base to 3).

Summary by CodeRabbit

  • 修复
    • 修复单选值在数字与字符串类型之间切换时,选中状态未正确更新的问题。
    • 修复多选值包含逗号且组合发生变化时,选中状态及无障碍选中状态未正确反映实际选项的问题。

@vercel

vercel Bot commented Dec 2, 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 Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

OptionList 的选中状态计算改为依赖 rawValues 集合引用。新增单选和多选回归测试,验证值变化后选中样式与 aria-selected 的更新。

Changes

选中状态更新

Layer / File(s) Summary
选中状态依赖与回归验证
src/OptionList.tsx, tests/OptionList.test.tsx
isSelected 和 isAriaSelected 改为依赖 rawValues 集合引用。新增测试,覆盖数字与字符串的单选切换,以及逗号拼接相同的多选值切换。

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

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: aojunhao123, nathanlao

Merge Risk: Minimal · up to 327d3

This change fixes stale selection display when distinct selections collapse to the same string. It adds regression tests and carries minimal merge risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed 标题“fix: depend on rawValues directly in OptionList selection callbacks”与变更内容一致。变更将 OptionList 中两个选择回调(isSelected 和 isAriaSelected)的 memo 依赖从 [...rawValues].toString() 改为 rawValues 本身,以修复字符串化依赖导致的选中状态过…
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.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

标题“fix: depend on rawValues directly in OptionList selection callbacks”与变更内容一致。变更将 OptionList 中两个选择回调(isSelected 和 isAriaSelected)的 memo 依赖从 [...rawValues].toString() 改为 rawValues 本身,以修复字符串化依赖导致的选中状态过期问题(例如 1 与 '1',或 ['a,b','c'] 与 ['a','b,c'])。标题简洁,指出了主要改动。

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

兔子捧着小清单,
数字字符串分得开。
逗号虽巧不能混,
选中状态更新来。
无障碍标记也跟上,
月光下安心跳开。

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: c76a9782-2664-47c4-8172-bfda9b4dab40
Commits

Reviewing files that changed from the base of the PR and between f21e0bd and 327d3d7.

Files selected for processing: 2

  • src/OptionList.tsx
  • tests/OptionList.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 @yoyo837, a friendly ping: this is ready for review whenever you have time. Happy to adjust anything. Thanks!

@yoyo837

yoyo837 commented Dec 3, 2026

Copy link
Copy Markdown
Member

CI failed.

@mokshupadhyay

Copy link
Copy Markdown
Author

Thanks for checking. The failing job is not caused by this change: it fails in 3 suites (Tags, Combobox, Select) with "Outdated guide link: The snapshot guide link at the top of this snapshot is outdated" (expected https://jestjs.io/docs/snapshot-testing, received https://goo.gl/fbAQLP). The CI install resolves a newer Jest that rejects the old header in the existing snapshot files, and this PR does not touch any snapshot. On my side, with the repo's installed Jest 29.7, all 24 suites (468 tests) pass on this branch. I think the existing snapshots need their header updated (e.g. jest -u / rc-test -u) in a separate change; I can open that as its own PR if you want.

This branch has not been deployed

No deployments
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