Skip to content

feat: enhance allowClear prop to support disabled#160

Merged
zombieJ merged 3 commits intoreact-component:masterfrom
guoyunhe:allow-clear-disabled
Mar 10, 2026
Merged

feat: enhance allowClear prop to support disabled#160
zombieJ merged 3 commits intoreact-component:masterfrom
guoyunhe:allow-clear-disabled

Conversation

@guoyunhe
Copy link
Contributor

@guoyunhe guoyunhe commented Mar 5, 2026

在一些情况下,allowClear 和 clearIcon 分别由不同的变量控制。

以前的写法(略显杂乱):

<Input allowClear={shouldDisabled ? false : { clearIcon: <MyIcon /> }} />

现在的写法(优雅一些):

<Input allowClear={{ disabled: shouldDisabled, clearIcon: <MyIcon /> }} />

尤其是在 ConfigProvider 里,我们更常需要替换 clearIcon 而不改变 allowClear 的开关状态。因此增加一个单独的 disabled 属性,有利于我们更灵活处理多种情况。

Summary by CodeRabbit

  • 新功能

    • 清除按钮支持对象式配置中的 disabled 标志,可针对单个输入实例单独禁用清除操作,提升交互控制。
  • 变更

    • 清除图标的显示与交互逻辑已调整,更严格地依据禁用/只读与 allowClear 配置决定可见性。
  • 测试

    • 新增用例覆盖在对象式 allowClear 中禁用清除图标的行为验证。

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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!

此拉取请求旨在增强输入组件的 allowClear 属性,使其能够通过一个内嵌的 disabled 属性来控制清除图标的禁用状态。这一改进简化了在某些场景下(例如在 ConfigProvider 中统一配置清除图标但又需要局部禁用清除功能时)的逻辑处理,提高了组件的灵活性和易用性。

Highlights

  • 增强 allowClear 属性: allowClear 属性现在支持一个对象,该对象可以包含一个 disabled 属性,从而允许更灵活地控制清除图标的可用性。
  • 简化条件逻辑: 通过引入 allowClear.disabled,开发者可以更简洁地处理清除图标的显示和禁用逻辑,避免了之前复杂的条件表达式。
  • 更新类型定义: src/interface.ts 中 allowClear 属性的类型定义已更新,以反映对新 disabled 属性的支持。
  • 调整清除逻辑: src/BaseInput.tsx 中用于判断是否需要显示清除图标的逻辑已更新,以正确考虑 allowClear.disabled 的状态。

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/BaseInput.tsx
    • 更新了清除图标的显示逻辑,以支持 allowClear 属性中的 disabled 字段。
  • src/interface.ts
    • 修改了 allowClear 属性的类型定义,使其可以接受一个包含可选 disabled 属性的对象。
    • 调整了 InputProps 接口的继承格式,使其更清晰。
Activity
  • 此拉取请求自创建以来,尚未有其他人工活动(例如评论或审查)。
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.

@coderabbitai
Copy link

coderabbitai bot commented Mar 5, 2026

总体说明

PR 修改了输入组件中清除按钮的行为:在 allowClear 配置中新增可选的 disabled 字段,并在清除按钮可见性判断中加入对该字段的检查,确保当 allowClear 为对象且显式禁用时不展示清除图标。相应的类型定义在 src/interface.ts 中已更新;新增测试覆盖 allowClear={{ disabled: true }} 场景。

Walkthrough

将 clear 按钮的可见性条件从仅检查 !disabled && !readOnly && value 改为还需满足 allowClear === true || !allowClear.disabled;同时将 allowClear 类型扩展为可含 disabled?: boolean,并新增测试验证禁用情形。

Changes

Cohort / File(s) Summary
清除按钮条件逻辑
src/BaseInput.tsx
调整 needClear 条件,新增对 allowClear 为对象时 disabled 字段的检查,影响清除图标/按钮的显示时机。
类型定义更新
src/interface.ts
CommonInputProps.allowClear 类型由 `boolean
测试
tests/index.test.tsx
新增用例:验证 allowClear={{ disabled: true }} 时不显示清除图标。

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 分钟

Possibly related PRs

Suggested reviewers

  • afc163

诗歌

🐰 我是兔子来报晓,
清除按钮换新貌,
allowClear 多了旗帜,
disabled 轻声来把关,
输入更清晰,心自好 🥕✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR标题清晰准确地总结了主要变更,即为allowClear属性增加了disabled字段支持,使其能够在对象配置中控制清除功能的开启/关闭状态。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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.

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

这个 PR 增强了 allowClear prop,使其支持 disabled 状态,这是一个很好的补充,可以实现更灵活的控制。实现是正确的,但缺少针对此新功能的测试。请添加测试用例,覆盖 allowClear 对象中 disabledtruefalse 和省略时的场景,以确保该功能按预期工作并防止未来出现回归。我还有一个关于提高代码可读性的小建议。

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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/BaseInput.tsx`:
- Around line 77-81: The current needClear calculation uses a truthy check on
value which treats 0 and 0n as empty; change the condition in BaseInput (the
needClear boolean calculation that references disabled, readOnly, value, and
allowClear) to check presence explicitly (e.g., value != null or value !==
undefined && value !== null) so numeric zero values count as "has value", and
extract this presence test into a small helper (e.g., isValuePresent or
hasValue) and reuse that helper at the other usage around Line 118 so both the
clear-button rendering and the style/state logic use the identical presence
check.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2ec2e8e8-86e6-4d54-bc12-a3a7b598a8b0

📥 Commits

Reviewing files that changed from the base of the PR and between da542f7 and 378fbc8.

