|
| 1 | +import type { VNodeChild } from 'vue' |
| 2 | +import type { |
| 3 | + VxeColgroupProps, |
| 4 | + VxeColgroupSlots, |
| 5 | + VxeColumnProps, |
| 6 | + VxeColumnSlots, |
| 7 | +} from 'vxe-table' |
| 8 | +import type { |
| 9 | + DefaultRow, |
| 10 | + FalsyTableColumnSchema, |
| 11 | + FunctionalTableColumnSchema, |
| 12 | + TableColumnSchemaSlots, |
| 13 | +} from '@/_internal/table' |
| 14 | +import { isFunction } from '@antfu/utils' |
| 15 | +import { defineComponent, isVNode } from 'vue' |
| 16 | +import { VxeColgroup, VxeColumn } from 'vxe-table' |
| 17 | + |
| 18 | +export type ObjectVxeTableColgroupSchema<T extends DefaultRow> = |
| 19 | + VxeColgroupProps & { |
| 20 | + /** |
| 21 | + * 列分组插槽 |
| 22 | + */ |
| 23 | + slots?: TableColumnSchemaSlots<VxeColgroupSlots<T>> |
| 24 | + |
| 25 | + /** |
| 26 | + * 子列定义 |
| 27 | + */ |
| 28 | + children?: VxeTableColumnSchema<T>[] |
| 29 | + } |
| 30 | + |
| 31 | +export type ObjectVxeTableColumnSchema<T extends DefaultRow> = |
| 32 | + VxeColumnProps<T> & { |
| 33 | + /** |
| 34 | + * 列插槽 |
| 35 | + */ |
| 36 | + slots?: TableColumnSchemaSlots<VxeColumnSlots<T>> |
| 37 | + } |
| 38 | + |
| 39 | +export type VxeTableColumnSchema<T extends DefaultRow> = |
| 40 | + | FalsyTableColumnSchema | |
| 41 | + ObjectVxeTableColgroupSchema<T> | |
| 42 | + ObjectVxeTableColumnSchema<T> | |
| 43 | + FunctionalTableColumnSchema |
| 44 | + |
| 45 | +export interface VxeTableSchemaColumnsProps<T extends DefaultRow> { |
| 46 | + /** |
| 47 | + * 列定义,同 `VxeColumn` 的属性,额外支持 `children` 用于定义子列, |
| 48 | + * 列的 `slots` 同 `VxeColumn` 的 `slots` |
| 49 | + */ |
| 50 | + columns: VxeTableColumnSchema<T>[] |
| 51 | +} |
| 52 | + |
| 53 | +export const VxeTableSchemaColumns = defineComponent( |
| 54 | + <T extends DefaultRow>(props: VxeTableSchemaColumnsProps<T>) => { |
| 55 | + return () => { |
| 56 | + const vNodes: VNodeChild = [] |
| 57 | + |
| 58 | + for (const column of props.columns) { |
| 59 | + if (!column) { |
| 60 | + continue |
| 61 | + } |
| 62 | + |
| 63 | + if (isFunction(column)) { |
| 64 | + const vNode = column() |
| 65 | + // 只有在返回 VNode 时才添加到列表中 |
| 66 | + isVNode(vNode) && vNodes.push(vNode) |
| 67 | + continue |
| 68 | + } |
| 69 | + |
| 70 | + if ('children' in column) { |
| 71 | + const { children, slots: finalSlots = {}, ...colgroupProps } = column |
| 72 | + |
| 73 | + vNodes.push( |
| 74 | + <VxeColgroup {...colgroupProps}> |
| 75 | + {{ |
| 76 | + ...finalSlots, |
| 77 | + default: |
| 78 | + children && !finalSlots.default |
| 79 | + ? () => <VxeTableSchemaColumns columns={children} /> |
| 80 | + : finalSlots.default, |
| 81 | + }} |
| 82 | + </VxeColgroup>, |
| 83 | + ) |
| 84 | + } |
| 85 | + else { |
| 86 | + const { slots: finalSlots = {}, ...columnProps } = column |
| 87 | + |
| 88 | + vNodes.push(<VxeColumn {...columnProps}>{finalSlots}</VxeColumn>) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return vNodes |
| 93 | + } |
| 94 | + }, |
| 95 | + { |
| 96 | + props: ['columns'] as never, |
| 97 | + }, |
| 98 | +) |
0 commit comments