Skip to content

Conversation

kartemdev
Copy link
Contributor

@kartemdev kartemdev commented Feb 5, 2025

My telecom company's products use this library in many parts of the application. I needed to separate the logic of entering a value into an input field from the arrows in the interface, from the arrows on the keyboard and from the mouse wheel.

The first thing I discovered was that there was no description of the onStep and changeOnWheel properties in the documentation. I have added these fixes to the document.

The second thing is that I was able to solve my problem without library edits, but the solution looks overhead. By examining the library source code, I found that in the second argument info of the onStep callback, you can add an emitter property: 'handler' | 'keyboard' | 'wheel' that will output what onStep was called with. For example: keyboard, mouse wheel, arrow buttons in the interface.

I think this function will be useful for more than just me, so I'm opening this pull-request. Have a nice day everyone!

Summary by CodeRabbit

  • 新功能

    • 为数字输入组件新增了通过鼠标滚轮调整数值的功能,并增强了对键盘和鼠标事件的响应能力,能够区分不同的输入来源。
  • 文档更新

    • 更新了相关 API 文档,增加了新的属性说明和代码示例,帮助用户更好地理解和使用新增交互功能。
  • 测试

    • 调整了测试用例,以确保新交互特性的正确性和一致性。

Copy link

vercel bot commented Feb 5, 2025

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

Name Status Preview Comments Updated (UTC)
input-number ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 5, 2025 11:29am

Copy link

coderabbitai bot commented Feb 5, 2025

Walkthrough

本次提交对 InputNumber 组件的文档、实现、演示以及测试用例进行了更新。文档中新增了 changeOnWheel 属性和 onStep 回调的说明,并修正了 readOnly 属性的格式问题。组件内部的 onStep 方法及相关接口签名均已更新,新增了 emitter 参数以标识事件来源。同时,新增了一个演示组件展示如何使用这些新特性,相关测试用例也已调整以匹配更新后的函数签名。

Changes

