import React, { useEffect, useState } from 'react'; import { TableProps as AntTableProps, TableColumnProps as AntTableColumnProps, Table, Dropdown, Menu, Tooltip, Avatar, Checkbox } from 'antd' import { DownOutlined, SettingOutlined } from '@ant-design/icons'; import { map, uniqueId } from 'lodash'; import Icons from '../Icon'; import { getPrefixCls, moneyHandle, dateDiff } from '../../utils'; import './index.less' export interface NewTableProps extends AntTableProps { custom?: boolean; columns?: Array; // 列设置 } export interface NewTableColumnProps extends AntTableColumnProps { styleType?: | 'name' | 'text' | 'money' | 'time' | 'avatar' | 'action' | 'group' | undefined; // 类型 defaultSymbol?: string; width?: number; defaultAvatar?: string; actionList?: Array | ((text?: any) => Array); groupMapping?: Array; defaultValue?: any; ediType?: 'input' | 'radio' | 'checkbox' | 'select' | 'searchSelect' | 'uploadImg' | 'uploadFile' | 'textArea' | 'custom' | 'switch' | 'inputNum' | 'datePicker' | 'rangePicker'; direction?: 'horizontal' | 'vertical'; checkId?: number; defaultVisible?: boolean; } interface MappingProps { label?: string | undefined; value?: string | number | undefined; icon?: any; color?: string | undefined; } interface actionProps { id?: number; label?: any; onClick?: (text: any) => void; children?: Array; } const Index = (props: NewTableProps) => { const [col, setCol] = useState>([]); const [scrollWidth, setScrollWidth] = useState(); const [showCustom, setShowCustom] = useState(false); // 是否展示自定义列 const [checkedcustomColumns, setCheckedcustomColumns] = useState>([]); // 自定义列数组 const [checkedcustomColumnsOrigin, setCheckedcustomColumnsOrigin] = useState>([]); // 自定义列原始数组 const [checkIndeterminate, setCheckIndeterminate] = useState(false); // 半选状态 const { custom = false, columns = [] } = props const menu = (list: Array, text: any) => ( {list.map((item: any, index: number) => { const key: string = item.id ? `${item.id}${index}` : `${index}`; return typeof item.label === 'function' ? ( {item.label(text)} ) : ( { if (item.onClick) { item.onClick(text); } else { console.warn('请传入点击事件'); } }} > <>{item.label} ); })} ); useEffect(() => { if (custom && checkedcustomColumns?.length !== 0 && checkedcustomColumns?.length < checkedcustomColumnsOrigin?.length) { setCheckIndeterminate(true); } else { setCheckIndeterminate(false); } }, [checkedcustomColumns]); const handleVisibleChange = (flag: boolean) => { setShowCustom(flag); }; const renderCustomColumns = () => { return (
) } const handleCheckedAll = (e: any) => { if (e.target.checked) { setCheckedcustomColumns(checkedcustomColumnsOrigin); setCol(columns) } else { setCheckedcustomColumns([]) setCol([]) } setCheckIndeterminate(false); } const handleChecked = (e: any, item: any) => { if (e.target.checked) { const arr = [...checkedcustomColumns] arr.push(item?.checkId) setCheckedcustomColumns(arr) const columnArr = [...columns].filter(item => arr.includes(item.checkId)) setCol(columnArr) } else { setCheckedcustomColumns([...checkedcustomColumns].filter(i => i !== item?.checkId)) setCol([...col].filter(i => i?.checkId !== item?.checkId)) } } const customColumns = (
请选择列表中要展示的信息
全部
{ columns?.map((item: any) => { return (
{ handleChecked(e, item) }} /> {typeof item?.title === 'function' ? item['title']() : item?.title}
) }) }
) useEffect(() => { const checkedArr = [] const checkedColumns: Array = [] columns?.forEach((item: NewTableColumnProps, index: number) => { const newItem = item; if (custom) { newItem.checkId = index; newItem.defaultVisible = newItem?.defaultVisible == false ? false : true } let actionList: Array; switch (item.styleType) { case 'name': newItem.align = newItem.align || 'left'; newItem.width = newItem.width || 132; newItem.render = newItem.render || ((text: any) =>
{text}
); break; case 'text': // 文本 newItem.align = newItem.align || 'left'; newItem.ellipsis = true; newItem.className = 'antd-min-width'; if (!newItem.render) newItem.render = (text: any) => { return (
{text}
); }; break; case 'money': // 金额 newItem.align = newItem.align || 'right'; newItem.render = newItem.render || ((text: any) => { return (
{moneyHandle(text || 0, '¥')}
) }); break; case 'time': // 时间 newItem.width = newItem.width || 132; if (!newItem.render) newItem.render = newItem.render || ((text: any) => { if (!text) { newItem.align = 'right'; } return ( <>{ text ?

