Skip to content

fix(data-table): capture row selection state in memo comparison - #5524

Merged
t0ng7u merged 2 commits into
QuantumNous:mainfrom
RedwindA:fix/channelCheckbox
Jun 15, 2026
Merged

t0ng7u merged 2 commits into
QuantumNous:mainfrom
RedwindA:fix/channelCheckbox

Conversation

@RedwindA

@RedwindA RedwindA commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

修复使用 DataTable 的页面中,复选框(checkbox)无法正确渲染选中状态的问题。

问题原因:
DataTableRow 使用 React.memo 并自定义了比较函数来减少不必要的重渲染。原比较函数在判断行选中状态是否变化时,直接调用 row.getIsSelected():

prev.row.getIsSelected() === next.row.getIsSelected()

但 TanStack Table 的 row 对象在选中状态切换时可能仍保持同一个引用,因此 prev.row 与 next.row 指向同一对象,getIsSelected() 读取到的也是相同的(最新的)值,比较结果恒为相等。memo 据此判定“无需更新”,导致行的 data-state 与复选框选中样式不会随选中状态切换而重新渲染。

修复方式:
将选中状态在外层组件中提前“快照”为显式的 isSelected 布尔属性,传入被 memo 包裹的内层组件,并改为对该属性值进行比较:

  • 新增外层包装组件 DataTableRow,在每次渲染时计算 isSelected={props.row.getIsSelected()} 并下传;
  • 内层 DataTableRowInner 使用传入的 isSelected 渲染 data-state,不再实时调用 row.getIsSelected();
  • memo 比较函数改为 prev.isSelected === next.isSelected,由于该值是每次渲染时按值捕获的快照,选中状态变化时比较结果会发生变化,从而正确触发重渲染。

这样即便 row 对象引用保持稳定,只要选中状态发生变化,行(及其复选框)也能正确重新渲染。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

  • Closes # (如有)

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 Issues 与 PRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

修复前:
image

修复后:
image

Summary by CodeRabbit

  • Refactor
    • Optimized data table row component's selection state handling for improved performance and reliability.

TanStack row objects may keep a stable reference while their selection
state changes, so reading row.getIsSelected() inside the React.memo
comparator could miss updates. Capture isSelected as an explicit prop
and compare it instead, ensuring rows re-render when selection toggles.
@coderabbitai

coderabbitai Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6ce76ed2-d193-4614-b0f8-1ba538530d89

📥 Commits

Reviewing files that changed from the base of the PR and between 8477f62 and 179f69d.

📒 Files selected for processing (1)
  • web/default/src/components/data-table/core/data-table-row.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • web/default/src/components/data-table/core/data-table-row.tsx

Walkthrough

DataTableRowInner gains an explicit isSelected: boolean prop (extending the existing props type) and reads data-state from it instead of calling row.getIsSelected(). A new MemoizedDataTableRow wraps DataTableRowInner with a custom comparator tracking row, className, and isSelected. The exported DataTableRow becomes a plain function component that computes isSelected and forwards it down.

Changes

DataTableRow memoization refactor

Layer / File(s) Summary
DataTableRowInner: explicit isSelected prop
web/default/src/components/data-table/core/data-table-row.tsx
Adds DataTableRowInnerProps as DataTableRowProps & { isSelected: boolean }. DataTableRowInner sets data-state from the isSelected prop instead of calling row.getIsSelected() during render.
MemoizedDataTableRow comparator and DataTableRow export
web/default/src/components/data-table/core/data-table-row.tsx
Replaces the old React.memo(DataTableRowInner, comparator) export with a MemoizedDataTableRow that includes isSelected in the comparator. DataTableRow becomes a function component that calls props.row.getIsSelected() and passes the result into MemoizedDataTableRow.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

A rabbit checked each row with care,
"Is it selected? I must declare!"
🐇 The memo now knows what to track,
No sneaky re-renders sneak through the back.
With isSelected passed plain and true,
The data table hops along brand new! 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately and concisely describes the main change: fixing a memo comparison issue in the DataTable component to properly capture row selection state.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@web/default/src/components/data-table/core/data-table-row.tsx`:
- Around line 64-74: The memo comparator in MemoizedDataTableRow is skipping the
comparison of the getColumnClassName prop, but this callback is used to render
cell classes at line 54. If getColumnClassName changes while row, className, and
isSelected remain equal, the component will not re-render and cell styling will
become stale. Add getColumnClassName to the comparator by comparing
prev.getColumnClassName === next.getColumnClassName alongside the existing
checks for row, className, and isSelected.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d3b9e869-d24d-415e-8040-f8b42fb1f6e4

📥 Commits

Reviewing files that changed from the base of the PR and between 9bc1a53 and 8477f62.

📒 Files selected for processing (1)
  • web/default/src/components/data-table/core/data-table-row.tsx

@t0ng7u
t0ng7u merged commit b4176de into QuantumNous:main Jun 15, 2026
@RedwindA
RedwindA deleted the fix/channelCheckbox branch July 23, 2026 07:03
zhaodechao2008 pushed a commit to zhaodechao2008/new-api that referenced this pull request Jul 27, 2026
330079598 pushed a commit to 330079598/new-api that referenced this pull request Aug 19, 2026
salem-2007 added a commit to salem-2007/new-api that referenced this pull request Sep 10, 2026
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 2026
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