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: ✨ Rate 评分新增支持半选和触摸滑动选中 #896

Merged
merged 1 commit into from
Feb 14, 2025

Conversation

Moonofweisheng
Copy link
Owner

@Moonofweisheng Moonofweisheng commented Feb 13, 2025

Closes: #669

🤔 这个 PR 的性质是?(至少选择一个)

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

🔗 相关 Issue

#669

💡 需求背景和解决方案

  1. 支持半选。
  2. 支持滑动触摸选中。

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

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

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充

Summary by CodeRabbit

  • 新功能
    • 为评分组件增加半星选择功能,允许用户以 0.5 的递增单位进行评分,并提供了新的示例体验该功能。
  • 文档
    • 在组件文档中新增“允许半选”部分,详细说明如何使用及配置半星选择功能。
  • 样式
    • 优化了组件的样式和触摸交互效果,确保评分时显示更为精准和流畅。

Copy link

vercel bot commented Feb 13, 2025

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

Name Status Preview Comments Updated (UTC)
wot-design-uni ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 13, 2025 5:18pm

Copy link

coderabbitai bot commented Feb 13, 2025

Walkthrough

此次提交在 wd-rate 组件中引入了允许半选的新功能。文档更新中增加了 allowHalf 属性的说明与示例,类型定义中也新增了该属性。页面新增了演示块展示半选效果,同时样式文件和组件逻辑进行了相应调整,包括触摸事件处理和评分计算的修改,以支持半星评级。

Changes

文件(s) 变更内容
docs/component/rate.md
uni_modules/wot-design-uni/components/wd-rate/types.ts
新增 allowHalf 属性文档说明与类型声明(类型为 boolean,默认值 false)。
src/pages/rate/Index.vue 新增演示块及变量 value8(初始值 2.5),展示半选功能示例。
uni_modules/wot-design-uni/components/wd-rate/index.scss 修改样式规则:为 item 添加 touch-action: none;,移除旧的 item-star 样式,新增 edeep(item-star)item-half 样式块。
uni_modules/wot-design-uni/components/wd-rate/wd-rate.vue 添加 onTouchMove 触摸事件处理;更新 changeRate 方法增加 isHalf 参数,并调整评分逻辑以支持半选。

Sequence Diagram(s)

sequenceDiagram
    participant U as 用户
    participant R as wd-rate组件
    participant T as onTouchMove
    participant C as changeRate
    U->>R: 触摸滑动(选择半星或全星)
    R->>T: 捕获 touch 事件
    T->>C: 调用 changeRate(index, isHalf)
    C->>R: 更新评分状态
    R->>U: 渲染更新后的评分
Loading

Poem

我是一只快乐的小兔子,
在代码草原上蹦跳自如,
半选新特性像双倍胡萝卜,
轻触之间点亮星光链条,
每次点击写下梦幻诗句,
欢庆变更,共享奇妙时光!

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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

netlify bot commented Feb 13, 2025

Deploy Preview for wot-design-uni ready!

Name Link
🔨 Latest commit 4d92c81
🔍 Latest deploy log https://app.netlify.com/sites/wot-design-uni/deploys/67ae29765bf19d0008c6a7d3
😎 Deploy Preview https://deploy-preview-896--wot-design-uni.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

Deploying wot-design-uni with  Cloudflare Pages  Cloudflare Pages

Latest commit: 4d92c81
Status: ✅  Deploy successful!
Preview URL: https://f16c96a0.wot-design-uni.pages.dev
Branch Preview URL: https://feature-rate-allow-half.wot-design-uni.pages.dev

View logs

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/pages/rate/Index.vue (1)

45-47: 建议添加示例说明

建议在示例代码中添加注释,说明半选功能的使用方法和初始值 2.5 的含义。

 <demo-block title="允许半选">
+      <!-- 支持 0.5 的倍数,如 0.5、1.5、2.5 等 -->
       <wd-rate v-model="value8" allow-half />
     </demo-block>
src/uni_modules/wot-design-uni/components/wd-rate/wd-rate.vue (2)

140-155: 建议优化触摸事件处理

当前触摸事件处理可以进行以下优化:

  1. 添加防抖处理,避免频繁触发状态更新
  2. 添加只读和禁用状态的判断
  3. 添加错误处理
+import { debounce } from '../common/util'
+
 async function onTouchMove(event: TouchEvent) {
+  if (props.readonly || props.disabled) return
+  
+  try {
   const { clientX } = event.touches[0]
   const rateItems = await getRect('.wd-rate__item', true, proxy)
   const targetIndex = Array.from(rateItems).findIndex((rect) => {
     return clientX >= rect.left! && clientX <= rect.right!
   })
   if (targetIndex !== -1) {
     const target = rateItems[targetIndex]
     const itemWidth = target.width!
     const isHalf = props.allowHalf && clientX - target.left! < itemWidth / 2
     const value = isHalf ? targetIndex + 0.5 : targetIndex + 1
     if (value >= 0.5) {
       changeRate(targetIndex, isHalf)
     }
   }
+  } catch (error) {
+    console.error('[Wot Design] error(wd-rate): Failed to handle touch move', error)
+  }
 }
