2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { ColumnsType, ColumnTitleProps, TransformColumns } from '../interface';
|
2019-11-15 14:35:25 +08:00
|
|
|
import { renderColumnTitle } from '../util';
|
|
|
|
|
|
|
|
function fillTitle<RecordType>(
|
|
|
|
columns: ColumnsType<RecordType>,
|
|
|
|
columnTitleProps: ColumnTitleProps<RecordType>,
|
|
|
|
) {
|
2022-11-19 13:47:33 +08:00
|
|
|
return columns.map((column) => {
|
2019-11-15 14:35:25 +08:00
|
|
|
const cloneColumn = { ...column };
|
|
|
|
|
|
|
|
cloneColumn.title = renderColumnTitle(column.title, columnTitleProps);
|
|
|
|
|
|
|
|
if ('children' in cloneColumn) {
|
|
|
|
cloneColumn.children = fillTitle(cloneColumn.children, columnTitleProps);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cloneColumn;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function useTitleColumns<RecordType>(
|
|
|
|
columnTitleProps: ColumnTitleProps<RecordType>,
|
|
|
|
): [TransformColumns<RecordType>] {
|
|
|
|
const filledColumns = React.useCallback(
|
|
|
|
(columns: ColumnsType<RecordType>) => fillTitle(columns, columnTitleProps),
|
|
|
|
[columnTitleProps],
|
|
|
|
);
|
|
|
|
|
|
|
|
return [filledColumns];
|
|
|
|
}
|