-
-
Notifications
You must be signed in to change notification settings - Fork 106
feat: support classnames and styles #376
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
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Walkthrough此次变更在多个组件中引入了对类名(classNames)和样式(styles)的自定义支持。通过添加新的类型 Changes
Sequence Diagram(s)sequenceDiagram
participant U as 用户
participant P as Preview组件
participant O as Operations组件
participant I as ImageInternal组件
U->>P: 提供classNames和styles
P->>O: 转发classNames和styles
O->>I: 传递classNames和styles至内部组件
I-->>U: 渲染并应用自定义样式与类名
Poem
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
src/Operations.tsxOops! 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. src/Image.tsxOops! 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. src/Preview.tsxOops! 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.
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
🧹 Nitpick comments (7)
tests/basic.test.tsx (1)
100-118
: 测试用例覆盖了基本功能,但可以进一步增强测试用例验证了自定义类名和样式的应用,这很好。不过,建议添加以下测试场景:
- 验证
mask
元素的自定义样式- 测试样式和类名的动态更新
- 测试空值或无效值的处理
建议添加如下测试用例:
it('support classnames and styles', () => { const customClassnames = { actions: 'custom-actions', + mask: 'custom-mask', }; const customStyles = { actions: { backgroundColor: 'blue' }, + mask: { backgroundColor: 'rgba(0, 0, 0, 0.5)' }, }; const { baseElement } = render( <Image styles={customStyles} classNames={customClassnames} src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" - preview={{ zIndex: 9999, visible: true }} + preview={{ zIndex: 9999, visible: true, mask: 'Click to Preview' }} />, ); const actionsElement = baseElement.querySelector('.rc-image-preview-operations'); + const maskElement = baseElement.querySelector('.rc-image-mask'); expect(actionsElement).toHaveClass('custom-actions'); expect(actionsElement).toHaveStyle({ backgroundColor: 'blue' }); + expect(maskElement).toHaveClass('custom-mask'); + expect(maskElement).toHaveStyle({ backgroundColor: 'rgba(0, 0, 0, 0.5)' }); });src/Image.tsx (3)
52-52
: 为 SemanticName 类型添加 JSDoc 文档建议为新增的类型添加详细的文档说明,以提高代码的可维护性。
+/** + * 语义化名称类型,用于自定义样式 + * - actions: 操作栏样式 + * - mask: 遮罩层样式 + */ export type SemanticName = 'actions' | 'mask';
72-73
: 为新增的属性添加 JSDoc 文档建议为新增的属性添加详细的类型说明和使用示例。
+ /** + * 自定义类名配置 + * @example + * { + * actions: 'custom-actions', + * mask: 'custom-mask' + * } + */ classNames?: Partial<Record<SemanticName, string>>; + /** + * 自定义样式配置 + * @example + * { + * actions: { backgroundColor: 'blue' }, + * mask: { backgroundColor: 'rgba(0, 0, 0, 0.5)' } + * } + */ styles?: Partial<Record<SemanticName, React.CSSProperties>>;
230-234
: 样式合并逻辑正确,建议添加注释说明当前的样式合并逻辑正确处理了自定义样式和默认样式的关系。建议添加注释说明优先级。
<div className={cn(`${prefixCls}-mask`, maskClassName, imageClassNames?.mask)} style={{ + // 保持与原有显示逻辑一致,同时支持自定义样式覆盖 display: style?.display === 'none' ? 'none' : undefined, ...styles?.mask, }} >
src/Operations.tsx (2)
11-11
: 避免重复导入 classNames 工具文件中同时导入了
classnames
和classNames
,这是重复的。建议统一使用一个。-import classNames from 'classnames';
202-206
: 优化工具栏样式处理逻辑当前的样式处理逻辑可以进一步优化,建议提取样式计算逻辑。
+const getToolbarClassName = (prefixCls: string, customClassName?: string) => + classnames(`${prefixCls}-operations`, customClassName); const toolbarNode = ( <div - className={classNames(`${prefixCls}-operations`, imageClassNames?.actions)} + className={getToolbarClassName(prefixCls, imageClassNames?.actions)} style={styles?.actions} >src/Preview.tsx (1)
344-345
: 属性传递逻辑正确,建议添加类型断言虽然属性传递逻辑正确,但建议添加类型断言以确保类型安全。
- classNames={imageClassNames} - styles={styles} + classNames={imageClassNames as Partial<Record<SemanticName, string>>} + styles={styles as Partial<Record<SemanticName, React.CSSProperties>>}
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
src/Image.tsx
(5 hunks)src/Operations.tsx
(4 hunks)src/Preview.tsx
(2 hunks)tests/basic.test.tsx
(1 hunks)tests/preview.test.tsx
(1 hunks)
🔇 Additional comments (1)
tests/preview.test.tsx (1)
1033-1052
: 测试用例设计合理且完整!测试用例很好地验证了自定义类名和样式的功能:
- 验证了自定义类名 'custom-mask' 是否正确应用
- 验证了自定义样式 { color: 'red' } 是否正确应用
- 测试断言清晰明确
classNames: imageClassNames, | ||
styles, |
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.
🛠️ Refactor suggestion
完善 PreviewProps 接口的类型定义
当前的类型定义不够严格,建议使用与 Image 组件相同的类型定义。
- classNames: imageClassNames,
- styles,
+ classNames?: Partial<Record<SemanticName, string>>;
+ styles?: Partial<Record<SemanticName, React.CSSProperties>>;
📝 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.
classNames: imageClassNames, | |
styles, | |
classNames?: Partial<Record<SemanticName, string>>; | |
styles?: Partial<Record<SemanticName, React.CSSProperties>>; |
@@ -195,7 +200,10 @@ const Operations: React.FC<OperationsProps> = props => { | |||
}); | |||
|
|||
const toolbarNode = ( | |||
<div className={`${prefixCls}-operations`}> | |||
<div | |||
className={classNames(`${prefixCls}-operations`, imageClassNames?.actions)} |
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.
诶,actions 原来是 Preview 的?我还以为指的是 Image hover 是那个 👁 预览
……
Summary by CodeRabbit
新功能
测试