2020-08-31 16:53:19 +08:00
|
|
|
import classNames from '../_util/classNames';
|
2020-03-26 11:53:51 +08:00
|
|
|
import CloseCircleFilled from '@ant-design/icons-vue/CloseCircleFilled';
|
2020-03-07 19:45:13 +08:00
|
|
|
import { getInputClassName } from './Input';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import { cloneElement } from '../_util/vnode';
|
2020-06-27 14:02:30 +08:00
|
|
|
import { getComponent } from '../_util/props-util';
|
2020-03-07 19:45:13 +08:00
|
|
|
|
|
|
|
export function hasPrefixSuffix(instance) {
|
|
|
|
return !!(
|
2020-06-27 14:02:30 +08:00
|
|
|
getComponent(instance, 'prefix') ||
|
|
|
|
getComponent(instance, 'suffix') ||
|
2020-03-07 19:45:13 +08:00
|
|
|
instance.$props.allowClear
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ClearableInputType = ['text', 'input'];
|
|
|
|
|
|
|
|
const ClearableLabeledInput = {
|
2020-06-27 14:02:30 +08:00
|
|
|
name: 'ClearableLabeledInput',
|
|
|
|
inheritAttrs: false,
|
2020-03-07 19:45:13 +08:00
|
|
|
props: {
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
inputType: PropTypes.oneOf(ClearableInputType),
|
|
|
|
value: PropTypes.any,
|
|
|
|
defaultValue: PropTypes.any,
|
2020-10-10 18:16:28 +08:00
|
|
|
allowClear: PropTypes.looseBool,
|
2020-03-07 19:45:13 +08:00
|
|
|
element: PropTypes.any,
|
|
|
|
handleReset: PropTypes.func,
|
2020-10-10 18:16:28 +08:00
|
|
|
disabled: PropTypes.looseBool,
|
2020-03-07 19:45:13 +08:00
|
|
|
size: PropTypes.oneOf(['small', 'large', 'default']),
|
|
|
|
suffix: PropTypes.any,
|
|
|
|
prefix: PropTypes.any,
|
|
|
|
addonBefore: PropTypes.any,
|
|
|
|
addonAfter: PropTypes.any,
|
2020-10-10 18:16:28 +08:00
|
|
|
readonly: PropTypes.looseBool,
|
2020-03-07 19:45:13 +08:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
renderClearIcon(prefixCls) {
|
2020-07-17 17:13:30 +08:00
|
|
|
const { allowClear, value, disabled, readonly, inputType, handleReset } = this.$props;
|
2020-03-07 19:45:13 +08:00
|
|
|
if (
|
|
|
|
!allowClear ||
|
|
|
|
disabled ||
|
2020-07-17 17:13:30 +08:00
|
|
|
readonly ||
|
2020-03-07 19:45:13 +08:00
|
|
|
value === undefined ||
|
|
|
|
value === null ||
|
|
|
|
value === ''
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const className =
|
|
|
|
inputType === ClearableInputType[0]
|
|
|
|
? `${prefixCls}-textarea-clear-icon`
|
|
|
|
: `${prefixCls}-clear-icon`;
|
2020-03-26 11:53:51 +08:00
|
|
|
return <CloseCircleFilled onClick={handleReset} class={className} role="button" />;
|
2020-03-07 19:45:13 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
renderSuffix(prefixCls) {
|
|
|
|
const { suffix, allowClear } = this.$props;
|
|
|
|
if (suffix || allowClear) {
|
|
|
|
return (
|
|
|
|
<span class={`${prefixCls}-suffix`}>
|
|
|
|
{this.renderClearIcon(prefixCls)}
|
|
|
|
{suffix}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
renderLabeledIcon(prefixCls, element) {
|
|
|
|
const props = this.$props;
|
|
|
|
const suffix = this.renderSuffix(prefixCls);
|
|
|
|
if (!hasPrefixSuffix(this)) {
|
|
|
|
return cloneElement(element, {
|
2020-06-27 14:02:30 +08:00
|
|
|
value: props.value,
|
2020-03-07 19:45:13 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const prefix = props.prefix ? (
|
|
|
|
<span class={`${prefixCls}-prefix`}>{props.prefix}</span>
|
|
|
|
) : null;
|
|
|
|
|
2020-07-31 12:43:01 +08:00
|
|
|
const affixWrapperCls = classNames(this.$attrs?.class, `${prefixCls}-affix-wrapper`, {
|
2020-03-07 19:45:13 +08:00
|
|
|
[`${prefixCls}-affix-wrapper-sm`]: props.size === 'small',
|
|
|
|
[`${prefixCls}-affix-wrapper-lg`]: props.size === 'large',
|
|
|
|
[`${prefixCls}-affix-wrapper-input-with-clear-btn`]:
|
|
|
|
props.suffix && props.allowClear && this.$props.value,
|
|
|
|
});
|
2020-04-15 20:38:58 +08:00
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
return (
|
|
|
|
<span class={affixWrapperCls} style={props.style}>
|
|
|
|
{prefix}
|
|
|
|
{cloneElement(element, {
|
|
|
|
style: null,
|
2020-06-27 14:02:30 +08:00
|
|
|
value: props.value,
|
2020-03-07 19:45:13 +08:00
|
|
|
class: getInputClassName(prefixCls, props.size, props.disabled),
|
|
|
|
})}
|
|
|
|
{suffix}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
renderInputWithLabel(prefixCls, labeledElement) {
|
2020-06-27 14:02:30 +08:00
|
|
|
const { addonBefore, addonAfter, size } = this.$props;
|
|
|
|
const { style, class: className } = this.$attrs;
|
2020-03-07 19:45:13 +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 class={addonClassName}>{addonBefore}</span>
|
|
|
|
) : null;
|
|
|
|
const addonAfterNode = addonAfter ? <span class={addonClassName}>{addonAfter}</span> : null;
|
|
|
|
|
|
|
|
const mergedWrapperClassName = classNames(`${prefixCls}-wrapper`, {
|
|
|
|
[wrapperClassName]: addonBefore || addonAfter,
|
|
|
|
});
|
|
|
|
|
|
|
|
const mergedGroupClassName = classNames(className, `${prefixCls}-group-wrapper`, {
|
|
|
|
[`${prefixCls}-group-wrapper-sm`]: size === 'small',
|
|
|
|
[`${prefixCls}-group-wrapper-lg`]: size === 'large',
|
|
|
|
});
|
|
|
|
|
|
|
|
// Need another wrapper for changing display:table to display:inline-block
|
|
|
|
// and put style prop in wrapper
|
|
|
|
return (
|
|
|
|
<span class={mergedGroupClassName} style={style}>
|
|
|
|
<span class={mergedWrapperClassName}>
|
|
|
|
{addonBeforeNode}
|
|
|
|
{cloneElement(labeledElement, { style: null })}
|
|
|
|
{addonAfterNode}
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
renderTextAreaWithClearIcon(prefixCls, element) {
|
2020-06-27 14:02:30 +08:00
|
|
|
const { value, allowClear } = this.$props;
|
|
|
|
const { style, class: className } = this.$attrs;
|
2020-03-07 19:45:13 +08:00
|
|
|
if (!allowClear) {
|
2020-06-27 14:02:30 +08:00
|
|
|
return cloneElement(element, { value });
|
2020-03-07 19:45:13 +08:00
|
|
|
}
|
|
|
|
const affixWrapperCls = classNames(
|
|
|
|
className,
|
|
|
|
`${prefixCls}-affix-wrapper`,
|
|
|
|
`${prefixCls}-affix-wrapper-textarea-with-clear-btn`,
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<span class={affixWrapperCls} style={style}>
|
|
|
|
{cloneElement(element, {
|
|
|
|
style: null,
|
2020-06-27 14:02:30 +08:00
|
|
|
value,
|
2020-03-07 19:45:13 +08:00
|
|
|
})}
|
|
|
|
{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;
|