import { ref, h, computed, PropType, reactive } from 'vue' import { defineVxeComponent } from '../../ui/src/comp' import XEUtils from 'xe-utils' import { VxeUI } from '@vxe-ui/core' import { toCssUnit } from '../../ui/src/dom' import type { AvatarReactData, VxeAvatarEmits, AvatarMethods, VxeAvatarPropTypes, VxeComponentStyleType, AvatarPrivateMethods, ValueOf, AvatarPrivateRef, VxeAvatarPrivateComputed, VxeAvatarConstructor, VxeAvatarPrivateMethods } from '../../../types' const { getConfig, createEvent, useFns, renderEmptyElement } = VxeUI export default /* define-vxe-component start */ defineVxeComponent({ name: 'VxeAvatar', props: { count: [String, Number] as PropType, dot: Boolean as PropType, content: [String, Number] as PropType, icon: String as PropType, src: String as PropType, width: [String, Number] as PropType, height: [String, Number] as PropType, circle: { type: Boolean as PropType, default: () => getConfig().avatar.circle }, status: { type: String as PropType, default: () => getConfig().avatar.status }, size: { type: String as PropType, default: () => getConfig().avatar.size || getConfig().size } }, emits: [] as VxeAvatarEmits, setup(props, context) { const { emit } = context const xID = XEUtils.uniqueId() const { computeSize } = useFns.useSize(props) const refElem = ref() const reactData = reactive({}) const refMaps: AvatarPrivateRef = { refElem } const computeAvatarStyle = computed(() => { const { width, height } = props const stys: VxeComponentStyleType = {} if (width) { stys.width = toCssUnit(width) } if (height) { stys.height = toCssUnit(height) } return stys }) const computeCountNum = computed(() => { const { count } = props return count ? XEUtils.toNumber(count) : 0 }) const computeMaps: VxeAvatarPrivateComputed = {} const $xeAvatar = { xID, props, context, reactData, getRefMaps: () => refMaps, getComputeMaps: () => computeMaps } as unknown as VxeAvatarConstructor & VxeAvatarPrivateMethods const dispatchEvent = (type: ValueOf, params: Record, evnt: Event | null) => { emit(type, createEvent(evnt, { $avatar: $xeAvatar }, params)) } const avatarMethods: AvatarMethods = { dispatchEvent } const avatarPrivateMethods: AvatarPrivateMethods = {} Object.assign($xeAvatar, avatarMethods, avatarPrivateMethods) const renderContent = () => { const { icon, content, src } = props if (icon) { return h( 'span', { class: 'vxe-avatar--icon' }, [ h('i', { class: icon }) ] ) } if (content) { return h( 'span', { class: 'vxe-avatar--content' }, `${content}` ) } if (src) { return h('img', { class: 'vxe-avatar--img', src }) } return renderEmptyElement($xeAvatar) } const renderVN = () => { const { circle, dot, status } = props const vSize = computeSize.value const countNum = computeCountNum.value const avatarStyle = computeAvatarStyle.value return h( 'div', { ref: refElem, class: [ 'vxe-avatar', { [`size--${vSize}`]: vSize, [`theme--${status}`]: status, 'is--circle': circle, 'is--dot': dot } ], style: avatarStyle }, [ renderContent(), countNum ? h( 'span', { class: 'vxe-avatar--count' }, countNum > 99 ? '99+' : `${countNum}` ) : renderEmptyElement($xeAvatar) ] ) } $xeAvatar.renderVN = renderVN return $xeAvatar }, render() { return this.renderVN() } }) /* define-vxe-component end */