Skip to content

Commit

Permalink
feat: support onClear callback (#71)
Browse files Browse the repository at this point in the history
* feat: support onClear callback

* fix: fix

* fix: fix

* fix: fix

* fix: fix

* fix: fix
  • Loading branch information
li-jia-nan authored Jul 16, 2024
1 parent c18cc42 commit fe176c3
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
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();
});
});
});
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

0 comments on commit fe176c3

Please sign in to comment.