|
|
|
@@ -10,32 +10,36 @@ import dayjs from 'dayjs'; |
|
|
|
|
|
|
|
export enum TableCellValueType { |
|
|
|
Index = 'Index', |
|
|
|
PageIndex = 'PageIndex', |
|
|
|
Text = 'Text', |
|
|
|
Date = 'Date', |
|
|
|
Array = 'Array', |
|
|
|
Link = 'Link', |
|
|
|
Custom = 'Custom', |
|
|
|
} |
|
|
|
|
|
|
|
export type TableCellValueOptions<T> = { |
|
|
|
page?: number; |
|
|
|
pageSize?: number; |
|
|
|
property?: string; |
|
|
|
onClick?: (record: T, e: React.MouseEvent) => void; |
|
|
|
page?: number; // 类型为 Index 时有效 |
|
|
|
pageSize?: number; // 类型为 Index 时有效 |
|
|
|
property?: string; // 类型为 Array 时有效 |
|
|
|
dateFormat?: string; // 类型为 Date 时有效 |
|
|
|
onClick?: (record: T, e: React.MouseEvent) => void; // 类型为 Link 时有效 |
|
|
|
format?: (value: any | undefined | null, record: T, index: number) => string | undefined | null; // 类型为 Custom 时有效 |
|
|
|
}; |
|
|
|
|
|
|
|
type TableCellFormatter = (value: any | undefined | null) => string | undefined | null; |
|
|
|
|
|
|
|
// 日期转换函数 |
|
|
|
const formatDateText: TableCellFormatter = (value?: any | null) => { |
|
|
|
if (value === undefined || value === null || value === '') { |
|
|
|
return null; |
|
|
|
} |
|
|
|
if (!dayjs(value).isValid()) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
return formatDate(value); |
|
|
|
}; |
|
|
|
function formatDateText(dateFormat?: string): TableCellFormatter { |
|
|
|
return (value: any | undefined | null): ReturnType<TableCellFormatter> => { |
|
|
|
if (value === undefined || value === null || value === '') { |
|
|
|
return null; |
|
|
|
} |
|
|
|
if (!dayjs(value).isValid()) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
return formatDate(value, dateFormat); |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 数组转换函数,将数组元素转换为字符串,用逗号分隔 |
|
|
|
@@ -43,7 +47,7 @@ const formatDateText: TableCellFormatter = (value?: any | null) => { |
|
|
|
* @returns {TableCellFormatter} Table cell 渲染函数 |
|
|
|
*/ |
|
|
|
function formatArray(property?: string): TableCellFormatter { |
|
|
|
return (value?: any | null): ReturnType<TableCellFormatter> => { |
|
|
|
return (value: any | undefined | null): ReturnType<TableCellFormatter> => { |
|
|
|
if ( |
|
|
|
value === undefined || |
|
|
|
value === null || |
|
|
|
@@ -68,21 +72,21 @@ function tableCellRender<T>( |
|
|
|
let text = value; |
|
|
|
switch (type) { |
|
|
|
case TableCellValueType.Index: |
|
|
|
text = index + 1; |
|
|
|
break; |
|
|
|
case TableCellValueType.PageIndex: |
|
|
|
text = (options?.page ?? 1) * (options?.pageSize ?? 10) + index + 1; |
|
|
|
text = (options?.page ?? 0) * (options?.pageSize ?? 0) + index + 1; |
|
|
|
break; |
|
|
|
case TableCellValueType.Text: |
|
|
|
case TableCellValueType.Link: |
|
|
|
text = value; |
|
|
|
break; |
|
|
|
case TableCellValueType.Date: |
|
|
|
text = formatDateText(value); |
|
|
|
text = formatDateText(options?.dateFormat)(value); |
|
|
|
break; |
|
|
|
case TableCellValueType.Array: |
|
|
|
text = formatArray(options?.property)(value); |
|
|
|
break; |
|
|
|
case TableCellValueType.Custom: |
|
|
|
text = options?.format?.(value, record, index); |
|
|
|
break; |
|
|
|
default: |
|
|
|
break; |
|
|
|
} |
|
|
|
|