Skip to content

Conversation

@zombieJ
Copy link
Member

@zombieJ zombieJ commented Nov 15, 2025

Summary by CodeRabbit

发布说明

  • 新功能

    • InputNumber 组件现已支持更多鼠标事件处理器(点击、按下、抬起、移入/移出/移动等),并将这些事件传递给组件根容器与输入元素
    • 支持公开的 disabled 与 readOnly 属性
    • 点击前缀或容器区域时,输入框将自动获取焦点
  • 测试

    • 新增测试,验证点击前缀时输入框会聚焦

@vercel
Copy link

vercel bot commented Nov 15, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
input-number Ready Ready Preview Comment Nov 15, 2025 3:21pm

@coderabbitai
Copy link

coderabbitai bot commented Nov 15, 2025

Caution

Review failed

The pull request is closed.

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

演练

该变更扩展了 InputNumber 组件的公开 API,显式暴露并转发更多输入与鼠标事件属性(包括 onMouse* 系列、value、defaultValue、onInput、onChange、prefix、suffix、disabled、readOnly 等),并添加了内部鼠标按下处理以在点击根容器或前缀/后缀时聚焦输入,同时新增相应测试用例覆盖前缀点击聚焦行为。

变更

队列 / 文件 摘要
InputNumber 公开 API 扩展
src/InputNumber.tsx
更新 InputNumberProps<T>,由原先对 React.InputHTMLAttributes<HTMLInputElement> 的 Omit 改为显式包含 valuedefaultValueonInputonChangeprefixsuffix 及一组鼠标事件(onMouseDownonClickonMouseUponMouseLeaveonMouseMoveonMouseEnteronMouseOut),并通过 Pick<React.HTMLAttributes<HTMLDivElement>, ...> 将 div 的鼠标事件类型纳入。新增 disabled?readOnly? 公共属性。
InputNumber 内部焦点与事件转发
src/InputNumber.tsx
添加 onInternalMouseDown: React.MouseEventHandler<HTMLDivElement>(内部鼠标按下处理),在根容器或前缀/后缀被点击时确保调用 input.focus() 并将事件转发给消费者提供的 onMouseDown。将根容器与底层输入元素的鼠标事件绑定到相应处理器。
属性转发与 restProps 使用
src/InputNumber.tsx
将原先的 inputProps 展开替换为更广泛的 restProps,以便把新增的鼠标事件和其它显式属性转发到底层 <input>。对 props 解构进行了相应调整以保留现有的值解析/格式化/校验逻辑。
前缀焦点测试覆盖
tests/input.test.tsx
新增测试用例 "click prefix should focus input":渲染带前缀的 InputNumber,模拟对前缀的 mouseDown,断言底层输入获得焦点。

序列图

sequenceDiagram
    participant User
    participant Prefix as 前缀/子元素
    participant Root as InputNumber 根容器
    participant Input as 底层 <input>

    User->>Prefix: 鼠标按下 (mouseDown)
    Prefix->>Root: 触发根容器 onMouseDown
    Note over Root: onInternalMouseDown 处理\n(调用 focus 并转发事件)
    Root->>Input: 调用 focus()
    Root->>User: 调用用户的 onMouseDown 回调(若提供)
    Note over Input: 输入获得焦点
Loading

预估代码审查工作量

🎯 3 (中等) | ⏱️ ~20 分钟

  • 需重点检查:
    • InputNumberProps 类型变更对现有调用方的兼容性与类型冲突风险
    • onInternalMouseDown 在不同 DOM 结构(prefix/suffix/自定义子元素)下的行为与事件转发准确性
    • restProps 替换 inputProps 后是否意外转发了不该传入 <input> 的属性或遗漏了必要属性
    • 测试用例对焦断言在不同运行环境(jsdom)下的稳定性

可能相关的 PR

🐰 点一点前缀小兔跳,
鼠标一落输入就到,
事件穿针又转发,
测试护航笑语高。

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题清晰准确地总结了主要变更:添加点击事件处理使输入框获得焦点,与代码更改(新增鼠标事件处理、内部mouseDown处理和焦点管理)完全相关。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

📜 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 96898fe and 44914eb.

