2019-11-15 14:35:25 +08:00
|
|
|
/* eslint-disable import/prefer-default-export */
|
2024-08-11 10:58:13 +08:00
|
|
|
import type { AnyObject } from '../_util/type';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { ColumnTitle, ColumnTitleProps, ColumnType, Key } from './interface';
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
export const getColumnKey = <RecordType extends AnyObject = AnyObject>(
|
|
|
|
column: ColumnType<RecordType>,
|
|
|
|
defaultKey: string,
|
|
|
|
): Key => {
|
2020-02-03 16:48:13 +08:00
|
|
|
if ('key' in column && column.key !== undefined && column.key !== null) {
|
2019-11-15 14:35:25 +08:00
|
|
|
return column.key;
|
|
|
|
}
|
|
|
|
if (column.dataIndex) {
|
2024-08-11 10:58:13 +08:00
|
|
|
return Array.isArray(column.dataIndex) ? column.dataIndex.join('.') : (column.dataIndex as Key);
|
2019-11-15 14:35:25 +08:00
|
|
|
}
|
|
|
|
return defaultKey;
|
2024-08-11 10:58:13 +08:00
|
|
|
};
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
export function getColumnPos(index: number, pos?: string) {
|
|
|
|
return pos ? `${pos}-${index}` : `${index}`;
|
|
|
|
}
|
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
export const renderColumnTitle = <RecordType extends AnyObject = AnyObject>(
|
2019-11-15 14:35:25 +08:00
|
|
|
title: ColumnTitle<RecordType>,
|
|
|
|
props: ColumnTitleProps<RecordType>,
|
2024-08-11 10:58:13 +08:00
|
|
|
) => {
|
2019-11-15 14:35:25 +08:00
|
|
|
if (typeof title === 'function') {
|
|
|
|
return title(props);
|
|
|
|
}
|
|
|
|
return title;
|
2024-08-11 10:58:13 +08:00
|
|
|
};
|
2022-11-05 12:44:37 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Safe get column title
|
|
|
|
*
|
|
|
|
* Should filter [object Object]
|
|
|
|
*
|
|
|
|
* @param title
|
|
|
|
* @returns
|
|
|
|
*/
|
2024-08-11 10:58:13 +08:00
|
|
|
export const safeColumnTitle = <RecordType extends AnyObject = AnyObject>(
|
2022-11-05 12:44:37 +08:00
|
|
|
title: ColumnTitle<RecordType>,
|
|
|
|
props: ColumnTitleProps<RecordType>,
|
2024-08-11 10:58:13 +08:00
|
|
|
) => {
|
|
|
|
const res = renderColumnTitle<RecordType>(title, props);
|
|
|
|
if (Object.prototype.toString.call(res) === '[object Object]') {
|
|
|
|
return '';
|
|
|
|
}
|
2022-11-05 12:44:37 +08:00
|
|
|
return res;
|
2024-08-11 10:58:13 +08:00
|
|
|
};
|