Skip to content

feat: add handle index on change events #1062

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

gabrielmar
Copy link

@gabrielmar gabrielmar commented May 9, 2025

Screenshot

Adds the index handle to change events, so it is possible to make calculations based on the index that the user selects.

Summary by CodeRabbit

  • 新功能
    • 滑块组件的 onChange 和 onChangeComplete 回调现在会额外提供当前操作手柄的索引参数,便于区分多手柄场景下的变化。

Copy link

vercel bot commented May 9, 2025

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

Name Status Preview Comments Updated (UTC)
slider ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 10, 2025 0:44am

Copy link
Contributor

coderabbitai bot commented May 9, 2025

"""

Walkthrough

本次更改主要调整了SliderProps接口中onChangeonChangeComplete回调的签名,新增了一个表示滑块句柄索引的index参数。相应地,组件内部所有调用这些回调的地方也同步传递了当前的draggingIndexonAfterChange回调未作变动。相关测试代码的断言也同步更新,新增了回调的第二个参数。

Changes

文件/分组 变更摘要
src/Slider.tsx onChangeonChangeComplete回调签名新增index参数,所有相关调用同步传递句柄索引,onAfterChange保持不变。
tests/Range.test.tsx 更新onChangeonChangeComplete回调断言,新增第二个参数index,覆盖键盘、鼠标、触摸等多种交互测试。
tests/Slider.test.js 更新onChangeonAfterChange回调断言,新增第二个参数-10,涵盖键盘、鼠标事件及范围滑块测试。
tests/marks.test.js 更新点击刻度标记时onChangeonChangeComplete回调断言,新增第二个参数-1,并调整组件渲染格式。

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Slider
    participant Consumer

    User->>Slider: 拖动滑块
    Slider->>Consumer: onChange(value, index)
    User-->>Slider: 释放滑块
    Slider->>Consumer: onChangeComplete(value, index)
Loading

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.

tests/Range.test.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the config "prettier" to extend from. Please check that the name of the config is correct.

The config "prettier" was referenced from the config file in "/.eslintrc.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

tests/Slider.test.js

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the config "prettier" to extend from. Please check that the name of the config is correct.

The config "prettier" was referenced from the config file in "/.eslintrc.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

tests/marks.test.js

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the config "prettier" to extend from. Please check that the name of the config is correct.

The config "prettier" was referenced from the config file in "/.eslintrc.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

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 42275bd and 70d3093.

📒 Files selected for processing (3)
  • tests/Range.test.tsx (16 hunks)
  • tests/Slider.test.js (30 hunks)
  • tests/marks.test.js (1 hunks)
🔇 Additional comments (16)
tests/marks.test.js (3)

40-42: 代码格式调整看起来不错

JSX元素格式调整为多行形式,提高了代码的可读性。


49-49: 正确更新了onChange回调的断言

测试断言已更新以验证onChange回调现在接收两个参数:点击的标记值和索引值(此处为-1)。这与组件接口的变更保持一致。


51-51: 正确更新了onChangeComplete回调的断言

测试断言已更新以验证onChangeComplete回调现在也接收两个参数:点击的标记值和索引值(此处为-1)。这与组件接口的变更保持一致。

tests/Range.test.tsx (6)

261-261: correctly updated keyboard event test assertions with index parameter

键盘事件测试断言已正确更新,现在期望onChange回调将接收值数组和索引参数。这与滑块组件接口的更改保持一致。

Also applies to: 273-273, 281-281


302-302: 正确更新了pushable与allowCross功能测试断言

推送功能(pushable)与允许交叉(allowCross)的相关测试断言已适当更新,包含了索引参数。这确保了处理复杂交互场景时回调参数的一致性。

Also applies to: 310-310, 318-318, 326-326


340-340: 正确更新了方向测试(LTR、RTL、垂直)的断言

不同方向配置(左到右、右到左、垂直)的测试断言已正确更新,包含了索引参数。这确保了在各种方向配置下回调参数的一致性。

Also applies to: 358-358, 370-370, 382-382


436-436: 正确更新了轨道可拖动功能的测试断言

轨道可拖动(draggableTrack)功能的测试断言已更新,包含索引参数,保持了回调接口的一致性。


666-666: 正确更新了可编辑范围功能的测试断言

可编辑范围(editable)功能的测试断言已正确更新,包含了索引参数,确保了这些特殊交互场景下回调接口的一致性。

Also applies to: 719-719, 722-722, 726-726


757-757: 正确更新了键盘删除和边界情况的测试断言

键盘删除操作和各种边界情况的测试断言已正确更新,包含了索引参数,确保了所有交互场景下回调接口的一致性。

Also applies to: 783-783, 792-792, 823-823, 839-839

tests/Slider.test.js (7)

114-114: 正确更新了基本键盘事件的测试断言

键盘事件(上、下、左、右、PageUp等)测试的断言已正确更新,包含了索引参数(此处为-1)。这确保了滑块组件在键盘操作时正确传递句柄索引信息。

Also applies to: 125-125, 136-136, 164-164, 175-175, 186-186, 200-200


217-217: 正确更新了范围滑块的键盘事件测试断言

范围滑块(Range)键盘事件测试的断言已正确更新,包含了索引参数。这确保了在多句柄场景下键盘操作的正确响应。

Also applies to: 222-222, 227-227, 232-232


243-243: 正确更新了高级键盘操作的测试断言

Home、End键以及带有固定值(marks)的滑块键盘操作测试断言已正确更新,包含索引参数,确保了所有键盘交互场景下回调接口的一致性。

Also applies to: 254-254, 265-265, 283-283, 300-300, 317-317, 334-334, 351-351, 368-368


390-390: 正确更新了混合步长和标记的键盘测试断言

同时包含步长(step)和标记(marks)的键盘操作测试断言已正确更新,确保了在这种复杂配置下回调接口的一致性。

Also applies to: 395-395, 400-400, 410-410, 415-415, 420-420


522-522: 正确更新了点击滑块改变值的测试断言

点击滑块轨道改变值的测试断言(包括不同方向的配置)已正确更新,包含索引参数,确保了鼠标点击交互的回调接口一致性。

Also applies to: 532-532, 542-542, 552-552, 562-562, 581-581, 586-586


619-619: 正确更新了边界值和特殊情况的测试断言

处理边界值、小数值以及拖动后测试的断言已正确更新,确保了所有特殊场景下回调接口的一致性。

Also applies to: 629-629, 642-642, 652-652, 658-658


668-668: 修复了tipFormatter测试的格式

tipFormatter测试代码格式调整,改进了代码的可读性,同时保持了测试的功能。

✨ Finishing Touches
  • 📝 Generate Docstrings

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

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.

Copy link
Contributor

@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

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 874875a and 42275bd.

📒 Files selected for processing (1)
  • src/Slider.tsx (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/Slider.tsx (2)
docs/examples/editable.tsx (1)
  • value (12-69)
docs/examples/multiple.tsx (1)
  • value (19-49)
🔇 Additional comments (4)
src/Slider.tsx (4)

80-80: 更新了 onChangeComplete 回调函数签名,添加了 index 参数

onChange 类似,onChangeComplete 回调函数签名也添加了 index 参数,保持了接口的一致性。这个改进使开发者能够在交互完成时也能获取到句柄索引信息。

同样需要注意,这是一个破坏性变更,可能需要更新现有代码。


303-303: 更新 onChange 调用以传递句柄索引

现在在 triggerChange 函数中调用 onChange 时会传递 draggingIndex 作为第二个参数,与更新后的接口签名保持一致。这确保了在值变化时,外部回调函数能够接收到正确的句柄索引。


322-322: 更新 onChangeComplete 调用以传递句柄索引

finishChange 函数中调用 onChangeComplete 时现在会传递 draggingIndex 作为第二个参数,与更新后的接口签名保持一致。注意,原有的废弃回调 onAfterChange 仍然保持不变,只传递值而不传递索引。


409-409: 在 changeToCloseValue 函数中更新 onChangeComplete 调用

changeToCloseValue 函数中调用 onChangeComplete 时也传递了 draggingIndex 作为第二个参数,保持了代码的一致性。这确保了在点击轨道或标记改变值时,正确的句柄索引也会被传递给回调函数。

@@ -72,12 +72,12 @@ export interface SliderProps<ValueType = number | number[]> {
step?: number | null;
value?: ValueType;
defaultValue?: ValueType;
onChange?: (value: ValueType) => void;
onChange?: (value: ValueType, index: number) => void;
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

更新了 onChange 回调函数签名,添加了 index 参数

SliderProps 接口中,onChange 回调函数签名现在包含了一个额外的 index 参数,用于表示正在操作的滑块句柄的索引。这个改进使开发者能够根据特定句柄的索引执行计算或逻辑。

请注意,这是一个破坏性变更,可能需要更新现有的使用此组件的代码。

建议在文档中明确说明这个变更,并更新示例代码以展示如何利用这个新参数。例如,在 docs/examples/multiple.tsxdocs/examples/editable.tsx 中的示例可以更新为:

-onChange={(nextValue) => {
+onChange={(nextValue, handleIndex) => {
+  console.log('Changed handle index:', handleIndex);
   // 其他逻辑...
}}

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents (early access)
In src/Slider.tsx at line 75, the onChange callback signature in the SliderProps
interface has been updated to include an additional index parameter representing
the handle index. To fix this, update all existing usages of the onChange prop
to accept and handle the new index argument accordingly. Additionally, update
the component documentation and example files such as docs/examples/multiple.tsx
and docs/examples/editable.tsx to reflect this signature change and demonstrate
how to use the index parameter in the callback.

Comment on lines +75 to +80
onChange?: (value: ValueType, index: number) => void;
/** @deprecated It's always better to use `onChange` instead */
onBeforeChange?: (value: ValueType) => void;
/** @deprecated Use `onChangeComplete` instead */
onAfterChange?: (value: ValueType) => void;
onChangeComplete?: (value: ValueType) => void;
onChangeComplete?: (value: ValueType, index: number) => void;
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

建议:考虑更新现有示例代码

从相关代码片段看,现有的示例代码(如 docs/examples/multiple.tsxdocs/examples/editable.tsx)还没有利用新增的 index 参数。为了帮助用户理解这个新功能,建议更新这些示例,展示如何使用句柄索引。


🏁 Script executed:

#!/bin/bash
# 检查当前是否有使用新 index 参数的示例代码

# 在 docs 目录下搜索 onChange 或 onChangeComplete 的使用
echo "检查示例代码中的回调函数使用情况:"
rg -A 2 "onChange(.*\).* =>\s*{|onChangeComplete(.*\).* =>\s*{" docs/

Length of output: 462


🏁 Script executed:

#!/bin/bash
# 重新检查 docs/examples 目录中是否有使用新 index 参数的回调
echo "查找 docs/examples 目录中 onChange 和 onChangeComplete 的调用:"
rg "onChange\s*=" -n docs/examples
rg "onChangeComplete\s*=" -n docs/examples

Length of output: 5236


更新示例以演示 index 参数

现有示例(如 docs/examples/multiple.tsxdocs/examples/editable.tsx 等)在调用 onChangeonChangeComplete 时仅使用了单个参数。为了帮助用户理解新加入的第二个 index 回调参数,建议:

  • 在相关示例中将回调签名由
    (value) => { … }
    改为
    (value, index) => { … }
  • 并在示例逻辑中展示如何根据 index 区分不同滑块句柄或进行不同处理。

请按需更新以下文件:

  • docs/examples/multiple.tsx
  • docs/examples/editable.tsx
  • (可选)其他使用 onChange/onChangeComplete 的示例文件
🤖 Prompt for AI Agents (early access)
In src/Slider.tsx around lines 75 to 80, the onChange and onChangeComplete
callbacks now include a second index parameter that is not demonstrated in the
example files. Update the example files docs/examples/multiple.tsx and
docs/examples/editable.tsx to change the callback signatures from using a single
value parameter to using both value and index parameters. Modify the example
logic to show how to handle different slider handles based on the index, helping
users understand and utilize the new index argument effectively.

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.

1 participant