Skip to content

Commit 9693d89

Browse files
committed
fix: table template error
1 parent 6e3e52b commit 9693d89

File tree

4 files changed

+44
-22
lines changed

4 files changed

+44
-22
lines changed

components/table/Table.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ import type { PropType } from 'vue';
4444
import { computed, defineComponent, ref, toRef, watchEffect } from 'vue';
4545
import type { DefaultRecordType } from '../vc-table/interface';
4646
import useBreakpoint from '../_util/hooks/useBreakpoint';
47-
import { convertChildrenToColumns } from '../vc-table/hooks/useColumns';
4847
import useConfigInject from '../_util/hooks/useConfigInject';
4948
import { useLocaleReceiver } from '../locale-provider/LocaleReceiver';
5049
import classNames from '../_util/classNames';
5150
import omit from '../_util/omit';
5251
import { initDefaultProps } from '../_util/props-util';
5352
import { ContextSlots, useProvideSlots } from './context';
5453
import useColumns from './hooks/useColumns';
54+
import { convertChildrenToColumns } from './util';
5555

5656
export type { ColumnsType, TablePaginationConfig };
5757

components/table/demo/template.vue

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ title:
88

99
## zh-CN
1010

11-
使用 template 风格的 API
11+
使用 template 风格的 API。
12+
13+
> 不推荐使用,会有一定的性能损耗。
1214

1315
> 这个只是一个描述 `columns` 的语法糖,所以你不能用其他组件去包裹 `Column` 和 `ColumnGroup`。
1416

1517
## en-US
1618

17-
Using template style API
19+
Using template style API.
20+
21+
> Not recommended, there will be a certain performance loss.
1822

1923
> Since this is just a syntax sugar for the prop `columns`, so that you can't compose `Column` and `ColumnGroup` with other Components.
2024

components/table/util.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import type { ColumnType, ColumnTitle, ColumnTitleProps, Key } from './interface';
1+
import { camelize } from 'vue';
2+
import { flattenChildren } from '../_util/props-util';
3+
import type { ColumnType, ColumnsType, ColumnTitle, ColumnTitleProps, Key } from './interface';
24

35
export function getColumnKey<RecordType>(column: ColumnType<RecordType>, defaultKey: string): Key {
46
if ('key' in column && column.key !== undefined && column.key !== null) {
@@ -25,3 +27,37 @@ export function renderColumnTitle<RecordType>(
2527

2628
return title;
2729
}
30+
31+
export function convertChildrenToColumns<RecordType>(
32+
elements: any[] = [],
33+
): ColumnsType<RecordType> {
34+
const flattenElements = flattenChildren(elements);
35+
const columns = [];
36+
flattenElements.forEach(element => {
37+
if (!element) {
38+
return;
39+
}
40+
const key = element.key;
41+
const style = element.props?.style || {};
42+
const cls = element.props?.class || '';
43+
const props = element.props || {};
44+
for (const [k, v] of Object.entries(props)) {
45+
props[camelize(k)] = v;
46+
}
47+
const { default: children, ...restSlots } = element.children || {};
48+
const column = { ...restSlots, ...props, style, class: cls };
49+
if (key) {
50+
column.key = key;
51+
}
52+
if (element.type?.__ANT_TABLE_COLUMN_GROUP) {
53+
column.children = convertChildrenToColumns(
54+
typeof children === 'function' ? children() : children,
55+
);
56+
} else {
57+
const customRender = element.children?.default;
58+
column.customRender = column.customRender || customRender;
59+
}
60+
columns.push(column);
61+
});
62+
return columns;
63+
}

components/vc-table/hooks/useColumns.tsx

-18
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,6 @@ import type {
1313
} from '../interface';
1414
import { INTERNAL_COL_DEFINE } from '../utils/legacyUtil';
1515

16-
export function convertChildrenToColumns<RecordType>(
17-
children: any[] = [],
18-
): ColumnsType<RecordType> {
19-
return children.map(({ key, props }) => {
20-
const { children: nodeChildren, ...restProps } = props;
21-
const column = {
22-
key,
23-
...restProps,
24-
};
25-
26-
if (nodeChildren) {
27-
column.children = convertChildrenToColumns(nodeChildren);
28-
}
29-
30-
return column;
31-
});
32-
}
33-
3416
function flatColumns<RecordType>(columns: ColumnsType<RecordType>): ColumnType<RecordType>[] {
3517
return columns.reduce((list, column) => {
3618
const { fixed } = column;

0 commit comments

Comments
 (0)