Skip to content

Conversation

@FatahChan
Copy link

@FatahChan FatahChan commented Nov 20, 2025

This pull request introduces a new column sorting demo using drag-and-drop, refactors the column resizing demo to use React hooks, and adds the necessary dependencies for drag-and-drop functionality. The main focus is on improving demo interactivity and modernizing the codebase.

New Features:

  • Added a drag-and-drop sortable columns demo in docs/examples/column-sortable.tsx, using the @dnd-kit library for interactive column reordering.
  • Created a documentation page for the column sorting demo at docs/demo/column-sortable.md.

Refactoring and Improvements:

  • Refactored the column resizing demo in docs/examples/column-resize.tsx from a class component to a functional component using React hooks, improving readability and maintainability. [1] [2]

Dependency Updates:

  • Added @dnd-kit/core, @dnd-kit/modifiers, and @dnd-kit/sortable to package.json to support drag-and-drop functionality in demos.

Summary by CodeRabbit

发布说明

  • 新功能

    • 新增表格列可拖拽排序示例,支持通过拖放改变列显示顺序。
  • 文档

    • 新增列可排序示例的演示页面与示例引用,便于快速查看与复现。
  • 优化 / 重构

    • 将列调整示例重构为函数式组件,使用 React hooks 与 memo 化优化渲染与状态管理。
  • 杂项

    • 增加用于实现拖拽交互的开发依赖以支持示例。

✏️ Tip: You can customize this high-level summary in your review settings.

@vercel
Copy link

vercel bot commented Nov 20, 2025

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

A member of the Team first needs to authorize it.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @FatahChan, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the demo section by introducing a modern, interactive column sorting example with drag-and-drop capabilities. Concurrently, it updates the codebase by refactoring an existing column resizing demo to utilize contemporary React hooks, aligning with current best practices and improving overall demo interactivity and code maintainability.

Highlights

  • New Drag-and-Drop Column Sorting Demo: A new interactive demo for sorting table columns using drag-and-drop functionality has been added, leveraging the @dnd-kit library.
  • Column Resizing Demo Refactoring: The existing column resizing demo has been refactored from a class component to a functional component, adopting React hooks for improved readability and maintainability.
  • Dependency Updates: New @dnd-kit packages have been added to package.json to support the drag-and-drop features in the new demo.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@coderabbitai
Copy link

coderabbitai bot commented Nov 20, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

新增列拖拽排序示例与文档,重构列宽调整示例为函数式组件,并在 devDependencies 中加入 dnd-kit 相关包以支持拖拽;未更改公共 API 声明。

Changes

内聚组 / 文件 变更摘要
文档页
docs/demo/column-sortable.md
新增文档页面,包含 front-matter 与对示例 ../examples/column-sortable.tsx 的引用。
列拖拽示例
docs/examples/column-sortable.tsx
新增 TypeScript React 示例:引入 dnd-kit(DndContext/SortableContext)、SortableHeaderCell、列顺序状态及 dragEnd 处理以实现水平列拖拽排序;导出默认 Demo 组件。
列调整大小示例重构
docs/examples/column-resize.tsx
Demo 从类组件重构为函数式组件,使用 useState/useMemo 管理 state 与 columns,重写列 resize 处理逻辑。
依赖配置
package.json
devDependencies 中新增 @dnd-kit/core@dnd-kit/modifiers@dnd-kit/sortable 三个包。

Sequence Diagram(s)

sequenceDiagram
    autonumber
    actor User as 用户
    participant UI as 表格 UI
    participant Header as 列头 (SortableHeaderCell)
    participant DndKit as dnd-kit (DndContext)
    participant Demo as Demo 组件

    Note over User,Header: 用户在列头上开始拖拽
    User->>Header: 拖动列头 (drag start)
    Header->>DndKit: 触发 dnd-kit 事件
    DndKit->>Demo: 传递 dragEnd 事件与 id/索引
    Demo->>Demo: 计算新的列顺序 (arrayMove)
    Demo->>UI: 更新 columns 状态并重新渲染
    UI->>User: 显示重新排序后的列
Loading

Estimated code review effort

🎯 3 (中等) | ⏱️ ~20 分钟

  • 重点审查:
    • docs/examples/column-sortable.tsx 中 dnd-kit 的事件处理(尤其 dragEnd 的索引边界与 id 映射)。
    • docs/examples/column-resize.tsx 中 hooks 的依赖数组与 setState(useState/useMemo)使用是否正确,避免不必要重渲染或 stale closures。
    • package.json 新增的 dnd-kit 版本是否与项目 TypeScript/构建配置兼容。

