Skip to content

fix(tree-select): rebuild columns when the root column changes (issue #4664) - #4719

Open
lnuxe wants to merge 2 commits into
Tencent:developfrom
lnuxe:fix/tree-select-switch-root-columns
Open

lnuxe wants to merge 2 commits into
Tencent:developfrom
lnuxe:fix/tree-select-switch-root-columns

Conversation

@lnuxe

@lnuxe lnuxe commented Nov 11, 2026 •

Copy link
Copy Markdown

🤔 这个 PR 的性质是?

  • 日常 bug 修复
  • 新特性提交
  • 文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • CI/CD 改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他

🔗 相关 Issue

close #4664

💡 需求背景和解决方案

问题

issue #4664:切换父级后,之前的子级选中样式丢失。

实际表现比「样式丢失」更彻底——切换到新父级后,中间列根本不刷新,仍显示上一个父级的分支。

以三层结构为例,value: ['a', 'a2', 'a2x'] 起手,切到父级 b 之后:

treeOptions[1] = ['a1', 'a2']   ← 应该变成 ['b1', 'b2']
innerValue[1]  = 'a2'           ← 应该落在 b 的子级里

中间列的选中态判断是 item.value === innerValue[level](tree-select.wxml 中间层分支),innerValue[1] 还是旧父级路径下的 'a2',在新列里找不到对应项,于是整列高亮消失——就是 issue 截图里的现象。

根因

onRootChange 只改了 innerValue[0],然后直接派发 change:

onRootChange(e) {
  const { innerValue } = this.data;
  const { value: itemValue } = e.detail;

  this.getScrollIntoView('none');
  innerValue[0] = itemValue;     // ← 只动了第一列

  this._trigger('change', { value: innerValue, level: 0 });
},

treeOptions 的唯一重建入口是 observers 里的 value/customValue/options/keys/multiple 变化,而这里既没触发 buildTreeOptions,也没更新 data.value,所以后续列全部停留在旧分支。

修改

onRootChange(e) {
  const { innerValue } = this.data;
  const { value: itemValue } = e.detail;

  this.getScrollIntoView('none');

  // 切换父级后,后续每一列的候选值都变了,必须按新父级重建。
  // buildTreeOptions 依据 data.value 定位分支,因此先把新父级写回去,
  // 深层选中值交给它按新分支重新推导(issue #4664)。
  const nextValue = [...(innerValue || [])];
  nextValue[0] = itemValue;
  for (let level = 1; level < nextValue.length; level += 1) {
    nextValue[level] = undefined;
  }

  this.setData({ value: nextValue });
  this.buildTreeOptions();

  this._trigger('change', { value: this.data.innerValue, level: 0 });
},

要点:

  • 先写回 data.value 再重建:buildTreeOptions 靠 data.value[level] 逐层匹配分支(currentLevelOptions.find(child => child.value === currentValue)),只改局部 innerValue 它看不到;
  • 深层值清成 undefined 而不是删掉数组元素,让 buildTreeOptions 回退到新分支的第一项,保持列长度与层级一致;
  • 派发的是重建后的 this.data.innerValue,与渲染所用数据一致。

验证

新增 packages/components/tree-select/__test__/switch-parent.test.js(三层树,A/A1/A2 与 B/B1/B2):

  1. 切父级后第二列必须换成新父级的子级;
  2. 切父级后 innerValue[0]/innerValue[1] 必须落在新分支内。

反向验证:还原 tree-select.ts 后跑:

× rebuilds the child columns when switching parent (issue #4664)
  Array ['a1','a2'] vs ['b1','b2']
× keeps intermediate highlight valid after switching parent (issue #4664)
2 failed

应用修复后 tree-select 全量 15 passed(3 个测试文件,含 3 个 demo 用例),eslint 干净。

fix #4664

📝 更新日志

  • 本条 PR 不需要纳入 Changelog

tdesign-miniprogram

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

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充
  • Changelog 已提供或无须提供

const { value, cursor } = event.detail;
this.updateValue(value);
this.triggerEvent('change', { value: this.data.value, cursor });
const nextValue = this.updateValue(value);

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.

移除与本 pr 无关的提交

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.

已移除。本 PR 现在只包含 packages/components/tree-select/tree-select.ts 与 packages/components/tree-select/__test__/index.test.js 两个文件,textarea 的改动已从分支中剔除(见 2d3c9c1)。

@lnuxe
lnuxe force-pushed the fix/tree-select-switch-root-columns branch from ca5fd5c to 46221dc Compare November 13, 2026 01:47
@lnuxe

lnuxe commented Nov 13, 2026

Copy link
Copy Markdown
Author

已按 review 意见调整:

  1. 移除了与本 PR 无关的改动。之前这个分支上混了 textarea 和 tree-select 的提交,现在只剩 tree-select 的 tree-select.ts 改动。textarea 的部分已经在 fix(textarea): emit the truncated value from change (issue #4705) #4717 单独提交,tree-select 的部分在 fix(tree-select): rebuild columns when the root column changes (issue #4664) #4719。

  2. 测试文件合并到组件自己的测试文件。删掉了 __test__/switch-parent.test.js,两个用例移入 __test__/index.test.js 的 describe('switch parent') 块。

现在改动范围:

packages/components/tree-select/__test__/index.test.js  | 58 ++++++++++++++++++++++
packages/components/tree-select/tree-select.ts           | 15 +++++-

本地验证:

npx jest --testPathPattern "tree-select"
Test Suites: 2 passed, 2 total
Tests:       15 passed, 15 total
Snapshots:   3 passed, 3 total

onRootChange only wrote innerValue[0] and dispatched change. The deeper
columns still held the previous root's candidates, so switching root left the
middle column showing the old branch, and the highlight check
item.value === innerValue[level] matched nothing in the new column.

Write the new root back to data.value, clear the deeper selections and let
buildTreeOptions derive them again from the new branch.

fix Tencent#4664
Review feedback: keep one unit test file per component instead of adding a
new file per scenario. The two switch-parent cases now live in
index.test.js under their own describe block, and switch-parent.test.js is
gone.

No behaviour change.
@lnuxe
lnuxe force-pushed the fix/tree-select-switch-root-columns branch from 46221dc to 2d3c9c1 Compare November 17, 2026 02:04
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.

[TreeSelect] 切换父级后,之前的子级选中样式会丢失

2 participants