Skip to content

Conversation

@oasis-cloud
Copy link
Collaborator

@oasis-cloud oasis-cloud commented Apr 1, 2025

Summary by CodeRabbit

  • 新特性
    • 更新了“Collapse”和“CollapseItem”组件至版本3.0.0,包含作者和显示属性的修改。
  • 样式
    • 改进了折叠组件的动画与过渡效果,展开和收起时更加流畅自然。
    • 更新了.nut-collapse-item组件的样式,重命名了一些类,并引入了条件样式。
  • 重构
    • 优化了组件的状态管理和高度计算,使内容区域在动态变化时自动适应,提升了交互体验。

@coderabbitai
Copy link

coderabbitai bot commented Apr 1, 2025

Walkthrough

本次 PR 更新了两个组件(Collapse 与 CollapseItem)的版本号,从 2.0.0 升级到 3.0.0,并同时更新了其元数据(如作者、dd 字段及 v15 值)。此外,还调整了 CollapseItem 组件的样式文件与逻辑:CSS 类名重命名、新嵌套类添加,以及状态管理和高度计算逻辑的重构。测试文件中的断言也已更新以适应新的 DOM 结构。

Changes

File(s) Change Summary
src/config.json 更新 Collapse 与 CollapseItem 的版本号,从 2.0.0 升级到 3.0.0;更新元数据(Collapse: v15 从 -1 改为 1,author 从 "zhenyulei" 改为 "oasis",dd 从 false 改为 true;CollapseItem: author 从 "zhenyulei" 改为 "oasis",dd 从 false 改为 true)。
src/packages/collapseitem/collapseitem.scss CSS 样式调整:重命名 .nut-collapse-item-header.disabled.nut-collapse-item-header-disabled.nut-collapse-item-content 重命名为 .nut-collapse-item-content-wrapper;添加嵌套类 &-tran,并修改伪元素 transform 属性,使用条件编译。
src/packages/collapseitem/collapseitem.taro.tsx
src/packages/collapseitem/collapseitem.tsx
重构 CollapseItem 组件状态管理和渲染逻辑:引入 useUuiduseRefState,替换原有 useState 钩子;移除 getRect,新增 updateRectHeight 处理异步高度计算;简化 toggle 逻辑并新增日志记录。
src/packages/collapse/__test__/collapse.spec.tsx 修改测试断言:由直接检查样式字符串替换为使用异步 waitFor 检测 .nut-collapse-item-content-wrapper 元素的高度属性(height: '0')。

Sequence Diagram(s)

sequenceDiagram
    participant U as 用户
    participant CI as CollapseItem组件
    participant NT as nextTick
    participant GR as getRectByTaro
    U->>CI: 点击折叠项头部
    CI->>CI: 调用toggle更新状态
    CI->>NT: 等待DOM更新(nextTick)
    NT->>GR: 异步获取内容高度
    GR-->>NT: 返回高度值
    NT-->>CI: 更新高度状态 (updateRectHeight)
    CI-->>U: 组件完成重新渲染
Loading

Possibly related PRs

Suggested reviewers

  • xiaoyatong
  • irisSong

Poem

嗨,我是一只快乐的小兔子 🎵
在代码花园中轻快跳跃,更新不停息,
版本升华如春雨,润物细无声,
样式新颜添风采,逻辑更加简洁,
bug 纷纷逃散,只留欢腾与希望,
跟随我,一起见证这奇妙的蜕变! (_/)
🥕 Happy Coding!

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

@github-actions github-actions bot added action:review This PR needs more reviews (less than 2 approvals) 3.x Target branch 3.x labels Apr 1, 2025
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 (8)
src/packages/collapseitem/collapseitem.tsx (4)

63-63: 添加调试日志和状态变量

添加了控制台日志输出用于调试,并引入了三个新的状态变量:trancurrentHeightwrapperHeight,用于精确控制折叠面板的高度和过渡动画。

不过,生产环境中应该移除调试日志。

-  console.log('oasis', expanded, name)

Also applies to: 71-73


75-87: 实现高度计算的异步更新机制

新增的 updateRectHeight 函数通过异步方式计算内容高度并更新状态,改进了高度计算的准确性和动画流畅度。

代码中的调试日志应在生产环境中移除。

-      console.log('oasis res', height)

89-94: 添加响应式高度更新

新增的 useEffect 钩子确保在 childrenexpanded 状态变化时重新计算高度,增强了组件对内容变化的响应能力。

同样,应移除调试日志。

-      console.log('oasis update', name)

96-106: 优化折叠切换逻辑

简化了 toggle 函数并更新了 handleClick 函数,使用 currentHeight.current 获取当前高度值,提高了代码的可维护性。

