import { ref, h, reactive, provide, PropType, computed, 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 { VxeRowPropTypes, RowReactData, RowPrivateRef, VxeRowEmits, RowMethods, RowPrivateMethods, VxeRowPrivateComputed, VxeRowConstructor, VxeRowPrivateMethods, ValueOf } from '../../../types' const { getConfig, createEvent, useFns } = VxeUI const { warnLog } = createComponentLog('row') export default /* define-vxe-component start */ defineVxeComponent({ name: 'VxeRow', props: { gridLayout: { type: Boolean as PropType, default: () => getConfig().row.gridLayout }, gutter: [Number, String, Array] as PropType, wrap: { type: Boolean as PropType, default: () => getConfig().row.wrap }, vertical: Boolean as PropType, size: { type: String as PropType, default: () => getConfig().row.size || getConfig().size } }, emits: ['click'] as VxeRowEmits, setup(props, context) { const { slots, emit } = context const xID = XEUtils.uniqueId() useFns.useSize(props) const refElem = ref() const reactData = reactive({}) const refMaps: RowPrivateRef = { refElem } const computeRowStyle = computed(() => { const { gridLayout, gutter, vertical } = props const style: Record = {} if (gutter) { let [lrGutter, tbGutter] = XEUtils.isArray(gutter) ? gutter : [gutter] if (gridLayout) { style.columnGap = toCssUnit(lrGutter) style.rowGap = toCssUnit(tbGutter || lrGutter) } else { if (vertical) { tbGutter = lrGutter lrGutter = '' } if (lrGutter) { const offsetSize = XEUtils.isNumber(lrGutter) ? toCssUnit(-(lrGutter / 2)) : `calc(${toCssUnit(lrGutter)} / 2 * -1)` style.marginLeft = offsetSize style.marginRight = offsetSize } if (tbGutter) { const offsetSize = XEUtils.isNumber(tbGutter) ? toCssUnit(-(tbGutter / 2)) : `calc(${toCssUnit(tbGutter)} / 2 * -1)` style.marginTop = offsetSize style.marginBottom = offsetSize } } } return style }) const computeMaps: VxeRowPrivateComputed = {} const $xeRow = { xID, props, context, reactData, getRefMaps: () => refMaps, getComputeMaps: () => computeMaps } as unknown as VxeRowConstructor & VxeRowPrivateMethods const clickEvent = (evnt: Event) => { dispatchEvent('click', {}, evnt) } const dispatchEvent = (type: ValueOf, params: Record, evnt: Event | null) => { emit(type, createEvent(evnt, { $row: $xeRow }, params)) } const rowMethods: RowMethods = { dispatchEvent } const rowPrivateMethods: RowPrivateMethods = {} Object.assign($xeRow, rowMethods, rowPrivateMethods) const renderVN = () => { const { gridLayout, vertical, wrap } = props const rowStyle = computeRowStyle.value const defaultSlot = slots.default return h( 'div', { ref: refElem, class: [ 'vxe-row', gridLayout ? 'grid--layout' : 'def--layout', { 'is--vertical': vertical, 'is--wrap': wrap } ], style: rowStyle, onClick: clickEvent }, defaultSlot ? defaultSlot({}) : [] ) } onMounted(() => { const { gridLayout, wrap } = props if (gridLayout && !wrap) { warnLog('vxe.error.notSupportProp', ['grid-layout', 'wrap=false', 'wrap=true']) } }) provide('$xeRow', $xeRow) $xeRow.renderVN = renderVN return $xeRow }, render() { return this.renderVN() } }) /* define-vxe-component end */