refactor: clearIcon in allowClear (#34340)

* refactor: Input allowClear api upadte

* test: add test
This commit is contained in:
MadCcc 2022-03-08 01:40:24 +08:00 committed by GitHub
parent ace24bff87
commit c7f6e1f199
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 7 deletions

View File

@ -129,7 +129,7 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
onBlur, onBlur,
onFocus, onFocus,
suffix, suffix,
clearIcon, allowClear,
...rest ...rest
} = props; } = props;
const { getPrefixCls, direction, input } = React.useContext(ConfigContext); const { getPrefixCls, direction, input } = React.useContext(ConfigContext);
@ -199,6 +199,14 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
const withPrefixSuffix = hasPrefixSuffix(props) || hasFeedback; const withPrefixSuffix = hasPrefixSuffix(props) || hasFeedback;
// Allow clear
let mergedAllowClear;
if (typeof allowClear === 'object' && allowClear?.clearIcon) {
mergedAllowClear = allowClear;
} else if (allowClear) {
mergedAllowClear = { clearIcon: <CloseCircleFilled /> };
}
return ( return (
<RcInput <RcInput
ref={composeRef(ref, inputRef)} ref={composeRef(ref, inputRef)}
@ -208,7 +216,7 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
onBlur={handleBlur} onBlur={handleBlur}
onFocus={handleFocus} onFocus={handleFocus}
suffix={suffixNode} suffix={suffixNode}
clearIcon={clearIcon || <CloseCircleFilled />} allowClear={mergedAllowClear}
inputClassName={classNames( inputClassName={classNames(
!withPrefixSuffix && { !withPrefixSuffix && {
[`${prefixCls}-sm`]: mergedSize === 'small', [`${prefixCls}-sm`]: mergedSize === 'small',

View File

@ -380,4 +380,9 @@ describe('Input allowClear', () => {
wrapper.setProps({ value: false }); wrapper.setProps({ value: false });
expect(wrapper.find('input').first().getDOMNode().value).toBe('false'); expect(wrapper.find('input').first().getDOMNode().value).toBe('false');
}); });
it('should support custom clearIcon', () => {
const wrapper = mount(<Input allowClear={{ clearIcon: 'clear' }} />);
expect(wrapper.find('.ant-input-clear-icon').text()).toBe('clear');
});
}); });

View File

@ -20,9 +20,8 @@ A basic widget for getting the user input is a text field. Keyboard and mouse ca
| --- | --- | --- | --- | --- | | --- | --- | --- | --- | --- |
| addonAfter | The label text displayed after (on the right side of) the input field | ReactNode | - | | | addonAfter | The label text displayed after (on the right side of) the input field | ReactNode | - | |
| addonBefore | The label text displayed before (on the left side of) the input field | ReactNode | - | | | addonBefore | The label text displayed before (on the left side of) the input field | ReactNode | - | |
| allowClear | If allow to remove input content with clear icon | boolean | false | | | allowClear | If allow to remove input content with clear icon | boolean \| { clearIcon: ReactNode } | false | |
| bordered | Whether has border style | boolean | true | 4.5.0 | | bordered | Whether has border style | boolean | true | 4.5.0 |
| clearIcon | Icon displayed when `allowClear` is enabled | ReactNode | <CloseCircleFilled /> | 4.19.0 |
| defaultValue | The initial input content | string | - | | | defaultValue | The initial input content | string | - | |
| disabled | Whether the input is disabled | boolean | false | | | disabled | Whether the input is disabled | boolean | false | |
| id | The ID for input | string | - | | | id | The ID for input | string | - | |

View File

@ -21,9 +21,8 @@ cover: https://gw.alipayobjects.com/zos/alicdn/xS9YEJhfe/Input.svg
| --- | --- | --- | --- | --- | | --- | --- | --- | --- | --- |
| addonAfter | 带标签的 input设置后置标签 | ReactNode | - | | | addonAfter | 带标签的 input设置后置标签 | ReactNode | - | |
| addonBefore | 带标签的 input设置前置标签 | ReactNode | - | | | addonBefore | 带标签的 input设置前置标签 | ReactNode | - | |
| allowClear | 可以点击清除图标删除内容 | boolean | - | | | allowClear | 可以点击清除图标删除内容 | boolean \| { clearIcon: ReactNode } | - | |
| bordered | 是否有边框 | boolean | true | 4.5.0 | | bordered | 是否有边框 | boolean | true | 4.5.0 |
| clearIcon | 清除按钮,与 `allowClear` 一同使用 | ReactNode | <CloseCircleFilled /> | 4.19.0 |
| defaultValue | 输入框默认内容 | string | - | | | defaultValue | 输入框默认内容 | string | - | |
| disabled | 是否禁用状态,默认为 false | boolean | false | | | disabled | 是否禁用状态,默认为 false | boolean | false | |
| id | 输入框的 id | string | - | | | id | 输入框的 id | string | - | |

View File

@ -129,7 +129,7 @@
"rc-dropdown": "~3.3.2", "rc-dropdown": "~3.3.2",
"rc-field-form": "~1.23.0", "rc-field-form": "~1.23.0",
"rc-image": "~5.2.5", "rc-image": "~5.2.5",
"rc-input": "^0.0.1-alpha.4", "rc-input": "^0.0.1-alpha.5",
"rc-input-number": "~7.3.0", "rc-input-number": "~7.3.0",
"rc-mentions": "~1.6.1", "rc-mentions": "~1.6.1",
"rc-menu": "~9.2.1", "rc-menu": "~9.2.1",