Skip to content

feat: enhance allowClear prop to support disabled#160

Open
guoyunhe wants to merge 2 commits intoreact-component:masterfrom
guoyunhe:allow-clear-disabled
Open

feat: enhance allowClear prop to support disabled#160
guoyunhe wants to merge 2 commits intoreact-component:masterfrom
guoyunhe:allow-clear-disabled

Conversation

@guoyunhe
Copy link

@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 配置决定可见性。

@gemini-code-assist
Copy link

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

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 45817859-4e19-424e-87cc-6aa9234c7287

📥 Commits

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

📒 Files selected for processing (1)
  • src/BaseInput.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/BaseInput.tsx

总体说明

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

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

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 标题准确概括了本次变更的主要目的:为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

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

@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
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
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
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;
}

@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 (4173498).

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.

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