import { ref, h, reactive, PropType, computed } from 'vue' import { defineVxeComponent } from '../../ui/src/comp' import XEUtils from 'xe-utils' import { VxeUI } from '@vxe-ui/core' import { getSlotVNs } from '../../ui/src/vn' import { toCssUnit } from '../../ui/src/dom' import { getDefaultConfig } from '../../ui/src/utils' import VxeLoadingComponent from '../../loading' import VxeScrollbarComponent from '../../scrollbar' import type { CardReactData, VxeCardEmits, VxeCardPropTypes, CardPrivateRef, ValueOf, CardMethods, CardPrivateMethods, VxeCardPrivateComputed, VxeCardConstructor, VxeCardPrivateMethods } from '../../../types' const { getConfig, createEvent, useFns, renderEmptyElement } = VxeUI export default /* define-vxe-component start */ defineVxeComponent({ name: 'VxeCard', props: { title: String as PropType, showTitleOverflow: { type: Boolean as PropType, default: () => getConfig().card.showTitleOverflow }, width: [String, Number] as PropType, height: [String, Number] as PropType, border: { type: Boolean as PropType, default: () => getConfig().card.border }, loading: Boolean as PropType, shadow: { type: Boolean as PropType, default: () => getConfig().card.shadow }, padding: { type: Boolean as PropType, default: () => getConfig().card.padding }, size: { type: String as PropType, default: () => getConfig().card.size || getConfig().size }, useScrollbar: { type: Boolean as PropType, default: () => getDefaultConfig(getConfig().card.useScrollbar, getConfig().useScrollbar) }, scrollbarConfig: Object as PropType }, emits: [] as VxeCardEmits, setup(props, context) { const { slots, emit } = context const xID = XEUtils.uniqueId() const refElem = ref() const { computeSize } = useFns.useSize(props) const { computeIsVScrollbar } = useFns.useVScrollbar(props) const reactData = reactive({}) const refMaps: CardPrivateRef = { refElem } const computeCardStyle = computed(() => { const { height, width } = props const stys: Record = {} if (width) { stys.width = toCssUnit(width) } if (height) { stys.height = toCssUnit(height) } return stys }) const computeScrollbarOpts = computed(() => { return Object.assign({}, getConfig().card.scrollbarConfig, props.scrollbarConfig) }) const computeScrollbarProps = computed(() => { const scrollbarOpts = computeScrollbarOpts.value return { width: scrollbarOpts.width, height: scrollbarOpts.height, autoHide: scrollbarOpts.autoHide, visible: scrollbarOpts.visible, xConfig: scrollbarOpts.xConfig, yConfig: scrollbarOpts.yConfig, className: scrollbarOpts.className, viewAttrs: scrollbarOpts.viewAttrs } }) const computeMaps: VxeCardPrivateComputed = { computeSize } const $xeCard = { xID, props, context, reactData, getRefMaps: () => refMaps, getComputeMaps: () => computeMaps } as unknown as VxeCardConstructor & VxeCardPrivateMethods const dispatchEvent = (type: ValueOf, params: Record, evnt: Event | null) => { emit(type, createEvent(evnt, { $card: $xeCard }, params)) } const cardMethods: CardMethods = { dispatchEvent } const cardPrivateMethods: CardPrivateMethods = {} Object.assign($xeCard, cardMethods, cardPrivateMethods) const renderVN = () => { const { title, border, shadow, padding, loading, showTitleOverflow } = props const defaultSlot = slots.default const headerSlot = slots.header const titleSlot = slots.title const extraSlot = slots.extra const footerSlot = slots.footer const leftSlot = slots.left const rightSlot = slots.right const vSize = computeSize.value const isVScrollbar = computeIsVScrollbar.value const cardStyle = computeCardStyle.value const scrollbarOpts = computeScrollbarOpts.value const scrollbarProps = computeScrollbarProps.value const { viewClassName, viewInnerClassName } = scrollbarOpts return h( 'div', { ref: refElem, class: [ 'vxe-card', { [`size--${vSize}`]: vSize, 'is--border': border, 'is--shadow': shadow, 'is--padding': padding } ], style: cardStyle }, [ title || titleSlot || headerSlot ? h( 'div', { class: 'vxe-card--header' }, headerSlot ? getSlotVNs(headerSlot({})) : [ h( 'div', { class: [ 'vxe-card--header-title', { 'is--ellipsis': showTitleOverflow } ] }, titleSlot ? getSlotVNs(titleSlot({})) : `${title || ''}` ), extraSlot ? h( 'div', { class: 'vxe-card--header-extra' }, getSlotVNs(extraSlot({})) ) : renderEmptyElement($xeCard) ] ) : renderEmptyElement($xeCard), h( 'div', { class: 'vxe-card--body' }, [ leftSlot ? isVScrollbar ? h( VxeScrollbarComponent, { class: 'vxe-card--body-scrollbar-left', ...scrollbarProps, viewClassName: 'vxe-card--body-scrollbar-left-view', viewInnerClassName: 'vxe-card--body-scrollbar-left-inner' }, { default: () => getSlotVNs(leftSlot({})) } ) : h( 'div', { class: 'vxe-card--body-left' }, getSlotVNs(leftSlot({})) ) : renderEmptyElement($xeCard), isVScrollbar ? h( VxeScrollbarComponent, { class: 'vxe-card--body-scrollbar-content', ...scrollbarProps, viewClassName: 'vxe-card--body-scrollbar-content-view' + (viewClassName ? ' ' + viewClassName : ''), viewInnerClassName: 'vxe-card--body-scrollbar-content-inner' + (viewClassName ? ' ' + viewInnerClassName : '') }, { default: () => (defaultSlot ? getSlotVNs(defaultSlot({})) : []) } ) : h( 'div', { class: 'vxe-card--body-content' }, defaultSlot ? getSlotVNs(defaultSlot({})) : [] ), rightSlot ? isVScrollbar ? h( VxeScrollbarComponent, { class: 'vxe-card--body-scrollbar-right', ...scrollbarProps, viewClassName: 'vxe-card--body-scrollbar-right-view', viewInnerClassName: 'vxe-card--body-scrollbar-right-inner' }, { default: () => getSlotVNs(rightSlot({})) } ) : h( 'div', { class: 'vxe-card--body-right' }, getSlotVNs(rightSlot({})) ) : renderEmptyElement($xeCard) ] ), footerSlot ? h( 'div', { class: 'vxe-card--footer' }, getSlotVNs(footerSlot({})) ) : renderEmptyElement($xeCard), /** * 加载中 */ h(VxeLoadingComponent, { class: 'vxe-card--loading', modelValue: loading }) ] ) } $xeCard.renderVN = renderVN return $xeCard }, render() { return this.renderVN() } }) /* define-vxe-component end */