Skip to content

fix expanded & sticky #1277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed

Conversation

crazyair
Copy link
Contributor

@crazyair crazyair commented May 21, 2025

image

Summary by CodeRabbit

  • 新功能

    • 表格组件新增 expandedRowOffset 属性,支持展开行的列偏移设置,提升表格扩展行的灵活性。
    • 新增“expandedSticky”演示示例,展示可展开且表头固定的表格用法。
  • 文档

    • 新增 expandedSticky.md 文档页面,便于用户查阅相关用法及示例。

Copy link

vercel bot commented May 21, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
table ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 24, 2025 6:03am

Copy link

coderabbitai bot commented May 21, 2025

Walkthrough

本次变更引入了一个可配置的 expandedRowOffset 属性,用于表格展开行的列偏移处理。相关类型定义、上下文、表体行和展开行组件均已适配此属性,并通过新增的文档和示例演示了其用法。未涉及其他控制流或错误处理调整。

Changes

文件或路径分组 变更摘要
docs/demo/expandedSticky.md 新增文档,嵌入 expandedSticky.tsx 示例,配置导航元数据。
docs/examples/expandedSticky.tsx 新增 React 组件 Demo,演示可展开且 sticky 的表格。
src/interface.ts ExpandableConfig 接口新增可选属性 expandedRowOffset。
src/context/TableContext.tsx TableContextProps 接口新增 expandedRowOffset 属性。
src/Table.tsx TableContextValue 增加 expandedRowOffset,useMemo 依赖同步更新。
src/Body/BodyRow.tsx BodyRowProps 新增 expandedRowOffset,展开行渲染逻辑支持偏移。
src/Body/ExpandedRow.tsx ExpandedRowProps 新增 offsetWidth 属性,默认值为 0。
src/Body/index.tsx Body 组件从上下文获取 expandedRowOffset 并传递给 BodyRow。

Sequence Diagram(s)

sequenceDiagram
    participant 用户
    participant Demo组件
    participant Table
    participant BodyRow
    participant ExpandedRow

    用户->>Demo组件: 渲染 Demo
    Demo组件->>Table: 配置 expandedRowOffset 并渲染
    Table->>BodyRow: 传递 expandedRowOffset
    BodyRow->>ExpandedRow: 计算 offsetWidth 并传递
    ExpandedRow-->>BodyRow: 渲染展开行内容(带偏移)
Loading

Suggested reviewers

  • zombieJ
  • afc163

Poem

(=^● ⋏ ●^=)
表格展开新偏移,
组件协作齐努力。
sticky 行头真神奇,
展示效果更灵犀。
代码如诗,文档如画,
小兔欢跳庆变化!

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

docs/examples/expandedSticky.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the config "prettier" to extend from. Please check that the name of the config is correct.

The config "prettier" was referenced from the config file in "/.eslintrc.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


📜 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 1dc05b8 and 1e9da7a.

📒 Files selected for processing (1)
  • docs/examples/expandedSticky.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • docs/examples/expandedSticky.tsx
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test / react component workflow
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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: 0

🧹 Nitpick comments (1)
docs/examples/expandedSticky.tsx (1)

14-19: onCell回调与expandedRowKeys的结合使用

这段代码展示了如何根据行是否展开来动态调整单元格的rowSpan属性。这是一个高级用例,展示了新特性如何与现有功能(如单元格合并)结合使用。

不过,有一个小建议:添加注释说明这里的逻辑,特别是条件判断expandedRowKeys.includes('b')的含义,以便其他开发者更容易理解这段代码的目的。

  onCell: (_, index) => {
    const props: React.TdHTMLAttributes<HTMLTableCellElement> = {};
-   if (index === 1) props.rowSpan = expandedRowKeys.includes('b') ? 3 : 2;
-   if (index === 2) props.rowSpan = 0;
+   // 根据是否展开行调整单元格rowSpan
+   if (index === 1) props.rowSpan = expandedRowKeys.includes('b') ? 3 : 2; // 如果b行展开,则跨3行,否则跨2行
+   if (index === 2) props.rowSpan = 0; // 隐藏第3行单元格,因为被第2行覆盖
    return props;
  },
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d696f75 and 65cdf0b.

📒 Files selected for processing (7)
  • docs/demo/expandedSticky.md (1 hunks)
  • docs/examples/expandedSticky.tsx (1 hunks)
  • src/Body/BodyRow.tsx (4 hunks)
  • src/Body/index.tsx (3 hunks)
  • src/Table.tsx (2 hunks)
  • src/context/TableContext.tsx (2 hunks)
  • src/interface.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
src/context/TableContext.tsx (1)
src/interface.ts (1)
  • ExpandableConfig (240-260)
src/Body/BodyRow.tsx (1)
src/interface.ts (1)
  • ExpandableConfig (240-260)
docs/examples/expandedSticky.tsx (2)
src/interface.ts (1)
  • Key (21-21)
tests/FixedColumn-IE.spec.jsx (1)
  • columns (27-40)
🔇 Additional comments (15)
src/interface.ts (1)

259-259: 类型定义正确添加

