import {computed, designComponent, getComponentCls, iHTMLElement, iMouseEvent, PropType, useClasses, useRefs, useStyles} from "@peryl/react-compose"; import {ThemeSize, ThemeStatus, useStyle} from "../../uses/useStyle"; import {iTagColor, TagColor} from "./tag.utils"; import Icon from "../Icon"; import Loading from "../Loading"; import './tag.scss'; export const Tag = designComponent({ name: '-tag', inheritPropsType: iHTMLElement, props: { label: {}, // 标签文本 icon: { type: String }, // 标签图标 loading: { type: Boolean }, // 开启加载状态 checked: { type: Boolean, default: null },// 标签的激活状态 color: { type: [String, Object] as PropType },// 标签颜色 closeable: { type: Boolean }, // 是否可删除 size: { type: String as PropType, default: ThemeSize.small },// 'large', 'normal', 'small', 'mini' }, slots: ['default'], emits: { onClose: (e: iMouseEvent) => true, onClick: (e: iMouseEvent) => true, }, setup({ props, event: { emit }, slots }) { const { refs, onRef } = useRefs({ closeWrapper: {} as typeof HTMLSpanElement }); const status = Object.keys(ThemeStatus); const colors = computed((): iTagColor => { let color = props.color || 'primary'; if (typeof color === "string") { if (color in TagColor) { return TagColor[color]; } if (status.indexOf(color) > -1) { return { fill: `var(--${getComponentCls(`${color}-6`)})`, text: 'white', }; } return { fill: color, text: 'white' }; } else { return color; } }); const { styleComputed } = useStyle(); const classes = useClasses(() => [ getComponentCls('tag'), `tag-size-${styleComputed.value.size}`, { 'tag-uncheck': props.checked === false, } ]); const styles = useStyles(styles => { if (props.checked === false) {return;} const { fill, text } = colors.value; styles.backgroundColor = fill; styles.color = text; }); const handler = { onClick: (e: iMouseEvent) => { if (refs.closeWrapper == e.target || (!!refs.closeWrapper && refs.closeWrapper.contains(e.target as HTMLElement))) { return; } emit.onClick(e); }, onClose: (e: iMouseEvent) => emit.onClose(e), }; return () => ( {!!props.icon && } {!!props.loading && } {slots.default(props.label as any)} {!!props.closeable && } ); }, });