import { ref, h, reactive, computed, PropType, inject, onMounted } 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 { createComponentLog } from '../../ui/src/log' import type { ColReactData, VxeColPropTypes, ColPrivateRef, VxeColEmits, ColMethods, ColPrivateMethods, VxeColPrivateComputed, VxeColConstructor, VxeColPrivateMethods, ValueOf, VxeRowConstructor, VxeRowPrivateMethods } from '../../../types' const { getConfig, createEvent, useFns } = VxeUI const { errLog } = createComponentLog('col') export default /* define-vxe-component start */ defineVxeComponent({ name: 'VxeCol', props: { span: [Number, String] as PropType, align: String as PropType, width: [Number, String] as PropType, fill: Boolean as PropType, ellipsis: Boolean as PropType, size: { type: String as PropType, default: () => getConfig().col.size || getConfig().size } }, emits: [], setup(props, context) { const { slots, emit } = context const xID = XEUtils.uniqueId() useFns.useSize(props) const refElem = ref() const reactData = reactive({}) const refMaps: ColPrivateRef = { refElem } const $xeRow = inject<(VxeRowConstructor & VxeRowPrivateMethods) | null>('$xeRow', null) const computeRowGutter = computed(() => { if ($xeRow) { return $xeRow.props.gutter } return null }) const computeRowVertical = computed(() => { if ($xeRow) { return $xeRow.props.vertical } return null }) const computeColStyle = computed(() => { const { width } = props const rowGutter = computeRowGutter.value const rowVertical = computeRowVertical.value let gridLayout = false if ($xeRow) { gridLayout = !!$xeRow.props.gridLayout } const style: Record = {} if (!gridLayout) { if (rowGutter) { let [lrGutter, tbGutter] = XEUtils.isArray(rowGutter) ? rowGutter : [rowGutter] if (rowVertical) { tbGutter = lrGutter lrGutter = '' } if (lrGutter) { const padding = XEUtils.isNumber(lrGutter) ? toCssUnit(lrGutter / 2) : `calc(${toCssUnit(lrGutter)} / 2)` style.paddingLeft = padding style.paddingRight = padding } if (tbGutter) { const padding = XEUtils.isNumber(tbGutter) ? toCssUnit(tbGutter / 2) : `calc(${toCssUnit(tbGutter)} / 2)` style.paddingTop = padding style.paddingBottom = padding } } if (width) { style.width = toCssUnit(width) } } return style }) const computeMaps: VxeColPrivateComputed = {} const $xeCol = { xID, props, context, reactData, getRefMaps: () => refMaps, getComputeMaps: () => computeMaps } as unknown as VxeColConstructor & VxeColPrivateMethods const dispatchEvent = (type: ValueOf, params: Record, evnt: Event | null) => { emit(type, createEvent(evnt, { $col: $xeCol }, params)) } const colMethods: ColMethods = { dispatchEvent } const colPrivateMethods: ColPrivateMethods = {} Object.assign($xeCol, colMethods, colPrivateMethods) const renderVN = () => { const { span, fill, align, width, ellipsis } = props const colStyle = computeColStyle.value const defaultSlot = slots.default return h( 'div', { ref: refElem, class: [ 'vxe-col', span ? `span${span}` : '', align ? `align--${align}` : '', { 'is--span': !!span, 'is--width': !!width, 'is--fill': fill, 'is--ellipsis': ellipsis } ], style: colStyle }, [ h( 'div', { class: 'vxe-col--inner' }, defaultSlot ? defaultSlot({}) : [] ) ] ) } onMounted(() => { const { width, fill } = props if ($xeRow) { if ($xeRow.props.gridLayout) { if (width) { errLog('vxe.error.notSupportProp', ['grid-layout', `width=${width}`, 'width=null']) } if (fill) { errLog('vxe.error.notSupportProp', ['grid-layout', `fill=${fill}`, 'fill=false']) } } } }) $xeCol.renderVN = renderVN return $xeCol }, render() { return this.renderVN() } }) /* define-vxe-component end */