Skip to content

Conversation

@hawwei913
Copy link

@hawwei913 hawwei913 commented Dec 7, 2025

This pull request introduces a new "allow clear" feature to the InputNumber component, enabling users to clear the input value using a clear icon. The feature is highly customizable, allowing for a custom icon and a custom value to reset to when cleared. Documentation and demos have been updated to reflect this new functionality.

New "allow clear" feature:

  • Added the allowClear prop to InputNumber, supporting both boolean and object forms for custom clear icon and value, and the onClear callback prop for handling clear actions. [1] [2]
  • Implemented the clear button logic in InputNumber, including rendering, event handling, and conditional display based on input state. [1] [2]
  • Added new CSS styles for the clear icon and its hidden state in assets/index.less.

Documentation and demos:

  • Updated API documentation in docs/api.md to describe the new allowClear and onClear props.
  • Added a comprehensive demo for the allow clear feature in docs/demo/allow-clear.tsx and referenced it in the example documentation. [1] [2]

Related Issue: ant-design/ant-design#50885

Summary by CodeRabbit

  • 新功能

    • InputNumber 组件新增清除功能(allowClear),支持自定义清除图标与清除后默认值,新增 onClear 回调。
  • 文档

    • 更新 API 文档以说明 allowClear 与 onClear。
    • 新增示例演示多种 allowClear 用法(基础、前后缀、自定义图标与默认值)。
  • 样式

    • 添加清除图标的样式与可见/隐藏变体。

✏️ Tip: You can customize this high-level summary in your review settings.

@vercel
Copy link

vercel bot commented Dec 7, 2025

@hawwei913 is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Dec 7, 2025

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.

Walkthrough

为 InputNumber 组件新增可清除值的功能,包含属性声明(allowClear、onClear)、渲染与清除逻辑、样式类以及示例与文档更新。

Changes

