rate icon indicating copy to clipboard operation
rate copied to clipboard

Rate support precision

Open yellowryan opened this issue 10 months ago • 3 comments

Rate组件支持更精细的控制

Related to antd#52986

  • 实现方式

    • 根据鼠标hover的移动,计算出百分比,和给定的precision进行一次rounded计算,最终赋值给相对应元素的宽度属性上。
  • 兼容处理

    • 原先有一个allowHalf(允许半星)的属性,既然有了precision的话,就没有必要继续用这个属性了,添加上了deprecated标记,内部做了一下兼容。

Summary by CodeRabbit

  • 新功能

    • 引入了更精细的评分功能,允许用户设置评分增量(如 0.1),以实现更精准的评分控制。
    • 示例中新增「Precision」展示区,直观展示全新评分效果。
  • 样式

    • 优化了评分组件的视觉外观,移除了部分过渡效果,并调整了星级显示样式。
  • 重构

    • 改进了评分计算逻辑和星级渲染,提升了评分显示的响应性和精确性。
  • 测试

    • 新增测试用例以验证精细评分功能的正确性。

yellowryan avatar Mar 06 '25 21:03 yellowryan

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

Name Status Preview Comments Updated (UTC)
rate ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 10, 2025 3:40am

vercel[bot] avatar Mar 06 '25 21:03 vercel[bot]

Walkthrough

此次变更涉及评分组件及其相关样式、逻辑和测试用例的更新。在 assets/index.less 中优化了评分星的样式,取消了一些转场效果并调整了宽度、透明度和颜色设定;在 docs/examples/simple.tsx 中新增了 "Precision" 示例;在 src/Rate.tsx 中引入了新的 precision 属性,优化了星级计算逻辑并废弃了 allowHalf;同时,src/Star.tsx 实现了 memoized 宽度计算,src/util.ts 添加了辅助工具函数,测试用例也随之更新。

Changes

文件 更改摘要
assets/index.less 移除转场效果,调整评分星样式:将 &-first 的宽度由 50% 改为 0%,透明度由 0 调整为 1,并添加了颜色属性;同时移除了部分悬停状态的样式。
docs/examples/simple.tsx, tests/simple.spec.js, src/Rate.tsx 实现并扩展了评分组件的 precision 特性:在组件中添加了 precision 属性,优化了事件处理和星级计算逻辑,同时在示例和测试用例中新增了相关验证。
src/Star.tsx 新增 memoized 计算逻辑,根据传入的 valueindex 动态计算星的宽度,修改了第一个星的渲染方式,添加内联样式以控制溢出。
src/util.ts 新增实用工具函数:导出 getBoundingClientRect ,以及 roundValueToPrecisionclamp 函数,同时添加了一个用于计算小数位数的内部辅助函数。

Sequence Diagram(s)

sequenceDiagram
    participant U as 用户
    participant R as Rate 组件
    participant S as Star 组件
    participant UTL as 工具函数

    U->>R: 触发鼠标事件 (click/move)
    R->>UTL: 获取元素边界 (getBoundingClientRect)
    UTL-->>R: 返回边界信息
    R->>UTL: 计算百分比 (calculatePercentage) 并执行四舍五入 (roundValueToPrecision)
    UTL-->>R: 返回计算结果
    R->>S: 更新星级显示 (通过 memoized 宽度)
    S-->>R: 完成渲染更新

Poem

嗨,我是一只跳跃的小兔子,
代码新意在风中轻舞,
样式清新如春日花开,
精度精准似晨光闪耀,
工具与测试齐奏欢歌,
我在代码森林中奔跑,快乐无限!
🐰🌸

[!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/Star.tsx

Oops! Something went wrong! :(

ESLint: 7.32.0

ESLint couldn't find the plugin "eslint-plugin-react".

(The package "eslint-plugin-react" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-react@latest --save-dev

The plugin "eslint-plugin-react" was referenced from the config file in ".eslintrc.js » /node_modules/.pnpm/@[email protected]/node_modules/@umijs/fabric/dist/eslint.js".

If you still can't figure out the problem, 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 c6e30f903aa899d350164f2c7fd61a127b1f077e and 8a888481365ab50cdc83ece2f655ee4eb8c02d5b.

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

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

coderabbitai[bot] avatar Mar 06 '25 21:03 coderabbitai[bot]

同时还有一个展示问题想请问下意见 ,例如设置的defaultValue是1.2,但是精度是1,那展示的时候是展示1颗星还是1.2颗星(目前的表现是会展示1.2颗星),但是hover、点击变化的时候会变成1、2、3这样。

yellowryan avatar Mar 06 '25 21:03 yellowryan