文件(s) 变更摘要
docs/api.mddocs/example.md 新增文档中 changeOnWheel 属性和 onStep 回调说明;修正文档格式;新增 "on-step" 小节与代码引用。
docs/demo/on-step.tsx 新增演示组件,展示 InputNumber 的 onChange 与 onStep 处理及状态管理。
src/InputNumber.tsxsrc/StepHandler.tsx 更新 onStep 回调的函数签名,添加 emitter 参数(取值:'handler'
tests/click.test.tsx 更新 testInputNumber 函数签名及断言,添加 emitter 参数,匹配 onStep 修改。

Sequence Diagram(s)

sequenceDiagram
    participant U as 用户
    participant I as InputNumber 组件
    participant F as 内部 onInternalStep
    participant C as onStep 回调
    U->>I: 用户触发(键盘/滚轮)
    I->>F: 调用 onInternalStep(up, emitter)
    F->>C: 传入当前值及 {offset, type, emitter}
Loading
sequenceDiagram
    participant D as 演示组件
    participant I as InputNumber 组件
    participant H as 事件处理函数
    D->>I: 传入 onChange 与 onStep 处理函数
    I->>H: 根据用户交互调用 onStep (含emitter 信息)
    H-->>D: 更新组件状态(value 与 emitter)
Loading

Possibly related PRs

Suggested reviewers

  • afc163
  • zombieJ

Poem

我是一只跳跃的小兔,代码花园满是趣,
新增属性添光彩,滚轮键声无处不。
onStep 回调传情意,emitter 轻诉心语,
演示组件欢声笑,代码世界梦悠悠。
🐰🌸

Tip

🌐 Web search-backed reviews and chat
  • We have enabled web search-based reviews and chat for all users. This feature allows CodeRabbit to access the latest documentation and information on the web.
  • You can disable this feature by setting web_search: false in the knowledge_base settings.
  • Please share any feedback in the Discord discussion.

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

@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 (4)
docs/demo/on-step.tsx (1)

6-46: 代码实现清晰,展示了新功能的用法。

示例组件很好地展示了如何使用 emitter 参数来区分不同的输入来源。

不过建议添加以下改进:

  • 为不同的输入来源添加国际化支持
  • 添加更多的用例来展示实际应用场景
-      setEmitter(`interface buttons (${info.type})`);
+      setEmitter(t(`input.source.interface.${info.type}`));

-      setEmitter(`keyboard (${info.type})`);
+      setEmitter(t(`input.source.keyboard.${info.type}`));

-      setEmitter(`mouse wheel (${info.type})`);
+      setEmitter(t(`input.source.wheel.${info.type}`));
tests/click.test.tsx (1)

26-26: 测试用例已正确更新。

测试用例已经正确地验证了新增的 emitter 参数:

  • 更新了 testInputNumber 函数签名
  • 添加了对 emitter 参数的断言
  • 为不同的输入来源(handler、keyboard)添加了相应的测试

建议添加以下测试场景:

  • 鼠标滚轮事件的测试用例
  • 边界情况的测试,如快速切换不同输入来源

Also applies to: 40-40, 46-48, 188-189, 206-207

src/InputNumber.tsx (1)

435-458: 处理逻辑正确实现。

onInternalStep 函数已正确实现了 emitter 参数的处理:

  • 正确接收并传递 emitter 参数
  • 保持了原有的步进逻辑不变

建议添加以下改进:

  • 考虑添加防抖处理,特别是对于滚轮事件
  • 添加输入源切换的过渡处理
+const debouncedOnStep = debounce((value, info) => {
+  onStep?.(value, info);
+}, 100);

-onStep?.(getDecimalValue(stringMode, updatedValue), {
+debouncedOnStep(getDecimalValue(stringMode, updatedValue), {
   offset: shiftKeyRef.current ? getDecupleSteps(step) : step,
   type: up ? 'up' : 'down',
   emitter,
 });
docs/api.md (1)

83-84: 格式调整:readOnly 属性说明
此次对 readOnly 属性的描述进行了格式上的微调,确保说明更加清晰、简洁。

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d9662d5 and 115f686.

📒 Files selected for processing (6)
  • docs/api.md (2 hunks)
  • docs/demo/on-step.tsx (1 hunks)
  • docs/example.md (1 hunks)
  • src/InputNumber.tsx (5 hunks)
  • src/StepHandler.tsx (2 hunks)
  • tests/click.test.tsx (6 hunks)
✅ Files skipped from review due to trivial changes (1)
  • docs/example.md
🔇 Additional comments (6)
src/StepHandler.tsx (2)

23-23: 接口定义更新正确。

onStep 回调函数的签名已正确更新,包含了 emitter 参数。


51-51: 正确传递了 emitter 参数。

onStepMouseDown 函数中正确地传递了 'handler' 作为 emitter 参数。

Also applies to: 55-55

src/InputNumber.tsx (2)

102-102: 接口定义完善。

onStep 回调函数的类型定义已正确更新,包含了 emitter 参数。


515-515: 事件处理正确。

键盘和滚轮事件处理已正确集成 emitter 参数。

Also applies to: 530-530

docs/api.md (2)

85-90: 新增 changeOnWheel 属性说明
已正确添加 changeOnWheel 属性,类型为 Boolean,默认值为 false,并且说明明确指出该属性允许通过鼠标滚轮来调整数值。


145-150: 更新 onStep 回调签名说明
这里新增了 onStep 属性,并在其回调参数中加入了 emitter 参数。不过目前 emitter 的可能取值为 'handler' | 'keydown' | 'wheel',而 PR 目标中提到的取值应为 'handler' | 'keyboard' | 'wheel'。请确认这两者是否一致,如有需要建议修改以保证文档与实现一致。

Copy link

codecov bot commented Feb 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.95%. Comparing base (d9662d5) to head (115f686).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #701   +/-   ##
=======================================
  Coverage   95.95%   95.95%           
=======================================
  Files           6        6           
  Lines         297      297           
  Branches       78       78           
=======================================
  Hits          285      285           
  Misses         12       12           

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

<td>Called when an element gets focus</td>
</tr>
<tr>
<td>onStep</td>
Copy link
Member

Choose a reason for hiding this comment

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

Any reason not to use onChange?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I was tasked with making a request to the server by clicking the arrows and not committing to setting values in the input field. And when entering a value it is through the input field to set it. Accordingly, I track the source of input via onStep and if an arrow was pressed, I forbid onChange to set the value.

@afc163 afc163 merged commit 4c83785 into react-component:master Feb 7, 2025
11 checks passed
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.

2 participants