import React, { useMemo, useContext, useRef, useImperativeHandle } from 'react'; import type { ListProps, PaginationProps } from 'antd'; import classNames from 'classnames'; import type { ProTableProps, ProColumnType, ActionType } from '@ant-design/pro-table'; import ProTable from '@ant-design/pro-table'; import type { ParamsType } from '@ant-design/pro-provider'; import { ConfigProvider, Form } from 'antd'; import type { LabelTooltipType } from 'antd/lib/form/FormItemLabel'; import ListView from './ListView'; import './index.less'; import type { ItemProps } from './Item'; type AntdListProps = Omit, 'rowKey'>; type ProListMeta = Pick< ProColumnType, 'dataIndex' | 'valueType' | 'render' | 'search' | 'title' | 'valueEnum' | 'editable' >; export type ProListMetas = { type?: ProListMeta; title?: ProListMeta; subTitle?: ProListMeta; description?: ProListMeta; avatar?: ProListMeta; extra?: ProListMeta; content?: ProListMeta; actions?: ProListMeta; [key: string]: ProListMeta | undefined; }; export type GetComponentProps = ( record: RecordType, index: number, ) => React.HTMLAttributes; export type ProListProps = Omit< ProTableProps, 'size' | 'footer' > & AntdListProps & { tooltip?: LabelTooltipType | string; metas?: ProListMetas; showActions?: 'hover' | 'always'; showExtra?: 'hover' | 'always'; onRow?: GetComponentProps; itemHeaderRender?: ItemProps['itemHeaderRender']; itemTitleRender?: ItemProps['itemTitleRender']; }; export type Key = React.Key; export type TriggerEventHandler = (record: RecordType) => void; function ProList< RecordType extends Record, U extends Record = Record, >(props: ProListProps) { const { metas: metals, split, footer, rowKey, tooltip, className, options = false, search = false, expandable, showActions, showExtra, rowSelection: propRowSelection = false, pagination: propsPagination = false, itemLayout, renderItem, grid, onRow, locale, itemHeaderRender, itemTitleRender, ...rest } = props; const actionRef = useRef(); useImperativeHandle(rest.actionRef, () => actionRef.current, [actionRef.current]); const { getPrefixCls } = useContext(ConfigProvider.ConfigContext); const proTableColumns: ProColumnType[] = useMemo(() => { const columns: ProColumnType[] = []; Object.keys(metals || {}).forEach((key) => { const meta = metals![key] || {}; let { valueType } = meta; if (!valueType) { // 给默认的 valueType if (key === 'avatar') { valueType = 'avatar'; } if (key === 'actions') { valueType = 'option'; } if (key === 'description') { valueType = 'textarea'; } } columns.push({ listKey: key, dataIndex: meta?.dataIndex || key, ...meta, valueType, }); }); return columns; }, [metals]); const prefixCls = getPrefixCls('pro-list'); const listClassName = classNames(prefixCls, { [`${prefixCls}-no-split`]: !split, }); return ( tooltip={tooltip} {...(rest as any)} actionRef={actionRef} pagination={propsPagination} rowSelection={propRowSelection} search={search} options={options} className={classNames(prefixCls, className, listClassName)} columns={proTableColumns} rowKey={rowKey} cardProps={{ style: { background: 'transparent', }, bodyStyle: { padding: 0, background: 'transparent', }, }} toolbar={{ style: { padding: '0 24px', }, }} tableViewRender={({ columns, size, pagination, rowSelection, dataSource, loading }) => { return (
); }} /> ); } export default ProList;