import { ref, h, reactive, PropType, watch } from 'vue' import { defineVxeComponent } from '../../ui/src/comp' import XEUtils from 'xe-utils' import { VxeUI } from '@vxe-ui/core' import type { VxeTagPropTypes, TagReactData, TagPrivateRef, VxeTagEmits, VxeTagPrivateComputed, TagMethods, TagPrivateMethods, VxeTagConstructor, VxeTagPrivateMethods, ValueOf } from '../../../types' const { getConfig, getIcon, createEvent, useFns, renderEmptyElement } = VxeUI export default /* define-vxe-component start */ defineVxeComponent({ name: 'VxeTag', props: { border: { type: Boolean as PropType, default: () => getConfig().tag.border }, visible: { type: Boolean as PropType, default: null }, status: String as PropType, title: [String, Number] as PropType, icon: String as PropType, closeIcon: { type: String as PropType, default: () => getConfig().tag.closeIcon }, content: [String, Number] as PropType, round: Boolean as PropType, closable: { type: Boolean as PropType, default: () => getConfig().tag.closable }, color: String as PropType, loading: Boolean as PropType, size: { type: String as PropType, default: () => getConfig().tag.size || getConfig().size } }, emits: ['click', 'dblclick', 'close', 'update:visible'] as VxeTagEmits, setup(props, context) { const { slots, emit } = context const xID = XEUtils.uniqueId() const { computeSize } = useFns.useSize(props) const refElem = ref() const reactData = reactive({ showTag: props.visible !== false }) const refMaps: TagPrivateRef = { refElem } const computeMaps: VxeTagPrivateComputed = {} const $xeTag = { xID, props, context, reactData, getRefMaps: () => refMaps, getComputeMaps: () => computeMaps } as unknown as VxeTagConstructor & VxeTagPrivateMethods const dispatchEvent = (type: ValueOf, params: Record, evnt: Event | null) => { emit(type, createEvent(evnt, { $tag: $xeTag }, params)) } const tagMethods: TagMethods = { dispatchEvent } const tagPrivateMethods: TagPrivateMethods = {} const updateVisible = () => { reactData.showTag = props.visible !== false } const clickEvent = (evnt: MouseEvent) => { const { loading } = props if (!loading) { dispatchEvent('click', {}, evnt) } } const dblclickEvent = (evnt: MouseEvent) => { const { loading } = props if (!loading) { dispatchEvent('dblclick', {}, evnt) } } const closeEvent = (evnt: MouseEvent) => { const { loading } = props if (!loading) { const visible = !reactData.showTag reactData.showTag = visible emit('update:visible', visible) dispatchEvent('close', { visible }, evnt) } } Object.assign($xeTag, tagMethods, tagPrivateMethods) const renderContent = () => { const { icon, content, closable, closeIcon, loading } = props const defaultSlot = slots.default const iconSlot = slots.icon const closeIconSlot = slots.closeIcon || slots['close-icon'] return [ iconSlot || icon ? h( 'span', { class: 'vxe-tag--icon' }, iconSlot ? iconSlot({}) : [ h('i', { class: icon }) ] ) : renderEmptyElement($xeTag), h( 'span', { class: 'vxe-tag--content' }, defaultSlot ? defaultSlot({}) : XEUtils.toValueString(content) ), loading || closable ? h( 'span', { class: loading ? 'vxe-tag--loading' : 'vxe-tag--close', onClick: closeEvent }, !loading && closeIconSlot ? closeIconSlot({}) : [ h('i', { class: loading ? getIcon().TAG_LOADING : closeIcon || getIcon().TAG_CLOSE }) ] ) : renderEmptyElement($xeTag) ] } const renderVN = () => { const { status, color, title, round, border, loading } = props const { showTag } = reactData const vSize = computeSize.value if (!showTag) { return renderEmptyElement($xeTag) } return h( 'span', { ref: refElem, class: [ 'vxe-tag', { [`size--${vSize}`]: vSize, [`theme--${status}`]: status && !color, [`color--${color}`]: color && !status, 'is--round': round, 'is--border': border, 'is--loading': loading } ], title, onClick: clickEvent, onDblclick: dblclickEvent }, renderContent() ) } watch( () => props.visible, () => { updateVisible() } ) updateVisible() $xeTag.renderVN = renderVN return $xeTag }, render() { return this.renderVN() } }) /* define-vxe-component end */