ant-design-vue/components/tag/index.tsx

161 lines
4.5 KiB
Vue
Raw Normal View History

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';
2023-01-26 03:13:29 +00:00
import { isPresetColor, isPresetStatusColor } from '../_util/colors';
2021-06-26 01:35:40 +00:00
import type { LiteralUnion } from '../_util/type';
2020-08-23 11:33:01 +00:00
import CheckableTag from './CheckableTag';
2023-01-27 08:00:17 +00:00
import useConfigInject from '../config-provider/hooks/useConfigInject';
2023-02-03 01:50:40 +00:00
import warning from '../_util/warning';
import useStyle from './style';
2020-08-23 11:33:01 +00:00
export const tagProps = () => ({
prefixCls: String,
2020-10-24 01:53:25 +00:00
color: {
2023-01-26 03:13:29 +00:00
type: String as PropType<LiteralUnion<PresetColorType | PresetStatusColorType>>,
2020-10-24 01:53:25 +00:00
},
closable: { type: Boolean, default: false },
2021-12-16 09:20:18 +00:00
closeIcon: PropTypes.any,
2023-02-03 01:50:40 +00:00
/** @deprecated `visible` will be removed in next major version. */
visible: { type: Boolean, default: undefined },
2020-10-24 01:53:25 +00:00
onClose: {
type: Function as PropType<(e: MouseEvent) => void>,
},
'onUpdate:visible': Function as PropType<(vis: boolean) => void>,
2021-12-16 09:20:18 +00:00
icon: PropTypes.any,
});
2020-10-24 01:53:25 +00:00
export type TagProps = HTMLAttributes & Partial<ExtractPropTypes<ReturnType<typeof tagProps>>>;
2020-08-23 11:33:01 +00:00
const Tag = defineComponent({
compatConfig: { MODE: 3 },
2020-10-01 15:01:31 +00:00
name: 'ATag',
2023-02-03 01:50:40 +00:00
inheritAttrs: false,
props: tagProps(),
// 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
2023-02-03 01:50:40 +00:00
const [wrapSSR, hashId] = useStyle(prefixCls);
2020-08-23 11:33:01 +00:00
const visible = ref(true);
2020-10-04 14:08:12 +00:00
2023-02-03 01:50:40 +00:00
// Warning for deprecated usage
if (process.env.NODE_ENV !== 'production') {
warning(
!('visible' in props),
'Tag',
'`visible` is deprecated, please use `<Tag v-show="visible" />` instead.',
);
}
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;
}
};
2023-01-26 03:13:29 +00:00
// const isPresetColor = computed(() => {
// const { color } = props;
// if (!color) {
// return false;
// }
// return PresetColorRegex.test(color) || PresetStatusColorRegex.test(color);
// });
const isInternalColor = computed(
() => isPresetColor(props.color) || isPresetStatusColor(props.color),
);
2021-06-09 14:50:40 +00:00
const tagClassName = computed(() =>
2023-02-03 01:50:40 +00:00
classNames(prefixCls.value, hashId.value, {
2023-01-26 03:13:29 +00:00
[`${prefixCls.value}-${props.color}`]: isInternalColor.value,
[`${prefixCls.value}-has-color`]: props.color && !isInternalColor.value,
2021-06-09 14:50:40 +00:00
[`${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 ? (
<span class={`${prefixCls.value}-close-icon`} onClick={handleCloseClick}>
2020-08-23 11:33:01 +00:00
{closeIcon}
</span>
2020-08-23 11:33:01 +00:00
) : (
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 = {
2023-01-26 03:13:29 +00:00
backgroundColor: color && !isInternalColor.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 = (
2023-02-03 01:50:40 +00:00
<span {...attrs} class={tagClassName.value} style={tagStyle}>
2020-08-23 11:33:01 +00:00
{kids}
{renderCloseIcon()}
</span>
);
2023-02-03 01:50:40 +00:00
return wrapSSR(isNeedWave ? <Wave>{tagNode}</Wave> : tagNode);
2020-08-23 11:33:01 +00:00
};
},
});
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;
};