mirror of
https://gitee.com/ant-design-vue/ant-design-vue.git
synced 2024-12-02 20:18:22 +08:00
00dc2add94
* feat: update prop type * feat: update ts type * feat: update ts type * feat: update ts type * feat: update ts type * test: update snap * feat: update ts type
143 lines
4.0 KiB
Vue
143 lines
4.0 KiB
Vue
import type { HTMLAttributes, App, PropType, ExtractPropTypes, Plugin } from 'vue';
|
|
import { ref, defineComponent, watchEffect, computed } from 'vue';
|
|
import classNames from '../_util/classNames';
|
|
import PropTypes from '../_util/vue-types';
|
|
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
|
|
import Wave from '../_util/wave';
|
|
import type { PresetColorType, PresetStatusColorType } from '../_util/colors';
|
|
import { PresetColorTypes, PresetStatusColorTypes } from '../_util/colors';
|
|
import type { LiteralUnion } from '../_util/type';
|
|
import CheckableTag from './CheckableTag';
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
|
|
|
const PresetColorRegex = new RegExp(`^(${PresetColorTypes.join('|')})(-inverse)?$`);
|
|
const PresetStatusColorRegex = new RegExp(`^(${PresetStatusColorTypes.join('|')})$`);
|
|
|
|
export const tagProps = () => ({
|
|
prefixCls: String,
|
|
color: {
|
|
type: String as PropType<LiteralUnion<PresetColorType | PresetStatusColorType, string>>,
|
|
},
|
|
closable: { type: Boolean, default: false },
|
|
closeIcon: PropTypes.any,
|
|
visible: { type: Boolean, default: undefined },
|
|
onClose: {
|
|
type: Function as PropType<(e: MouseEvent) => void>,
|
|
},
|
|
'onUpdate:visible': Function as PropType<(vis: boolean) => void>,
|
|
icon: PropTypes.any,
|
|
});
|
|
|
|
export type TagProps = HTMLAttributes & Partial<ExtractPropTypes<ReturnType<typeof tagProps>>>;
|
|
|
|
const Tag = defineComponent({
|
|
name: 'ATag',
|
|
props: tagProps(),
|
|
// emits: ['update:visible', 'close'],
|
|
slots: ['closeIcon', 'icon'],
|
|
setup(props: TagProps, { slots, emit, attrs }) {
|
|
const { prefixCls, direction } = useConfigInject('tag', props);
|
|
|
|
const visible = ref(true);
|
|
|
|
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;
|
|
}
|
|
};
|
|
|
|
const isPresetColor = computed(() => {
|
|
const { color } = props;
|
|
if (!color) {
|
|
return false;
|
|
}
|
|
return PresetColorRegex.test(color) || PresetStatusColorRegex.test(color);
|
|
});
|
|
|
|
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',
|
|
}),
|
|
);
|
|
|
|
return () => {
|
|
const {
|
|
icon = slots.icon?.(),
|
|
color,
|
|
closeIcon = slots.closeIcon?.(),
|
|
closable = false,
|
|
} = props;
|
|
|
|
const renderCloseIcon = () => {
|
|
if (closable) {
|
|
return closeIcon ? (
|
|
<div class={`${prefixCls.value}-close-icon`} onClick={handleCloseClick}>
|
|
{closeIcon}
|
|
</div>
|
|
) : (
|
|
<CloseOutlined class={`${prefixCls.value}-close-icon`} onClick={handleCloseClick} />
|
|
);
|
|
}
|
|
return null;
|
|
};
|
|
|
|
const tagStyle = {
|
|
backgroundColor: color && !isPresetColor.value ? color : undefined,
|
|
};
|
|
|
|
const iconNode = icon || null;
|
|
const children = slots.default?.();
|
|
const kids = iconNode ? (
|
|
<>
|
|
{iconNode}
|
|
<span>{children}</span>
|
|
</>
|
|
) : (
|
|
children
|
|
);
|
|
|
|
const isNeedWave = 'onClick' in attrs;
|
|
|
|
const tagNode = (
|
|
<span class={tagClassName.value} style={tagStyle}>
|
|
{kids}
|
|
{renderCloseIcon()}
|
|
</span>
|
|
);
|
|
|
|
return isNeedWave ? <Wave>{tagNode}</Wave> : tagNode;
|
|
};
|
|
},
|
|
});
|
|
|
|
Tag.CheckableTag = CheckableTag;
|
|
|
|
Tag.install = function (app: App) {
|
|
app.component(Tag.name, Tag);
|
|
app.component(CheckableTag.name, CheckableTag);
|
|
return app;
|
|
};
|
|
|
|
export { CheckableTag };
|
|
|
|
export default Tag as typeof Tag &
|
|
Plugin & {
|
|
readonly CheckableTag: typeof CheckableTag;
|
|
};
|