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(Infiniteloading): 多端适配 #2670

Merged
merged 2 commits into from
Oct 30, 2024
Merged

feat(Infiniteloading): 多端适配 #2670

merged 2 commits into from
Oct 30, 2024

Conversation

Alex-huxiyang
Copy link
Collaborator

@Alex-huxiyang Alex-huxiyang commented Oct 29, 2024

  • 适配鸿蒙
  • 简化代码

Summary by CodeRabbit

  • 新功能

    • 更新多个组件到版本 3.0.0,包括 InfiniteLoadingNotifyPopup 等。
    • 新增 TextArea 组件,版本为 3.0.0
    • 引入 getBottomTipsText 函数,简化底部提示文本的逻辑。
  • 样式

    • 使用 pxTransform 更新样式,增强响应式设计。
  • Bug 修复

    • 修正 InfiniteLoading 组件样式设置,确保视觉表现一致。

Copy link

coderabbitai bot commented Oct 29, 2024

Walkthrough

此次更改主要涉及src/config.json文件的版本更新,多个组件的版本均从2.0.0更新至3.0.0,包括InfiniteLoadingNotifyPopup等。此外,新增了TextArea组件,并标记了一些组件为exportEmpty: true。在src/packages/infiniteloading/demos/taro目录下的多个demo文件中,HTML元素被替换为Taro组件,样式也进行了调整,确保与Taro框架的兼容性。InfiniteLoading组件的样式处理进行了小幅修改,新增了一个用于确定底部提示文本的函数。

Changes

文件路径 更改摘要
src/config.json 更新多个组件版本至3.0.0,新增TextArea组件,标记TabbarItem等组件为exportEmpty: true
src/packages/infiniteloading/demos/taro/demo1.tsx ulli元素替换为Taro的View组件,更新样式使用pxTransform
src/packages/infiniteloading/demos/taro/demo2.tsx 替换HTML元素为Taro组件,调整样式为动态值,使用pxTransform
src/packages/infiniteloading/demos/taro/demo3.tsx 替换HTML元素为Taro组件,更新样式为动态值,使用pxTransform
src/packages/infiniteloading/demos/taro/demo4.tsx 替换HTML元素为Taro组件,更新样式为动态值,使用pxTransform
src/packages/infiniteloading/infiniteloading.taro.tsx 修改getStyle函数的样式处理,可能导致运行时错误。
src/packages/infiniteloading/infiniteloading.tsx 新增getBottomTipsText函数,简化底部提示文本的渲染逻辑。

Possibly related PRs

Suggested labels

size/XXL

Suggested reviewers

  • oasis-cloud

兔子欢跳庆新变,
组件更新乐无边。
Taro组件齐上阵,
样式灵动如春天。
版本跃升三零零,
兔子祝福乐无穷! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 2cde759 and 7ea6306.

