Skip to content

fix(select): derive the check-all direction from the current value - #4484

Open
lnuxe wants to merge 2 commits into
Tencent:developfrom
lnuxe:fix/select-check-all-direction
Open

lnuxe wants to merge 2 commits into
Tencent:developfrom
lnuxe:fix/select-check-all-direction

Conversation

@lnuxe

@lnuxe lnuxe commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

🤔 这个 PR 的性质是?

  • 日常 bug 修复

🔗 相关 Issue

无(本 PR 直接修复 Select 全选方向与受控 value 不同步)

💡 需求背景和解决方案

onCheckAllChange 用 checkAll 决定是「全选」还是「取消全选」,而 checkAll 来自 Option 组件内部的 allSelectableChecked state:

if (checkAll) {
  // 全选
} else {
  // 取消全选
}

这个内部 state 只在点击时翻转,从不与 value prop 同步。所以一旦父组件重置或裁剪了选中值,两边就对不上:再点「全选」会走取消分支,回传空数组。

改动

以当前 value 为准计算方向——只要还有未选中的可用选项,就是「全选」:

+        // 以当前 value 为准计算全选方向,避免 Option 内部状态与外部 value 不同步时反向操作
+        const isOptionSelected = (opt: SelectOption) =>
+          isObjectType
+            ? currentValues.some((v) => get(v, valueKey) === opt[valueKey])
+            : currentValues.includes(opt[valueKey]);
+        const allEnabledSelected = enabledOptions.length > 0 && enabledOptions.every(isOptionSelected);
+        const shouldCheckAll = enabledOptions.length > 0 ? !allEnabledSelected : checkAll;
+
-        if (checkAll) {
+        if (shouldCheckAll) {
...
-          trigger: checkAll ? 'check' : 'uncheck',
+          trigger: shouldCheckAll ? 'check' : 'uncheck',

🔍 自测

修复前(还原 Select.tsx 后跑新测试):

× Select > scenarios > check all > re-selects every option after one option is unchecked
  → expected [] to deeply equal [ 'apple', 'orange', 'banana' ]
Tests  1 failed (1)

修复后:

✓ Select > scenarios > check all > re-selects every option after one option is unchecked
Tests  1 passed (1)

用例覆盖完整交互链:全选 → 取消一项 → 再点全选,第三次点击必须重新全选而不是清空。

回归:

npx vitest run packages/components/select
Test Files  3 passed (3)
     Tests  47 passed (47)

✅ 贡献者检查

  • 我的代码遵循这个项目的代码风格
  • 我已经自己检查了自己的代码
  • 我已经添加了必要的测试
  • 我已经在本地通过了所有测试

📝 更新日志

  • 本条 PR 不需要纳入 Changelog

tdesign-react

  • fix(select): derive the check-all direction from the current value

☑️ 请求合并前的自查清单

⚠️ 请自检并全部勾选全部选项。⚠️

  • 标题遵循 Conventional Commits 格式
  • 相关 Issue 已关联
  • 无需更新组件文档(无 API 变化)
  • 已通过仓库相关单元测试

@tdesign-bot

tdesign-bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

TDesign Component Site Preview Open

Component Preview
tdesign-react 完成
@tdesign-react/chat 完成

@pkg-pr-new

pkg-pr-new Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
  • tdesign-react-demo

    npm i https://pkg.pr.new/tdesign-react@4484
    
    npm i https://pkg.pr.new/@tdesign-react/chat@4484
    

commit: 2497b4b

@lnuxe
lnuxe force-pushed the fix/select-check-all-direction branch from 5bf7cb8 to fc97866 Compare November 17, 2026 02:06
The check-all option decided its direction from the internal selected state:

    if (checkAll) { ... trigger: isAllSelected ? 'uncheck' : 'check' }

When the caller resets the value from outside, the internal state and the
rendered options disagree for one render, so clicking check-all could fire
the opposite action - unchecking when everything was already cleared.

Compute the direction from the options that are actually rendered and
enabled.

Verified:
- new test fails on the previous implementation
  (expected [] to deeply equal [ 'apple', 'orange', 'banana' ])
- new test passes with the fix
- select: 47 tests pass
The check-all test has to reach the option list through document because the
popup is teleported to body, so render()'s container does not contain it.
Nothing removed those nodes afterwards, which leaves them in body for every
later case in the same file - the exact shape of the E-001 mistake.

Add an afterEach that runs cleanup() and clears body.

Verified:
- select: 3 files / 47 tests passed
- preflight.sh: 0 failures
@lnuxe

lnuxe commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

已 rebase 到 develop 2638507b(含 #4419 与 1.19.0 release),冲突只在 Select.tsx 的 onCheckAllChange。

两处改动互补,不重复:

改动 解决的问题
#4419(已在 develop) 搜索过滤后只增删当前列表内的可选项,引入 preservedValues / isSameOptionValue
本 PR 全选方向原本取自 Option 内部 state,与受控 value 不同步,改为由 value 派生 shouldCheckAll

冲突解决后完整保留 #4419 的分支(preservedValues 语义、keys.disabled 处理),只把 if (checkAll) 换成 if (shouldCheckAll),trigger 同步改为 shouldCheckAll ? 'check' : 'uncheck';方向判定复用 develop 新增的 isSameOptionValue,不再自己写比较。

验证:vitest run packages/components/select → 54 passed / 3 files。

说明:CONFLICTING 是因为 #4419 在本 PR 提交 13 天后改了同一函数;develop 上目前仍是 if (checkAll),也就是说内部 state 与 value 不同步的问题尚未被上游修复。

@lnuxe
lnuxe force-pushed the fix/select-check-all-direction branch from fc97866 to 2497b4b Compare November 26, 2026 19:19
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