📒 Files selected for processing (2)
  • src/BaseInput.tsx
  • src/interface.ts

!disabled &&
!readOnly &&
value &&
!(typeof allowClear === 'object' && allowClear.disabled);
Copy link
Member

Choose a reason for hiding this comment

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

null 也是 object

Copy link
Contributor Author

Choose a reason for hiding this comment

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

外层的 if (allowClear) 已经确保不为 null 了,因此这里就没有再判断了

suffix?: CSSProperties;
};
allowClear?: boolean | { clearIcon?: ReactNode };
allowClear?: boolean | { disabled?: boolean; clearIcon?: ReactNode };
Copy link
Member

Choose a reason for hiding this comment

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

antd 里有没有类似的属性?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

antd 里是直接 import 的这个类型复用了

Copy link
Member

Choose a reason for hiding this comment

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

我意思是 antd 里是否有 config 里带有 enabled or disabled 的属性可以参考一下。免得未来这个名字出现多种情况

Copy link
Contributor Author

Choose a reason for hiding this comment

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

搜索了一下,大部分使用的是 disabled,比如 useClosable:

interface ClosableCollection {
  closable?: ClosableType;
  closeIcon?: ReactNode;
  disabled?: boolean;
}

比如 WaveConfig:

export interface WaveConfig {
  disabled?: boolean;
  showEffect?: ShowWaveEffect;
}

而 enabled 只有一处使用:

export interface MaskConfig {
  enabled?: boolean;
  blur?: boolean;
  closable?: boolean;
}

Copy link
Member

Choose a reason for hiding this comment

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

赞,那加个 test。我没其他问题了

@codecov
Copy link

codecov bot commented Mar 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.57%. Comparing base (da542f7) to head (9bec969).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #160      +/-   ##
==========================================
+ Coverage   97.53%   97.57%   +0.03%     
==========================================
  Files           4        4              
  Lines         203      206       +3     
  Branches       75       80       +5     
==========================================
+ Hits          198      201       +3     
  Misses          5        5              

☔ 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

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

🧹 Nitpick comments (1)
tests/index.test.tsx (1)

281-290: 测试用例正确验证了新功能的核心行为。

测试逻辑与 src/BaseInput.tsx 中的 needClear 条件(第 81 行:!(typeof allowClear === 'object' && allowClear.disabled))相匹配,并且遵循了现有测试(第 265-279 行)的模式。

建议考虑增加补充测试用例以提高覆盖率:

  1. disabled: false 时应显示清除图标 - 验证显式设置为 false 时的正确行为
  2. disabledclearIcon 组合使用 - 验证 PR 描述中的主要用例
💡 建议的补充测试用例
   it('should not support allowClear when allowClear is disabled', () => {
     const { container } = render(
       <Input
         prefixCls="rc-input"
         allowClear={{ disabled: true }}
         defaultValue="111"
       />,
     );
     expect(container.querySelector('.rc-input-clear-icon-hidden')).toBeTruthy();
   });
+
+  it('should support allowClear when allowClear.disabled is false', () => {
+    const { container } = render(
+      <Input
+        prefixCls="rc-input"
+        allowClear={{ disabled: false }}
+        defaultValue="111"
+      />,
+    );
+    expect(container.querySelector('.rc-input-clear-icon-hidden')).toBeFalsy();
+  });
+
+  it('should hide clear icon but keep custom clearIcon when disabled', () => {
+    const { container } = render(
+      <Input
+        prefixCls="rc-input"
+        allowClear={{ disabled: true, clearIcon: <span className="custom-clear">X</span> }}
+        defaultValue="111"
+      />,
+    );
+    expect(container.querySelector('.rc-input-clear-icon-hidden')).toBeTruthy();
+    expect(container.querySelector('.custom-clear')).toBeTruthy();
+  });
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/index.test.tsx` around lines 281 - 290, Add two supplementary tests in
tests/index.test.tsx mirroring existing patterns: 1) render <Input allowClear={{
disabled: false }} defaultValue="111" /> and assert the clear icon is shown
(opposite of the current test) to validate the needClear logic in BaseInput.tsx
(condition using !(typeof allowClear === 'object' && allowClear.disabled)); 2)
render <Input allowClear={{ disabled: true, clearIcon: <span>X</span> }}
defaultValue="111" /> (and a variant with disabled: false) to verify clearIcon
is respected when combined with disabled flag; use the same
container.querySelector/assert pattern as surrounding tests to check
presence/absence of .rc-input-clear-icon-hidden or the custom clear icon.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@tests/index.test.tsx`:
- Around line 281-290: Add two supplementary tests in tests/index.test.tsx
mirroring existing patterns: 1) render <Input allowClear={{ disabled: false }}
defaultValue="111" /> and assert the clear icon is shown (opposite of the
current test) to validate the needClear logic in BaseInput.tsx (condition using
!(typeof allowClear === 'object' && allowClear.disabled)); 2) render <Input
allowClear={{ disabled: true, clearIcon: <span>X</span> }} defaultValue="111" />
(and a variant with disabled: false) to verify clearIcon is respected when
combined with disabled flag; use the same container.querySelector/assert pattern
as surrounding tests to check presence/absence of .rc-input-clear-icon-hidden or
the custom clear icon.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9b578598-8999-4c67-b588-8164bc278372

📥 Commits

Reviewing files that changed from the base of the PR and between 4173498 and 9bec969.

📒 Files selected for processing (1)
  • tests/index.test.tsx

@zombieJ zombieJ merged commit 25e4044 into react-component:master Mar 10, 2026
7 checks passed
@guoyunhe guoyunhe deleted the allow-clear-disabled branch March 10, 2026 06:11
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