📒 Files selected for processing (1)
  • src/InputNumber.tsx (5 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

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @zombieJ, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue where clicking on non-input areas of the InputNumber component, such as the prefix or suffix, did not automatically focus the input field. The changes refactor event handling to capture mouse events at the component's root level and programmatically focus the internal input element when a click occurs outside the input itself. This enhancement improves the component's usability and consistency with expected UI behavior.

Highlights

  • Input Focus on Click: Implemented a mechanism to ensure the internal input element gains focus when any part of the InputNumber component (e.g., prefix, suffix, or wrapper) is clicked, not just the input field itself.
  • Event Handling Refinement: Mouse event handlers are now managed at the root div level of the InputNumber component, with a specific onMouseDown handler to direct focus to the input.
  • New Test Case: Added a unit test to verify that clicking the prefix successfully focuses the input, ensuring the fix works as intended.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@codecov
Copy link

codecov bot commented Nov 15, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.30%. Comparing base (5613cbc) to head (44914eb).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #727      +/-   ##
==========================================
+ Coverage   95.23%   95.30%   +0.06%     
==========================================
  Files           6        6              
  Lines         294      298       +4     
  Branches       81       83       +2     
==========================================
+ Hits          280      284       +4     
  Misses         14       14              

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

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a fix to focus the input element when other parts of the InputNumber component, such as a prefix, are clicked. This is achieved by adding an onMouseDown handler to the component's wrapper element. The changes also include updating the component's props interface to correctly handle mouse events on the wrapper and adding a test case to verify the new behavior. My review includes one suggestion to prevent a potential runtime error by adding a null check.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
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 (3)
tests/input.test.tsx (1)

241-249: 前缀点击聚焦用例合理,可考虑适当扩展覆盖面

这个用例准确验证了修复目标:点击 prefix 时,input 会获得焦点,断言也很直接清晰,和组件当前行为匹配。

可以作为后续优化考虑再补一两个类似用例,例如点击 suffix、整个 .rc-input-number 容器或步进按钮时同样会聚焦输入框,以便完整覆盖 “点击 wrapper 内任意区域应聚焦 input” 的约定。

src/InputNumber.tsx (2)

55-83: onMouse 事件从 input 属性切到 div 属性,类型与行为都有所改变,请确认是否是预期 API*

这里通过:

  • InputNumberProps 中从 InputHTMLAttributes<HTMLInputElement>Omit 掉所有 mouse 事件键(Line 55-70),
  • 再用 Pick<HTMLAttributes<HTMLDivElement>, ...> 引入同名 mouse 事件键(Line 71-80),
  • 并在渲染时把这些 handler 直接挂到最外层 <div> 上(Line 669-675),

实际上把 onMouseDown/onClick/... 等 props 的事件目标从 input 改成了 wrapper div

  • TypeScript 维度:这些回调现在推断为 React.MouseEvent<HTMLDivElement>,而不是之前的 React.MouseEvent<HTMLInputElement>。如果用户在回调签名中显式写了 event: React.MouseEvent<HTMLInputElement>,会出现类型不兼容或需要改动。
  • 运行时维度:点击 input 时事件依然会冒泡到 wrapper,因此回调仍会触发,但 event.currentTarget 现在是 div 而不是 input;依赖 event.currentTarget 去读取 value 或做类型断言为 HTMLInputElement 的代码会被破坏。另一方面,点击 prefix/suffix 时,同一个回调现在也会触发,这属于行为增强。

从“修复点击 prefix 聚焦 input”的 PR 目标来看,这个改动稍微超出了一点范围,而且可能带来微妙的破坏性变更。建议:

  • 明确确认:是否希望今后 onMouseDown/onClick/... 的语义就是「以 wrapper 为 currentTarget」?如果是,建议在 CHANGELOG 或文档里标明这个行为变更;
  • 如果希望兼顾原有 TS 类型/语义,可以考虑自定义一组更宽泛的 handler 类型(例如 React.MouseEventHandler<HTMLInputElement | HTMLDivElement>),或区分 input 与 wrapper 的事件 props(例如新增 onWrapperMouseDown),从而减少对现有用户代码的冲击。

Also applies to: 170-178, 669-675


597-605: onInternalMouseDown 聚焦逻辑工作正常,但可以更好地与 disabled/用户回调协作

当前实现为:

  • 在 wrapper 的 onMouseDown 中调用 onInternalMouseDown
  • event.target !== inputRef.current 则调用 inputRef.current.focus(),然后再调用用户传入的 onMouseDown(Line 597-604);
  • ...restProps 只下发到 <input>(Line 708),因此 input 本身不再直接接收 onMouseDown 等鼠标事件 props。

行为上可以满足「点击 prefix(或其它子节点)时聚焦 input」的需求,但有两点可以考虑优化:

  1. disabled 时是否应跳过聚焦
    目前即便 disabled 为 true,点击 prefix 仍会尝试 focus()。浏览器一般不会真正聚焦到 disabled input,但从语义上看可以在 onInternalMouseDown 里先判断 disabled,避免多余操作,也更符合“完全不可交互”的直觉。

  2. 用户无法通过 preventDefault 阻止聚焦
    现在是先聚焦再调用 onMouseDown?.(event)。如果有需求让外部通过 event.preventDefault() 来阻止自动聚焦,可以改为:

    • 先调用用户的 onMouseDown
    • !event.defaultPrevented && event.target !== inputRef.current,再去 focus()

这两点都不影响当前修复的正确性,只是让行为对外更可控、与常见交互约定更一致,可视情况选择是否调整。

Also applies to: 708-708

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5613cbc and 96898fe.

📒 Files selected for processing (2)
  • src/InputNumber.tsx (5 hunks)
  • tests/input.test.tsx (1 hunks)
🧰 Additional context used
🧠 Learnings (3)
📚 Learning: 2024-10-08T21:56:37.546Z
Learnt from: bombillazo
Repo: react-component/input-number PR: 644
File: tests/validator.test.tsx:34-83
Timestamp: 2024-10-08T21:56:37.546Z
Learning: In `tests/validator.test.tsx`, the test cases focus on the `validator` function. When the validator condition is not met, the input value should not change. The `formatter` is tested separately in another file.

Applied to files:

  • tests/input.test.tsx
📚 Learning: 2024-09-29T06:18:11.993Z
Learnt from: bombillazo
Repo: react-component/input-number PR: 644
File: src/InputNumber.tsx:173-174
Timestamp: 2024-09-29T06:18:11.993Z
Learning: In `src/InputNumber.tsx`, within the `InternalInputNumber` component, the state variables `prevValueRef` and `inputValueRef` are typed as `string | number` to maintain consistency with existing code.

Applied to files:

  • tests/input.test.tsx
  • src/InputNumber.tsx
📚 Learning: 2024-10-08T21:56:37.546Z
Learnt from: bombillazo
Repo: react-component/input-number PR: 644
File: src/InputNumber.tsx:393-395
Timestamp: 2024-10-08T21:56:37.546Z
Learning: The `InputNumber` component does not use error states or messages; it is designed to prevent the value from updating if validation fails without displaying additional feedback to the user.

Applied to files:

  • src/InputNumber.tsx

@zombieJ zombieJ merged commit 32e0ed8 into master Nov 15, 2025
9 of 10 checks passed
@zombieJ zombieJ deleted the event branch November 15, 2025 15:24
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