2020-03-02 12:09:38 +08:00
|
|
|
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
|
2022-02-14 17:09:35 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import * as React from 'react';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { DirectionType } from '../config-provider';
|
|
|
|
import type { SizeType } from '../config-provider/SizeContext';
|
|
|
|
import type { FormItemStatusContextProps } from '../form/context';
|
|
|
|
import { FormItemInputContext } from '../form/context';
|
2020-05-14 20:54:49 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { InputStatus } from '../_util/statusUtils';
|
|
|
|
import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils';
|
2022-02-14 17:09:35 +08:00
|
|
|
import type { InputProps } from './Input';
|
2019-11-01 18:19:29 +08:00
|
|
|
|
2022-12-13 14:57:40 +08:00
|
|
|
const ClearableInputType = ['text', 'input'] as const;
|
2019-11-01 18:19:29 +08:00
|
|
|
|
2020-10-30 15:27:37 +08:00
|
|
|
function hasAddon(props: InputProps | ClearableInputProps) {
|
|
|
|
return !!(props.addonBefore || props.addonAfter);
|
|
|
|
}
|
|
|
|
|
2020-12-28 15:30:18 +08:00
|
|
|
/** This basic props required for input and textarea. */
|
2019-11-01 18:19:29 +08:00
|
|
|
interface BasicProps {
|
|
|
|
prefixCls: string;
|
2019-12-23 18:33:08 +08:00
|
|
|
inputType: typeof ClearableInputType[number];
|
2019-11-01 18:19:29 +08:00
|
|
|
value?: any;
|
|
|
|
allowClear?: boolean;
|
2020-06-12 18:50:36 +08:00
|
|
|
element: React.ReactElement;
|
2019-11-01 18:19:29 +08:00
|
|
|
handleReset: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void;
|
|
|
|
className?: string;
|
2021-01-21 17:16:51 +08:00
|
|
|
style?: React.CSSProperties;
|
2019-11-01 18:19:29 +08:00
|
|
|
disabled?: boolean;
|
2020-11-04 14:12:45 +08:00
|
|
|
direction?: DirectionType;
|
2020-01-14 11:59:38 +08:00
|
|
|
focused?: boolean;
|
2020-02-20 19:56:22 +08:00
|
|
|
readOnly?: boolean;
|
2020-07-16 00:25:47 +08:00
|
|
|
bordered: boolean;
|
2022-01-13 16:23:27 +08:00
|
|
|
hidden?: boolean;
|
2019-11-01 18:19:29 +08:00
|
|
|
}
|
|
|
|
|
2020-12-28 15:30:18 +08:00
|
|
|
/** This props only for input. */
|
2021-08-17 16:22:42 +08:00
|
|
|
export interface ClearableInputProps extends BasicProps {
|
2020-01-03 13:38:16 +08:00
|
|
|
size?: SizeType;
|
2019-11-01 18:19:29 +08:00
|
|
|
suffix?: React.ReactNode;
|
|
|
|
prefix?: React.ReactNode;
|
|
|
|
addonBefore?: React.ReactNode;
|
|
|
|
addonAfter?: React.ReactNode;
|
2020-11-18 11:27:58 +08:00
|
|
|
triggerFocus?: () => void;
|
2022-02-14 17:09:35 +08:00
|
|
|
status?: InputStatus;
|
2022-03-14 20:57:01 +08:00
|
|
|
hashId?: string;
|
2019-11-01 18:19:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class ClearableLabeledInput extends React.Component<ClearableInputProps> {
|
|
|
|
renderClearIcon(prefixCls: string) {
|
2022-03-01 14:17:48 +08:00
|
|
|
const { value, disabled, readOnly, handleReset, suffix } = this.props;
|
2020-04-14 23:04:23 +08:00
|
|
|
const needClear = !disabled && !readOnly && value;
|
2020-10-13 21:16:20 +08:00
|
|
|
const className = `${prefixCls}-clear-icon`;
|
2020-04-14 23:04:23 +08:00
|
|
|
return (
|
|
|
|
<CloseCircleFilled
|
|
|
|
onClick={handleReset}
|
2021-06-30 13:48:22 +08:00
|
|
|
// Do not trigger onBlur when clear input
|
|
|
|
// https://github.com/ant-design/ant-design/issues/31200
|
2022-11-19 13:47:33 +08:00
|
|
|
onMouseDown={(e) => e.preventDefault()}
|
2020-09-06 13:07:39 +08:00
|
|
|
className={classNames(
|
|
|
|
{
|
|
|
|
[`${className}-hidden`]: !needClear,
|
2021-08-10 19:21:02 +08:00
|
|
|
[`${className}-has-suffix`]: !!suffix,
|
2020-09-06 13:07:39 +08:00
|
|
|
},
|
|
|
|
className,
|
|
|
|
)}
|
2020-04-14 23:04:23 +08:00
|
|
|
role="button"
|
|
|
|
/>
|
|
|
|
);
|
2019-11-01 18:19:29 +08:00
|
|
|
}
|
|
|
|
|
2022-02-14 17:09:35 +08:00
|
|
|
renderTextAreaWithClearIcon(
|
|
|
|
prefixCls: string,
|
|
|
|
element: React.ReactElement,
|
|
|
|
statusContext: FormItemStatusContextProps,
|
|
|
|
) {
|
|
|
|
const {
|
|
|
|
value,
|
|
|
|
allowClear,
|
|
|
|
className,
|
|
|
|
style,
|
|
|
|
direction,
|
|
|
|
bordered,
|
|
|
|
hidden,
|
|
|
|
status: customStatus,
|
2022-03-14 20:57:01 +08:00
|
|
|
hashId,
|
2022-02-14 17:09:35 +08:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const { status: contextStatus, hasFeedback } = statusContext;
|
|
|
|
|
2019-11-01 18:19:29 +08:00
|
|
|
if (!allowClear) {
|
2020-05-14 20:54:49 +08:00
|
|
|
return cloneElement(element, {
|
2019-11-01 18:19:29 +08:00
|
|
|
value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const affixWrapperCls = classNames(
|
|
|
|
`${prefixCls}-affix-wrapper`,
|
|
|
|
`${prefixCls}-affix-wrapper-textarea-with-clear-btn`,
|
2022-02-16 11:48:24 +08:00
|
|
|
getStatusClassNames(
|
|
|
|
`${prefixCls}-affix-wrapper`,
|
|
|
|
getMergedStatus(contextStatus, customStatus),
|
|
|
|
hasFeedback,
|
|
|
|
),
|
2020-07-16 00:25:47 +08:00
|
|
|
{
|
|
|
|
[`${prefixCls}-affix-wrapper-rtl`]: direction === 'rtl',
|
|
|
|
[`${prefixCls}-affix-wrapper-borderless`]: !bordered,
|
2020-10-30 15:27:37 +08:00
|
|
|
// className will go to addon wrapper
|
|
|
|
[`${className}`]: !hasAddon(this.props) && className,
|
2020-07-16 00:25:47 +08:00
|
|
|
},
|
2022-03-14 20:57:01 +08:00
|
|
|
hashId,
|
2019-11-01 18:19:29 +08:00
|
|
|
);
|
|
|
|
return (
|
2022-01-15 22:08:19 +08:00
|
|
|
<span className={affixWrapperCls} style={style} hidden={hidden}>
|
2020-05-14 20:54:49 +08:00
|
|
|
{cloneElement(element, {
|
2019-11-01 18:19:29 +08:00
|
|
|
style: null,
|
|
|
|
value,
|
|
|
|
})}
|
|
|
|
{this.renderClearIcon(prefixCls)}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:11:11 +08:00
|
|
|
render() {
|
2022-02-14 17:09:35 +08:00
|
|
|
return (
|
2022-03-24 21:54:20 +08:00
|
|
|
<FormItemInputContext.Consumer>
|
2022-11-19 13:47:33 +08:00
|
|
|
{(statusContext) => {
|
2022-02-14 17:09:35 +08:00
|
|
|
const { prefixCls, inputType, element } = this.props;
|
|
|
|
if (inputType === ClearableInputType[0]) {
|
|
|
|
return this.renderTextAreaWithClearIcon(prefixCls, element, statusContext);
|
|
|
|
}
|
|
|
|
}}
|
2022-03-24 21:54:20 +08:00
|
|
|
</FormItemInputContext.Consumer>
|
2022-02-14 17:09:35 +08:00
|
|
|
);
|
2019-11-01 18:19:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ClearableLabeledInput;
|