2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2020-03-26 03:53:51 +00:00
|
|
|
import CloseCircleFilled from '@ant-design/icons-vue/CloseCircleFilled';
|
2020-03-07 11:45:13 +00:00
|
|
|
import { getInputClassName } from './Input';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import { cloneElement } from '../_util/vnode';
|
2020-06-27 06:02:30 +00:00
|
|
|
import { getComponent } from '../_util/props-util';
|
2020-10-16 06:24:14 +00:00
|
|
|
import { defineComponent, VNode } from 'vue';
|
|
|
|
import { tuple } from '../_util/type';
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2020-10-16 06:24:14 +00:00
|
|
|
export function hasPrefixSuffix(instance: any) {
|
2020-03-07 11:45:13 +00:00
|
|
|
return !!(
|
2020-06-27 06:02:30 +00:00
|
|
|
getComponent(instance, 'prefix') ||
|
|
|
|
getComponent(instance, 'suffix') ||
|
2020-03-07 11:45:13 +00:00
|
|
|
instance.$props.allowClear
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ClearableInputType = ['text', 'input'];
|
|
|
|
|
2020-10-16 06:24:14 +00:00
|
|
|
const ClearableLabeledInput = defineComponent({
|
2020-06-27 06:02:30 +00:00
|
|
|
name: 'ClearableLabeledInput',
|
|
|
|
inheritAttrs: false,
|
2020-03-07 11:45:13 +00:00
|
|
|
props: {
|
|
|
|
prefixCls: PropTypes.string,
|
2020-10-16 06:24:14 +00:00
|
|
|
inputType: PropTypes.oneOf(tuple('text', 'input')),
|
2020-03-07 11:45:13 +00:00
|
|
|
value: PropTypes.any,
|
|
|
|
defaultValue: PropTypes.any,
|
2020-10-10 10:16:28 +00:00
|
|
|
allowClear: PropTypes.looseBool,
|
2020-10-16 06:24:14 +00:00
|
|
|
element: PropTypes.VNodeChild,
|
2020-03-07 11:45:13 +00:00
|
|
|
handleReset: PropTypes.func,
|
2020-10-10 10:16:28 +00:00
|
|
|
disabled: PropTypes.looseBool,
|
2020-10-16 06:24:14 +00:00
|
|
|
size: PropTypes.oneOf(tuple('small', 'large', 'default')),
|
|
|
|
suffix: PropTypes.VNodeChild,
|
|
|
|
prefix: PropTypes.VNodeChild,
|
|
|
|
addonBefore: PropTypes.VNodeChild,
|
|
|
|
addonAfter: PropTypes.VNodeChild,
|
2020-10-10 10:16:28 +00:00
|
|
|
readonly: PropTypes.looseBool,
|
2020-10-27 15:47:20 +00:00
|
|
|
isFocused: PropTypes.looseBool,
|
2020-03-07 11:45:13 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2020-10-16 06:24:14 +00:00
|
|
|
renderClearIcon(prefixCls: string) {
|
2020-07-17 09:13:30 +00:00
|
|
|
const { allowClear, value, disabled, readonly, inputType, handleReset } = this.$props;
|
2020-09-29 14:11:06 +00:00
|
|
|
if (!allowClear) {
|
2020-03-07 11:45:13 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-09-29 14:11:06 +00:00
|
|
|
const showClearIcon =
|
|
|
|
!disabled && !readonly && value !== undefined && value !== null && value !== '';
|
2020-03-07 11:45:13 +00:00
|
|
|
const className =
|
|
|
|
inputType === ClearableInputType[0]
|
|
|
|
? `${prefixCls}-textarea-clear-icon`
|
|
|
|
: `${prefixCls}-clear-icon`;
|
2020-09-29 14:11:06 +00:00
|
|
|
return (
|
|
|
|
<CloseCircleFilled
|
|
|
|
onClick={handleReset}
|
|
|
|
class={classNames(className, {
|
|
|
|
[`${className}-hidden`]: !showClearIcon,
|
|
|
|
})}
|
|
|
|
role="button"
|
|
|
|
/>
|
|
|
|
);
|
2020-03-07 11:45:13 +00:00
|
|
|
},
|
|
|
|
|
2020-10-16 06:24:14 +00:00
|
|
|
renderSuffix(prefixCls: string) {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { suffix, allowClear } = this.$props;
|
|
|
|
if (suffix || allowClear) {
|
|
|
|
return (
|
|
|
|
<span class={`${prefixCls}-suffix`}>
|
|
|
|
{this.renderClearIcon(prefixCls)}
|
|
|
|
{suffix}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2020-10-16 06:24:14 +00:00
|
|
|
renderLabeledIcon(prefixCls: string, element: VNode): VNode {
|
2020-03-07 11:45:13 +00:00
|
|
|
const props = this.$props;
|
2020-10-26 03:28:56 +00:00
|
|
|
const { style } = this.$attrs;
|
2020-03-07 11:45:13 +00:00
|
|
|
const suffix = this.renderSuffix(prefixCls);
|
|
|
|
if (!hasPrefixSuffix(this)) {
|
|
|
|
return cloneElement(element, {
|
2020-06-27 06:02:30 +00:00
|
|
|
value: props.value,
|
2020-03-07 11:45:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const prefix = props.prefix ? (
|
|
|
|
<span class={`${prefixCls}-prefix`}>{props.prefix}</span>
|
|
|
|
) : null;
|
|
|
|
|
2020-07-31 04:43:01 +00:00
|
|
|
const affixWrapperCls = classNames(this.$attrs?.class, `${prefixCls}-affix-wrapper`, {
|
2020-09-29 14:11:06 +00:00
|
|
|
[`${prefixCls}-affix-wrapper-focused`]: props.isFocused,
|
|
|
|
[`${prefixCls}-affix-wrapper-disabled`]: props.disabled,
|
2020-03-07 11:45:13 +00: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,
|
|
|
|
});
|
|
|
|
return (
|
2020-10-26 03:28:56 +00:00
|
|
|
<span class={affixWrapperCls} style={style}>
|
2020-03-07 11:45:13 +00:00
|
|
|
{prefix}
|
|
|
|
{cloneElement(element, {
|
|
|
|
style: null,
|
2020-06-27 06:02:30 +00:00
|
|
|
value: props.value,
|
2020-03-07 11:45:13 +00:00
|
|
|
class: getInputClassName(prefixCls, props.size, props.disabled),
|
|
|
|
})}
|
|
|
|
{suffix}
|
|
|
|
</span>
|
2020-10-16 06:24:14 +00:00
|
|
|
) as VNode;
|
2020-03-07 11:45:13 +00:00
|
|
|
},
|
|
|
|
|
2020-10-16 06:24:14 +00:00
|
|
|
renderInputWithLabel(prefixCls: string, labeledElement: VNode) {
|
2020-06-27 06:02:30 +00:00
|
|
|
const { addonBefore, addonAfter, size } = this.$props;
|
|
|
|
const { style, class: className } = this.$attrs;
|
2020-03-07 11:45:13 +00: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>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2020-10-16 06:24:14 +00:00
|
|
|
renderTextAreaWithClearIcon(prefixCls: string, element: VNode) {
|
2020-06-27 06:02:30 +00:00
|
|
|
const { value, allowClear } = this.$props;
|
|
|
|
const { style, class: className } = this.$attrs;
|
2020-03-07 11:45:13 +00:00
|
|
|
if (!allowClear) {
|
2020-06-27 06:02:30 +00:00
|
|
|
return cloneElement(element, { value });
|
2020-03-07 11:45:13 +00: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 06:02:30 +00:00
|
|
|
value,
|
2020-03-07 11:45:13 +00:00
|
|
|
})}
|
|
|
|
{this.renderClearIcon(prefixCls)}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
renderClearableLabeledInput() {
|
2020-10-16 06:24:14 +00:00
|
|
|
const { prefixCls, inputType, element } = this.$props as any;
|
2020-03-07 11:45:13 +00:00
|
|
|
if (inputType === ClearableInputType[0]) {
|
|
|
|
return this.renderTextAreaWithClearIcon(prefixCls, element);
|
|
|
|
}
|
|
|
|
return this.renderInputWithLabel(prefixCls, this.renderLabeledIcon(prefixCls, element));
|
|
|
|
},
|
|
|
|
},
|
|
|
|
render() {
|
|
|
|
return this.renderClearableLabeledInput();
|
|
|
|
},
|
2020-10-16 06:24:14 +00:00
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
|
|
|
|
export default ClearableLabeledInput;
|