expandedRowColSpan属性已正确添加到ExpandableConfig接口中,使表格组件能够支持自定义展开行的跨列数。这个改动与其他相关文件的变更一致。

src/Body/index.tsx (3)

34-34: 正确从上下文中解构新属性

expandedRowColSpan属性已从TableContext中正确解构出来,使Body组件能够访问该配置项。


44-44: 依赖数组更新完整

expandedRowColSpan已添加到useContext的依赖数组中,确保当此属性变化时组件能够正确重新渲染。


79-79: 属性正确传递给子组件

expandedRowColSpan属性已正确传递给BodyRow组件,这样展开行的跨列数就可以在渲染时被正确应用。

docs/demo/expandedSticky.md (1)

1-8: 文档结构清晰

文档格式正确,提供了适当的标题和导航信息,并引用了示例代码。这个演示文档将帮助用户理解如何使用新添加的expandedRowColSpan功能。

src/context/TableContext.tsx (2)

6-6: 导入类型定义正确

ExpandableConfig类型已从接口文件中正确导入,这是在Context中使用该类型的必要准备。


56-56: 上下文属性定义完整

expandedRowColSpan属性已正确添加到TableContextProps接口中,并使用了索引访问类型来保持与原始定义的一致性。这确保了展开行跨列数的配置能够通过Context正确传递给子组件。

src/Table.tsx (2)

847-847: 新增的expandedRowColSpan属性已正确添加到上下文中

该更改正确地将expandedRowColSpan属性从expandableConfig传递到表格上下文值中,使该属性可以被表格组件的其他部分访问。


898-898: 依赖数组更新确保上下文能响应expandedRowColSpan变化

expandableConfig.expandedRowColSpan添加到useMemo依赖数组中,确保当这个属性变化时,上下文值会被重新计算。这是正确的做法。

src/Body/BodyRow.tsx (4)

7-7: 正确导入ExpandableConfig类型

添加了ExpandableConfig类型的导入,用于定义expandedRowColSpan属性的类型。类型引用正确。


22-22: props接口中正确添加expandedRowColSpan属性

BodyRowProps接口中添加了可选的expandedRowColSpan属性,并使用了正确的类型引用。


106-106: 解构props中的expandedRowColSpan

从props中正确解构了expandedRowColSpan属性,为后续使用做准备。


200-200: 实现expandedRowColSpan逻辑

使用空值合并运算符(??)优雅地实现了当expandedRowColSpan有值时使用该值,否则回退到原有的flattenColumns.length。这确保了向后兼容性,同时提供了自定义展开行列跨度的能力。

docs/examples/expandedSticky.tsx (2)

1-51: 示例代码展示了expandedRowColSpan的使用

这个示例很好地展示了如何使用新的expandedRowColSpan属性来控制展开行的列跨度。示例同时演示了与sticky表头的结合使用,以及如何动态计算展开行应该跨越的列数(通过columns.length - 1)。

代码组织清晰,功能实现完整,对用户理解新特性有很好的帮助。


39-39: expandedRowColSpan属性使用示例

这里展示了expandedRowColSpan的具体用法,设置为columns.length - 1表示展开行跨越除了展开控制列之外的所有列。这是一个常见的使用场景,示例选择得当。

