-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(x-markdown): add componentsProps to pass extra props to custom components #1993
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
Div627
wants to merge
1
commit into
main
Choose a base branch
from
feat/x-markdown-components-props
base: main
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 all commits
Commits
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
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
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 |
|---|---|---|
|
|
@@ -31,9 +31,33 @@ import { Mermaid, Think, XMarkdown } from '@ant-design/x'; | |
| | children | 包裹在组件中的内容,包含 DOM 节点的文本内容 | `React.ReactNode` | - | | ||
| | rest | 组件属性,支持所有标准 HTML 属性(如 `href`、`title`、`className` 等)和自定义数据属性 | `Record<string, any>` | - | | ||
|
|
||
| ## 传递额外的 props | ||
|
|
||
| 自定义组件常常需要接收业务数据(如主题、回调函数等)。如果通过内联函数传递,每次渲染都会产生新的组件引用,导致组件被反复卸载重建,在流式场景下会丢失内部状态并造成明显的性能损耗。使用 `componentsProps` 可以在保持组件引用稳定的同时传入额外的 props: | ||
|
|
||
| ```tsx | ||
| import React from 'react'; | ||
| import { XMarkdown } from '@ant-design/x'; | ||
|
|
||
| // ❌ 内联函数:每次渲染都是新组件类型,子树整体重建 | ||
| <XMarkdown | ||
| components={{ | ||
| 'custom-chart': (props) => <CustomChart {...props} theme={theme} onSelect={onSelect} />, | ||
| }} | ||
| />; | ||
|
|
||
| // ✅ 组件引用稳定,额外数据通过 componentsProps 传入 | ||
| <XMarkdown | ||
| components={{ 'custom-chart': CustomChart }} | ||
| componentsProps={{ 'custom-chart': { theme, onSelect } }} | ||
| />; | ||
| ``` | ||
|
|
||
| `componentsProps` 以标签名为 key,对应的 props 会与解析出的 HTML 属性合并后传给组件(同名时 `componentsProps` 优先)。`componentsProps` 变化时组件只会正常更新 props,不会被重新挂载。 | ||
|
Contributor
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. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 同步修正文档中的 props 优先级说明。 Renderer 中
📍 Affects 2 files
🤖 Prompt for AI Agents |
||
|
|
||
| ## 最佳实践 | ||
|
|
||
| 1. 保持组件引用稳定,避免在 `components` 中写内联函数组件。 | ||
| 1. 保持组件引用稳定,避免在 `components` 中写内联函数组件;需要传递额外数据时使用 `componentsProps`。 | ||
| 2. 使用 `streamStatus` 区分加载态(`loading`)和完成态(`done`)。 | ||
| 3. 依赖完整语法的数据解析,尽量在 `streamStatus === 'done'` 后执行。 | ||
| 4. 自定义标签命名尽量语义化,减少 Markdown 与 HTML 混写歧义。 | ||
|
|
||
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
Oops, something went wrong.
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.
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.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
修正
componentsProps的优先级顺序。当前实现允许额外 props 覆盖内部生成的
domNode和streamStatus;同时,componentsProps.className会与 HTML 的class拼接而非覆盖。应先合并解析属性与额外 props,再写入内部字段,并让额外 class 属性覆盖解析出的 class。补充这些冲突场景的测试。🤖 Prompt for AI Agents