Skip to content

Commit

Permalink
fix(table): fix no has key when maxsize call error
Browse files Browse the repository at this point in the history
close #6062
  • Loading branch information
chenshuai2144 committed Oct 13, 2022
1 parent b98c387 commit 657e971
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
17 changes: 14 additions & 3 deletions packages/table/src/components/ColumnSetting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,17 @@ const CheckboxList: React.FC<{
const checkedKeys: string[] = [];
const treeMap = new Map<string | number, DataNode>();

const loopData = (data: any[], parentConfig?: ColumnsState): DataNode[] =>
const loopData = (
data: any[],
parentConfig?: ColumnsState & {
columnKey: string;
},
): DataNode[] =>
data.map(({ key, dataIndex, children, ...rest }) => {
const columnKey = genColumnKey(key, rest.index);
const columnKey = genColumnKey(
key,
[parentConfig?.columnKey, rest.index].filter(Boolean).join('-'),
);
const config = columnsMap[columnKey || 'null'] || { show: true };
if (config.show !== false && !children) {
checkedKeys.push(columnKey);
Expand All @@ -152,7 +160,10 @@ const CheckboxList: React.FC<{
isLeaf: parentConfig ? true : undefined,
};
if (children) {
item.children = loopData(children, config);
item.children = loopData(children, {
...config,
columnKey,
});
// 如果children 已经全部是show了,把自己也设置为show
if (
item.children?.every((childrenItem) =>
Expand Down
46 changes: 29 additions & 17 deletions packages/table/src/utils/genProColumnToColumn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ import type { ProColumnGroupType, ProColumns } from '../typing';
import { columnRender, defaultOnFilter, renderColumnsTitle } from './columnRender';
import { genColumnKey } from './index';

type ColumnToColumnReturnType<T> = (TableColumnType<T> & {
index?: number;
isExtraColumns?: boolean;
extraColumn?: typeof Table.EXPAND_COLUMN | typeof Table.SELECTION_COLUMN;
})[];

type ColumnToColumnParams<T> = {
columns: ProColumns<T, any>[];
counter: ReturnType<typeof useContainer>;
columnEmptyText: ProFieldEmptyText;
type: ProSchemaComponentTypes;
editableUtils: UseEditableUtilType;
} & Pick<TableProps<T>, 'rowKey' | 'childrenColumnName'>;

/**
* 转化 columns 到 pro 的格式 主要是 render 方法的自行实现
*
Expand All @@ -17,18 +31,9 @@ import { genColumnKey } from './index';
* @param columnEmptyText
*/
export function genProColumnToColumn<T>(
params: {
columns: ProColumns<T, any>[];
counter: ReturnType<typeof useContainer>;
columnEmptyText: ProFieldEmptyText;
type: ProSchemaComponentTypes;
editableUtils: UseEditableUtilType;
} & Pick<TableProps<T>, 'rowKey' | 'childrenColumnName'>,
): (TableColumnType<T> & {
index?: number;
isExtraColumns?: boolean;
extraColumn?: typeof Table.EXPAND_COLUMN | typeof Table.SELECTION_COLUMN;
})[] {
params: ColumnToColumnParams<T>,
parents?: ProColumnGroupType<T, any>,
): ColumnToColumnReturnType<T> {
const {
columns,
counter,
Expand All @@ -52,7 +57,11 @@ export function genProColumnToColumn<T>(
onFilter,
filters = [],
} = columnProps as ProColumnGroupType<T, any>;
const columnKey = genColumnKey(key || dataIndex?.toString(), columnsIndex);
const columnKey = genColumnKey(
key || dataIndex?.toString(),
[parents?.key, columnsIndex].filter(Boolean).join('-'),
);
console.log(key, dataIndex?.toString());
// 这些都没有,说明是普通的表格不需要 pro 管理
const noNeedPro = !valueEnum && !valueType && !children;
if (noNeedPro) {
Expand Down Expand Up @@ -106,10 +115,13 @@ export function genProColumnToColumn<T>(
fixed: config.fixed,
width: columnProps.width || (columnProps.fixed ? 200 : undefined),
children: (columnProps as ProColumnGroupType<T, any>).children
? genProColumnToColumn({
...params,
columns: (columnProps as ProColumnGroupType<T, any>)?.children,
})
? genProColumnToColumn(
{
...params,
columns: (columnProps as ProColumnGroupType<T, any>)?.children,
},
{ ...columnProps, key: columnKey } as ProColumnGroupType<T, any>,
)
: undefined,
render: (text: any, rowData: T, index: number) => {
if (typeof rowKey === 'function') {
Expand Down
5 changes: 4 additions & 1 deletion packages/table/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ export const isMergeCell = (
* @param dataIndex 在对象中的数据
* @param index 序列号,理论上唯一
*/
export const genColumnKey = (key?: React.ReactText | undefined, index?: number): string => {
export const genColumnKey = (
key?: React.ReactText | undefined,
index?: number | string,
): string => {
if (key) {
return Array.isArray(key) ? key.join('-') : key.toString();
}
Expand Down

0 comments on commit 657e971

Please sign in to comment.