2019-11-01 18:19:29 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import classNames from 'classnames';
|
2020-03-02 12:09:38 +08:00
|
|
|
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
|
2019-11-01 18:19:29 +08:00
|
|
|
import { tuple } from '../_util/type';
|
2020-01-03 13:38:16 +08:00
|
|
|
import { InputProps, getInputClassName } from './Input';
|
|
|
|
import { SizeType } from '../config-provider/SizeContext';
|
2019-11-01 18:19:29 +08:00
|
|
|
|
|
|
|
const ClearableInputType = tuple('text', 'input');
|
|
|
|
|
|
|
|
export function hasPrefixSuffix(props: InputProps | ClearableInputProps) {
|
|
|
|
return !!(props.prefix || props.suffix || props.allowClear);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This basic props required for input and textarea.
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
element: React.ReactElement<any>;
|
|
|
|
handleReset: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void;
|
|
|
|
className?: string;
|
|
|
|
style?: object;
|
|
|
|
disabled?: boolean;
|
2020-01-02 19:10:16 +08:00
|
|
|
direction?: any;
|
2020-01-14 11:59:38 +08:00
|
|
|
focused?: boolean;
|
2020-02-20 19:56:22 +08:00
|
|
|
readOnly?: boolean;
|
2019-11-01 18:19:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This props only for input.
|
|
|
|
*/
|
|
|
|
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-02-17 19:12:42 +08:00
|
|
|
triggerFocus: () => void;
|
2019-11-01 18:19:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class ClearableLabeledInput extends React.Component<ClearableInputProps> {
|
2020-04-03 16:11:41 +08:00
|
|
|
/** @private Do not use out of this class. We do not promise this is always keep. */
|
|
|
|
private containerRef = React.createRef<HTMLSpanElement>();
|
|
|
|
|
|
|
|
onInputMouseUp: React.MouseEventHandler = e => {
|
|
|
|
if (this.containerRef.current?.contains(e.target as Element)) {
|
|
|
|
const { triggerFocus } = this.props;
|
|
|
|
triggerFocus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-01 18:19:29 +08:00
|
|
|
renderClearIcon(prefixCls: string) {
|
2020-02-20 19:56:22 +08:00
|
|
|
const { allowClear, value, disabled, readOnly, inputType, handleReset } = this.props;
|
|
|
|
if (
|
|
|
|
!allowClear ||
|
|
|
|
disabled ||
|
|
|
|
readOnly ||
|
|
|
|
value === undefined ||
|
|
|
|
value === null ||
|
|
|
|
value === ''
|
|
|
|
) {
|
2019-11-01 18:19:29 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const className =
|
|
|
|
inputType === ClearableInputType[0]
|
|
|
|
? `${prefixCls}-textarea-clear-icon`
|
|
|
|
: `${prefixCls}-clear-icon`;
|
2019-11-06 10:34:53 +08:00
|
|
|
return <CloseCircleFilled onClick={handleReset} className={className} role="button" />;
|
2019-11-01 18:19:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
renderSuffix(prefixCls: string) {
|
|
|
|
const { suffix, allowClear } = this.props;
|
|
|
|
if (suffix || allowClear) {
|
|
|
|
return (
|
|
|
|
<span className={`${prefixCls}-suffix`}>
|
|
|
|
{this.renderClearIcon(prefixCls)}
|
|
|
|
{suffix}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderLabeledIcon(prefixCls: string, element: React.ReactElement<any>) {
|
2020-01-14 11:59:38 +08:00
|
|
|
const {
|
|
|
|
focused,
|
|
|
|
value,
|
|
|
|
prefix,
|
|
|
|
className,
|
|
|
|
size,
|
|
|
|
suffix,
|
|
|
|
disabled,
|
|
|
|
allowClear,
|
|
|
|
direction,
|
|
|
|
style,
|
|
|
|
} = this.props;
|
|
|
|
const suffixNode = this.renderSuffix(prefixCls);
|
|
|
|
if (!hasPrefixSuffix(this.props)) {
|
2019-11-01 18:19:29 +08:00
|
|
|
return React.cloneElement(element, {
|
2020-01-14 11:59:38 +08:00
|
|
|
value,
|
2019-11-01 18:19:29 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-14 11:59:38 +08:00
|
|
|
const prefixNode = prefix ? <span className={`${prefixCls}-prefix`}>{prefix}</span> : null;
|
2019-11-01 18:19:29 +08:00
|
|
|
|
2020-01-14 11:59:38 +08:00
|
|
|
const affixWrapperCls = classNames(className, `${prefixCls}-affix-wrapper`, {
|
|
|
|
[`${prefixCls}-affix-wrapper-focused`]: focused,
|
|
|
|
[`${prefixCls}-affix-wrapper-disabled`]: disabled,
|
|
|
|
[`${prefixCls}-affix-wrapper-sm`]: size === 'small',
|
|
|
|
[`${prefixCls}-affix-wrapper-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-affix-wrapper-input-with-clear-btn`]: suffix && allowClear && value,
|
|
|
|
[`${prefixCls}-affix-wrapper-rtl`]: direction === 'rtl',
|
2019-11-01 18:19:29 +08:00
|
|
|
});
|
|
|
|
return (
|
2020-04-03 16:11:41 +08:00
|
|
|
<span
|
|
|
|
ref={this.containerRef}
|
|
|
|
className={affixWrapperCls}
|
|
|
|
style={style}
|
|
|
|
onMouseUp={this.onInputMouseUp}
|
|
|
|
>
|
2020-01-14 11:59:38 +08:00
|
|
|
{prefixNode}
|
2019-11-01 18:19:29 +08:00
|
|
|
{React.cloneElement(element, {
|
|
|
|
style: null,
|
2020-01-14 11:59:38 +08:00
|
|
|
value,
|
|
|
|
className: getInputClassName(prefixCls, size, disabled),
|
2019-11-01 18:19:29 +08:00
|
|
|
})}
|
2020-01-14 11:59:38 +08:00
|
|
|
{suffixNode}
|
2019-11-01 18:19:29 +08:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderInputWithLabel(prefixCls: string, labeledElement: React.ReactElement<any>) {
|
2020-01-02 19:10:16 +08:00
|
|
|
const { addonBefore, addonAfter, style, size, className, direction } = this.props;
|
2019-11-01 18:19:29 +08:00
|
|
|
// Not wrap when there is not addons
|
|
|
|
if (!addonBefore && !addonAfter) {
|
|
|
|
return labeledElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrapperClassName = `${prefixCls}-group`;
|
|
|
|
const addonClassName = `${wrapperClassName}-addon`;
|
|
|
|
const addonBeforeNode = addonBefore ? (
|
|
|
|
<span className={addonClassName}>{addonBefore}</span>
|
|
|
|
) : null;
|
|
|
|
const addonAfterNode = addonAfter ? <span className={addonClassName}>{addonAfter}</span> : null;
|
|
|
|
|
|
|
|
const mergedWrapperClassName = classNames(`${prefixCls}-wrapper`, {
|
|
|
|
[wrapperClassName]: addonBefore || addonAfter,
|
2020-01-02 19:10:16 +08:00
|
|
|
[`${wrapperClassName}-rtl`]: direction === 'rtl',
|
2019-11-01 18:19:29 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const mergedGroupClassName = classNames(className, `${prefixCls}-group-wrapper`, {
|
|
|
|
[`${prefixCls}-group-wrapper-sm`]: size === 'small',
|
|
|
|
[`${prefixCls}-group-wrapper-lg`]: size === 'large',
|
2020-01-02 19:10:16 +08:00
|
|
|
[`${prefixCls}-group-wrapper-rtl`]: direction === 'rtl',
|
2019-11-01 18:19:29 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Need another wrapper for changing display:table to display:inline-block
|
|
|
|
// and put style prop in wrapper
|
|
|
|
return (
|
|
|
|
<span className={mergedGroupClassName} style={style}>
|
|
|
|
<span className={mergedWrapperClassName}>
|
|
|
|
{addonBeforeNode}
|
|
|
|
{React.cloneElement(labeledElement, { style: null })}
|
|
|
|
{addonAfterNode}
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderTextAreaWithClearIcon(prefixCls: string, element: React.ReactElement<any>) {
|
|
|
|
const { value, allowClear, className, style } = this.props;
|
|
|
|
if (!allowClear) {
|
|
|
|
return React.cloneElement(element, {
|
|
|
|
value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const affixWrapperCls = classNames(
|
|
|
|
className,
|
|
|
|
`${prefixCls}-affix-wrapper`,
|
|
|
|
`${prefixCls}-affix-wrapper-textarea-with-clear-btn`,
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<span className={affixWrapperCls} style={style}>
|
|
|
|
{React.cloneElement(element, {
|
|
|
|
style: null,
|
|
|
|
value,
|
|
|
|
})}
|
|
|
|
{this.renderClearIcon(prefixCls)}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderClearableLabeledInput() {
|
|
|
|
const { prefixCls, inputType, element } = this.props;
|
|
|
|
if (inputType === ClearableInputType[0]) {
|
|
|
|
return this.renderTextAreaWithClearIcon(prefixCls, element);
|
|
|
|
}
|
|
|
|
return this.renderInputWithLabel(prefixCls, this.renderLabeledIcon(prefixCls, element));
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return this.renderClearableLabeledInput();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ClearableLabeledInput;
|