+
+// 使用防抖包装触摸事件处理函数
+const debouncedTouchMove = debounce(onTouchMove, 16)

93-114: 建议优化评分列表计算逻辑

当前的评分列表计算逻辑可以更加简洁和高效:

 function computeRateList() {
   const { modelValue, num, allowHalf } = props
   // value和num都准备好才能计算
   if (modelValue === null || !num) return
   if (typeof modelValue !== 'number') {
     console.error('[Wot Design] error(wd-rate): the value of wd-rate should be a number')
     return
   }
-  const tempRateList: string[] = []
-  const fullLength = Math.floor(modelValue)
-  for (let i = 0; i < num; i++) {
-    if (i < fullLength) {
-      tempRateList.push('100%')
-    } else if (i === fullLength && allowHalf && modelValue % 1 !== 0) {
-      tempRateList.push('50%')
-    } else {
-      tempRateList.push('0')
-    }
-  }
+  const tempRateList = Array(num).fill('0').map((_, i) => {
+    if (i < Math.floor(modelValue)) return '100%'
+    if (i === Math.floor(modelValue) && allowHalf && modelValue % 1 !== 0) return '50%'
+    return '0'
+  })
   rateList.value = tempRateList
   computeActiveValue()
 }
src/uni_modules/wot-design-uni/components/wd-rate/index.scss (1)

12-24: 建议增加浏览器兼容性处理

为确保在不同浏览器中的一致性,建议添加相关的浏览器前缀:

   touch-action: none; // 禁用默认触摸行为
 }
 @include edeep(item-star) {
   -webkit-background-clip: text !important;
+  background-clip: text !important;
   color: transparent;
 }
 @include e(item-half) {
   position: absolute;
   left: 0;
   top: 0;
   overflow: hidden;
   width: 50%;
+  -webkit-box-sizing: border-box;
+  box-sizing: border-box;
 }
docs/component/rate.md (1)

62-68: 建议补充半选功能的具体使用说明

建议在示例代码中补充以下内容:

  • 半选状态下的具体分值说明(如 0.5、1.5 等)
  • 触摸滑动选择的操作说明
  • 建议添加一个包含实际分值的完整示例
 ## 允许半选

 设置 `allowHalf` 属性。

 ```html
-<wd-rate v-model="value" allow-half />
+<wd-rate v-model="value" allow-half />
+
+<!-- 支持 0.5、1.5 等分值 -->
+<wd-rate v-model="2.5" allow-half />
+
+<!-- 支持触摸滑动选择 -->
+<wd-rate v-model="value" allow-half />
+
+<script setup lang="ts">
+const value = ref(3.5)
+</script>

</blockquote></details>

</blockquote></details>

<details>
<summary>📜 Review details</summary>

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

<details>
<summary>📥 Commits</summary>

Reviewing files that changed from the base of the PR and between 8e569ac19c9d91064ce6bd41222b469e348f5e1a and 4d92c81c46ffd69510f9ce98bc81005ccbe8e92e.

</details>

<details>
<summary>📒 Files selected for processing (5)</summary>

* `docs/component/rate.md` (2 hunks)
* `src/pages/rate/Index.vue` (3 hunks)
* `src/uni_modules/wot-design-uni/components/wd-rate/index.scss` (1 hunks)
* `src/uni_modules/wot-design-uni/components/wd-rate/types.ts` (1 hunks)
* `src/uni_modules/wot-design-uni/components/wd-rate/wd-rate.vue` (5 hunks)

</details>

<details>
<summary>⏰ Context from checks skipped due to timeout of 90000ms (1)</summary>

* GitHub Check: Cloudflare Pages

</details>

<details>
<summary>🔇 Additional comments (2)</summary><blockquote>

<details>
<summary>src/uni_modules/wot-design-uni/components/wd-rate/types.ts (1)</summary>

`90-95`: **代码实现正确!**

`allowHalf` 属性的类型定义和文档注释都很完整,符合组件库的规范。

</details>
<details>
<summary>docs/component/rate.md (1)</summary>

`85-85`: **需要更新版本号占位符**

`$LOWEST_VERSION$` 占位符需要替换为实际的版本号。


请确认该功能将在哪个版本发布,并更新文档中的版本信息。

</details>

</blockquote></details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->

@Moonofweisheng Moonofweisheng merged commit 9d34f2e into master Feb 14, 2025
8 checks passed
@Moonofweisheng Moonofweisheng deleted the feature/rate-allow-half branch February 14, 2025 05:00
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.

[新功能需求] wd-rate 支持半星
1 participant