Poem

🐰 新列轻搬跳一圈,
指尖牵引风不眠,
钩子替旧类添新篇,
文档示例共欢颜,
小兔鼓掌又打盹。

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR标题清晰准确地描述了主要变更,即添加可排序列的示例文档和演示。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between acc7f7a and 71e3873.

📒 Files selected for processing (1)
  • docs/examples/column-sortable.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • docs/examples/column-sortable.tsx

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.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new sortable columns demo and refactors the column resizing demo to use React hooks. The refactoring is a good improvement, but it seems to have missed a required height prop on the Resizable component. The new sortable columns example is a great addition, but it contains a critical bug in the drag-and-drop logic due to an inconsistency between key and dataIndex usage for identifying columns. I've also pointed out a few other areas for improvement in the new example, such as props not being passed correctly and lack of typing. The dependency changes in package.json look correct.

@socket-security
Copy link

socket-security bot commented Nov 20, 2025

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Added@​dnd-kit/​core@​6.3.11001007985100
Added@​dnd-kit/​sortable@​10.0.01001007985100
Added@​dnd-kit/​modifiers@​9.0.010010010085100

View full report

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c8d2df1 and 3a5240e.

📒 Files selected for processing (4)
  • docs/demo/column-sortable.md (1 hunks)
  • docs/examples/column-resize.tsx (1 hunks)
  • docs/examples/column-sortable.tsx (1 hunks)
  • package.json (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2024-11-08T12:53:09.293Z
Learnt from: bbb169
Repo: react-component/table PR: 1202
File: src/Table.tsx:903-904
Timestamp: 2024-11-08T12:53:09.293Z
Learning: 在 `src/Table.tsx` 文件的 React 组件 `Table` 中,即使 `bodyScrollLeft` 频繁更新,也需要在 `TableContextValue` 的 `useMemo` 依赖数组中包含 `bodyScrollLeft` 和 `headerCellRefs`,因为每次滚动时重新计算 `TableContextValue` 是解决该问题所必须的。

Applied to files:

  • docs/examples/column-sortable.tsx
  • docs/examples/column-resize.tsx
🧬 Code graph analysis (1)
docs/examples/column-resize.tsx (1)
src/interface.ts (2)
  • ColumnsType (135-138)
  • ColumnType (118-133)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Socket Security: Pull Request Alerts
🔇 Additional comments (1)
docs/examples/column-sortable.tsx (1)

91-92: 删除 JSX 中多余的分号

这里的 <Table {...tableProps} />; 会把分号当作文本节点渲染在页面上,示例内容尾部会多出一个 ;。去掉它即可。

-        <Table {...tableProps} />;
+        <Table {...tableProps} />


<details>
<summary>⛔ Skipped due to learnings</summary>

Learnt from: bbb169
Repo: react-component/table PR: 1202
File: src/Table.tsx:903-904
Timestamp: 2024-11-08T12:53:09.293Z
Learning: 在 src/Table.tsx 文件的 React 组件 Table 中,即使 bodyScrollLeft 频繁更新,也需要在 TableContextValueuseMemo 依赖数组中包含 bodyScrollLeftheaderCellRefs,因为每次滚动时重新计算 TableContextValue 是解决该问题所必须的。


</details>

</blockquote></details>

</blockquote></details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (2)
docs/examples/column-sortable.tsx (2)

10-25: SortableHeaderCell 增加显式类型声明

当前 SortableHeaderCellpropsany,在 TS 示例里不利于提示和约束。可以考虑为它声明更明确的类型(例如基于 React.ThHTMLAttributes<HTMLTableCellElement> 并扩展 dnd 所需字段),方便后续读者直接复制使用。


34-47: columns 添加 ColumnsType 泛型以提升类型安全

这里的 columns 状态没有显式类型,推断为宽松的 any[]。建议参考 column-resize 示例,显式使用 ColumnsType<(typeof data)[number]>,这样 TS 能更好校验 dataIndex / key / width 等字段,也便于读者照抄:

-  const [columns, setColumns] = useState([
+  const [columns, setColumns] =
+    useState<import('rc-table').ColumnsType<(typeof data)[number]>>([
🧹 Nitpick comments (3)
docs/examples/column-sortable.tsx (1)

48-76: 拖拽 id 与 dataIndex 的耦合可适当抽象,避免未来扩展示例时踩坑

当前拖拽 id 在三处手动依赖 dataIndex

  • onHeaderCell 中通过 col.dataIndex?.toString() 生成 id
  • SortableContext.items 使用 columns.map(col => col.dataIndex?.toString())
  • handleDragEnd 中用 col.dataIndex === active.id/over.id 查找索引

在本示例下是工作正常的,但一旦后续示例中某些列使用数组形式的 dataIndex(如 ['user', 'name'])或 key !== dataIndex,这三处就容易再次出现不一致的问题。建议抽一个小工具函数统一计算列 id,例如:

const getColumnId = (col: (typeof columns)[number]) =>
  col.dataIndex?.toString() ?? '';

然后在 onHeaderCellSortableContext.itemshandleDragEnd 中都通过 getColumnId 来对比,避免未来改动 dataIndex 结构时出现隐蔽 bug。

docs/examples/column-resize.tsx (2)

7-18: width 的判空建议区分 0undefined

ResizableTitle 中用 if (!width) 判断是否启用可拖拽,这会把 width === 0 也当成「无宽度」处理,将直接返回普通 <th>。当前示例列宽都大于 0,因此没有问题,但如果后续有人把某列初始宽度设为 0,这一列会意外变得不可拖拽。

可以改为只在 width == null 时禁用:

-  if (!width) {
+  if (width == null) {
     return <th {...restProps} />;
   }

42-50: handleResize 更符合直觉的写法是使用 useCallback(当前逻辑正确,仅风格建议)

目前通过 useMemo 返回一个函数工厂同样可以保证引用稳定,但对大部分 React 开发者来说,用 useCallback 表达「稳定回调」的语义更直观。可以按需(或在后续重构时)调整为:

-  const handleResize = useMemo(() => {
-    return (index: number) => {
-      return (_: React.MouseEvent<HTMLTableCellElement>, { size }: { size: { width: number } }) => {
-        setColumns(prevColumns =>
-          prevColumns.map((col, i) => (i === index ? { ...col, width: size.width } : col)),
-        );
-      };
-    };
-  }, []);
+  const handleResize = React.useCallback(
+    (index: number) =>
+      (_: React.MouseEvent<HTMLTableCellElement>, { size }: { size: { width: number } }) => {
+        setColumns(prevColumns =>
+          prevColumns.map((col, i) => (i === index ? { ...col, width: size.width } : col)),
+        );
+      },
+    [],
+  );

功能等价,可读性略好一些。

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3a5240e and acc7f7a.

📒 Files selected for processing (2)
  • docs/examples/column-resize.tsx (1 hunks)
  • docs/examples/column-sortable.tsx (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2024-11-08T12:53:09.293Z
Learnt from: bbb169
Repo: react-component/table PR: 1202
File: src/Table.tsx:903-904
Timestamp: 2024-11-08T12:53:09.293Z
Learning: 在 `src/Table.tsx` 文件的 React 组件 `Table` 中,即使 `bodyScrollLeft` 频繁更新,也需要在 `TableContextValue` 的 `useMemo` 依赖数组中包含 `bodyScrollLeft` 和 `headerCellRefs`,因为每次滚动时重新计算 `TableContextValue` 是解决该问题所必须的。

Applied to files:

  • docs/examples/column-resize.tsx
  • docs/examples/column-sortable.tsx
🧬 Code graph analysis (1)
docs/examples/column-resize.tsx (1)
src/interface.ts (2)
  • ColumnsType (135-138)
  • ColumnType (118-133)
🔇 Additional comments (1)
docs/examples/column-resize.tsx (1)

20-69: 整体列宽类型、数据建模与 Hook 重构实现良好

data/RecordType 的建模、ColumnsType<RecordType> 的使用以及 columnsWithResizable/tablePropsuseMemo 拆分都比较清晰,状态更新也保持了不可变更新风格,适合作为文档示例代码。

@codecov
Copy link

codecov bot commented Nov 21, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.09%. Comparing base (c8d2df1) to head (71e3873).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1415   +/-   ##
=======================================
  Coverage   96.09%   96.09%           
=======================================
  Files          57       57           
  Lines        3434     3434           
  Branches      632      632           
=======================================
  Hits         3300     3300           
  Misses        129      129           
  Partials        5        5           

☔ View full report in Codecov by Sentry.
📢 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.

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.

1 participant