2019-11-15 14:35:25 +08:00
|
|
|
/* eslint-disable import/prefer-default-export */
|
2022-10-23 15:28:26 +08:00
|
|
|
import React from 'react';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { ColumnTitle, ColumnTitleProps, ColumnType, Key } from './interface';
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
export function getColumnKey<RecordType>(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) {
|
2021-01-28 01:21:58 +08:00
|
|
|
return (Array.isArray(column.dataIndex) ? column.dataIndex.join('.') : column.dataIndex) as Key;
|
2019-11-15 14:35:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return defaultKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getColumnPos(index: number, pos?: string) {
|
|
|
|
return pos ? `${pos}-${index}` : `${index}`;
|
|
|
|
}
|
|
|
|
|
2022-10-23 15:28:26 +08:00
|
|
|
/**
|
|
|
|
* Get first text content in Element
|
|
|
|
*
|
|
|
|
* @param node
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
function getElementFirstTextContent(node: React.ReactElement): string {
|
|
|
|
if (!node || !node.props || !node.props.children) return '';
|
|
|
|
if (typeof node.props.children === 'string') return node.props.children;
|
|
|
|
return (
|
|
|
|
node.props.children?.map?.((item: React.ReactElement) =>
|
|
|
|
getElementFirstTextContent(item),
|
|
|
|
)?.[0] || ''
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-15 14:35:25 +08:00
|
|
|
export function renderColumnTitle<RecordType>(
|
|
|
|
title: ColumnTitle<RecordType>,
|
|
|
|
props: ColumnTitleProps<RecordType>,
|
|
|
|
) {
|
|
|
|
if (typeof title === 'function') {
|
|
|
|
return title(props);
|
|
|
|
}
|
2022-10-23 15:28:26 +08:00
|
|
|
// fix: #38155
|
|
|
|
if (React.isValidElement(title)) {
|
|
|
|
// if title is a React Element, we should get first text content as result,
|
|
|
|
// if there has not text content in React Element, return origin title
|
|
|
|
return getElementFirstTextContent(title) || title;
|
|
|
|
}
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
return title;
|
|
|
|
}
|