Skip to content

fix: 🐛 修复 Slider 处理边界值异常的问题,优化样式和事件处理逻辑 - #1050

Merged
Moonofweisheng merged 2 commits into
masterfrom
fix/slider-step
May 14, 2025
Merged

Moonofweisheng merged 2 commits into
masterfrom
fix/slider-step

Conversation

@Moonofweisheng

@Moonofweisheng Moonofweisheng commented May 13, 2025 •

Copy link
Copy Markdown
Owner

✅ Closes: #1023

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

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

🔗 相关 Issue

#1023

💡 需求背景和解决方案

修复 Slider 处理边界值异常的问题,优化样式和事件处理逻辑

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

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

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

Summary by CodeRabbit

  • 新功能
    • 在“指定选择范围”演示区新增滑块组件,支持自定义最小值、最大值和步长。
  • 样式
    • 优化滑块按钮的左右对齐样式,提升视觉一致性。
  • 重构
    • 重构滑块组件内部状态和事件处理逻辑,统一单滑块与区间滑块模式,提升健壮性和可维护性。
  • 类型增强
    • 完善滑块组件的类型定义和事件类型声明,提升类型安全。
  • 测试
    • 更新滑块步长相关测试用例,确保行为与最新逻辑一致。

@vercel

vercel Bot commented May 13, 2025 •

Copy link
Copy Markdown

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 May 14, 2025 1:43pm

@netlify

netlify Bot commented May 13, 2025 •

Copy link
Copy Markdown

✅ Deploy Preview for wot-design-uni ready!

Name Link
🔨 Latest commit 0350529
🔍 Latest deploy log https://app.netlify.com/sites/wot-design-uni/deploys/68249dc379b309000853ae2e
😎 Deploy Preview https://deploy-preview-1050--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.

@coderabbitai

coderabbitai Bot commented May 13, 2025 •

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

"""

Walkthrough

本次变更主要针对 wd-slider 组件的内部状态管理、事件处理和类型定义进行了重构,修复了当 min/max 均大于 100 时的滑块异常问题,并完善了滑块的样式和测试用例。还在演示页面中新增了相应的滑块示例。

Changes

文件/分组 变更摘要
src/subPages/slider/Index.vue 新增 min=160, max=280, step=30 的滑块示例,增加 value8 变量,修正注释路径和时间戳。
src/uni_modules/wot-design-uni/components/wd-slider/index.scss 优化 .button-wrapper 的定位和 transform,新增 m(left) 修饰符以区分左右手柄样式。
src/uni_modules/wot-design-uni/components/wd-slider/types.ts 新增 SliderValue、SliderDragEvent、SliderEmits 等类型,完善 props 和 emits 的类型定义。
src/uni_modules/wot-design-uni/components/wd-slider/wd-slider.vue 重构内部状态管理,合并手柄逻辑,修复 min/max 边界问题,统一事件处理,优化数值归一化。
tests/components/wd-slider.test.ts 更新 step 相关测试用例,调整初始化时的数值处理预期。

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant SliderComponent
    participant ParentComponent

    User->>SliderComponent: 拖动滑块
    SliderComponent->>SliderComponent: 归一化数值、边界校验、步进处理
    SliderComponent->>ParentComponent: emit('update:modelValue', value)
    SliderComponent->>ParentComponent: emit('dragmove', { value })
Loading

Assessment against linked issues

Objective Addressed Explanation
修复 wd-slider 在 min/max 都大于 100 时滑动异常,确保 min=160、max=280、step=30 时滑块正常 ✅
滑块边界值显示正确、手柄不超出轨道,滑动时能获取对应值 (#1023) ✅

Poem

小兔滑滑条,
最小一百六,最大二百八,
步进三十跳,
左右不再飘,
代码焕新貌,
Bug 终被消,
快乐滑到底,兔兔笑哈哈!
"""

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.


📜 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 25c640e and 0350529.

📒 Files selected for processing (2)
  • src/uni_modules/wot-design-uni/components/wd-slider/index.scss (1 hunks)
  • src/uni_modules/wot-design-uni/components/wd-slider/wd-slider.vue (2 hunks)

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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in 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.

@petercat-assistant

Copy link
Copy Markdown

Walkthrough

