Skip to content

Commit 25e4044

Browse files
feat: enhance allowClear prop to support disabled (#160)
* feat: enhance allowClear prop to support disabled * Update src/BaseInput.tsx Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * feat: add test for allowClear prop when disabled --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent da542f7 commit 25e4044

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/BaseInput.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ const BaseInput = React.forwardRef<HolderRef, BaseInputProps>((props, ref) => {
7474
// ================== Clear Icon ================== //
7575
let clearIcon: ReactNode = null;
7676
if (allowClear) {
77-
const needClear = !disabled && !readOnly && value;
77+
const needClear =
78+
!disabled &&
79+
!readOnly &&
80+
value &&
81+
!(typeof allowClear === 'object' && allowClear.disabled);
7882
const clearIconCls = `${prefixCls}-clear-icon`;
7983
const iconNode =
8084
typeof allowClear === 'object' && allowClear?.clearIcon

src/interface.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export interface CommonInputProps {
3333
prefix?: CSSProperties;
3434
suffix?: CSSProperties;
3535
};
36-
allowClear?: boolean | { clearIcon?: ReactNode };
36+
allowClear?: boolean | { disabled?: boolean; clearIcon?: ReactNode };
3737
}
3838

3939
type DataAttr = Record<`data-${string}`, string>;
@@ -86,7 +86,8 @@ export interface CountConfig {
8686
}
8787

8888
export interface InputProps
89-
extends CommonInputProps,
89+
extends
90+
CommonInputProps,
9091
Omit<
9192
InputHTMLAttributes<HTMLInputElement>,
9293
'size' | 'prefix' | 'type' | 'value'

tests/index.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,17 @@ describe('Input allowClear', () => {
278278
});
279279
});
280280

281+
it('should not support allowClear when allowClear is disabled', () => {
282+
const { container } = render(
283+
<Input
284+
prefixCls="rc-input"
285+
allowClear={{ disabled: true }}
286+
defaultValue="111"
287+
/>,
288+
);
289+
expect(container.querySelector('.rc-input-clear-icon-hidden')).toBeTruthy();
290+
});
291+
281292
it('classNames and styles should work', () => {
282293
const { container } = render(
283294
<>

0 commit comments

Comments
 (0)