import React, { useMemo, useContext, useRef, useImperativeHandle } from 'react'; import type { ListProps, PaginationProps } from 'infrad'; import classNames from 'classnames'; import type { ProTableProps, ProColumnType, ActionType } from 'infrad-pro-table'; import ProTable from 'infrad-pro-table'; import { ConfigProvider } from 'infrad'; import type { LabelTooltipType } from 'infrad/lib/form/FormItemLabel'; import ListView from './ListView'; import './index.less'; import type { ItemProps } from './Item'; import type { ProCardProps } from 'infrad-pro-card'; export type AntdListProps = Omit, 'rowKey'>; export type ProListMeta = Pick< ProColumnType, | 'dataIndex' | 'valueType' | 'render' | 'search' | 'title' | 'valueEnum' | 'editable' | 'fieldProps' | 'formItemProps' >; export type ProListMetas = { [key: string]: any; type?: ProListMeta; title?: ProListMeta; subTitle?: ProListMeta; description?: ProListMeta; avatar?: ProListMeta; content?: ProListMeta; actions?: ProListMeta & { /** * @example * `cardActionProps = 'actions';`; * * @name 选择映射到 card 上的 props,默认为extra */ cardActionProps?: 'extra' | 'actions'; }; }; export type GetComponentProps = ( record: RecordType, index: number, ) => React.HTMLAttributes; export type ProListProps, ValueType = 'text'> = Omit< ProTableProps, 'size' | 'footer' > & AntdListProps & { tooltip?: LabelTooltipType | string; metas?: ProListMetas; showActions?: 'hover' | 'always'; showExtra?: 'hover' | 'always'; onRow?: GetComponentProps; onItem?: GetComponentProps; itemCardProps?: ProCardProps; rowClassName?: string | ((item: RecordType, index: number) => string); 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, itemCardProps, onRow, onItem, rowClassName, locale, itemHeaderRender, itemTitleRender, ...rest } = props; const actionRef = useRef(); useImperativeHandle(rest.actionRef, () => 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) { // 根据 key 给不同的 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', props.prefixCls); const listClassName = classNames(prefixCls, { [`${prefixCls}-no-split`]: !split, }); return ( tooltip={tooltip} {...(rest as any)} actionRef={actionRef} pagination={propsPagination} type="list" rowSelection={propRowSelection} search={search} options={options} className={classNames(prefixCls, className, listClassName)} columns={proTableColumns} rowKey={rowKey} tableViewRender={({ columns, size, pagination, rowSelection, dataSource, loading }) => { return ( ); }} /> ); } function BaseProList< RecordType extends Record, U extends Record = Record, >(props: ProListProps) { return ; } export { BaseProList }; export default ProList;