Skip to content

Commit af0dbc0

Browse files
authored
feat: support semantic classNames and styles for clear (#162)
1 parent c4b6e72 commit af0dbc0

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

src/BaseInput.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,15 @@ const BaseInput = React.forwardRef<HolderRef, BaseInputProps>((props, ref) => {
9696
// Do not trigger onBlur when clear input
9797
// https://github.com/ant-design/ant-design/issues/31200
9898
onMouseDown={(e) => e.preventDefault()}
99-
className={clsx(clearIconCls, {
100-
[`${clearIconCls}-hidden`]: !needClear,
101-
[`${clearIconCls}-has-suffix`]: !!suffix,
102-
})}
99+
className={clsx(
100+
clearIconCls,
101+
{
102+
[`${clearIconCls}-hidden`]: !needClear,
103+
[`${clearIconCls}-has-suffix`]: !!suffix,
104+
},
105+
classNames?.clear,
106+
)}
107+
style={styles?.clear}
103108
>
104109
{iconNode}
105110
</button>

src/interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface CommonInputProps {
2424
affixWrapper?: string;
2525
prefix?: string;
2626
suffix?: string;
27+
clear?: string;
2728
groupWrapper?: string;
2829
wrapper?: string;
2930
variant?: string;
@@ -32,6 +33,7 @@ export interface CommonInputProps {
3233
affixWrapper?: CSSProperties;
3334
prefix?: CSSProperties;
3435
suffix?: CSSProperties;
36+
clear?: CSSProperties;
3537
};
3638
allowClear?: boolean | { disabled?: boolean; clearIcon?: ReactNode };
3739
}

tests/BaseInput.test.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,20 @@ describe('BaseInput', () => {
236236
prefixCls="rc-input"
237237
prefix="prefix"
238238
addonBefore="addon"
239+
allowClear
239240
styles={{
240241
affixWrapper: {
241242
color: 'red',
242243
},
243244
prefix: {
244245
color: 'blue',
245246
},
247+
clear: {
248+
color: 'green',
249+
},
246250
}}
247251
>
248-
<input />
252+
<input defaultValue="test" />
249253
</BaseInput>,
250254
);
251255

@@ -258,6 +262,11 @@ describe('BaseInput', () => {
258262
container.container.querySelector<HTMLSpanElement>('.rc-input-prefix')
259263
?.style.color,
260264
).toBe('blue');
265+
expect(
266+
container.container.querySelector('.rc-input-clear-icon'),
267+
).toHaveStyle({
268+
color: 'green',
269+
});
261270
});
262271

263272
it('with addon and disabled', () => {

tests/__snapshots__/index.test.tsx.snap

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`Input allowClear classNames and styles should work 1`] = `
44
<div>
55
<span
6-
class="rc-input-affix-wrapper custom-class"
6+
class="rc-input-affix-wrapper rc-input-affix-wrapper-input-with-clear-btn custom-class"
77
style="background-color: red;"
88
>
99
<span
@@ -22,6 +22,14 @@ exports[`Input allowClear classNames and styles should work 1`] = `
2222
class="rc-input-suffix custom-suffix"
2323
style="color: yellow;"
2424
>
25+
<button
26+
class="rc-input-clear-icon rc-input-clear-icon-has-suffix custom-clear"
27+
style="color: orange;"
28+
tabindex="-1"
29+
type="button"
30+
>
31+
32+
</button>
2533
<span
2634
class="rc-input-show-count-suffix rc-input-show-count-has-suffix custom-count"
2735
style="color: green;"

tests/index.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,18 +298,21 @@ describe('Input allowClear', () => {
298298
prefixCls="rc-input"
299299
prefix="prefix"
300300
suffix="suffix"
301+
allowClear
301302
className="custom-class"
302303
style={{ backgroundColor: 'red' }}
303304
classNames={{
304305
input: 'custom-input',
305306
prefix: 'custom-prefix',
306307
suffix: 'custom-suffix',
308+
clear: 'custom-clear',
307309
count: 'custom-count',
308310
}}
309311
styles={{
310312
input: { color: 'red' },
311313
prefix: { color: 'blue' },
312314
suffix: { color: 'yellow' },
315+
clear: { color: 'orange' },
313316
count: { color: 'green' },
314317
}}
315318
/>
@@ -362,6 +365,12 @@ describe('Input allowClear', () => {
362365
/>
363366
</>,
364367
);
368+
expect(container.querySelector('.rc-input-clear-icon')).toHaveClass(
369+
'custom-clear',
370+
);
371+
expect(container.querySelector('.rc-input-clear-icon')).toHaveStyle({
372+
color: 'orange',
373+
});
365374
expect(container).toMatchSnapshot();
366375
});
367376
});

0 commit comments

Comments
 (0)