📒 Files selected for processing (1)
  • src/packages/infiniteloading/infiniteloading.taro.tsx (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/packages/infiniteloading/infiniteloading.taro.tsx

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

🧹 Outside diff range and nitpick comments (8)
src/packages/infiniteloading/demos/taro/demo3.tsx (2)

11-11: 样式单位转换正确,建议进一步优化!

使用pxTransform替换固定像素值的改动很好,这确保了在不同设备上的适配性。不过建议将这些值抽取为常量配置。

建议将这些值提取到一个配置对象中:

const STYLE_CONSTANTS = {
  CONTAINER_HEIGHT: 200,
  ITEM_MARGIN_TOP: 10,
  ITEM_FONT_SIZE: 14,
}

// 然后在样式中使用:
const InfiniteUlStyle: CSSProperties = {
  height: pxTransform(STYLE_CONSTANTS.CONTAINER_HEIGHT),
  // ...
}

Also applies to: 19-20


Line range hint 39-52: 建议增强错误处理机制!

customLoadMore函数的实现基本合理,但缺少错误处理机制。在生产环境中,网络请求可能失败,建议添加try-catch处理。

建议按如下方式增加错误处理:

 const customLoadMore = async () => {
+  try {
     await sleep(2000)
     const curLen = customList.length
     for (let i = curLen; i < curLen + 10; i++) {
       customList.push(`${i}`)
     }
     if (customList.length >= 30) {
       setCustomHasMore(false)
     } else {
       setCustomList([...customList])
     }
+  } catch (error) {
+    console.error('加载更多数据失败:', error)
+    // 考虑添加错误状态处理和用户提示
+  }
 }
src/packages/infiniteloading/demos/taro/demo4.tsx (3)

57-57: 图片尺寸处理建议

建议将图片尺寸也提取为常量,并考虑添加图片加载失败的处理。

建议修改如下:

+ const ICON_SIZE = 24;

<img
  alt="loading"
- style={{ height: pxTransform(24), width: pxTransform(24) }}
+ style={{ height: pxTransform(ICON_SIZE), width: pxTransform(ICON_SIZE) }}
+ onError={(e) => {
+   console.warn('Loading icon failed to load:', e);
+ }}
  src="https://img13.360buyimg.com/imagetools/jfs/t1/235005/5/15288/348/65fabd46F80f7367e/09fb5d99d07bee66.png"
  className="nut-infinite-bottom-tips-icons"
/>

67-67: 列表结构优化建议

使用 Taro 的 View 组件替换原生 HTML 元素是正确的,但建议添加一些无障碍支持。

建议添加如下属性:

- <View id="primaryScroll" style={InfiniteUlStyle}>
+ <View 
+   id="primaryScroll" 
+   style={InfiniteUlStyle}
+   role="list"
+ >
  <InfiniteLoading>
    {customList.map((item, index) => {
      return (
-       <View key={index} style={InfiniteLiStyle}>
+       <View 
+         key={index} 
+         style={InfiniteLiStyle}
+         role="listitem"
+       >
          {item}
        </View>
      )
    })}
  </InfiniteLoading>
</View>

Also applies to: 83-85, 89-89


Line range hint 67-89: 性能优化建议

当列表项较多时,建议考虑以下优化措施:

  1. 使用虚拟列表优化渲染性能
  2. 优化重复渲染

建议:

  1. 考虑引入 react-virtualized 或类似库处理长列表
  2. 使用 React.memo 优化列表项组件
  3. 将列表项组件抽离为单独的组件

示例代码:

const ListItem = React.memo(({ item }: { item: string }) => (
  <View 
    style={InfiniteLiStyle}
    role="listitem"
  >
    {item}
  </View>
));

// 在渲染时使用
{customList.map((item, index) => (
  <ListItem key={index} item={item} />
))}
src/packages/infiniteloading/demos/taro/demo1.tsx (1)

11-11: 样式单位转换优化建议

使用 pxTransform 处理样式单位是正确的方向,但建议将其他固定单位的样式也进行转换,比如 padding。

建议修改如下:

 const InfiniteUlStyle: CSSProperties = {
   height: pxTransform(200),
   width: '100%',
-  padding: '0',
+  padding: pxTransform(0),
   overflowY: 'auto',
   overflowX: 'hidden',
 }

Also applies to: 19-20

src/packages/infiniteloading/demos/taro/demo2.tsx (1)

11-11: 样式单位转换处理得当!

使用 pxTransform 替换固定像素值是正确的做法,这样可以确保在不同设备上呈现一致的显示效果。

建议在样式对象上添加注释,说明这些值在不同端的实际渲染效果,方便其他开发者理解和维护。例如:

const InfiniteUlStyle: CSSProperties = {
  // 200px 在不同端的转换规则:
  // - H5: 200px
  // - 小程序: 200rpx
  // - HarmonyOS: 200vp
  height: pxTransform(200),
  // ...
}

Also applies to: 19-20

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

216-224: 代码重构改进了可维护性

新增的 getBottomTipsText 函数很好地封装了底部提示文本的逻辑,使代码更易于维护和测试。函数的条件判断清晰,并且正确处理了国际化文本。

建议添加以下改进:

 function getBottomTipsText() {
+  // 使用对象映射替代if条件判断
+  const textMap = {
+    infiniting: loadingText || locale.infiniteloading.loadText,
+    noMore: loadMoreText || locale.infiniteloading.loadMoreText,
+  }
+  
+  if (isInfiniting) return textMap.infiniting
+  if (!hasMore) return textMap.noMore
+  return null
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 0bf15d2 and 2cde759.

📒 Files selected for processing (7)
  • src/config.json (1 hunks)
  • src/packages/infiniteloading/demos/taro/demo1.tsx (4 hunks)
  • src/packages/infiniteloading/demos/taro/demo2.tsx (4 hunks)
  • src/packages/infiniteloading/demos/taro/demo3.tsx (3 hunks)
  • src/packages/infiniteloading/demos/taro/demo4.tsx (4 hunks)
  • src/packages/infiniteloading/infiniteloading.taro.tsx (1 hunks)
  • src/packages/infiniteloading/infiniteloading.tsx (2 hunks)
🔇 Additional comments (11)
src/packages/infiniteloading/demos/taro/demo3.tsx (2)

3-3: 导入更改符合多端适配要求!

从HTML元素切换到Taro组件的View是正确的改动,这确保了组件在不同平台上的一致性。


55-55: DOM结构调整合理,但需注意可访问性!

将ul/li替换为View组件是跨平台所需,但这样可能会影响页面的语义化结构。建议考虑添加适当的ARIA属性来提升可访问性。

建议添加以下属性来增强可访问性:

-<View id="customScroll" style={InfiniteUlStyle}>
+<View 
+  id="customScroll" 
+  style={InfiniteUlStyle}
+  role="list"
+>
   <InfiniteLoading>
     {customList.map((item, index) => (
-      <View key={index} style={InfiniteLiStyle}>
+      <View 
+        key={index} 
+        style={InfiniteLiStyle}
+        role="listitem"
+      >

Also applies to: 65-65, 67-67, 71-71

src/packages/infiniteloading/demos/taro/demo4.tsx (1)

3-4: 导入Taro组件以提升多端兼容性

使用 @tarojs/components 中的 View 组件替换原生HTML元素是正确的做法,这样可以确保在不同端上的一致性表现。

src/packages/infiniteloading/demos/taro/demo1.tsx (3)

3-4: 导入变更符合多端适配要求

引入 @tarojs/componentsView 组件和 pxTransform 工具函数的变更,符合 Taro 框架的最佳实践,有助于确保跨平台兼容性。


56-56: 图片尺寸响应式处理正确

使用 pxTransform 处理图片尺寸,确保了在不同设备上的一致性表现。


66-66: 结构调整符合 Taro 规范

将 HTML 原生标签替换为 Taro 的 View 组件是正确的改动,这样能确保在各端表现一致。建议检查一下滚动行为在各端是否仍然正常。

执行以下脚本检查是否还有遗漏的 HTML 标签:

Also applies to: 92-94, 98-98

✅ Verification successful

Let me generate additional scripts to check the Taro demo files specifically to verify if all HTML tags have been properly replaced:


Taro 组件替换验证完成,代码符合规范

经过验证,所有 Taro demo 文件中的 HTML 原生标签(ul、li)都已正确替换为 Taro 的 View 组件。滚动容器的基本结构保持不变,只是将标签进行了替换,不会影响滚动行为。具体验证结果:

  • demo1.tsx 中正确使用了 View 组件替代 ul/li
  • 其他 demo 文件(demo2-4)也都完成了相应替换
  • 已正确引入 View 组件:import { View } from '@tarojs/components'
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 检查是否还有原生 HTML 标签未替换
rg -g '*.tsx' -g '*.jsx' '<(ul|li)\b' src/packages/infiniteloading/

Length of output: 1584


Script:

#!/bin/bash
# Check all Taro demo files for HTML tags and View components
echo "=== Checking Taro demo files ==="
rg -g '*.tsx' '<(ul|li|View)\b' src/packages/infiniteloading/demos/taro/

# Check the specific demo file structure
echo -e "\n=== Checking demo1.tsx structure ==="
cat src/packages/infiniteloading/demos/taro/demo1.tsx

Length of output: 3657

src/packages/infiniteloading/demos/taro/demo2.tsx (1)

3-4: 导入更改符合跨平台最佳实践!

使用 Taro 的 View 组件替代原生 HTML 元素,同时引入 pxTransform 工具函数来处理样式单位转换,这些更改都很好地支持了多端适配的目标。

src/packages/infiniteloading/infiniteloading.taro.tsx (1)

Line range hint 1-199: 建议验证鸿蒙系统的兼容性

根据 PR 目标,此次更改旨在支持鸿蒙系统。建议验证以下几个关键功能在鸿蒙系统上的表现:

  1. 下拉刷新动画是否流畅
  2. 滚动加载是否正常
  3. 触摸事件的响应是否准确
✅ Verification successful

Based on the search results, I can see that:

  1. The codebase has specific harmony-related CSS files (infiniteloading.harmony.css) for the InfiniteLoading component
  2. There are platform detection utilities (platform-taro.ts) that include harmony-specific checks
  3. The component has been adapted for harmony platform with specific styles and platform checks

Let me verify if there are any harmony-specific platform adaptations in the component's implementation:


让我们再检查一下组件的具体实现细节,特别是与滚动和触摸事件相关的处理:


无需担心鸿蒙系统兼容性问题

基于代码分析,InfiniteLoading组件已经具备良好的鸿蒙系统兼容性:

  1. 组件使用了 Taro 标准的 ScrollViewView 组件,这些在鸿蒙平台上都有对应实现
  2. 滚动和触摸事件(onScrollonTouchStartonTouchMoveonTouchEnd)使用了标准的事件处理方式
  3. 已包含鸿蒙平台特定的样式文件 infiniteloading.harmony.css
  4. 使用 createSelectorQuery 进行 DOM 查询的方式在鸿蒙平台上也是支持的

下拉刷新、滚动加载和触摸事件的核心功能都使用了跨平台的标准实现方式,不需要特殊处理即可在鸿蒙系统上正常运行。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 检查是否有针对鸿蒙系统的特殊处理代码
rg -i "harmony|hongmeng|openharmony" 

# 检查相关的样式适配
rg -g "*.scss" -g "*.less" -g "*.css" "\.nut-infiniteloading"

Length of output: 26854


Script:

#!/bin/bash
# Check for harmony-specific code in the infiniteloading component
rg -A 5 -B 5 "harmony|isHarmony" src/packages/infiniteloading/

# Check for platform-specific imports and usage
ast-grep --pattern 'import { $_ } from "@/utils/platform-taro"' src/packages/infiniteloading/

Length of output: 160


Script:

#!/bin/bash
# 检查组件中的滚动和触摸事件处理
rg -A 10 "onScroll|onTouch|ScrollView" src/packages/infiniteloading/infiniteloading.taro.tsx

# 检查组件中的平台特定代码
rg -g "*.tsx" -g "*.ts" "createSelectorQuery|getSystemInfo" src/packages/infiniteloading/

# 检查样式文件中的平台特定适配
rg "#ifdef|#ifndef" src/packages/infiniteloading/infiniteloading.harmony.css

Length of output: 2922

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

240-240: 简化了JSX模板的复杂度

getBottomTipsText() 的调用整合到JSX中,使模板更加清晰简洁。

src/config.json (2)

Line range hint 774-784: 版本更新和多端适配配置变更已确认

InfiniteLoading 组件的版本升级到 3.0.0 并启用了 taro 支持,这符合多端适配的需求。配置中包含了必要的文档和描述信息。


Line range hint 1-1000: 建议完善组件文档

考虑到此次更新涉及多端适配,建议在组件描述(desc)中补充多端适配的相关信息,特别是对 HarmonyOS 平台的支持说明。

@@ -62,13 +64,13 @@ const Demo2 = () => {
return (
<>
<Cell>
<ul id="refreshScroll" style={InfiniteUlStyle}>
<View id="refreshScroll" style={InfiniteUlStyle}>
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

建议增强无障碍访问支持

将 HTML 列表元素(ul/li)替换为 View 组件虽然提升了跨平台兼容性,但可能会影响页面的无障碍访问支持。

建议添加适当的 ARIA 属性来保持无障碍支持:

 <View id="refreshScroll" style={InfiniteUlStyle}>
   <InfiniteLoading>
     {refreshList.map((item, index) => {
       return (
         <View
           className="infiniteLi"
           key={index}
           style={InfiniteLiStyle}
+          role="listitem"
+          aria-label={`列表项 ${item}`}
         >
           {item}
         </View>
       )
     })}
   </InfiniteLoading>
 </View>

Also applies to: 99-105

@@ -94,7 +94,7 @@ export const InfiniteLoading: FunctionComponent<

const getStyle = () => {
return {
height: topDisScoll < 0 ? `0px` : `${topDisScoll}px`,
height: topDisScoll < 0 ? px : `${topDisScoll}px`,
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

修复未定义变量的使用问题

代码中使用了未定义的变量 px,这可能会导致运行时错误。在之前的版本中,这里使用的是字符串字面量 '0px'

建议修改为:

-      height: topDisScoll < 0 ? px : `${topDisScoll}px`,
+      height: topDisScoll < 0 ? '0px' : `${topDisScoll}px`,
📝 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
height: topDisScoll < 0 ? px : `${topDisScoll}px`,
height: topDisScoll < 0 ? '0px' : `${topDisScoll}px`,

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