内聚体 / 文件(s) 变更摘要
样式更新
assets/index.less
新增 .{inputNumberPrefixCls}-clear-icon(清除按钮样式)和嵌套的 .{inputNumberPrefixCls}-clear-icon-hidden(隐藏状态)两个 CSS 选择器,用于控制清除图标的外观与显示。
核心实现
src/InputNumber.tsx
InputNumberProps 中添加 `allowClear?: boolean
文档更新(API)
docs/api.md
在 API 表格中新增 allowClearonClear 两行,说明类型、默认值与用途。
演示代码
docs/demo/allow-clear.tsx
新增示例组件,展示四种场景:基础清除、带前缀/后缀、自定义 clearValue、以及自定义 clearIcon 的用法。
文档索引
docs/example.md
新增 "allow-clear" 小节并引用新增演示文件。

Estimated code review effort

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

需重点检查:

  • src/InputNumber.tsx 中清除逻辑与状态更新(triggerValueUpdate)与现有边界条件(disabled/readOnly、范围校验)的交互;
  • allowClear 的类型签名与默认行为(boolean vs object);
  • onClear 的触发时机以及是否影响其他事件流;
  • 新增样式类名与现有类命名空间的一致性。

Suggested reviewers

  • zombieJ

Poem

🐰
轻点小爪按一按,数字瞬间像风散,
图标可换又可藏,示例文档都装满。
InputNumber 又添彩,嗨皮开发别担心~ 🥕✨

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 The PR title 'feat: support allowClear' directly and clearly describes the main feature being added—support for the allowClear functionality in the InputNumber component, which is the primary objective of this pull request.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 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 65a6681 and b9afe24.

📒 Files selected for processing (2)
  • docs/api.md (1 hunks)
  • src/InputNumber.tsx (6 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
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.
📚 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:

  • docs/api.md
  • 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:

  • docs/api.md
  • src/InputNumber.tsx
🔇 Additional comments (1)
src/InputNumber.tsx (1)

99-123: 新增 allowClear / onClear 的 API 设计与实现基本合理

  • allowClear?: boolean | { clearIcon?: React.ReactNode; clearValue?: T } 与文档、Demo 的用法一致,并且将 clearValue 绑定到泛型 T,对 TS 使用者比较友好。
  • onClear?: () => void 只在清空动作时触发,且在内部通过 triggerValueUpdate 走统一的数值更新/校验流程后再调用,事件顺序清晰。

这里整体看起来没有明显逻辑问题。建议后续只需确保外部文档中的类型声明也保持为 () => void 即可(见 docs/api.md 评论)。

Also applies to: 160-172


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 @hawwei913, 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 enhances the InputNumber component by integrating a highly customizable "allow clear" feature. Users can now easily clear the input field using a dedicated icon, with options to define a custom icon and a specific value to reset to upon clearing. This functionality is fully documented and demonstrated through new examples.

Highlights

  • New allowClear Prop: Introduced a new allowClear prop to the InputNumber component, enabling users to clear the input value. This prop supports both a boolean value for simple enabling/disabling and an object for advanced customization, including a clearIcon and a clearValue to reset to.
  • onClear Callback: Added an onClear callback prop, which is triggered when the input value is cleared.
  • Clear Button Logic: Implemented the rendering, event handling, and conditional display logic for the clear button within the InputNumber component.
  • Styling: New CSS styles were added in assets/index.less to support the clear icon and its hidden state.
  • Documentation & Demos: The API documentation (docs/api.md) has been updated to reflect the new props, and a new comprehensive demo (docs/demo/allow-clear.tsx) has been added to showcase the feature's functionality and customization options.
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.

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 new allowClear feature to the InputNumber component, which is a great addition. The implementation is solid, and the new feature is well-documented with demos. I have a few suggestions to improve code clarity and fix some minor inconsistencies in the documentation. Specifically, I've suggested a refactoring in InputNumber.tsx to simplify the logic and pointed out a potentially unused CSS class. I've also recommended updates to the API documentation for the allowClear and onClear props to make them more accurate.

docs/api.md Outdated
</tr>
<tr>
<td>allowClear</td>
<td>boolean | { clearIcon?: React.ReactNode; clearValue: T }</td>
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The type for allowClear is documented as having a required clearValue property when it's an object. However, in the implementation (src/InputNumber.tsx) and the demo (docs/demo/allow-clear.tsx), clearValue is optional. Please update the documentation to reflect that clearValue is optional to avoid confusion for users of the component.

It should be clearValue?: T.

Suggested change
<td>boolean | { clearIcon?: React.ReactNode; clearValue: T }</td>
<td>boolean | { clearIcon?: React.ReactNode; clearValue?: T }</td>

</tr>
<tr>
<td>onClear</td>
<td>Function</td>
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The type for onClear is documented as Function, which is a bit vague. It would be more helpful to provide the actual function signature, which is () => void.

Suggested change
<td>Function</td>
<td>() => void</td>

Comment on lines 666 to 691
const iconNode =
typeof allowClear === 'object' && allowClear.clearIcon ? allowClear.clearIcon : '✖';

const onInternalClear = () => {
const value = getMiniDecimal(typeof allowClear === 'object' && allowClear.clearValue);

triggerValueUpdate(value, false);
};

clearButton = (
<button
type="button"
tabIndex={-1}
onClick={() => {
onInternalClear();
onClear?.();
}}
onMouseDown={(e) => e.preventDefault()}
className={clsx(`${prefixCls}-clear-icon`, {
[`${clearIconCls}-hidden`]: !needClear,
[`${clearIconCls}-has-suffix`]: !!suffix,
})}
>
{iconNode}
</button>
);
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The logic for handling allowClear can be simplified to improve readability and reduce code duplication. You can destructure clearIcon and clearValue from an options object, and consolidate the onClick handler.

Additionally, the CSS class ${clearIconCls}-has-suffix is being added, but there are no corresponding styles for it in assets/index.less. If this class is not used, it should be removed to avoid dead code. I've kept it in the suggestion below, but please consider removing it if it's not needed.

    const clearOptions = typeof allowClear === 'object' ? allowClear : {};
    const { clearIcon = '✖', clearValue } = clearOptions;

    const onInternalClear = () => {
      const value = getMiniDecimal(clearValue);
      triggerValueUpdate(value, false);
      onClear?.();
    };

    clearButton = (
      <button
        type="button"
        tabIndex={-1}
        onClick={onInternalClear}
        onMouseDown={(e) => e.preventDefault()}
        className={clsx(`${prefixCls}-clear-icon`, {
          [`${clearIconCls}-hidden`]: !needClear,
          [`${clearIconCls}-has-suffix`]: !!suffix,
        })}
      >
        {clearIcon}
      </button>
    );

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: 2

🧹 Nitpick comments (2)
assets/index.less (1)

108-117: 建议增强按钮的交互样式。

清除按钮缺少以下交互反馈:

  • 缺少 cursor: pointer 使鼠标悬停时显示为可点击状态
  • 缺少悬停(:hover)和聚焦(:focus)样式以提供视觉反馈
  • 建议添加焦点可见性样式以改善键盘导航的可访问性

应用此差异以改进按钮样式:

   &-clear-icon {
     padding: 0;
     font-size: 12px;
     background: none;
     border: none;
+    cursor: pointer;
+    color: rgba(0, 0, 0, 0.45);
+    transition: color 0.3s;
+
+    &:hover {
+      color: rgba(0, 0, 0, 0.85);
+    }
+
+    &:focus-visible {
+      outline: 2px solid #1890ff;
+      outline-offset: 2px;
+    }

     &-hidden {
       display: none;
     }
   }
docs/demo/allow-clear.tsx (1)

33-52: 建议添加 onClear 回调的演示。

当前演示涵盖了 allowClear 的各种配置(基础用法、自定义 clearValue、自定义 clearIcon),但缺少对 onClear 回调的演示。建议添加一个示例来展示清除操作时的回调用法。

可以在现有的某个 InputNumber 中添加 onClear 回调:

<InputNumber
  style={{ width: 200 }}
  value={value}
  onChange={onChange}
  allowClear={true}
  onClear={() => console.log('onClear triggered')}
/>
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fdde44f and 65a6681.

📒 Files selected for processing (5)
  • assets/index.less (1 hunks)
  • docs/api.md (1 hunks)
  • docs/demo/allow-clear.tsx (1 hunks)
  • docs/example.md (1 hunks)
  • src/InputNumber.tsx (6 hunks)
🧰 Additional context used
🧠 Learnings (2)
📚 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:

  • docs/demo/allow-clear.tsx
  • docs/api.md
  • 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:

  • docs/demo/allow-clear.tsx
  • docs/api.md
🔇 Additional comments (6)
docs/example.md (1)

63-66: 文档更新符合规范。

新增的 allow-clear 示例章节格式与其他示例保持一致,引用路径正确。

docs/demo/allow-clear.tsx (1)

6-12: 状态管理正确。

使用 string | number 类型与 InputNumber 的值类型保持一致,onChange 处理逻辑清晰。

src/InputNumber.tsx (4)

99-99: 类型定义准确。

allowClearonClear 的类型定义合理,支持布尔值或自定义配置对象,与 API 文档一致(需修正文档中的小错误)。

Also applies to: 123-123


748-748: 清除按钮的渲染位置合理。

清除按钮放置在输入框之后、suffix 之前,符合常见的 UI 模式和用户预期。


679-682: Verify whether onClear should fire only when the value actually changes.

Currently, the onClear callback is invoked unconditionally whenever the clear button is clicked, even if the value does not change (for example, when clearValue is the same as the current value). Clarify whether this is the intended behavior or if onClear should only be triggered when the value is actually modified.

Consider checking whether triggerValueUpdate indicates a value change occurred, and conditionally invoke onClear only in that case.


669-673: No action needed — the code correctly handles clearing.

When allowClear is true or an object without a clearValue property, getMiniDecimal(undefined) returns an empty Decimal object with isEmpty() returning true. The component correctly treats this as a cleared state, and downstream logic checks isEmpty() before converting to a numeric value. The implementation is correct.

Comment on lines +684 to +687
className={clsx(`${prefixCls}-clear-icon`, {
[`${clearIconCls}-hidden`]: !needClear,
[`${clearIconCls}-has-suffix`]: !!suffix,
})}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

CSS 类名可能未定义。

第 686 行引用了 CSS 类 ${clearIconCls}-has-suffix,但在 assets/index.less 中未找到该类的定义。这可能导致该类名不起作用。

如果该类名用于在存在 suffix 时调整清除按钮的位置,请在 assets/index.less 中添加相应的样式定义。如果不需要,请移除此类名。

🤖 Prompt for AI Agents
In src/InputNumber.tsx around lines 684 to 687, the class
`${clearIconCls}-has-suffix` is being applied but no corresponding CSS exists in
assets/index.less; either add a style block for `.${clearIconCls}-has-suffix` in
assets/index.less that adjusts the clear-button positioning when a suffix is
present (mirror the existing `.${clearIconCls}-hidden`/base clear-icon rules and
include margin/positioning tweaks), or remove the `${clearIconCls}-has-suffix`
entry from the clsx call if no special styling is required.

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