2021-06-26 01:35:40 +00:00
|
|
|
import type { HTMLAttributes, App, PropType, ExtractPropTypes, Plugin } from 'vue';
|
|
|
|
import { ref, defineComponent, watchEffect, computed } from 'vue';
|
2020-09-23 09:51:02 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2020-10-04 14:08:12 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-08-23 11:33:01 +00:00
|
|
|
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
|
|
|
|
import Wave from '../_util/wave';
|
2021-06-26 01:35:40 +00:00
|
|
|
import type { PresetColorType, PresetStatusColorType } from '../_util/colors';
|
|
|
|
import { PresetColorTypes, PresetStatusColorTypes } from '../_util/colors';
|
|
|
|
import type { LiteralUnion } from '../_util/type';
|
2020-08-23 11:33:01 +00:00
|
|
|
import CheckableTag from './CheckableTag';
|
2021-06-09 14:50:40 +00:00
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
2020-08-23 11:33:01 +00:00
|
|
|
|
|
|
|
const PresetColorRegex = new RegExp(`^(${PresetColorTypes.join('|')})(-inverse)?$`);
|
|
|
|
const PresetStatusColorRegex = new RegExp(`^(${PresetStatusColorTypes.join('|')})$`);
|
|
|
|
|
2020-10-24 01:53:25 +00:00
|
|
|
const tagProps = {
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
color: {
|
|
|
|
type: String as PropType<LiteralUnion<PresetColorType | PresetStatusColorType, string>>,
|
|
|
|
},
|
|
|
|
closable: PropTypes.looseBool.def(false),
|
|
|
|
closeIcon: PropTypes.VNodeChild,
|
|
|
|
visible: PropTypes.looseBool,
|
|
|
|
onClose: {
|
|
|
|
type: Function as PropType<(e: MouseEvent) => void>,
|
|
|
|
},
|
|
|
|
icon: PropTypes.VNodeChild,
|
|
|
|
};
|
|
|
|
|
|
|
|
export type TagProps = HTMLAttributes & Partial<ExtractPropTypes<typeof tagProps>>;
|
2020-08-23 11:33:01 +00:00
|
|
|
|
|
|
|
const Tag = defineComponent({
|
2020-10-01 15:01:31 +00:00
|
|
|
name: 'ATag',
|
2021-06-08 07:00:15 +00:00
|
|
|
props: tagProps,
|
2020-10-04 14:08:12 +00:00
|
|
|
emits: ['update:visible', 'close'],
|
2021-06-09 14:50:40 +00:00
|
|
|
slots: ['closeIcon', 'icon'],
|
2020-10-04 14:08:12 +00:00
|
|
|
setup(props: TagProps, { slots, emit, attrs }) {
|
2021-06-09 14:50:40 +00:00
|
|
|
const { prefixCls, direction } = useConfigInject('tag', props);
|
2020-08-23 11:33:01 +00:00
|
|
|
|
|
|
|
const visible = ref(true);
|
2020-10-04 14:08:12 +00:00
|
|
|
|
|
|
|
watchEffect(() => {
|
|
|
|
if (props.visible !== undefined) {
|
|
|
|
visible.value = props.visible!;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleCloseClick = (e: MouseEvent) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
emit('update:visible', false);
|
|
|
|
emit('close', e);
|
|
|
|
|
|
|
|
if (e.defaultPrevented) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (props.visible === undefined) {
|
|
|
|
visible.value = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-09 14:50:40 +00:00
|
|
|
const isPresetColor = computed(() => {
|
2020-10-04 14:08:12 +00:00
|
|
|
const { color } = props;
|
|
|
|
if (!color) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return PresetColorRegex.test(color) || PresetStatusColorRegex.test(color);
|
2021-06-09 14:50:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const tagClassName = computed(() =>
|
|
|
|
classNames(prefixCls.value, {
|
|
|
|
[`${prefixCls.value}-${props.color}`]: isPresetColor.value,
|
|
|
|
[`${prefixCls.value}-has-color`]: props.color && !isPresetColor.value,
|
|
|
|
[`${prefixCls.value}-hidden`]: !visible.value,
|
|
|
|
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
|
|
|
|
}),
|
|
|
|
);
|
2020-09-23 09:51:02 +00:00
|
|
|
|
2020-08-23 11:33:01 +00:00
|
|
|
return () => {
|
|
|
|
const {
|
2020-11-15 15:18:42 +00:00
|
|
|
icon = slots.icon?.(),
|
2020-08-23 11:33:01 +00:00
|
|
|
color,
|
2020-10-04 14:08:12 +00:00
|
|
|
closeIcon = slots.closeIcon?.(),
|
2020-08-23 11:33:01 +00:00
|
|
|
closable = false,
|
2020-09-23 09:51:02 +00:00
|
|
|
} = props;
|
2020-08-23 11:33:01 +00:00
|
|
|
|
|
|
|
const renderCloseIcon = () => {
|
|
|
|
if (closable) {
|
|
|
|
return closeIcon ? (
|
2021-09-14 14:09:24 +00:00
|
|
|
<div class={`${prefixCls.value}-close-icon`} onClick={handleCloseClick}>
|
2020-08-23 11:33:01 +00:00
|
|
|
{closeIcon}
|
|
|
|
</div>
|
|
|
|
) : (
|
2021-09-14 14:09:24 +00:00
|
|
|
<CloseOutlined class={`${prefixCls.value}-close-icon`} onClick={handleCloseClick} />
|
2020-08-23 11:33:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const tagStyle = {
|
2021-06-09 14:50:40 +00:00
|
|
|
backgroundColor: color && !isPresetColor.value ? color : undefined,
|
2020-08-23 11:33:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const iconNode = icon || null;
|
|
|
|
const children = slots.default?.();
|
|
|
|
const kids = iconNode ? (
|
|
|
|
<>
|
|
|
|
{iconNode}
|
|
|
|
<span>{children}</span>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
children
|
|
|
|
);
|
|
|
|
|
2020-10-04 14:08:12 +00:00
|
|
|
const isNeedWave = 'onClick' in attrs;
|
2020-08-23 11:33:01 +00:00
|
|
|
|
|
|
|
const tagNode = (
|
2021-06-09 14:50:40 +00:00
|
|
|
<span class={tagClassName.value} style={tagStyle}>
|
2020-08-23 11:33:01 +00:00
|
|
|
{kids}
|
|
|
|
{renderCloseIcon()}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
|
|
|
|
return isNeedWave ? <Wave>{tagNode}</Wave> : tagNode;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Tag.CheckableTag = CheckableTag;
|
|
|
|
|
2021-06-23 15:08:16 +00:00
|
|
|
Tag.install = function (app: App) {
|
2020-08-23 11:33:01 +00:00
|
|
|
app.component(Tag.name, Tag);
|
|
|
|
app.component(CheckableTag.name, CheckableTag);
|
2020-10-13 11:14:56 +00:00
|
|
|
return app;
|
2020-08-23 11:33:01 +00:00
|
|
|
};
|
|
|
|
|
2021-06-23 13:47:53 +00:00
|
|
|
export { CheckableTag };
|
|
|
|
|
2020-11-01 07:03:33 +00:00
|
|
|
export default Tag as typeof Tag &
|
|
|
|
Plugin & {
|
|
|
|
readonly CheckableTag: typeof CheckableTag;
|
|
|
|
};
|