ant-design/components/table/hooks/useTitleColumns.ts
lijianan 857ee24e38
type(Table): TypeScript improvement (#50351)
* type: Table TypeScript improvement

* type: Table TypeScript improvement

* type: Table TypeScript improvement

* fix: fix demo

* fix: fix demo

* fix: fix type

* fix: fix type

* fix: fix type

* fix: fix type
2024-08-11 10:58:13 +08:00

39 lines
1.2 KiB
TypeScript

import * as React from 'react';
import type { AnyObject } from 'antd/es/_util/type';
import type {
ColumnGroupType,
ColumnsType,
ColumnTitleProps,
ColumnType,
TransformColumns,
} from '../interface';
import { renderColumnTitle } from '../util';
const fillTitle = <RecordType extends AnyObject = AnyObject>(
columns: ColumnsType<RecordType>,
columnTitleProps: ColumnTitleProps<RecordType>,
) => {
const finalColumns = columns.map((column) => {
const cloneColumn: ColumnGroupType<RecordType> | ColumnType<RecordType> = { ...column };
cloneColumn.title = renderColumnTitle(column.title, columnTitleProps);
if ('children' in cloneColumn) {
cloneColumn.children = fillTitle<RecordType>(cloneColumn.children, columnTitleProps);
}
return cloneColumn;
});
return finalColumns;
};
const useTitleColumns = <RecordType extends AnyObject = AnyObject>(
columnTitleProps: ColumnTitleProps<RecordType>,
) => {
const filledColumns = React.useCallback<TransformColumns<RecordType>>(
(columns) => fillTitle<RecordType>(columns, columnTitleProps),
[columnTitleProps],
);
return [filledColumns] as const;
};
export default useTitleColumns;