@crazyair crazyair changed the title feat: fix expanded & sticky fix expanded & sticky May 21, 2025
@@ -19,6 +19,7 @@ export interface BodyRowProps<RecordType> {
scopeCellComponent: CustomizeComponent;
indent?: number;
rowKey: React.Key;
expandedRowOffset?: ExpandableConfig<RecordType>['expandedRowOffset'];
Copy link
Member

Choose a reason for hiding this comment

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

那个 rowSpan 的挪过来? expandedRowOffset 如果没有那个合并的能力,这里其实是个半吊子

Copy link
Contributor Author

Choose a reason for hiding this comment

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

合也行,主要是另外哪个我还想做个模式,支持合并单元格是 + 1,还是分割
image

Copy link

codecov bot commented May 24, 2025

Codecov Report

Attention: Patch coverage is 82.35294% with 3 lines in your changes missing coverage. Please review.

Project coverage is 98.04%. Comparing base (d696f75) to head (1e9da7a).

Files with missing lines Patch % Lines
src/Body/BodyRow.tsx 70.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1277      +/-   ##
==========================================
- Coverage   98.08%   98.04%   -0.04%     
==========================================
  Files          77       77              
  Lines        7609     7624      +15     
  Branches     1164     1165       +1     
==========================================
+ Hits         7463     7475      +12     
- Misses        140      143       +3     
  Partials        6        6              

☔ 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.

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 (1)
src/Body/BodyRow.tsx (1)

25-25: 属性类型定义正确

使用 ExpandableConfig<RecordType>['expandedRowOffset'] 确保了类型安全性和一致性。

🧹 Nitpick comments (1)
src/Body/BodyRow.tsx (1)

204-206: 添加测试覆盖

静态分析工具指出这些行缺少测试覆盖。考虑到这是新增的偏移宽度计算逻辑,建议添加相应的单元测试。

这些行需要测试覆盖以确保在各种宽度配置下的正确性。你希望我帮助生成针对此功能的测试用例吗?

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 204-206: src/Body/BodyRow.tsx#L204-L206
Added lines #L204 - L206 were not covered by tests

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 54e4fac and f159a58.

📒 Files selected for processing (3)
  • src/Body/BodyRow.tsx (5 hunks)
  • src/Table.tsx (2 hunks)
  • src/context/TableContext.tsx (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/context/TableContext.tsx
  • src/Table.tsx
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/Body/BodyRow.tsx (1)
src/interface.ts (1)
  • ExpandableConfig (240-260)
🪛 GitHub Check: codecov/patch
src/Body/BodyRow.tsx

[warning] 204-206: src/Body/BodyRow.tsx#L204-L206
Added lines #L204 - L206 were not covered by tests

🔇 Additional comments (3)
src/Body/BodyRow.tsx (3)

7-7: 类型导入正确添加

正确引入了 ExpandableConfig 类型,用于 expandedRowOffset 属性的类型定义。


111-111: 默认值设置合理

expandedRowOffset 默认值设为 0 是合理的选择,保证了向后兼容性。


220-221:

✅ Verification successful

ExpandedRow 属性传递正确

正确地将计算出的 offsetWidth 和调整后的 colSpan 传递给 ExpandedRow 组件,实现了展开行的列偏移功能。

但需要注意,宽度计算的准确性依赖于上述提到的改进建议。

运行以下脚本验证 ExpandedRow 组件是否正确接收这些新属性:


🏁 Script executed:

#!/bin/bash
# 验证 ExpandedRow 组件是否支持 offsetWidth 属性
ast-grep --pattern 'interface $_ {
  $$$
  offsetWidth?: $_
  $$$
}'

Length of output: 639


验证通过:ExpandedRowProps 正确支持 offsetWidth 和 colSpan

已确认 src/Body/ExpandedRow.tsx 中的 ExpandedRowProps 接口包含

  • offsetWidth?: number
  • colSpan: number

属性定义无误,确保了在 BodyRow.tsx 中向 ExpandedRow 传递的 offsetWidthcolSpan 能够被正确接收,无需进一步修改。

Comment on lines +201 to +207
const offsetColumns = flattenColumns.filter((_, idx) => idx < expandedRowOffset);
let offsetWidth = 0;
offsetColumns.forEach(item => {
if (typeof item.width === 'number') {
offsetWidth = offsetWidth + (item.width ?? 0);
}
});
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

改进偏移宽度计算逻辑

当前的宽度计算实现存在几个问题:

  1. 只处理数字类型的宽度,忽略了字符串类型(如 "100px")和百分比宽度
  2. 没有验证 expandedRowOffset 是否在有效范围内
  3. 使用 forEach 而不是更简洁的 reduce 方法

建议改进为:

-    const offsetColumns = flattenColumns.filter((_, idx) => idx < expandedRowOffset);
-    let offsetWidth = 0;
-    offsetColumns.forEach(item => {
-      if (typeof item.width === 'number') {
-        offsetWidth = offsetWidth + (item.width ?? 0);
-      }
-    });
+    // 验证偏移值的有效性
+    const validOffset = Math.min(Math.max(0, expandedRowOffset), flattenColumns.length);
+    const offsetColumns = flattenColumns.slice(0, validOffset);
+    
+    const offsetWidth = offsetColumns.reduce((total, column) => {
+      if (typeof column.width === 'number') {
+        return total + column.width;
+      }
+      // 处理字符串宽度(可选:解析 px 值)
+      if (typeof column.width === 'string' && column.width.endsWith('px')) {
+        const numValue = parseInt(column.width, 10);
+        return isNaN(numValue) ? total : total + numValue;
+      }
+      return total;
+    }, 0);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const offsetColumns = flattenColumns.filter((_, idx) => idx < expandedRowOffset);
let offsetWidth = 0;
offsetColumns.forEach(item => {
if (typeof item.width === 'number') {
offsetWidth = offsetWidth + (item.width ?? 0);
}
});
// 验证偏移值的有效性
const validOffset = Math.min(Math.max(0, expandedRowOffset), flattenColumns.length);
const offsetColumns = flattenColumns.slice(0, validOffset);
const offsetWidth = offsetColumns.reduce((total, column) => {
if (typeof column.width === 'number') {
return total + column.width;
}
// 处理字符串宽度(可选:解析 px 值)
if (typeof column.width === 'string' && column.width.endsWith('px')) {
const numValue = parseInt(column.width, 10);
return isNaN(numValue) ? total : total + numValue;
}
return total;
}, 0);
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 204-206: src/Body/BodyRow.tsx#L204-L206
Added lines #L204 - L206 were not covered by tests

🤖 Prompt for AI Agents
In src/Body/BodyRow.tsx around lines 201 to 207, improve the offset width
calculation by first validating that expandedRowOffset is within the valid range
of flattenColumns indices. Then, replace the forEach loop with a reduce method
to accumulate the total width. Modify the logic to handle width values that are
numbers, strings with pixel units (e.g., "100px"), and percentage strings by
parsing and converting them appropriately before summing. This will make the
calculation more robust and concise.

@crazyair crazyair closed this May 24, 2025
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