Commit c5ac6107 authored by GuanHua's avatar GuanHua
parents 804c371b 1a3a0dea
import React, { useCallback } from 'react'
import { Table } from 'antd'
import { TableProps, ColumnsType } from 'antd/es/table'
import { CaretRightOutlined, CaretDownOutlined } from '@ant-design/icons'
export interface NestTableProps extends TableProps<any> {
/**
* 扁平化的递归嵌套类型, 后面一项永远为前一项的直系子集
*/
NestColumns: ColumnsType<any>[],
// 指定获得的子集数据类型
childrenDataKey: string
}
/**
* 嵌套表格
* @todo 实现无限嵌套, 目前暂时实现两层
*/
const NestTable:React.FC<NestTableProps> = (props) => {
const { NestColumns, childrenDataKey, dataSource, ...resetProps } = props
if (NestColumns.length > 2) {
throw new Error('暂时不支持2项以上的嵌套table')
}
const [parentColumns = [], childColumns = []] = NestColumns
const childRenderTable = useCallback((record) => {
return <Table
columns={childColumns}
dataSource={dataSource[childrenDataKey] || []}
/>
}, [childColumns, dataSource])
return (
<Table
columns={parentColumns}
dataSource={dataSource}
expandable={{
expandedRowRender: childRenderTable,
expandIcon: ({ expanded, onExpand, record }) =>
expanded ? (
<CaretRightOutlined onClick={e => onExpand(record, e)} />
) : (
<CaretDownOutlined onClick={e => onExpand(record, e)} />
)
}}
pagination={false}
{...resetProps}
/>
)
}
NestTable.defaultProps = {}
export default NestTable
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment