Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/examples/addon.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Input from 'rc-input';
import type { FC } from 'react';
import React from 'react';
import '../../assets/index.less';
import Input from 'rc-input';

const Demo: FC = () => {
return (
Expand Down
6 changes: 5 additions & 1 deletion src/BaseInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const BaseInput = React.forwardRef<HolderRef, BaseInputProps>((props, ref) => {
dataAttrs,
styles,
components,
onClear,
} = props;

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

clearIcon = (
<span
onClick={handleReset}
onClick={(event) => {
handleReset?.(event);
onClear?.();
}}
// Do not trigger onBlur when clear input
// https://github.com/ant-design/ant-design/issues/31200
onMouseDown={(e) => e.preventDefault()}
Expand Down
3 changes: 2 additions & 1 deletion src/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import React, {
useRef,
useState,
} from 'react';
import BaseInput, { HolderRef } from './BaseInput';
import type { HolderRef } from './BaseInput';
import BaseInput from './BaseInput';
import useCount from './hooks/useCount';
import type { ChangeEventInfo, InputProps, InputRef } from './interface';
import type { InputFocusOptions } from './utils/commonUtils';
Expand Down
2 changes: 2 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface BaseInputProps extends CommonInputProps {
triggerFocus?: () => void;
readOnly?: boolean;
handleReset?: MouseEventHandler;
onClear?: () => void;
hidden?: boolean;
dataAttrs?: {
affixWrapper?: DataAttr;
Expand Down Expand Up @@ -136,6 +137,7 @@ export interface InputProps
count?: CSSProperties;
};
count?: CountConfig;
onClear?: () => void;
}

export interface InputRef {
Expand Down
13 changes: 13 additions & 0 deletions tests/BaseInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,5 +311,18 @@ describe('BaseInput', () => {
container.querySelector('.rc-input-group-wrapper'),
);
});

it('support onClear', () => {
const onClear = jest.fn();
const { container } = render(
<BaseInput prefixCls="rc-input" onClear={onClear} allowClear>
<input defaultValue="test" />
</BaseInput>,
);
fireEvent.click(
container.querySelector<HTMLSpanElement>('.rc-input-clear-icon')!,
);
expect(onClear).toHaveBeenCalled();
Comment thread
li-jia-nan marked this conversation as resolved.
});
});
});
11 changes: 11 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,17 @@ describe('Input ref', () => {
expect(ref.current?.input).toBe(inputEl);
expect(ref.current?.nativeElement).toBe(inputEl);
});

it('support onClear', () => {
const onClear = jest.fn();
const { container } = render(
<Input onClear={onClear} defaultValue="test" allowClear />,
);
fireEvent.click(
container.querySelector<HTMLSpanElement>('.rc-input-clear-icon')!,
);
expect(onClear).toHaveBeenCalled();
});
});

describe('resolveChange should work', () => {
Expand Down