Skip to content

Commit fe176c3

Browse files
authored
feat: support onClear callback (#71)
* feat: support onClear callback * fix: fix * fix: fix * fix: fix * fix: fix * fix: fix
1 parent c18cc42 commit fe176c3

6 files changed

Lines changed: 34 additions & 3 deletions

File tree

docs/examples/addon.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import Input from 'rc-input';
12
import type { FC } from 'react';
23
import React from 'react';
34
import '../../assets/index.less';
4-
import Input from 'rc-input';
55

66
const Demo: FC = () => {
77
return (

src/BaseInput.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const BaseInput = React.forwardRef<HolderRef, BaseInputProps>((props, ref) => {
3333
dataAttrs,
3434
styles,
3535
components,
36+
onClear,
3637
} = props;
3738

3839
const inputElement = children ?? inputEl;
@@ -80,7 +81,10 @@ const BaseInput = React.forwardRef<HolderRef, BaseInputProps>((props, ref) => {
8081

8182
clearIcon = (
8283
<span
83-
onClick={handleReset}
84+
onClick={(event) => {
85+
handleReset?.(event);
86+
onClear?.();
87+
}}
8488
// Do not trigger onBlur when clear input
8589
// https://github.com/ant-design/ant-design/issues/31200
8690
onMouseDown={(e) => e.preventDefault()}

src/Input.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import React, {
88
useRef,
99
useState,
1010
} from 'react';
11-
import BaseInput, { HolderRef } from './BaseInput';
11+
import type { HolderRef } from './BaseInput';
12+
import BaseInput from './BaseInput';
1213
import useCount from './hooks/useCount';
1314
import type { ChangeEventInfo, InputProps, InputRef } from './interface';
1415
import type { InputFocusOptions } from './utils/commonUtils';

src/interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export interface BaseInputProps extends CommonInputProps {
5252
triggerFocus?: () => void;
5353
readOnly?: boolean;
5454
handleReset?: MouseEventHandler;
55+
onClear?: () => void;
5556
hidden?: boolean;
5657
dataAttrs?: {
5758
affixWrapper?: DataAttr;
@@ -136,6 +137,7 @@ export interface InputProps
136137
count?: CSSProperties;
137138
};
138139
count?: CountConfig;
140+
onClear?: () => void;
139141
}
140142

141143
export interface InputRef {

tests/BaseInput.test.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,5 +311,18 @@ describe('BaseInput', () => {
311311
container.querySelector('.rc-input-group-wrapper'),
312312
);
313313
});
314+
315+
it('support onClear', () => {
316+
const onClear = jest.fn();
317+
const { container } = render(
318+
<BaseInput prefixCls="rc-input" onClear={onClear} allowClear>
319+
<input defaultValue="test" />
320+
</BaseInput>,
321+
);
322+
fireEvent.click(
323+
container.querySelector<HTMLSpanElement>('.rc-input-clear-icon')!,
324+
);
325+
expect(onClear).toHaveBeenCalled();
326+
});
314327
});
315328
});

tests/index.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,17 @@ describe('Input ref', () => {
392392
expect(ref.current?.input).toBe(inputEl);
393393
expect(ref.current?.nativeElement).toBe(inputEl);
394394
});
395+
396+
it('support onClear', () => {
397+
const onClear = jest.fn();
398+
const { container } = render(
399+
<Input onClear={onClear} defaultValue="test" allowClear />,
400+
);
401+
fireEvent.click(
402+
container.querySelector<HTMLSpanElement>('.rc-input-clear-icon')!,
403+
);
404+
expect(onClear).toHaveBeenCalled();
405+
});
395406
});
396407

397408
describe('resolveChange should work', () => {

0 commit comments

Comments
 (0)