Skip to content
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

feat(avatar): rn 适配 #2343

Merged
merged 21 commits into from
Jun 19, 2024
Merged

feat(avatar): rn 适配 #2343

merged 21 commits into from
Jun 19, 2024

Conversation

xiaoyatong
Copy link
Collaborator

@xiaoyatong xiaoyatong commented Jun 12, 2024

🤔 这个变动的性质是?

  • 新特性提交
  • 日常 bug 修复
  • 站点、文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • TypeScript 定义更新
  • 包体积优化
  • 性能优化
  • 功能增强
  • 国际化改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他改动(是关于什么的改动?)

🔗 相关 Issue

💡 需求背景和解决方案

☑️ 请求合并前的自查清单

⚠️ 请自检并全部勾选全部选项⚠️

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充
  • fork仓库代码是否为最新避免文件冲突
  • Files changed 没有 package.json lock 等无关文件

Summary by CodeRabbit

  • 新功能

    • 为应用添加了头像(Avatar)和头像组(AvatarGroup)组件。
  • 更新

    • 更新了 config.json 文件,将 Avatar 和 AvatarGroup 组件的版本号从 "2.0.0" 增加到 "3.0.0"。
  • 功能改进

    • 在 Avatar 组件中添加了 isFirst 属性,增强了组件的渲染逻辑。
    • 修改了 AvatarGroup 组件,改进了子元素的迭代和条件克隆逻辑。

@xiaoyatong xiaoyatong marked this pull request as draft June 12, 2024 01:38
@xiaoyatong xiaoyatong marked this pull request as ready for review June 17, 2024 09:03
Copy link

coderabbitai bot commented Jun 17, 2024

Walkthrough:

这次更新主要涉及到三个方面:首先,添加了'avatar''avatargroup'adapted.js的导出列表中。其次,更新了config.json中的相应组件版本号从"2.0.0"提高到“3.0.0”。最后,在代码实现上,对AvatarAvatarGroup组件进行了一些功能增强和优化,包括添加新的属性和修改渲染逻辑。

Changes:

文件路径 变更摘要
packages/nutui-taro-demo-rn/scripts/taro/adapted.js 添加'avatar''avatargroup'到导出列表。
src/config.json 更新AvatarAvatarGroup组件的版本号,从"2.0.0"提高到“3.0.0”。
src/packages/avatar/avatar.taro.tsx - 添加isFirst属性到AvatarProps
- 导入pxTransform
- 更新Avatar组件逻辑处理isFirst属性。
- 修改图像渲染逻辑。
src/packages/avatargroup/avatargroup.taro.tsx - 修改AvatarGroup组件,遍历子元素并根据类型克隆特定元素。

Poem:

在代码的光辉中舞蹈,
头像和组群齐步走,
新版本号闪闪发,
更新逻辑如水流。
编译通过心头喜,
未来功能更卓越!
🌟🐰✨


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9771aaa and 9a1c7ce.

