2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2024-08-11 10:58:13 +08:00
|
|
|
import type { AnyObject } from 'antd/es/_util/type';
|
2024-04-08 14:04:08 +08:00
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
import type {
|
|
|
|
ColumnGroupType,
|
|
|
|
ColumnsType,
|
|
|
|
ColumnTitleProps,
|
|
|
|
ColumnType,
|
|
|
|
TransformColumns,
|
|
|
|
} from '../interface';
|
2019-11-15 14:35:25 +08:00
|
|
|
import { renderColumnTitle } from '../util';
|
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
const fillTitle = <RecordType extends AnyObject = AnyObject>(
|
2019-11-15 14:35:25 +08:00
|
|
|
columns: ColumnsType<RecordType>,
|
|
|
|
columnTitleProps: ColumnTitleProps<RecordType>,
|
2024-08-11 10:58:13 +08:00
|
|
|
) => {
|
|
|
|
const finalColumns = columns.map((column) => {
|
|
|
|
const cloneColumn: ColumnGroupType<RecordType> | ColumnType<RecordType> = { ...column };
|
2019-11-15 14:35:25 +08:00
|
|
|
cloneColumn.title = renderColumnTitle(column.title, columnTitleProps);
|
|
|
|
if ('children' in cloneColumn) {
|
2024-08-11 10:58:13 +08:00
|
|
|
cloneColumn.children = fillTitle<RecordType>(cloneColumn.children, columnTitleProps);
|
2019-11-15 14:35:25 +08:00
|
|
|
}
|
|
|
|
return cloneColumn;
|
|
|
|
});
|
2024-08-11 10:58:13 +08:00
|
|
|
return finalColumns;
|
|
|
|
};
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
const useTitleColumns = <RecordType extends AnyObject = AnyObject>(
|
2019-11-15 14:35:25 +08:00
|
|
|
columnTitleProps: ColumnTitleProps<RecordType>,
|
2024-08-11 10:58:13 +08:00
|
|
|
) => {
|
|
|
|
const filledColumns = React.useCallback<TransformColumns<RecordType>>(
|
|
|
|
(columns) => fillTitle<RecordType>(columns, columnTitleProps),
|
2019-11-15 14:35:25 +08:00
|
|
|
[columnTitleProps],
|
|
|
|
);
|
2024-08-11 10:58:13 +08:00
|
|
|
return [filledColumns] as const;
|
|
|
|
};
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
export default useTitleColumns;
|