-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
225 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
packages/search-table-react/src/hooks/useSummary/helpers.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import type { IObjectAny } from '@schema-render/core-react' | ||
import { utils } from '@schema-render/core-react' | ||
import { Table } from 'antd' | ||
import type { Key, ReactNode } from 'react' | ||
|
||
import { EColumnsKeys } from '../../constants' | ||
import type { IColumnType } from '../../typings/table.d' | ||
import { forEach } from '../../utils/common' | ||
|
||
const { isArray, isNil } = utils | ||
|
||
export interface ICreateTableSummaryParams { | ||
/** 平铺的列数据 */ | ||
flattenedColumns: IColumnType[] | ||
/** 「合计」数据 */ | ||
summaryData: IObjectAny | ||
/** 「合计」文案 */ | ||
summaryText?: ReactNode | ||
/** | ||
* 表格是否存在 checkbox 选择框,存在会给「合计」索引自动 +1 | ||
*/ | ||
hasRowSelection?: boolean | ||
/** | ||
* 是否存在序号栏 | ||
*/ | ||
hasRowNumber?: boolean | ||
} | ||
|
||
/** | ||
* 获取默认文案 | ||
*/ | ||
function getSummaryDefaultText(dataKey: Key, summaryText: ReactNode) { | ||
// 序号栏显示“合计”文案 | ||
if (dataKey === EColumnsKeys.rowNumber) { | ||
return summaryText | ||
} | ||
|
||
// 操作栏默认为空 | ||
if (dataKey === EColumnsKeys.actions) { | ||
return '' | ||
} | ||
|
||
// 其他列数据不存在显示中横线 | ||
return '-' | ||
} | ||
|
||
/** | ||
* 获取平铺的 columns | ||
*/ | ||
export function getFlattenedColumns(columns?: IColumnType[]) { | ||
const flattened: IColumnType[] = [] | ||
|
||
function traverse(list?: IColumnType[]) { | ||
forEach(list, (item) => { | ||
if (isArray(item.children)) { | ||
traverse(item.children) | ||
} else { | ||
flattened.push(item) | ||
} | ||
}) | ||
} | ||
|
||
traverse(columns) | ||
|
||
return flattened | ||
} | ||
|
||
/** | ||
* 创建 Antd Table 总结栏列表数据 | ||
*/ | ||
function createTableSummaryItems({ | ||
flattenedColumns, | ||
summaryData, | ||
summaryText = '合计', | ||
hasRowNumber = false, | ||
hasRowSelection = false, | ||
}: ICreateTableSummaryParams) { | ||
const resultList: { index: number; text: ReactNode; colSpan?: number }[] = [] | ||
|
||
let index = -1 | ||
|
||
if (hasRowSelection) { | ||
index++ | ||
resultList.push({ | ||
index, | ||
text: hasRowNumber ? '' : summaryText, | ||
}) | ||
} | ||
|
||
forEach(flattenedColumns, ({ key, dataIndex }) => { | ||
const strDataIndex = (isArray(dataIndex) ? dataIndex.join('.') : dataIndex) as string | ||
const dataKey = key || strDataIndex | ||
const content = summaryData[dataKey] | ||
|
||
index++ | ||
|
||
resultList.push({ | ||
index, | ||
text: isNil(content) ? getSummaryDefaultText(dataKey, summaryText) : content, | ||
}) | ||
}) | ||
|
||
return resultList | ||
} | ||
|
||
/** | ||
* 创建 antd table 总结栏 | ||
*/ | ||
export function createTableSummary(p: ICreateTableSummaryParams) { | ||
const items = createTableSummaryItems(p) | ||
return ( | ||
<Table.Summary fixed="bottom"> | ||
<Table.Summary.Row> | ||
{items.map((item) => { | ||
return ( | ||
<Table.Summary.Cell | ||
align="center" | ||
key={item.index} | ||
index={item.index} | ||
colSpan={item.colSpan} | ||
> | ||
{item.text} | ||
</Table.Summary.Cell> | ||
) | ||
})} | ||
</Table.Summary.Row> | ||
</Table.Summary> | ||
) | ||
} |
48 changes: 48 additions & 0 deletions
48
packages/search-table-react/src/hooks/useSummary/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { IObjectAny } from '@schema-render/core-react' | ||
import { useMemoizedFn } from '@schema-render/core-react' | ||
import { useMemo } from 'react' | ||
|
||
import type { ISearchTableProps } from '../../typings/index.d' | ||
import type { IColumnType } from '../../typings/table' | ||
import { isEmpty } from '../../utils/common' | ||
import { createTableSummary, getFlattenedColumns } from './helpers' | ||
|
||
interface IUseSummaryParams { | ||
table: ISearchTableProps['table'] | ||
finalColumns: IColumnType[] | ||
summaryData: IObjectAny | ||
} | ||
|
||
/** | ||
* 总结栏处理:支持合计数据分布 | ||
*/ | ||
export default function useSummary({ | ||
table, | ||
finalColumns, | ||
summaryData, | ||
}: IUseSummaryParams) { | ||
const flattenedColumns = useMemo( | ||
() => getFlattenedColumns(finalColumns), | ||
[finalColumns] | ||
) | ||
|
||
const renderSummary = useMemoizedFn(() => { | ||
return createTableSummary({ | ||
flattenedColumns, | ||
summaryData, | ||
summaryText: table.summaryText, | ||
hasRowSelection: !!table.rowSelection, | ||
hasRowNumber: table.showRowNumber, | ||
}) | ||
}) | ||
|
||
const finalSummary = table.summary | ||
? table.summary | ||
: isEmpty(summaryData) | ||
? undefined | ||
: renderSummary | ||
|
||
return { | ||
finalSummary, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters