ant-design/components/table/util.ts
kiner-tang(文辉) 2dbfbea502
fix(table): fix Table aria-label contains [object Object] (#38389)
* fix: columns title render not as expected

* feat: code optimize

* feat: update test case

* feat: update test case

* feat: code optimize

* Update components/table/__tests__/Table.test.tsx

Co-authored-by: afc163 <afc163@gmail.com>

* feat: update aria-label

* feat: update test case

* feat: update snapshots

* feat: update snapshots

Co-authored-by: tangwenhui <tangwenhui@rd.netease.com>
Co-authored-by: afc163 <afc163@gmail.com>
2022-11-05 12:44:37 +08:00

46 lines
1.1 KiB
TypeScript

/* eslint-disable import/prefer-default-export */
import type { ColumnTitle, ColumnTitleProps, ColumnType, Key } from './interface';
export function getColumnKey<RecordType>(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 function renderColumnTitle<RecordType>(
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
* @returns
*/
export function safeColumnTitle<RecordType>(
title: ColumnTitle<RecordType>,
props: ColumnTitleProps<RecordType>,
) {
const res = renderColumnTitle(title, props);
if (Object.prototype.toString.call(res) === '[object Object]') return '';
return res;
}