Files selected for processing (4)
  • packages/nutui-taro-demo-rn/scripts/taro/adapted.js (1 hunks)
  • src/config.json (2 hunks)
  • src/packages/avatar/avatar.taro.tsx (7 hunks)
  • src/packages/avatargroup/avatargroup.taro.tsx (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • src/config.json
  • src/packages/avatargroup/avatargroup.taro.tsx
Additional context used
Biome
packages/nutui-taro-demo-rn/scripts/taro/adapted.js

[error] 1-1: A global variable should not be reassigned. (lint/suspicious/noGlobalAssign)

Assigning to a global variable can override essential functionality.

src/packages/avatar/avatar.taro.tsx

[error] 131-135: Change to an optional chain. (lint/complexity/useOptionalChain)

Unsafe fix: Change to an optional chain.

Additional comments not posted (3)
packages/nutui-taro-demo-rn/scripts/taro/adapted.js (1)

29-30: 添加 avataravatargroup 到导出列表是恰当的,确保了这些组件可以被外部引用。

src/packages/avatar/avatar.taro.tsx (2)

9-9: 引入 pxTransformAvatarContext 是适当的,以支持更复杂的样式和状态管理需求。

Also applies to: 16-16


26-26: 组件逻辑有较大改动,建议进行彻底测试,确保新逻辑不会引入错误。特别是处理 avatarIndexgroupMax 的逻辑,需要确保它们在不同条件下都能正确工作。

Also applies to: 35-42, 58-58, 73-76, 79-82, 85-87, 91-95, 101-101, 112-120, 136-173, 178-183


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:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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

Outside diff range and nitpick comments (4)
scripts/rn/copy-file.js (1)

Line range hint 41-73: 此函数表达式可以转换为箭头函数,以提高代码的简洁性和现代性。

- const copyFile = async (from, to, success, isSingle = false) => {
+ const copyFile = async (from, to, success, isSingle = false) => {
src/packages/avatar/avatar.tsx (1)

[!TIP]
Codebase Verification

建议在代码中使用可选链来简化逻辑,例如:

children[i].classList?.[0] === 'nut-avatar'

目前代码中没有使用可选链,建议进行修改。

  • src/packages/avatar/avatar.tsx 第 74-81 行
Analysis chain

Line range hint 114-115: 建议使用可选链来简化这里的代码逻辑。

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# 检查可选链的使用是否安全。
ast-grep --lang javascript --pattern $'$_?.propAvatarGroup?.maxBackground'

Length of output: 75



Script:

#!/bin/bash
# 查找在 src/packages/avatar/avatar.tsx 文件中是否使用了可选链(Optional Chaining)。
rg --multiline --multiline-dotall --glob "src/packages/avatar/avatar.tsx" -n --pcre2 "children\[\d+\]\.classList\?\.\[0\] === 'nut-avatar'"

Length of output: 145



Script:

#!/bin/bash
# 查找 src/packages/avatar/avatar.tsx 文件中第 74-81 行的内容,以确认是否使用了可选链(Optional Chaining)。
sed -n '74,81p' src/packages/avatar/avatar.tsx

Length of output: 401

src/packages/image/image.tsx (1)

Line range hint 223-235: 您需要为懒加载图片的点击事件添加键盘事件处理器,以确保键盘用户也能触发相关功能。这是无障碍访问的一部分要求。

+ onKeyUp={handleKeyUp}
+ onKeyDown={handleKeyDown}
+ onKeyPress={handleKeyPress}
src/packages/avatar/avatar.taro.tsx (1)

186-227: 在这一大段代码中,处理了不同条件下的头像渲染逻辑。建议在这部分代码中增加一些注释,说明每个条件分支的用途和逻辑,这样可以提高代码的可读性和可维护性。

Comment on lines +186 to +192
<div className={`${classPrefix}-error`}>
<ImageError />
</div>
)
}
if (React.isValidElement(error) && !innerLoading) {
return <div className="nut-img-error">{error}</div>
return <div className={`${classPrefix}-error`}>{error}</div>
Copy link

Choose a reason for hiding this comment

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

在错误图像的渲染函数中,建议使用可选链来提高代码的健壮性。目前的实现方式可能在某些情况下导致运行时错误。

- if (typeof error === 'boolean' && error === true && !innerLoading) {
+ if (typeof error === 'boolean' && error?.true && !innerLoading) {

Committable suggestion was skipped due to low confidence.

Tools
Biome

[error] 186-186: Change to an optional chain. (lint/complexity/useOptionalChain)

Unsafe fix: Change to an optional chain.

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: 2

Comment on lines +29 to +30
'avatar',
'avatargroup',
Copy link

Choose a reason for hiding this comment

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

全局变量应避免重新赋值。

- exports = module.exports = [
+ const exports = module.exports = [
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
'avatar',
'avatargroup',
const exports = module.exports = [
'avatar',
'avatargroup',

Comment on lines 116 to 118
// @todo children 在 rn 下 无法获取,同时会影响:
// 1)children 在group下无法展示触发max时的样式
// 2)无法处理层级关系
Copy link

Choose a reason for hiding this comment

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

存在对子元素无法获取的问题,这可能会影响样式触发和层级关系的处理。建议寻找替代方法来处理这些问题。

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: 2

Comment on lines +181 to +186
<View
className={`nut-avatar-text nut-avatar-${groupSize || 'normal'}-text`}
>
{parent?.propAvatarGroup?.maxContent
? parent?.propAvatarGroup?.maxContent
: `+ ${
avatarIndex - Number(parent?.propAvatarGroup?.max || 0)
}`}
: `+ ${avatarIndex - Number(groupMax || 0)}`}
Copy link

Choose a reason for hiding this comment

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

处理超过最大头像数时的渲染逻辑,建议简化这部分代码以提高可读性和维护性。

-          {showMax && (
-            <View
-              className={`nut-avatar-text nut-avatar-${groupSize || 'normal'}-text`}
-            >
-              {parent?.propAvatarGroup?.maxContent
-                ? parent?.propAvatarGroup?.maxContent
-                : `+ ${avatarIndex - Number(groupMax || 0)}`}
-            </View>
-          )}
+          {showMax && renderMaxAvatarContent()}

Committable suggestion was skipped due to low confidence.

Comment on lines +115 to +123
const maxCount = groupMax || groupCount
if (
index === children.length &&
index !== maxCount &&
children.length > maxCount
avatarIndex === groupCount &&
avatarIndex !== maxCount &&
groupCount > maxCount
) {
setShowMax(true)
}
}
}, [avatarIndex, groupCount])
Copy link

Choose a reason for hiding this comment

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

useEffect 中处理显示最大头像数的逻辑,确保在逻辑处理中使用可选链来避免潜在的运行时错误。

-    avatarIndex === groupCount &&
+    avatarIndex === groupCount?.value &&

Committable suggestion was skipped due to low confidence.

Copy link
Collaborator

Choose a reason for hiding this comment

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

需要删除 image 样式

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

web h5 还需要这个样式~

Copy link
Collaborator

Choose a reason for hiding this comment

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

需要删除 image 样式

@@ -23,7 +23,8 @@ export interface AvatarProps extends BasicComponent {
color: string
fit: 'contain' | 'cover' | 'fill' | 'none' | 'scale-down'
src: string
alt: string
isFirst: boolean
Copy link
Collaborator

Choose a reason for hiding this comment

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

属性会对外暴露,可以考虑 context 方式实现,隐藏这个属性

@@ -32,14 +33,15 @@ export type AvatarShape = 'round' | 'square'

const defaultProps = {
...ComponentDefaults,
size: '',
size: harmonyAndRn() ? '40' : '',
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里直接用 pxTransform 方式

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

这里是处理 非鸿蒙和rn下 size 为空时,走 normal 的样式~

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

Outside diff range and nitpick comments (2)
packages/nutui-taro-demo-rn/scripts/taro/adapted.js (1)

Line range hint 1-1: 全局变量应避免重新赋值。

- exports = module.exports = [
+ const exports = module.exports = [
src/packages/avatar/avatar.taro.tsx (1)

Line range hint 131-135: 建议使用可选链来增强代码的健壁性。

- avatarIndex === groupCount &&
+ avatarIndex === groupCount?.value &&

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants