import React, { useMemo } from 'react'; import { Loading } from '@alifd/next'; import { Form, Button } from '@alifd/meet-react'; import { ICardItemProps, ICardItem, ISimpleCardTableMobileProps, ESimpleCardTableColumnType, getSimpleCardTypeArray, } from './type'; import Action from '../Action'; import { fetchDataByPath } from '../../utils'; import cn from 'classnames'; import './index.scss'; // 操作列标识 data: '_action' // const ActionKey = '_action'; const typeArray = getSimpleCardTypeArray(); // 渲染kv function renderContent(content, editable) { if (!content || content.length === 0) return null; const renderEdit = (
{content.map((item) => { const { label, value, isLabel } = item; return {value}; })}
); const renderView = content.map((item) => { const { label, value } = item; return (
{value}
); }); return
{editable ? renderEdit : renderView}
; } /** * 编辑态操作区 * title: '新增', type: 'primary', icon: 'add', onClick: () => console.log('新增操作'), */ function EditAction(props) { const { actionColumns, index: dsIndex, record, className } = props; const filterHiddenColumns = (actions) => { if (!actions) return []; return actions.filter((item) => { const { hidden = () => {} } = item; return !hidden({ value: undefined, index: dsIndex, record }); }); }; const columns = filterHiddenColumns(actionColumns).reverse(); return (
{columns.map((col) => { const { title, ...more } = col; return ( ); })}
); } // 渲染单个卡片 function CardItem(props: ICardItemProps) { const { data, record, index, actionColumn, onRowClick, editable } = props; const content = data[ESimpleCardTableColumnType.Content]; const renderActionColumn = !!(actionColumn && actionColumn.length); const noHeader = !data[ESimpleCardTableColumnType.Title] && !data[ESimpleCardTableColumnType.SubTitle] && !data[ESimpleCardTableColumnType.Type] && !data[ESimpleCardTableColumnType.Tag]; return (
{!noHeader && (

{data[ESimpleCardTableColumnType.Title]}

{data[ESimpleCardTableColumnType.SubTitle]}
{data[ESimpleCardTableColumnType.Type]} {data[ESimpleCardTableColumnType.Tag]}
)} {data[ESimpleCardTableColumnType.Desc] && (
{data[ESimpleCardTableColumnType.Desc]}
)} {renderContent(content, editable)} {renderActionColumn && (editable ? ( ) : ( ))}
); } // 获取渲染item的value, 可能是value,可能是cell自定义 const getDsValue = (item, dataIndex, index, cell) => { const realValue = fetchDataByPath(item, dataIndex); return cell && typeof cell === 'function' ? cell(realValue, index, item) : realValue; }; // 转换成卡片识别的格式 const transferData = (columns, ds): ICardItem[] => { if (!columns || !ds) return []; const typeMap = columns.reduce((total, item) => { const { title, type, dataIndex, cell, isLabel = true } = item; if (type && typeArray.includes(type) && dataIndex) { if (!total[ESimpleCardTableColumnType.Content]) { total[ESimpleCardTableColumnType.Content] = []; } const dataItem = { title, dataIndex, cell, isLabel, }; if (type === ESimpleCardTableColumnType.Content) { total[ESimpleCardTableColumnType.Content].push(dataItem); } else { total[type] = dataItem; } } return total; }, {}); return ds.map((item, idx) => { const resultItem = {}; for (const key in typeMap) { if (key === ESimpleCardTableColumnType.Content) { if (!resultItem[key]) { resultItem[key] = []; } typeMap[key].forEach((contentItemMap) => { const { dataIndex, cell, title, isLabel } = contentItemMap; resultItem[key].push({ label: title, value: getDsValue(item, dataIndex, idx, cell), isLabel: isLabel, }); }); } else { const { dataIndex, cell } = typeMap[key]; resultItem[key] = getDsValue(item, dataIndex, idx, cell); } } return { item, index: idx, map: resultItem, }; }); }; // 填充columns的type属性 const fillColumnType = (columns) => { if (!columns) return []; return columns.map((item) => ({ ...item, type: item.type || ESimpleCardTableColumnType.Content, })); }; // 拆分出actionCol 和 正常col // const splitActionAndColumns = (columns) => { // const result = { // action: [], // realColumns: [], // }; // if (!columns) return result; // result.realColumns = columns.filter((item) => { // if (item.data === ActionKey) { // result.action.push(item); // return false; // } // return true; // }); // return result; // }; function SimpleCardTableMobile(props: ISimpleCardTableMobileProps) { const { columns, dataSource, actionColumn, className, onRowClick = () => {}, loading = false, editable = false, } = props; console.log('props: ', props); // const { action, realColumns } = splitActionAndColumns(columns); const cardDatas = useMemo(() => { return transferData(fillColumnType(columns), dataSource); }, [columns, dataSource]); return (
{cardDatas.map((card) => { const { map, item, index } = card; return ( onRowClick(item, index, e)} editable={editable} /> ); })}
); } export default SimpleCardTableMobile;