import { CloseCircleOutlined } from '@ant-design/icons'; import type { ProColumnType } from '@ant-design/pro-components'; import { EditableProTable, FooterToolbar, PageContainer, ProForm, ProFormDateRangePicker, ProFormSelect, ProFormText, ProFormTimePicker, } from '@ant-design/pro-components'; import { Card, Col, message, Popover, Row } from 'antd'; import type { FC } from 'react'; import { useState } from 'react'; import { fakeSubmitForm } from './service'; import useStyles from './style.style'; interface TableFormDateType { key: string; workId?: string; name?: string; department?: string; isNew?: boolean; editable?: boolean; } type InternalNamePath = (string | number)[]; const fieldLabels = { name: '仓库名', url: '仓库域名', owner: '仓库管理员', approver: '审批人', dateRange: '生效日期', type: '仓库类型', name2: '任务名', url2: '任务描述', owner2: '执行人', approver2: '责任人', dateRange2: '生效日期', type2: '任务类型', }; const tableData = [ { key: '1', workId: '00001', name: 'John Brown', department: 'New York No. 1 Lake Park', }, { key: '2', workId: '00002', name: 'Jim Green', department: 'London No. 1 Lake Park', }, { key: '3', workId: '00003', name: 'Joe Black', department: 'Sidney No. 1 Lake Park', }, ]; interface ErrorField { name: InternalNamePath; errors: string[]; } const AdvancedForm: FC> = () => { const { styles } = useStyles(); const [error, setError] = useState([]); const getErrorInfo = (errors: ErrorField[]) => { const errorCount = errors.filter((item) => item.errors.length > 0).length; if (!errors || errorCount === 0) { return null; } const scrollToField = (fieldKey: string) => { const labelNode = document.querySelector(`label[for="${fieldKey}"]`); if (labelNode) { labelNode.scrollIntoView(true); } }; const errorList = errors.map((err) => { if (!err || err.errors.length === 0) { return null; } const key = err.name[0] as | 'name' | 'url' | 'owner' | 'approver' | 'dateRange' | 'type'; return (
  • scrollToField(key)} >
    {err.errors[0]}
    {fieldLabels[key]}
  • ); }); return ( { if (trigger?.parentNode) { return trigger.parentNode as HTMLElement; } return trigger; }} > {errorCount} ); }; const onFinish = async (values: Record) => { setError([]); try { await fakeSubmitForm(values); message.success('提交成功'); } catch { // console.log } }; const onFinishFailed = (errorInfo: any) => { setError(errorInfo.errorFields); }; const columns: ProColumnType[] = [ { title: '成员姓名', dataIndex: 'name', key: 'name', width: '20%', }, { title: '工号', dataIndex: 'workId', key: 'workId', width: '20%', }, { title: '所属部门', dataIndex: 'department', key: 'department', width: '40%', }, { title: '操作', key: 'action', valueType: 'option', render: (_, record: TableFormDateType, _index, action) => { return [ { action?.startEditable(record.key); }} > 编辑 , ]; }, }, ]; return ( { return ( {getErrorInfo(error)} {dom} ); }, }} initialValues={{ members: tableData, }} onFinish={onFinish} onFinishFailed={onFinishFailed} > recordCreatorProps={{ record: () => { return { key: `0${Date.now()}`, }; }, }} columns={columns} rowKey="key" /> ); }; export default AdvancedForm;