From 9693d89342c9777c17e28f6fedb5b091d1c3a575 Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Thu, 9 Sep 2021 17:20:08 +0800 Subject: [PATCH] fix: table template error --- components/table/Table.tsx | 2 +- components/table/demo/template.vue | 8 +++-- components/table/util.ts | 38 +++++++++++++++++++++++- components/vc-table/hooks/useColumns.tsx | 18 ----------- 4 files changed, 44 insertions(+), 22 deletions(-) diff --git a/components/table/Table.tsx b/components/table/Table.tsx index 866a74d9b..b2e8b5557 100644 --- a/components/table/Table.tsx +++ b/components/table/Table.tsx @@ -44,7 +44,6 @@ import type { PropType } from 'vue'; import { computed, defineComponent, ref, toRef, watchEffect } from 'vue'; import type { DefaultRecordType } from '../vc-table/interface'; import useBreakpoint from '../_util/hooks/useBreakpoint'; -import { convertChildrenToColumns } from '../vc-table/hooks/useColumns'; import useConfigInject from '../_util/hooks/useConfigInject'; import { useLocaleReceiver } from '../locale-provider/LocaleReceiver'; import classNames from '../_util/classNames'; @@ -52,6 +51,7 @@ import omit from '../_util/omit'; import { initDefaultProps } from '../_util/props-util'; import { ContextSlots, useProvideSlots } from './context'; import useColumns from './hooks/useColumns'; +import { convertChildrenToColumns } from './util'; export type { ColumnsType, TablePaginationConfig }; diff --git a/components/table/demo/template.vue b/components/table/demo/template.vue index 15f7031cf..051d3099c 100644 --- a/components/table/demo/template.vue +++ b/components/table/demo/template.vue @@ -8,13 +8,17 @@ title: ## zh-CN -使用 template 风格的 API +使用 template 风格的 API。 + +> 不推荐使用,会有一定的性能损耗。 > 这个只是一个描述 `columns` 的语法糖,所以你不能用其他组件去包裹 `Column` 和 `ColumnGroup`。 ## en-US -Using template style API +Using template style API. + +> Not recommended, there will be a certain performance loss. > Since this is just a syntax sugar for the prop `columns`, so that you can't compose `Column` and `ColumnGroup` with other Components. diff --git a/components/table/util.ts b/components/table/util.ts index cdb423d20..79fa7d809 100644 --- a/components/table/util.ts +++ b/components/table/util.ts @@ -1,4 +1,6 @@ -import type { ColumnType, ColumnTitle, ColumnTitleProps, Key } from './interface'; +import { camelize } from 'vue'; +import { flattenChildren } from '../_util/props-util'; +import type { ColumnType, ColumnsType, ColumnTitle, ColumnTitleProps, Key } from './interface'; export function getColumnKey(column: ColumnType, defaultKey: string): Key { if ('key' in column && column.key !== undefined && column.key !== null) { @@ -25,3 +27,37 @@ export function renderColumnTitle( return title; } + +export function convertChildrenToColumns( + elements: any[] = [], +): ColumnsType { + const flattenElements = flattenChildren(elements); + const columns = []; + flattenElements.forEach(element => { + if (!element) { + return; + } + const key = element.key; + const style = element.props?.style || {}; + const cls = element.props?.class || ''; + const props = element.props || {}; + for (const [k, v] of Object.entries(props)) { + props[camelize(k)] = v; + } + const { default: children, ...restSlots } = element.children || {}; + const column = { ...restSlots, ...props, style, class: cls }; + if (key) { + column.key = key; + } + if (element.type?.__ANT_TABLE_COLUMN_GROUP) { + column.children = convertChildrenToColumns( + typeof children === 'function' ? children() : children, + ); + } else { + const customRender = element.children?.default; + column.customRender = column.customRender || customRender; + } + columns.push(column); + }); + return columns; +} diff --git a/components/vc-table/hooks/useColumns.tsx b/components/vc-table/hooks/useColumns.tsx index a9f3ad927..ab45c2cab 100644 --- a/components/vc-table/hooks/useColumns.tsx +++ b/components/vc-table/hooks/useColumns.tsx @@ -13,24 +13,6 @@ import type { } from '../interface'; import { INTERNAL_COL_DEFINE } from '../utils/legacyUtil'; -export function convertChildrenToColumns( - children: any[] = [], -): ColumnsType { - return children.map(({ key, props }) => { - const { children: nodeChildren, ...restProps } = props; - const column = { - key, - ...restProps, - }; - - if (nodeChildren) { - column.children = convertChildrenToColumns(nodeChildren); - } - - return column; - }); -} - function flatColumns(columns: ColumnsType): ColumnType[] { return columns.reduce((list, column) => { const { fixed } = column;