refactor: rewrite Transfer search with hooks (#28895)

* Refac: rewrite transfer search with hook

* [CodeFactor] Apply fixes to commit 3ad857f

[ci skip] [skip ci]

* chore: minor change in search

Co-authored-by: codefactor-io <support@codefactor.io>
This commit is contained in:
susiwen8 2021-01-16 23:52:03 +08:00 committed by GitHub
parent 9daf50f3a9
commit 19f3e6cdae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,50 +13,41 @@ export interface TransferSearchProps {
disabled?: boolean; disabled?: boolean;
} }
export default class Search extends React.Component<TransferSearchProps, any> { export default function Search(props: TransferSearchProps) {
static defaultProps = { const { placeholder = '', value, prefixCls, disabled, onChange, handleClear } = props;
placeholder: '',
};
handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { const handleChange = React.useCallback(
const { onChange } = this.props; (e: React.ChangeEvent<HTMLInputElement>) => {
if (onChange) { onChange?.(e);
onChange(e); },
} [onChange],
}; );
handleClear = (e: React.MouseEvent<HTMLAnchorElement>) => { const handleClearFn = (e: React.MouseEvent<HTMLAnchorElement>) => {
e.preventDefault(); e.preventDefault();
const { handleClear, disabled } = this.props;
if (!disabled && handleClear) { if (!disabled && handleClear) {
handleClear(e); handleClear(e);
} }
}; };
render() {
const { placeholder, value, prefixCls, disabled } = this.props;
const icon =
value && value.length > 0 ? (
<a className={`${prefixCls}-action`} onClick={this.handleClear}>
<CloseCircleFilled />
</a>
) : (
<span className={`${prefixCls}-action`}>
<SearchOutlined />
</span>
);
return ( return (
<> <>
<Input <Input
placeholder={placeholder} placeholder={placeholder}
className={prefixCls} className={prefixCls}
value={value} value={value}
onChange={this.handleChange} onChange={handleChange}
disabled={disabled} disabled={disabled}
/> />
{icon} {value && value.length > 0 ? (
<a className={`${prefixCls}-action`} onClick={handleClearFn}>
<CloseCircleFilled />
</a>
) : (
<span className={`${prefixCls}-action`}>
<SearchOutlined />
</span>
)}
</> </>
); );
}
} }