-
Notifications
You must be signed in to change notification settings - Fork 140
refactor: 重构collapse为details实现 #362
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
Open
IVLIU
wants to merge
14
commits into
react-component:master
Choose a base branch
from
IVLIU:refactor/native-details-element
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3cfdc2e
feat: 重构collapse为details实现
IVLIU 5ffc99a
fix: 用例失败
IVLIU df7f09e
fix: 补全缺失的包
IVLIU ce3ba4c
feat: 阻止onItemClick事件冒泡
IVLIU ff26366
feat: 完善动画支持
IVLIU 925207f
feat: 将useEffect中状态设置改为派生状态
IVLIU 5026c13
fix: 移除rc-motion & 修复用例错误
IVLIU 9f5873f
Merge branch 'master' into refactor/native-details-element
IVLIU a834f41
feat: 支持view transition过渡
IVLIU 3bd0abf
Merge branch 'refactor/native-details-element' of github.com:IVLIU/co…
IVLIU 60430c7
feat: 阻止默认行为
IVLIU 4ddcf52
feat: 移除startTransition
IVLIU dba7f64
feat: 恢复CSSMotion调用
IVLIU 5ed0807
feat: 恢复PanelContent style使用
IVLIU File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ coverage | |
es | ||
yarn.lock | ||
package-lock.json | ||
pnpm-lock.yaml | ||
.storybook | ||
.doc | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,7 +5,7 @@ import React from 'react'; | |||||
import type { CollapsePanelProps } from './interface'; | ||||||
import PanelContent from './PanelContent'; | ||||||
|
||||||
const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((props, ref) => { | ||||||
const CollapsePanel = React.forwardRef<HTMLDetailsElement, CollapsePanelProps>((props, ref) => { | ||||||
const { | ||||||
showArrow = true, | ||||||
headerClass, | ||||||
|
@@ -33,8 +33,9 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop | |||||
const ifExtraExist = extra !== null && extra !== undefined && typeof extra !== 'boolean'; | ||||||
|
||||||
const collapsibleProps = { | ||||||
onClick: () => { | ||||||
onClick: (e: React.MouseEvent) => { | ||||||
onItemClick?.(panelKey); | ||||||
e.stopPropagation(); | ||||||
}, | ||||||
onKeyDown: (e: React.KeyboardEvent) => { | ||||||
if (e.key === 'Enter' || e.keyCode === KeyCode.ENTER || e.which === KeyCode.ENTER) { | ||||||
|
@@ -79,16 +80,16 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop | |||||
); | ||||||
|
||||||
// ======================== HeaderProps ======================== | ||||||
const headerProps: React.HTMLAttributes<HTMLDivElement> = { | ||||||
const headerProps: React.HTMLAttributes<HTMLElement> = { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Since
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
className: headerClassName, | ||||||
style: styles?.header, | ||||||
...(['header', 'icon'].includes(collapsible) ? {} : collapsibleProps), | ||||||
}; | ||||||
|
||||||
// ======================== Render ======================== | ||||||
return ( | ||||||
<div {...resetProps} ref={ref} className={collapsePanelClassNames}> | ||||||
<div {...headerProps}> | ||||||
<details {...resetProps} ref={ref} className={collapsePanelClassNames} open={isActive}> | ||||||
<summary {...headerProps}> | ||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
{showArrow && iconNode} | ||||||
<span | ||||||
className={classNames(`${prefixCls}-title`, customizeClassNames?.title)} | ||||||
|
@@ -98,7 +99,7 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop | |||||
{header} | ||||||
</span> | ||||||
{ifExtraExist && <div className={`${prefixCls}-extra`}>{extra}</div>} | ||||||
</div> | ||||||
</summary> | ||||||
<CSSMotion | ||||||
visible={isActive} | ||||||
leavedClassName={`${prefixCls}-panel-hidden`} | ||||||
|
@@ -124,7 +125,7 @@ const CollapsePanel = React.forwardRef<HTMLDivElement, CollapsePanelProps>((prop | |||||
); | ||||||
}} | ||||||
</CSSMotion> | ||||||
</div> | ||||||
</details> | ||||||
); | ||||||
}); | ||||||
|
||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
阻止
<summary>
默认行为,避免双重状态切换点击位于
<summary>
内部的元素时,浏览器会先执行<details>
的默认展开 / 折叠,再执行 React 更新逻辑,导致「闪动」及状态竞争;尤其在受控模式下容易出现不一致。应同时
preventDefault
与stopPropagation
,并在之后触发onItemClick
。📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看看这个?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@afc163 CodeRabbit 提出的建议是正确的!这个问题很重要,因为在使用原生
<details>
元素的重构版本中:问题分析:
e.stopPropagation()
但缺少e.preventDefault()
<summary>
的默认行为是切换父级<details>
的open
属性open
状态preventDefault()
会导致原生切换和 React 状态更新同时发生,造成状态竞争和闪烁修复建议:
这个修复确保了:
这个修改对于从 div 实现迁移到原生
<details>
实现的稳定性至关重要。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已处理
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.