import {AutoColumn, AutoText, isNullOrTrue, Message} from "auto-antd"; import {Button, Drawer, Modal, Switch, Table, TableProps} from "antd"; import React, {useEffect, useMemo, useRef, useState} from "react"; import {ColumnsType} from "antd/lib/table"; import {CurdForm, ICurdForm, IProps as IFormProps} from "./Form.tsx"; import {LDAP} from "../../api/ldap.ts"; import {DeleteOutlined, EditOutlined, LoadingOutlined, PlusOutlined, SyncOutlined} from "@ant-design/icons"; import './index.less' import {ICurdConfig, ICurdData} from "./types.ts"; import {Cell} from "./cell"; export type CurdColumn = AutoColumn & { search?: boolean editable?: boolean // 编辑时是否可编辑 hidden?: boolean addable?: boolean // 添加时可编辑 onlyAdd?: boolean // 仅新增时展示 editHide?: boolean // 编辑时隐藏 ellipsis?: boolean // 完整显示 } type PageResp = { total: number, list: T[], current: number, pageSize: number } type IProps = IFormProps & { columns: CurdColumn[], getList: (query: any) => Promise | undefined>, getDetail: (data: Partial, add?: boolean) => Promise onDelete?: (data: T) => Promise config?: ICurdConfig onSuccess?: (data: Partial) => void operationRender?: React.ReactNode operation?: { add?: boolean delete?: boolean }, expandable?: TableProps['expandable'] } /** * 一个完成的增删查改界面 * @constructor */ export function CurdPage( { columns, getList, config, operationRender, onSuccess, operation, expandable, ...props }: IProps) { const [list, setList] = useState([]); const [total, setTotal] = useState(0) const [query, setQuery] = useState({current: 1, pageSize: 10}); const [loading, setLoading] = useState(false); // edit or add const [visible, setVisible] = useState(false); const [editData, setEditData] = useState>() const formRef = useRef>() const onAddOrEdit = (edit: Partial, add?: boolean) => { setVisible(true) if (props.getDetail) { setLoading(true) props.getDetail?.(edit,add) .then(res => { setEditData(res) }) .catch(err => { Message.warning(err.message) setVisible(false) }) .finally(() => { setLoading(false) }) } else { setEditData({...edit}) setVisible(true) } } const handleDelete = async (edit: T) => { Modal.confirm({ type: 'warning', title: '您确定要删除该数据吗?', content: '删除操作不可撤销,请谨慎操作', onOk: async () => { setLoading(true) try { await props.onDelete?.(edit) setQuery((q: any)=>({...q})) }catch(err: any) { if(err instanceof Error){ Message.warning(err.message) }else if (typeof err === "string") { Message.warning(err) }else if (err?.msg) { Message.warning('删除失败,错误信息为:'+ err.msg) }else { Message.warning('删除失败,错误信息为:'+ JSON.stringify(err,null,2)) } }finally { setLoading(false) } } }) } const tableColumns = useMemo(() => { const cols = columns.filter(item => !item.hidden) .map((column: CurdColumn) => { const col = { ...column, dataIndex: column.key, title: column.title, width: column.width, onCell: (record: Partial) => { return { record, config: column, dataIndex: column.key, } } } if (col.type == 'switch' && !col.render){ col.render = (value: boolean) => } // @ts-ignore if (col.type == 'select' && !col.render && column.mode!='tags'){ col.render = (value: any) => } return col; }) as ColumnsType cols.push({ dataIndex: "", title: "操作", width: config?.operationWidth ?? 180, render: (_, record, index) => { return (
{ isNullOrTrue(config?.editable) ? (
) } }) return cols; }, [columns]); const onClose = () => { setVisible(false) setEditData(undefined) } useEffect(() => { setLoading(true) getList(query).then(res => { if (res) { setList(res.list) setTotal(res.total) } else { Message.warning('无数据!') } }).finally(() => { setLoading(false) }) }, [getList, query]); const onPageChange = (current: number, pageSize: number) => { setQuery((q: any) => ({...q, current, pageSize})) } const onSaveSuccess = (data: Partial) => { setQuery({...query}) onSuccess?.(data) setVisible(false) } return
{ config?.hideAdd ? null : (
{ loading ? () : ( ) }
}