forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseColumns.tsx
54 lines (48 loc) · 1.76 KB
/
useColumns.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import devWarning from '../../vc-util/devWarning';
import type { Ref } from 'vue';
import type { ContextSlots } from '../context';
import type { TransformColumns, ColumnsType } from '../interface';
import { SELECTION_COLUMN } from './useSelection';
import { EXPAND_COLUMN } from '../../vc-table';
import { customRenderSlot } from '../../_util/vnode';
function fillSlots<RecordType>(columns: ColumnsType<RecordType>, contextSlots: Ref<ContextSlots>) {
const $slots = contextSlots.value;
return columns.map(column => {
if (column === SELECTION_COLUMN || column === EXPAND_COLUMN) return column;
const cloneColumn = { ...column };
const { slots = {} } = cloneColumn;
cloneColumn.__originColumn__ = column;
devWarning(
!('slots' in cloneColumn),
'Table',
'`column.slots` is deprecated. Please use `v-slot:headerCell` `v-slot:bodyCell` instead.',
);
Object.keys(slots).forEach(key => {
const name = slots[key];
if (cloneColumn[key] === undefined && $slots[name]) {
cloneColumn[key] = $slots[name];
}
});
if (contextSlots.value.headerCell && !column.slots?.title) {
cloneColumn.title = customRenderSlot(
contextSlots.value,
'headerCell',
{
title: column.title,
column,
},
() => [column.title as any],
);
}
if ('children' in cloneColumn && Array.isArray(cloneColumn.children)) {
cloneColumn.children = fillSlots(cloneColumn.children, contextSlots);
}
return cloneColumn;
});
}
export default function useColumns<RecordType>(
contextSlots: Ref<ContextSlots>,
): [TransformColumns<RecordType>] {
const filledColumns = (columns: ColumnsType<RecordType>) => fillSlots(columns, contextSlots);
return [filledColumns];
}