mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-30 20:38:45 +08:00
863f61d908
Co-authored-by: afc163 <afc163@gmail.com>
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import type { AnyObject } from '../_util/type';
|
|
import type { ColumnTitle, ColumnTitleProps, ColumnType, Key } from './interface';
|
|
|
|
export const getColumnKey = <RecordType extends AnyObject = AnyObject>(
|
|
column: ColumnType<RecordType>,
|
|
defaultKey: string,
|
|
): Key => {
|
|
if ('key' in column && column.key !== undefined && column.key !== null) {
|
|
return column.key;
|
|
}
|
|
if (column.dataIndex) {
|
|
return Array.isArray(column.dataIndex) ? column.dataIndex.join('.') : (column.dataIndex as Key);
|
|
}
|
|
return defaultKey;
|
|
};
|
|
|
|
export function getColumnPos(index: number, pos?: string) {
|
|
return pos ? `${pos}-${index}` : `${index}`;
|
|
}
|
|
|
|
export const renderColumnTitle = <RecordType extends AnyObject = AnyObject>(
|
|
title: ColumnTitle<RecordType>,
|
|
props: ColumnTitleProps<RecordType>,
|
|
) => {
|
|
if (typeof title === 'function') {
|
|
return title(props);
|
|
}
|
|
return title;
|
|
};
|
|
|
|
/**
|
|
* Safe get column title
|
|
*
|
|
* Should filter [object Object]
|
|
*
|
|
* @param title
|
|
*/
|
|
export const safeColumnTitle = <RecordType extends AnyObject = AnyObject>(
|
|
title: ColumnTitle<RecordType>,
|
|
props: ColumnTitleProps<RecordType>,
|
|
) => {
|
|
const res = renderColumnTitle<RecordType>(title, props);
|
|
if (Object.prototype.toString.call(res) === '[object Object]') {
|
|
return '';
|
|
}
|
|
return res;
|
|
};
|