修复了 Slider 组件在处理边界值时的异常问题,并优化了样式和事件处理逻辑。此次更新还包括对双滑块模式的支持和事件类型的定义。

Changes

文件 概要
src/subPages/slider/Index.vue 添加了新的 Slider 示例,展示了不同的最小值、最大值和步长设置。
src/uni_modules/wot-design-uni/components/wd-slider/index.scss 修改了样式,调整了滑块按钮的位置和变换。
src/uni_modules/wot-design-uni/components/wd-slider/types.ts 更新了类型定义,增加了对滑块值和事件的类型支持。
src/uni_modules/wot-design-uni/components/wd-slider/wd-slider.vue 重构了 Slider 组件的逻辑,支持双滑块模式,优化了事件处理和样式计算。
tests/components/wd-slider.test.ts 更新了测试用例以验证新的边界值处理逻辑。

let newValue = deepClone(startValue.value)

if (isArray(newValue)) {
;(newValue as number[])[touchIndex.value] += diff

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

In the onSliderTouchMove function, ensure that the updated value does not exceed the defined range. Consider using the clamp function to restrict the value within minProp and maxProp after calculating the new value.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented May 13, 2025 •

Copy link
Copy Markdown

Deploying wot-design-uni with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0350529
Status:⚡️  Build in progress...

View logs

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 70.58824% with 45 lines in your changes missing coverage. Please review.

Project coverage is 73.51%. Comparing base (8177978) to head (25c640e).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
.../wot-design-uni/components/wd-slider/wd-slider.vue 70.39% 45 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

❗ There is a different number of reports uploaded between BASE (8177978) and HEAD (25c640e). Click for more details.

HEAD has 178 uploads less than BASE
Flag BASE (8177978) HEAD (25c640e)
wd-rate 2 0
h5 90 1
wd-slider 2 1
wd-col-picker 2 0
wd-drop-menu 2 0
wd-loading 1 0
wd-notice-bar 1 0
wd-index-bar 1 0
wd-progress 1 0
wd-navbar 1 0
wd-popover 1 0
wd-button 1 0
wd-circle 1 0
wd-card 1 0
wd-backtop 1 0
wd-form 1 0
wd-img-cropper 1 0
wd-floating-panel 1 0
wd-skeleton 1 0
wd-steps 1 0
wd-gap 1 0
wd-swiper-nav 1 0
wd-cell 1 0
wd-sticky-box 1 0
wd-collapse 1 0
wd-count-down 1 0
wd-step 1 0
wd-swipe-action 1 0
wd-datetime-picker 1 0
wd-watermark 1 0
wd-radio-group 1 0
wd-number-keyboard 1 0
wd-video-preview 1 0
wd-grid-item 1 0
wd-index-anchor 1 0
wd-layout 1 0
wd-swiper 1 0
wd-loadmore 1 0
wd-sidebar 1 0
wd-divider 1 0
wd-fab 1 0
wd-signature 1 0
wd-img 1 0
wd-calendar-view 1 0
wd-action-sheet 1 0
wd-keyboard 1 0
wd-select-picker 1 0
wd-password-input 1 0
wd-sticky 1 0
wd-cell-group 1 0
wd-tooltip 1 0
wd-toast 1 0
wd-popup 1 0
wd-config-provider 1 0
wd-transition 1 0
wd-badge 1 0
wd-icon 1 0
wd-input-number 1 0
wd-overlay 1 0
wd-picker-view 1 0
wd-count-to 1 0
wd-switch 1 0
wd-datetime-picker-view 1 0
wd-curtain 1 0
wd-resize 1 0
wd-tabs 1 0
wd-checkbox-group 1 0
wd-picker 1 0
wd-pagination 1 0
wd-calendar 1 0
wd-tabbar 1 0
wd-navbar-capsule 1 0
wd-segmented 1 0
wd-radio 1 0
wd-notify 1 0
wd-grid 1 0
wd-upload 1 0
wd-table 1 0
wd-textarea 1 0
wd-checkbox 1 0
wd-text 1 0
wd-input 1 0
wd-search 1 0
wd-message-box 1 0
wd-status-tip 1 0
wd-sort-button 1 0
wd-tag 1 0
Additional details and impacted files
@@             Coverage Diff             @@
##           master    #1050       +/-   ##
===========================================
- Coverage   84.82%   73.51%   -11.31%     
===========================================
  Files         215        3      -212     
  Lines       17273      253    -17020     
  Branches     2727       40     -2687     
===========================================
- Hits        14652      186    -14466     
+ Misses       2621       67     -2554     
Flag Coverage Δ
h5 73.51% <70.58%> (-11.31%) ⬇️
wd-action-sheet ?
wd-backtop ?
wd-badge ?
wd-button ?
wd-calendar ?
wd-calendar-view ?
wd-card ?
wd-cell ?
wd-cell-group ?
wd-checkbox ?
wd-checkbox-group ?
wd-circle ?
wd-col-picker ?
wd-collapse ?
wd-config-provider ?
wd-count-down ?
wd-count-to ?
wd-curtain ?
wd-datetime-picker ?
wd-datetime-picker-view ?
wd-divider ?
wd-drop-menu ?
wd-fab ?
wd-floating-panel ?
wd-form ?
wd-gap ?
wd-grid ?
wd-grid-item ?
wd-icon ?
wd-img ?
wd-img-cropper ?
wd-index-anchor ?
wd-index-bar ?
wd-input ?
wd-input-number ?
wd-keyboard ?
wd-layout ?
wd-loading ?
wd-loadmore ?
wd-message-box ?
wd-navbar ?
wd-navbar-capsule ?
wd-notice-bar ?
wd-notify ?
wd-number-keyboard ?
wd-overlay ?
wd-pagination ?
wd-password-input ?
wd-picker ?
wd-picker-view ?
wd-popover ?
wd-popup ?
wd-progress ?
wd-radio ?
wd-radio-group ?
wd-rate ?
wd-resize ?
wd-search ?
wd-segmented ?
wd-select-picker ?
wd-sidebar ?
wd-signature ?
wd-skeleton ?
wd-slider 73.51% <70.58%> (+1.71%) ⬆️
wd-sort-button ?
wd-status-tip ?
wd-step ?
wd-steps ?
wd-sticky ?
wd-sticky-box ?
wd-swipe-action ?
wd-swiper ?
wd-swiper-nav ?
wd-switch ?
wd-tabbar ?
wd-table ?
wd-tabs ?
wd-tag ?
wd-text ?
wd-textarea ?
wd-toast ?
wd-tooltip ?
wd-transition ?
wd-upload ?
wd-video-preview ?
wd-watermark ?

Flags with carried forward coverage won't be shown. Click here to find out more.

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

Copy link
Copy Markdown
Contributor

组件测试摘要 (H5 平台)

测试时间: Tue May 13 05:29:20 UTC 2025

测试结果

组件 状态 覆盖率
wd-slider ✅ 通过 73.51%

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (3)
src/uni_modules/wot-design-uni/components/wd-slider/wd-slider.vue (3)

14-17: 避免在模板中创建新的箭头函数

在模板里每次渲染都会重新创建 (event) => onSliderTouchStart(event, idx) 这样的匿名箭头函数,容易造成不必要的性能损耗,并导致依赖 === 比较的 diff 算法无法复用旧的监听器。
可考虑改为:

  1. 在模板中直接传 data-index,在事件里通过 event.currentTarget.dataset.index 判定手柄索引;
  2. 或在 setup 中预先缓存两个绑定好索引的函数。

这样既能避免重复创建函数,也能提升可读性。

Also applies to: 26-30, 39-43


269-277: 小数步长精度处理可考虑 Number.EPSILON

toFixed(10)+parseFloat 虽能规避浮点误差,但会丢失原本的小数位数控制且性能一般。可改为常见的“加上一个极小值再四舍五入”的写法,更直观:

-return parseFloat((minProp.value + steps * stepProp.value).toFixed(10))
+const raw = minProp.value + steps * stepProp.value
+return Math.round((raw + Number.EPSILON) * 1e10) / 1e10

同时将 1e10 抽成常量,便于维护。


84-87: 未使用的变量/属性可删除

trackLeft 在当前实现中并未使用;同理,isDef 也未被引用。保留无用代码会增加认知负担,建议删除或在后续功能中补充使用。

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8177978 and 25c640e.

📒 Files selected for processing (5)
  • src/subPages/slider/Index.vue (3 hunks)
  • src/uni_modules/wot-design-uni/components/wd-slider/index.scss (1 hunks)
  • src/uni_modules/wot-design-uni/components/wd-slider/types.ts (2 hunks)
  • src/uni_modules/wot-design-uni/components/wd-slider/wd-slider.vue (2 hunks)
  • tests/components/wd-slider.test.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (10)
tests/components/wd-slider.test.ts (1)

66-67: 测试用例更新符合组件行为变更

测试用例已正确更新,以反映组件初始化逻辑的变更。现在滑块组件在初始化时只处理边界值而不四舍五入到步长值,这是一个合理的优化。

src/subPages/slider/Index.vue (4)

4-4: 修正了时间戳

文件的最后编辑时间已更新。


7-7: 修正了文件路径

文件路径从 /src/pages/slider/Index.vue 更正为 /src/subPages/slider/Index.vue,提高了文档的准确性。


17-17: 新增了边界值测试场景

新增的滑块示例设置了较大范围的 min/max 值(160-280),这正好验证了修复的边界值处理问题。这是一个很好的示例,能够直观展示组件对大数值范围的支持能力。


38-39: 为新增滑块示例添加了初始值

为新添加的滑块组件设置了合适的初始值 185,该值位于设定的范围内(160-280)且不是步长(30)的整数倍,这有助于测试组件对非整步长值的处理逻辑。

src/uni_modules/wot-design-uni/components/wd-slider/types.ts (5)

1-1: 导入类型定义更加完整

从 Vue 中添加了 ExtractPropTypes 的导入,用于正确提取组件属性的类型,增强了类型安全性。


4-8: 新增了滑块值类型定义

添加了 SliderValue 类型别名,明确了滑块值可以是数字(单滑块)或数字数组(双滑块),这增强了代码的可读性和类型安全性。


88-88: 使用新定义的类型替换原始类型

将 modelValue 属性的类型从原始的 [Number, Array] 改为使用新定义的 SliderValue 类型别名,保持了类型定义的一致性和清晰度。


93-127: 完善了事件类型定义

新增了 SliderDragEvent 接口和 SliderEmits 类型,为滑块组件的拖动事件和值更新事件提供了完整的类型定义:

  1. SliderDragEvent 定义了拖动事件的参数结构
  2. SliderEmits 明确了组件所有事件的类型和参数

这些类型定义大大提升了组件的类型安全性,有助于在开发时及早发现类型错误。


136-138: 增强了属性和实例类型定义

更新了 SliderProps 和 SliderInstance 类型定义:

  1. 使用 ExtractPropTypes 提取属性类型,确保与组件定义一致
  2. 更新了组件实例类型,包含属性和暴露的方法

这些改进使得组件的类型系统更加完整和严谨,有利于 IDE 提供更准确的代码提示。

Comment thread src/uni_modules/wot-design-uni/components/wd-slider/index.scss
Comment thread src/uni_modules/wot-design-uni/components/wd-slider/wd-slider.vue
Comment on lines +165 to +178
const barStyle = computed(() => {
const style: CSSProperties = {}

if (isRange.value) {
// 双滑块模式
const [left, right] = normalizeRangeValues(modelValue.value as number[])
style.width = `${((right - left) * 100) / scope.value}%`
style.left = `${((left - minProp.value) * 100) / scope.value}%`
} else {
// 单滑块模式
style.width = `${(((modelValue.value as number) - minProp.value) * 100) / scope.value}%`
style.left = '0'
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue

需处理 min 与 max 相等导致的除零风险

当外部误把 min、max 设成同一值时,scope = max - min 将为 0,后续 (… / scope.value) 计算会出现除零,渲染结果为 NaN%。

-const scope = computed(() => maxProp.value - minProp.value)
+const scope = computed(() => {
+  const diff = maxProp.value - minProp.value
+  return diff === 0 ? 1 : diff   // 保底值 1,或抛出警告
+})

同时可在 barStyle 中检测 scope.value === 0 并提前返回默认样式,避免异常。

This branch was successfully deployed

1 active deployment
Preview — 0350529b Deployed May 14, 2025 by vercel[bot]
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.

[Bug 上报] wd-slider在min-max都大于100时功能表现错误

2 participants