Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

thinkasany
Copy link
Contributor

@thinkasany thinkasany commented Feb 18, 2025

Summary by CodeRabbit

  • 新功能

    • 为图片展示、操作和预览界面增加了对自定义类名和样式的支持,使界面样式更加灵活可定制。
  • 测试

    • 增加了新的测试用例,确保自定义样式和类名能在各个组件中正确应用,从而提升整体稳定性与用户体验。

Copy link

vercel bot commented Feb 18, 2025

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

Name Status Preview Comments Updated (UTC)
image ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 18, 2025 9:43am

Copy link

coderabbitai bot commented Feb 18, 2025

Walkthrough

此次变更在多个组件中引入了对类名(classNames)和样式(styles)的自定义支持。通过添加新的类型 SemanticName(定义为 'actions''mask'),更新了 ImagePropsOperationsPropsPreviewProps 接口,同时调整了相关组件(如 ImageInternal 和工具栏节点)的属性处理逻辑。新增的测试用例确保了这些自定义属性能够正确应用于预览和遮罩。

Changes

文件 变更概要
src/Image.tsx, src/Operations.tsx,
src/Preview.tsx
引入新类型 SemanticName;在接口中增加了 classNamesstyles 属性;组件内部相应调整以支持传递与应用这些自定义类名和样式。
tests/basic.test.tsx,
tests/preview.test.tsx
新增测试用例,验证组件正确处理并渲染自定义的类名和样式。

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: 渲染并应用自定义样式与类名
Loading

Poem

我是一只跳跃的小兔子,
在代码林间欢歌起舞,
新增的classNames与styles,
像胡萝卜般散发着甜蜜魅力,
每行代码都闪耀着光芒,
为组件增添一抹灵动的春意! 🐇🌸

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.

src/Operations.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.

src/Image.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.

src/Preview.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.

  • 2 others

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?

❤️ 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.
    • 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. (Beta)
  • @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: 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 工具

文件中同时导入了 classnamesclassNames,这是重复的。建议统一使用一个。

-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

📥 Commits

Reviewing files that changed from the base of the PR and between 20afe8d and d438e6d.

📒 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' } 是否正确应用
  • 测试断言清晰明确

Comment on lines +137 to +138
classNames: imageClassNames,
styles,
Copy link

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.

Suggested change
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)}
Copy link
Member

Choose a reason for hiding this comment

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

诶,actions 原来是 Preview 的?我还以为指的是 Image hover 是那个 👁 预览 ……

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