{dateDiff(text)[0]}

{dateDiff(text)[1]}

: {newItem?.defaultSymbol || '-'} } ); }); break; case 'avatar': // 头像 newItem.align = newItem.align || 'center'; newItem.width = newItem.width || 132; newItem.render = newItem.render || ((text: any) => ( )); break; case 'action': // 操作 newItem.direction = newItem.direction || 'horizontal' newItem.align = newItem.align || 'left'; newItem.render = newItem.render || ((text: any) => { let nActionList: Array = []; if (newItem.actionList !== undefined) { if (Array.isArray(newItem.actionList)) { nActionList = newItem.actionList; } else { nActionList = newItem.actionList(text); } } actionList = [...new Set(nActionList)]; if (nActionList && nActionList.length > 3) { actionList = [...new Set(nActionList)].slice(0, 2); actionList.push({ id: 0, label: '更多', children: nActionList.slice(2, nActionList.length), }); } return (
{actionList.map((actionItem: actionProps) => { return ( // eslint-disable-next-line no-nested-ternary actionItem.children && actionItem.children.length ? ( {actionItem.label} ) : typeof actionItem.label === 'function' ? ( {actionItem.label(text)} ) : ( { if (actionItem.onClick) { actionItem.onClick(text); } else { console.warn('请输入点击事件'); } }} > {actionItem.label} ) ); })}
); }); break; case 'group': // 组别 newItem.align = newItem.align || 'left'; if (!newItem.render) newItem.render = (text: any) => { let el = text; if (newItem.groupMapping) { newItem.groupMapping.map((mitem: MappingProps) => { if (mitem.value === text) { el = (
{mitem.color ? ( ) : ( mitem.icon )} {mitem.label}
); } return el; }); } return el; }; break; default: if (!newItem.render) newItem.render = (text: any) => text; } if (newItem?.defaultVisible) { checkedArr.push(index) checkedColumns.push(newItem) } return true; }); if (custom) { setCol(checkedColumns); setCheckedcustomColumns(map(checkedColumns, 'checkId')) setCheckedcustomColumnsOrigin(map(columns, 'checkId')) } else { setCol(columns || []) } }, [columns]); useEffect(() => { let width = 0; for (let i = 0; i < col.length; i += 1) { if (col[i]?.styleType === 'text') { width += 272; } width += col[i].width || 0; } setScrollWidth(width); }, [col]); // @ts-ignore const expandIcon = ({ record, expanded, onExpand }) => { if (record?.children?.length > 0) { if (expanded) { return ( { onExpand(record, e) }}> ) } return ( { onExpand(record, e) }}> ) } return } if (props.expandable) { // eslint-disable-next-line no-param-reassign props = { ...props, expandable: { ...props.expandable, expandIcon } } } return (

暂无数据

}} {...props} columns={custom ? [...col, { title: () =>
{renderCustomColumns()}
, dataIndex: 'set', key: 'set', width: 51, fixed: 'right' }] : [...col]} /> ); }; Index.defaultProps = { columns: [], }; export default Index;