请移除调试日志。

-      console.log('oasis name', name)
src/packages/collapseitem/collapseitem.taro.tsx (4)

60-61: 使用 UUID 生成唯一标识符和添加调试日志

引入 useUuid 钩子生成唯一 ID,替代了之前的随机 ID 方式,增强了组件在多实例场景下的稳定性。

请移除生产环境中的调试日志。

-  console.log('oasis', expanded, name)

Also applies to: 69-69


81-93: 实现 Taro 环境下的高度计算

新增的 updateRectHeight 函数使用 nextTickgetRectByTaro 异步获取内容高度,适应了小程序环境下的渲染特性。

请移除调试日志。

-      console.log('oasis res', res.height)

94-99: 添加响应式高度更新

优化了 useEffect 钩子,使用 nextTick 确保在小程序环境下正确计算高度,增强了组件对内容变化的响应能力。

请移除调试日志。

-      console.log('oasis update', name)

101-113: 优化 Taro 环境下的状态更新流程

简化了 toggle 函数并在 handleClick 中使用 nextTick 确保状态更新的正确顺序,适应小程序环境下的异步渲染特性。

请移除调试日志。

-      console.log('oasis name', name)
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ecb47df and aa6d886.

📒 Files selected for processing (4)
  • src/config.json (1 hunks)
  • src/packages/collapseitem/collapseitem.scss (2 hunks)
  • src/packages/collapseitem/collapseitem.taro.tsx (3 hunks)
  • src/packages/collapseitem/collapseitem.tsx (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (12)
src/config.json (2)

1130-1130: 版本更新和元数据变更

Collapse 组件的版本从 "2.0.0" 升级到 "3.0.0",同时更新了 v15 值从 -1 到 1,作者从 "zhenyulei" 变更为 "oasis",并将 dd 属性从 false 修改为 true。这些变更与组件的鸿蒙适配相关联。

Also applies to: 1138-1140


1143-1143: CollapseItem 组件元数据更新

CollapseItem 组件的版本从 "2.0.0" 升级到 "3.0.0",作者从 "zhenyulei" 变更为 "oasis",同时将 dd 属性从 false 修改为 true,保持与 Collapse 组件一致。

Also applies to: 1151-1152

src/packages/collapseitem/collapseitem.scss (3)

41-46: 优化添加针对鸿蒙系统的条件编译指令

为伪元素添加了鸿蒙系统的专用样式支持,通过条件编译指令区分不同平台的变换效果,增强了跨平台兼容性。


97-105: 重构内容包装器的类结构

将原 &-content 类重命名为 &-content-wrapper,并添加了关键的宽度、位置和盒模型属性,同时引入了 &-tran 子类来处理过渡动画效果,优化了组件的布局结构。


107-112: 改进内容元素的定位方式

&-content 类进行了重构,设置为绝对定位并明确了高度和宽度属性,改善了滑动动画的实现方式,使过渡效果更加平滑。

src/packages/collapseitem/collapseitem.tsx (3)

5-8: 引入新的 Hooks 增强状态管理

引入了 useEffectuseState 钩子,并添加了自定义的 useRefState 钩子,为组件提供更强大的状态管理能力,特别是对高度变化的处理。

Also applies to: 13-13


123-134: 更新渲染结构和条件样式

重构了包装器 div 的类名和样式,使用条件性样式应用基于 tran 状态的高度值,实现了更平滑的高度过渡效果。


136-140: 重构内容渲染结构

优化了内容元素的嵌套结构,使用 ref 引用获取内容高度,提高了高度计算的准确性。

src/packages/collapseitem/collapseitem.taro.tsx (4)

5-8: 引入 Taro 特定的工具和 Hooks

更新了导入,添加了 nextTickgetRectByTarouseUuiduseRefState 等特定于 Taro 的工具和钩子,以适应小程序环境下的组件行为。

Also applies to: 12-17


77-79: 添加状态管理变量

引入了三个新的状态变量:trancurrentHeightwrapperHeight,用于精确控制折叠面板在 Taro 环境下的高度和过渡动画。


130-141: 更新 Taro 组件的渲染结构和条件样式

重构了包装器 View 的类名和样式,使用条件性样式应用基于 tran 状态的高度值,实现了更平滑的高度过渡效果。


143-145: 重构内容渲染结构

优化了内容元素的嵌套结构,使用 ref 引用和唯一 ID 获取内容高度,适应 Taro 环境下的元素选择和高度计算需求。

@codecov
Copy link

codecov bot commented Apr 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.12%. Comparing base (d6f1f6e) to head (a0260d7).
Report is 3 commits behind head on feat_v3.x.

Additional details and impacted files
@@              Coverage Diff              @@
##           feat_v3.x    #3139      +/-   ##
=============================================
+ Coverage      87.08%   87.12%   +0.03%     
=============================================
  Files            280      280              
  Lines          18535    18530       -5     
  Branches        2818     2815       -3     
=============================================
+ Hits           16142    16144       +2     
+ Misses          2388     2381       -7     
  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.

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

🧹 Nitpick comments (7)
src/packages/collapseitem/collapseitem.tsx (3)

63-63: 建议移除调试日志

这行调试日志在生产环境中不应该保留,建议在合并前删除。

-  console.log('oasis', expanded, name)

77-78: 建议移除调试日志

请移除这行调试日志,它在生产环境中不应该保留。

-    console.log('oasis res', height)

95-101: 优化点击事件处理

handleClick函数现在更清晰地处理点击事件,但同样包含了不必要的调试日志。

   const handleClick = () => {
     if (!disabled) {
-      console.log('oasis name', name)
       context.updateValue(name)
       toggle()
     }
   }
src/packages/collapseitem/collapseitem.taro.tsx (4)

69-69: 建议移除调试日志

这行调试日志应在合并前删除。

-  console.log('oasis', expanded, name)

84-84: 建议移除调试日志

请移除这行调试日志。

-      console.log('oasis res', res.height)

96-96: 建议移除调试日志

请移除这行调试日志。

-      console.log('oasis update', name)

107-107: 建议移除调试日志

请移除这行调试日志。

-      console.log('oasis name', name)
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between aa6d886 and f1f2cb0.

⛔ Files ignored due to path filters (1)
  • src/packages/collapse/__test__/__snapshots__/collapse.spec.tsx.snap is excluded by !**/*.snap
📒 Files selected for processing (4)
  • src/packages/collapse/__test__/collapse.spec.tsx (1 hunks)
  • src/packages/collapseitem/collapseitem.scss (3 hunks)
  • src/packages/collapseitem/collapseitem.taro.tsx (3 hunks)
  • src/packages/collapseitem/collapseitem.tsx (3 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
src/packages/collapseitem/collapseitem.tsx

[warning] 79-83: src/packages/collapseitem/collapseitem.tsx#L79-L83
Added lines #L79 - L83 were not covered by tests


[warning] 126-128: src/packages/collapseitem/collapseitem.tsx#L126-L128
Added lines #L126 - L128 were not covered by tests

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (24)
src/packages/collapse/__test__/collapse.spec.tsx (1)

66-72: 测试用例更新适配组件结构变更

这里将测试从直接检查 .nut-collapse-item-content 元素的内联样式更改为异步等待并检查 .nut-collapse-item-content-wrapper 元素的样式对象。这个更新与组件结构的变化保持一致,确保测试能正确验证折叠面板的收缩状态。

src/packages/collapseitem/collapseitem.scss (4)

41-46: 为鸿蒙系统添加条件样式适配

通过条件编译指令针对鸿蒙系统优化了变换样式。在鸿蒙环境下使用 scale(1, 1),在非鸿蒙环境下使用 scale(1, 0.5),这有助于解决不同平台上的样式呈现差异问题。


85-95: 优化BEM命名规范

将类名从 &-header.disabled 改为 &-header-disabled 更符合BEM命名规范,提高了代码的可维护性。


97-105: 重构内容包装器结构与过渡效果

新增的 &-content-wrapper 类及其子类 &-content-wrapper-tran 优化了过渡动画效果的实现。这种结构更有利于控制内容展开/收起的动画表现。


107-117: 调整内容定位策略

修改 &-content 类的定位属性为绝对定位(position: absolute),并设置了宽高,这使得内容区域的尺寸计算和动画过渡更加精确可控。

src/packages/collapseitem/collapseitem.tsx (8)

5-8: 扩展组件所需的React hooks

引入了更多的React hooks以支持新的状态管理和DOM操作需求,这是重构组件的必要准备。


13-13: 引入自定义hooks增强状态管理

引入useRefState钩子,它比普通的useState更适合在异步操作中保持最新的状态引用,这对动态高度计算非常有用。


71-73: 引入新的状态管理变量

新增了三个状态变量来管理折叠面板的高度和过渡效果:

  • tran: 控制是否应用过渡效果
  • currentHeight: 使用useRefState记录内容的当前高度
  • wrapperHeight: 控制包装器的高度

这种方法比以前的实现更具响应性和可预测性。


87-89: 添加副作用处理

使用useEffect在组件内容或展开状态变化时触发高度更新,这确保了组件能够响应这些变化并正确调整高度。


91-94: 简化切换逻辑

新的toggle函数简化了高度切换逻辑,直接基于当前展开状态设置适当的高度值。


106-108: 更新类名命名规范

使用${classPrefix}-header-disabled替代.disabled选择器,符合BEM命名规范,提高了代码可读性和维护性。


119-130: 改进渲染逻辑与样式控制

包装器div的渲染逻辑更新为基于tran状态有条件地应用样式,提供了更平滑的过渡效果。这部分代码也缺少测试覆盖,建议添加相应测试。

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 126-128: src/packages/collapseitem/collapseitem.tsx#L126-L128
Added lines #L126 - L128 were not covered by tests


133-137: 优化内容结构

调整后的内容div结构更清晰,将内容文本包装在单独的div中,便于精确计算高度和应用样式。

src/packages/collapseitem/collapseitem.taro.tsx (11)

5-8: 扩展Taro组件所需的React hooks

引入更多的React hooks以支持新的状态管理和DOM操作需求,这是重构Taro版本组件的必要准备。


12-17: 引入Taro特定的工具和自定义hooks

新增导入:

  • nextTick: 确保DOM更新后执行操作
  • getRectByTaro: 获取Taro环境下元素尺寸的工具
  • useUuid: 生成唯一标识符
  • useRefState: 增强状态管理

这些工具对于适配Taro环境下的组件行为至关重要。


60-61: 优化元素标识方式

使用useUuid钩子生成唯一ID,替代原来的随机ID生成方式,提高了标识符的可靠性和唯一性。


77-79: 引入新的状态管理变量

与React版本一致,新增三个状态变量来管理折叠面板的高度和过渡效果:

  • tran: 控制是否应用过渡效果
  • currentHeight: 使用useRefState记录内容的当前高度
  • wrapperHeight: 控制包装器的高度

81-93: 实现Taro环境下的高度计算逻辑

新的updateRectHeight函数使用Taro特定的API(nextTickgetRectByTaro)来异步获取和更新内容高度。这确保了在Taro环境下组件能正确计算和应用高度。


94-99: 优化副作用处理

使用useEffectnextTick的组合确保在组件内容或展开状态变化时正确更新高度,这在Taro环境中尤为重要。


101-104: 简化Taro环境下的切换逻辑

简化后的toggle函数与React版本保持一致,直接基于当前展开状态设置适当的高度值。


105-113: 优化Taro环境下的点击事件处理

handleClick函数现在在Taro环境下使用nextTick确保状态更新后再调用toggle,提高了状态更新的可靠性。


118-120: 更新Taro组件的类名命名规范

与React版本一致,使用${classPrefix}-header-disabled替代.disabled选择器,保持了代码风格的一致性。


132-142: 改进Taro环境下的渲染逻辑与样式控制

包装器View的渲染逻辑更新为基于tran状态有条件地应用样式,与React版本保持一致,提供了在Taro环境下的平滑过渡效果。


145-146: 优化Taro环境下的内容结构

调整后的内容View结构更清晰,添加了对应的引用和ID,便于在Taro环境下进行尺寸计算。

Comment on lines 75 to 85
const updateRectHeight = async () => {
const height = contentRef.current.offsetHeight
console.log('oasis res', height)
if (height) {
setCurrentHeight(height)
setWrapperHeight(expanded ? height : 0)
setTimeout(() => {
setTran(1)
})
}
}
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

❓ Verification inconclusive

实现高度计算逻辑

新的updateRectHeight函数通过读取DOM元素的实际高度来更新状态,确保动态内容能够正确展示。但是这部分代码缺少测试覆盖,建议添加相应的单元测试。


🏁 Script executed:

#!/bin/bash
# 检查是否有测试覆盖updateRectHeight函数
grep -r "updateRectHeight" --include="*.spec.tsx" --include="*.test.tsx" src/

Length of output: 77


请补充 updateRectHeight 函数的单元测试

经过验证,目前代码库中未发现与 updateRectHeight 相关的单元测试。建议在相应测试文件中添加单元测试,重点覆盖以下场景:

  • contentRef.current.offsetHeight 有效时,验证 setCurrentHeightsetWrapperHeight 分别传入正确的值(根据 expanded 状态分别为 height0)。
  • 针对 setTimeout 内部逻辑,建议使用模拟时钟(例如 Jest fake timers)确认 setTran 在延时后被正确调用。

这样能够确保动态高度逻辑在不同场景下的稳定性和正确性。

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 79-83: src/packages/collapseitem/collapseitem.tsx#L79-L83
Added lines #L79 - L83 were not covered by tests

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 (4)
src/packages/collapseitem/collapseitem.taro.tsx (4)

60-62: 需要移除调试日志

代码中包含多个 console.log 调试语句,这些应该在生产环境代码中移除,以避免不必要的控制台输出和潜在的性能影响。

-  const target = `nut-collapse-content-${uid}`
-  console.log('oasistarget', target)
+  const target = `nut-collapse-content-${uid}`

-  console.log('oasis', expanded, name)
+  

-      console.log('oasis res', res.height)
+      

Also applies to: 70-70, 85-85


82-94: 高度计算方法优化

新的 updateRectHeight 方法通过 nextTick 确保DOM更新完成后再进行高度测量,并使用 getRectByTaro 配合唯一ID进行元素查询,这比之前的实现更可靠,特别是在不同运行环境(如鸿蒙系统)下。

建议考虑添加错误处理,以防止 getRectByTaro 在某些情况下失败:

  const updateRectHeight = async () => {
    nextTick(async () => {
-      const res = await getRectByTaro(contentRef.current, target)
-      console.log('oasis res', res.height)
-      if (res?.height) {
+      try {
+        const res = await getRectByTaro(contentRef.current, target)
+        if (res?.height) {
          setCurrentHeight(res.height)
          setWrapperHeight(expanded ? res.height : 0)
          setTimeout(() => {
            setTran(1)
          })
+        }
+      } catch (error) {
+        console.error('获取元素高度失败:', error)
       }
    })
  }

95-100: 副作用处理优化

useEffect 中使用 nextTick 确保DOM更新后再调用 updateRectHeight 是合理的。依赖项数组包含 childrenexpanded,确保了当内容或展开状态变化时重新计算高度。

不过,应移除不必要的日志输出:

  useEffect(() => {
    nextTick(() => {
-      console.log('oasis update', name)
      updateRectHeight()
    })
  }, [children, expanded])

106-114: handleClick方法改进

使用 nextTick 包装 toggle 调用是一个好的改进,确保状态更新后再进行高度调整。不过同样需要移除调试日志:

  const handleClick = () => {
    if (!disabled) {
-      console.log('oasis name', name)
      context.updateValue(name)
      nextTick(() => {
        toggle()
      })
    }
  }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f1f2cb0 and 91fdf37.

📒 Files selected for processing (2)
  • src/packages/collapseitem/collapseitem.taro.tsx (3 hunks)
  • src/utils/get-rect-by-taro.ts (1 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
src/packages/collapseitem/collapseitem.taro.tsx (1)
src/utils/get-rect-by-taro.ts (1)
  • getRectByTaro (26-46)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (6)
src/utils/get-rect-by-taro.ts (1)

37-37: 增加了元素上下文范围,提高了选择器查询精度

通过添加 .in(element) 方法,将选择器查询限定在指定元素的上下文中,这样可以更准确地获取目标元素的位置和尺寸信息。这个改动对于正确适配鸿蒙系统环境下的元素查询非常重要,有助于确保在嵌套组件结构中能够准确定位元素。

src/packages/collapseitem/collapseitem.taro.tsx (5)

5-8: 导入变更使组件功能更完整

新增导入的 hooks 和工具函数很好地增强了组件功能,特别是:

  • nextTick 确保DOM更新后再进行计算
  • getRectByTaro 用于获取元素尺寸
  • useUuid 生成唯一标识符比随机ID更可靠
  • useRefState 结合了state和ref的优点,适合需要在回调中访问的状态值

这些变更对于鸿蒙系统的适配非常重要,能够确保组件在不同环境中的一致性表现。

Also applies to: 12-17


78-80: 状态管理优化

使用 useRefState 替代普通的 useState 来管理 currentHeight 是一个很好的改进。这种方式结合了 state 更新视图和 ref 在回调中稳定访问的优势,特别适合在异步操作中需要获取最新值的场景。


102-105: toggle方法简化

新的 toggle 方法更加简洁和直接,通过 currentHeight.current 获取最新的高度值是使用 useRefState 的好处体现。这种实现方式在动画过渡中更可靠,有助于确保在鸿蒙环境下的流畅体验。


119-121: 样式处理改进

类名条件渲染和样式动态计算的改进增强了组件的视觉表现:

  1. 添加了禁用状态的专用类名
  2. 引入了 tran 状态来控制过渡效果的时机
  3. 直接设置高度值而不是使用类名切换

这些变更有助于在鸿蒙系统下获得更一致的动画效果。

Also applies to: 133-143


146-147: 元素结构优化

为内容元素添加唯一ID并改进类名结构,有助于更精确地获取元素并应用样式。这种改进对于跨平台兼容性(特别是鸿蒙系统)非常重要。

Copy link
Collaborator

@xiaoyatong xiaoyatong left a comment

Choose a reason for hiding this comment

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

jd 下 无法收起展开

@oasis-cloud
Copy link
Collaborator Author

jd 下 无法收起展开

fixed

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

🧹 Nitpick comments (2)
src/packages/collapseitem/collapseitem.taro.tsx (1)

100-107: 在handleClick中添加setTimeout延迟

handleClick函数现在包含一个setTimeout来延迟调用toggle函数。这可能是为了确保状态更新已完成,但缺少明确的注释说明此延迟的目的。

建议添加注释说明使用setTimeout的原因:

const handleClick = () => {
  if (!disabled) {
    context.updateValue(name)
+   // 延迟执行toggle以确保状态更新已完成
    setTimeout(() => {
      toggle()
    }, 150)
  }
}
src/packages/collapseitem/collapseitem.tsx (1)

89-98: 简化toggle和handleClick函数

toggle函数逻辑变得更加简洁,直接使用currentHeight.current来获取最新的高度值。
handleClick函数更新了状态并直接调用toggle,与Taro版本相比,这里没有使用setTimeout延迟。

为保持与Taro版本的一致性,建议考虑是否也需要添加延迟:

const handleClick = () => {
  if (!disabled) {
    context.updateValue(name)
-    toggle()
+    // 为保持与Taro版本一致,考虑添加延迟
+    setTimeout(() => {
+      toggle()
+    }, 0)
  }
}

或者在注释中说明为什么Web版本不需要延迟。

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 91fdf37 and 2fc1a55.

📒 Files selected for processing (2)
  • src/packages/collapseitem/collapseitem.taro.tsx (4 hunks)
  • src/packages/collapseitem/collapseitem.tsx (3 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
src/packages/collapseitem/collapseitem.taro.tsx (1)
src/utils/get-rect-by-taro.ts (1)
  • getRectByTaro (26-45)
🪛 GitHub Check: codecov/patch
src/packages/collapseitem/collapseitem.tsx

[warning] 77-81: src/packages/collapseitem/collapseitem.tsx#L77-L81
Added lines #L77 - L81 were not covered by tests


[warning] 123-125: src/packages/collapseitem/collapseitem.tsx#L123-L125
Added lines #L123 - L125 were not covered by tests

🔇 Additional comments (12)
src/packages/collapseitem/collapseitem.taro.tsx (7)

5-17: 改进了导入依赖和工具函数

代码引入了一些新的依赖和工具函数,如nextTickgetRectByTarouseUuiduseRefState,这些工具函数的使用增强了组件的功能和状态管理能力。


60-62: 使用useUuid替代随机ID生成,提高了稳定性

使用useUuid钩子来生成唯一标识符,并用于构建目标ID,这比使用随机数更可靠,特别是在组件重新渲染时能保持ID的一致性。


76-78: 新增状态变量,优化过渡动画管理

引入了三个新的状态变量:

  • tran:控制过渡动画的触发
  • currentHeight:使用useRefState存储当前高度,便于在异步回调中访问最新值
  • wrapperHeight:管理包装器的高度

这种状态管理方式使动画过渡更加平滑可控。


80-89: 新增updateRectHeight函数,改进高度计算逻辑

新的updateRectHeight函数通过getRectByTaro异步获取内容元素的高度,并相应地更新状态变量。这种方法适合在小程序环境中使用,但缺少错误处理机制。

建议添加错误处理逻辑,处理res?.height可能不存在的情况:

const updateRectHeight = async () => {
  const res = await getRectByTaro(contentRef.current, target)
  if (res?.height) {
    setCurrentHeight(res.height)
    setWrapperHeight(expanded ? res.height : 0)
    nextTick(() => {
      setTran(1)
    })
+  } else {
+    console.warn('无法获取内容高度')
+    setCurrentHeight(0)
+    setWrapperHeight(0)
  }
}

与前一版审查意见一致,此代码仍缺少单元测试覆盖。请添加适当的测试用例。

#!/bin/bash
# 检查是否有测试覆盖updateRectHeight函数
grep -r "updateRectHeight" --include="*.spec.tsx" --include="*.test.tsx" src/

90-94: 优化useEffect依赖项和执行时机

使用nextTick确保DOM更新后再计算高度,并且合理地设置了childrenexpanded作为依赖项,这样当内容或展开状态变化时都能重新计算高度。


96-99: 简化toggle函数逻辑

toggle函数逻辑变得更加简洁,直接使用currentHeight.current来获取最新的高度值,这种方式比之前的实现更直接有效。


125-137: 优化内容包装器的样式和结构

通过条件样式和类名的应用,改进了内容包装器的结构和样式处理。根据tran状态条件性地应用高度样式,使过渡动画更加流畅。

src/packages/collapseitem/collapseitem.tsx (5)

5-13: 改进导入依赖和钩子函数

更新了导入的React钩子函数,并引入了useRefState钩子,这有助于在异步回调中访问最新的状态值。


70-72: 新增状态变量,优化过渡动画管理

引入了三个新的状态变量:

  • tran:控制过渡动画的触发
  • currentHeight:使用useRefState存储当前高度
  • wrapperHeight:管理包装器的高度

这种状态管理方式使动画过渡更加平滑可控。


74-83: 新增updateRectHeight函数,改进高度计算逻辑

updateRectHeight函数使用offsetHeight直接获取内容元素的高度,并相应地更新状态变量。这种方法适合在浏览器环境中使用,但缺少错误处理机制。

建议添加错误处理逻辑:

const updateRectHeight = async () => {
  const height = contentRef.current?.offsetHeight
+  if (!height) {
+    console.warn('无法获取内容高度')
+    setCurrentHeight(0)
+    setWrapperHeight(0)
+    return
+  }
  if (height) {
    setCurrentHeight(height)
    setWrapperHeight(expanded ? height : 0)
    setTimeout(() => {
      setTran(1)
    })
  }
}

代码覆盖率工具显示这段代码缺少测试覆盖。请添加适当的测试用例。

#!/bin/bash
# 检查是否有测试覆盖updateRectHeight函数
grep -r "updateRectHeight" --include="*.spec.tsx" --include="*.test.tsx" src/
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 77-81: src/packages/collapseitem/collapseitem.tsx#L77-L81
Added lines #L77 - L81 were not covered by tests


85-87: 优化useEffect依赖项

合理地设置了childrenexpanded作为依赖项,这样当内容或展开状态变化时都能重新计算高度。


130-134: 重构内容元素的DOM结构

调整了内容元素的DOM结构,将ref属性移动到了内部的div元素上,并添加了新的CSS类名。这种结构调整有助于更精确地获取实际内容的高度。

Comment on lines +117 to +127
className={classNames({
[`${classPrefix}-content-wrapper`]: true,
[`${classPrefix}-content-wrapper-tran`]: true,
})}
style={
tran
? {
height: wrapperHeight,
}
: {}
}
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

❓ Verification inconclusive

优化内容包装器的样式和结构

通过条件样式和类名的应用,改进了内容包装器的结构和样式处理。根据tran状态条件性地应用高度样式,使过渡动画更加流畅。

代码覆盖率工具显示这段样式逻辑缺少测试覆盖。请添加测试用例验证样式的正确应用。


🏁 Script executed:

#!/bin/bash
# 查找测试中是否有验证style属性的逻辑
grep -r "style" --include="*collapseitem*.test.tsx" --include="*collapseitem*.spec.tsx" src/

Length of output: 92


建议添加针对样式逻辑的单元测试

覆盖率工具提示这段逻辑目前缺乏测试用例。请在 collapseitem 的测试文件(如 collapseitem.test.tsxcollapseitem.spec.tsx)中添加测试用例,验证以下情况:

  • trantrue 时,组件应正确应用内联样式 { height: wrapperHeight }
  • tranfalse 时,组件不应设置内联样式(或样式为空对象)。

这样可以确保样式和过渡效果按照预期工作。

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 123-125: src/packages/collapseitem/collapseitem.tsx#L123-L125
Added lines #L123 - L125 were not covered by tests

@xiaoyatong
Copy link
Collaborator

单测覆盖率 看一下

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/packages/collapseitem/collapseitem.tsx (1)

74-81: ⚠️ Potential issue

实现高度计算逻辑需要添加测试覆盖

updateRectHeight 函数通过读取 DOM 元素的实际高度来更新状态,确保动态内容能够正确展示。但是这部分代码缺少测试覆盖,之前的审查已经指出这个问题。

另外,第 78-80 行中使用了不带延迟参数的 setTimeout,这可能导致行为不一致。建议添加一个小的延迟值(如 10ms)以确保 DOM 更新完成。

建议修改为:

- setTimeout(() => {
-   setTran(1)
- })
+ setTimeout(() => {
+   setTran(1)
+ }, 10)
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2fc1a55 and a0260d7.

📒 Files selected for processing (1)
  • src/packages/collapseitem/collapseitem.tsx (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (8)
src/packages/collapseitem/collapseitem.tsx (8)

5-8: 导入必要的 React 钩子和自定义钩子

代码正确地引入了实现折叠功能所需的钩子,包括 useEffectuseRefuseState 和自定义的 useRefState 钩子,为组件提供了必要的状态管理和 DOM 引用能力。

Also applies to: 13-13


70-72: 添加了必要的状态变量以支持动画过渡

添加了三个状态变量来实现平滑的高度过渡:

  • tran: 控制过渡动画的触发
  • currentHeight: 使用自定义钩子 useRefState 保存内容的实际高度
  • wrapperHeight: 控制包装器的当前高度

这些状态变量的设计很合理,特别是使用 useRefState 确保在异步回调中能访问到最新的高度值。


83-85: 使用 useEffect 监听关键状态变化

这个 useEffect 钩子很好地监听了 childrenexpanded 的变化,确保在内容或展开状态变化时重新计算高度。这对于动态内容的正确显示是必要的。


87-90: 优化 toggle 函数

toggle 函数现在使用 currentHeight.current 来获取最新的高度值,这是一个很好的改进。这确保了即使在异步操作中也能获取到准确的高度信息。


91-96: 简化的 handleClick 函数实现

handleClick 函数现在简洁明了,先更新折叠状态,然后调用 toggle 函数来设置高度。这种分离关注点的做法使代码更易于维护。


101-103: 优化标题区域的类名设置

使用 classNames 工具来有条件地添加类名是一个很好的做法,特别是对于禁用状态的处理。


115-125: 优化内容包装器的样式和结构需要测试覆盖

通过条件样式和类名的应用,改进了内容包装器的结构和样式处理。根据 tran 状态条件性地应用高度样式,使过渡动画更加流畅。

然而,代码覆盖率工具显示这段样式逻辑缺少测试覆盖。这个问题在之前的审查中已经指出,但尚未解决。


128-131: 改进内容区域的 DOM 结构

新的嵌套结构将内容文本与容器分离,并使用 ref 引用内容元素,这有助于准确计算高度。这种结构改进支持了动态高度计算和平滑过渡效果。

Comment on lines +74 to +85
const updateRectHeight = async () => {
const height = contentRef.current.offsetHeight
setCurrentHeight(height)
setWrapperHeight(expanded ? height : 0)
setTimeout(() => {
setTran(1)
})
}

const getOffsetHeight = () => {
const height = contentRef.current?.offsetHeight
return height ? `${height}px` : ''
}
useEffect(() => {
updateRectHeight()
}, [children, expanded])
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

建议添加完整的单元测试覆盖

我发现重要的高度计算逻辑(updateRectHeight 函数)和样式应用逻辑(条件样式)缺少测试覆盖。建议添加以下测试用例:

  1. 对于 updateRectHeight 函数:

    • 测试当 DOM 元素存在时,高度计算是否正确
    • 测试 expanded 状态变化时,wrapperHeight 是否正确更新
    • 使用 Jest 的 fake timers 测试 setTimeout 异步逻辑
  2. 对于样式应用逻辑:

    • 测试当 trantrue 时,内容包装器是否应用了正确的高度样式
    • 测试当 tranfalse 时,是否没有应用高度样式

这些测试将确保组件在不同设备(包括鸿蒙系统)上的稳定性和一致性。

Also applies to: 115-125


🏁 Script executed:

#!/bin/bash
# 检查是否有测试覆盖 updateRectHeight 函数和样式逻辑
echo "查找与 updateRectHeight 和样式相关的测试:"
grep -r "updateRectHeight\|style" --include="*collapse*.test.tsx" --include="*collapse*.spec.tsx" src/
echo "------------------------"
echo "检查折叠组件测试文件结构:"
find src/packages -name "*collapse*.test.tsx" -o -name "*collapse*.spec.tsx" | xargs cat

Length of output: 7053


建议增加针对 updateRectHeight 与样式逻辑的单元测试

目前已有测试用例覆盖了折叠组件的整体行为和部分样式校验,但对以下细节仍缺乏直接验证:

  • updateRectHeight 函数

    • 测试 DOM 元素存在时,能否正确获取 offsetHeight 并更新 currentHeightwrapperHeight
    • 模拟 expanded 状态切换时,验证 wrapperHeight 是否根据状态正确设置
    • 使用 Jest 的 fake timers 检查 setTimeout 异步调用是否按预期触发 setTran(1)
  • 样式应用逻辑

    • 测试 tran 为真时,内容包装器是否正确应用了高度样式
    • 测试 tran 为假时,不应应用不必要的高度样式

建议针对上述逻辑补充相应的单元测试(包括独立测试 updateRectHeight 和样式状态变化),以确保不同设备(例如鸿蒙系统)下组件的稳定性与一致性。

@xiaoyatong xiaoyatong merged commit e392273 into jdf2e:feat_v3.x Apr 2, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3.x Target branch 3.x action:review This PR needs more reviews (less than 2 approvals) size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants