Skip to content

feat: support classnames and styles #378

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

Merged
merged 3 commits into from
Mar 5, 2025
Merged

Conversation

thinkasany
Copy link
Contributor

@thinkasany thinkasany commented Mar 3, 2025

Summary by CodeRabbit

  • 新功能
    • 为图片预览、操作面板和对话框组件增加了自定义类名与样式的支持,用户可根据需求灵活调整外观和布局。
  • 测试
    • 添加新的测试用例以验证自定义样式和类名的正确应用,确保各组件在定制化配置下稳定运行。

Copy link

coderabbitai bot commented Mar 3, 2025

Walkthrough

此次提交在 Image、Operations 和 Preview 组件中新增了可选的 classNamesstyles 属性,允许开发者自定义组件中各部分的类名和内联样式。为此,引入了 SemanticName 类型,其定义了三个可能的字符串:'root''actions''mask'。这些属性在组件内部通过解构传入并用于条件渲染,同时也往下传递到子组件中。测试用例也相应新增,用以验证自定义样式和类名是否能正确应用。

Changes

文件(s) 变更描述
src/Image.tsx, src/Operations.tsx, src/Preview.tsx 在各自的接口中新增可选属性 classNamesstyles(类型均为 Partial<Record<SemanticName, string>>Partial<Record<SemanticName, React.CSSProperties>>),并定义了 SemanticName'root''actions''mask')。组件内部相应调整了解构和属性传递逻辑。
tests/preview.test.tsx 新增测试用例,验证 Image 组件预览功能中自定义类名与样式能否正确应用于 mask、actions 与 root 等元素。

Sequence Diagram(s)

sequenceDiagram
    participant Parent as 父组件
    participant ImageInternal as ImageInternal 组件
    participant Preview as Preview 组件
    participant Dialog as Dialog 组件
    participant PreviewImage as PreviewImage 组件

    Parent->>ImageInternal: 传递包含自定义 classNames/styles 的预览属性
    ImageInternal->>Preview: 转发 classNames/styles 属性
    Preview->>Dialog: 将属性传递给 Dialog 用于定制化
    Preview->>PreviewImage: 将属性传递给 PreviewImage 用于定制化
Loading
sequenceDiagram
    participant Parent as 父组件
    participant Operations as Operations 组件
    participant Toolbar as 工具栏元素

    Parent->>Operations: 传递包含自定义 classNames/styles 的属性
    Operations->>Toolbar: 应用 classNames/styles 到工具栏的相关元素
Loading

Possibly related PRs

  • react-component/image#376: 此 PR 与当前提交在代码层面均引入了 classNamesstyles 属性,旨在增强组件的样式与类名自定义能力。

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


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between bea7e8b and 2d81785.

📒 Files selected for processing (2)
  • src/Image.tsx (5 hunks)
  • src/Preview.tsx (8 hunks)
🔇 Additional comments (13)
src/Image.tsx (5)

38-39: 类型定义清晰明确

在接口中添加这两个可选属性使组件样式更加灵活,符合组件定制化的需求。类型定义使用Partial<Record<>> 很恰当,允许用户只指定需要的部分样式。


54-54: 语义化类型定义合理

定义 SemanticName 类型明确了可以自定义样式的元素范围,提高了类型安全性,同时也方便了开发者理解可以定制的部分。


110-111: 属性解构命名良好

classNames 重命名为 imageClassNames 很好地避免了与内部变量名冲突,提高了代码可读性。


230-234: 样式应用实现正确

正确使用了可选链操作符 ?. 来安全地访问 imageClassNames?.mask,防止了潜在的空引用错误。样式扩展也使用了正确的语法。


261-262: 组件属性传递完整

classNamesstyles 属性正确地传递给 Preview 组件,确保样式自定义能力在组件树中传递。

src/Preview.tsx (8)

7-7: 类型导入更新合理

从 './Image' 导入 SemanticName 类型,保持了类型定义的一致性,避免了重复定义。


45-45: 接口定义排除冲突属性

通过在 Omit 中排除 'styles' 和 'classNames',避免了与 IDialogPropTypes 中可能存在的同名属性冲突,是一个谨慎的设计决策。


85-89: 属性类型定义完善

新增的属性类型定义清晰,包含了详细的注释说明 wrapper 样式的用途,有助于代码维护和理解。


142-143: 属性解构命名一致

与 Image 组件保持一致,将 classNames 重命名为 imageClassNames,保持了代码风格的一致性。


274-276: 转换样式字符串优化

转换样式字符串中正确处理了 flipX 和 flipY 的情况,通过条件字符串连接实现反转效果,实现优雅。


308-310: 类名传递实现正确

在 Dialog 组件中正确应用了自定义类名,使用可选链安全地访问 imageClassNames?.mask


311-316: 样式传递完整

完整地将各种样式应用到不同的元素上,包括 mask、wrapper 和 root,实现了全面的样式自定义能力。


356-357: 属性向下传递正确

classNamesstyles 属性正确地传递给 Operations 组件,确保样式自定义能力在整个组件树中一致传递。

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
  • @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

vercel bot commented Mar 3, 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 Mar 5, 2025 6:34am

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 (5)
src/Operations.tsx (2)

7-7: 导入命名可能导致混淆,建议修改

这里同时导入了两个命名相似的模块:一个是用于类名组合的函数classnames,另一个是从./Image导入的类型classNames。虽然一个是函数一个是类型,但名称非常接近,可能导致开发者混淆。

建议将从./Image导入的类型改为更明确的命名,比如:

- import classNames from 'classnames';
+ import cn from 'classnames';

Also applies to: 11-11


99-100: 变量命名建议与属性名保持一致

属性名为classNames但解构为imageClassNames,可能会使代码阅读者困惑。

- classNames: imageClassNames,
+ classNames,
src/Image.tsx (1)

38-40: 类型定义清晰且可复用

SemanticName类型的定义很好,将其作为独立的导出类型有助于在其他组件中重用。对ImagePreviewType接口的扩展也很干净。

建议添加JSDoc注释说明这些属性的用途和用法,这将使API更加清晰。

+ /**
+  * 语义化元素名称,用于定义可自定义的样式目标
+  */
export type SemanticName = 'root' | 'actions' | 'mask' | 'wrapper';

export interface ImagePreviewType
  extends Omit<
    IDialogPropTypes,
    'mask' | 'visible' | 'closable' | 'prefixCls' | 'onClose' | 'afterClose' | 'wrapClassName'
  > {
  src?: string;
  visible?: boolean;
  minScale?: number;
  maxScale?: number;
  onVisibleChange?: (value: boolean, prevValue: boolean) => void;
  getContainer?: GetContainer | false;
  mask?: React.ReactNode;
  maskClassName?: string;
+ /**
+  * 自定义类名,按语义结构组织
+  */
  classNames?: Partial<Record<SemanticName, string>>;
+ /**
+  * 自定义样式,按语义结构组织
+  */
  styles?: Partial<Record<SemanticName, React.CSSProperties>>;

Also applies to: 54-54

src/Preview.tsx (2)

139-140: 变量命名可以更一致

与Operations组件类似,这里也将classNames解构为imageClassNames,建议保持命名一致性,直接使用classNames

- classNames: imageClassNames,
+ classNames,

227-227: 格式化问题

这里的右花括号单独一行,与项目的其他代码风格不一致。

-  };
+  }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a93f870 and bea7e8b.

📒 Files selected for processing (4)
  • src/Image.tsx (5 hunks)
  • src/Operations.tsx (4 hunks)
  • src/Preview.tsx (8 hunks)
  • tests/preview.test.tsx (1 hunks)
🔇 Additional comments (8)
tests/preview.test.tsx (1)

1033-1064: 测试用例补充得很全面!

这个测试用例很好地验证了新增的自定义类名和样式功能。对mask、actions和root三个关键元素都进行了测试。

几点建议:

  1. 可以考虑也测试一下wrapper语义名称的应用
  2. 建议添加对样式应用失败的边缘情况测试,例如使用不存在的语义名称时的行为
src/Operations.tsx (2)

65-66: 新增的类型定义良好!

使用Partial<Record<SemanticName, string>>Partial<Record<SemanticName, React.CSSProperties>>是很好的类型设计,允许用户只指定需要的部分样式和类名。


203-206: 样式和类名应用实现良好

很好地在classNames函数中使用了可选链操作符?.,确保即使imageClassNames为undefined也不会出错。对styles?.actions的应用也是如此。

src/Image.tsx (2)

110-111: props解构和应用实现良好

解构和条件应用classNames和styles的方式符合React最佳实践。使用了可选链操作符确保即使用户没有提供这些props也不会出错。

Also applies to: 230-234


261-263: 向子组件传递props的方式正确

将新增的props正确地传递给了Preview组件,这样保证了样式可以向下传递。

src/Preview.tsx (3)

7-7: 从Image导入SemanticName类型是个好决定

从Image导入SemanticName类型保证了类型定义的一致性,避免了重复定义可能导致的不一致。


45-45: 接口扩展使用Omit很合适

使用Omit<IDialogPropTypes, 'onClose' | 'styles' | 'classNames'>来扩展接口是正确的,避免了属性命名冲突。新增的classNamesstyles属性定义与其他组件保持一致。

Also applies to: 85-86


304-311: 样式和类名应用实现得很全面

对Dialog组件应用classNames和styles的方式很全面,分别处理了mask、wrapper和root元素。向Operations组件传递这些props也保证了样式可以继续向下传递。

Also applies to: 313-313, 353-354

Copy link

codecov bot commented Mar 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.79%. Comparing base (20afe8d) to head (2d81785).
Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #378   +/-   ##
=======================================
  Coverage   99.78%   99.79%           
=======================================
  Files          15       15           
  Lines         470      486   +16     
  Branches      134      139    +5     
=======================================
+ Hits          469      485   +16     
  Misses          1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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