import React, { useState } from 'react'; import type { ProColumns } from '@ant-design/pro-table'; import { EditableProTable } from '@ant-design/pro-table'; import ProField from '@ant-design/pro-field'; import { ProFormRadio } from '@ant-design/pro-form'; import ProCard from '@ant-design/pro-card'; const waitTime = (time: number = 100) => { return new Promise((resolve) => { setTimeout(() => { resolve(true); }, time); }); }; type DataSourceType = { id: React.Key; title?: string; decs?: string; state?: string; created_at?: string; update_at?: string; children?: DataSourceType[]; }; const defaultData: DataSourceType[] = [ { id: 624748504, title: '活动名称一', decs: '这个活动真好玩', state: 'open', created_at: '2020-05-26T09:42:56Z', update_at: '2020-05-26T09:42:56Z', }, { id: 624691229, title: '活动名称二', decs: '这个活动真好玩', state: 'closed', created_at: '2020-05-26T08:19:22Z', update_at: '2020-05-26T08:19:22Z', }, ]; export default () => { const [editableKeys, setEditableRowKeys] = useState([]); const [dataSource, setDataSource] = useState([]); const [position, setPosition] = useState<'top' | 'bottom' | 'hidden'>('bottom'); const columns: ProColumns[] = [ { title: '活动名称', dataIndex: 'title', formItemProps: (form, { rowIndex }) => { return { rules: rowIndex > 2 ? [{ required: true, message: '此项为必填项' }] : [], }; }, // 第二行不允许编辑 editable: (text, record, index) => { return index !== 0; }, width: '30%', }, { title: '状态', key: 'state', dataIndex: 'state', valueType: 'select', valueEnum: { all: { text: '全部', status: 'Default' }, open: { text: '未解决', status: 'Error', }, closed: { text: '已解决', status: 'Success', }, }, }, { title: '描述', dataIndex: 'decs', fieldProps: (from, { rowKey, rowIndex }) => { if (from.getFieldValue([rowKey || '', 'title']) === '不好玩') { return { disabled: true, }; } if (rowIndex > 9) { return { disabled: true, }; } return {}; }, }, { title: '活动时间', dataIndex: 'created_at', valueType: 'date', }, { title: '操作', valueType: 'option', width: 200, render: (text, record, _, action) => [ { action?.startEditable?.(record.id); }} > 编辑 , { setDataSource(dataSource.filter((item) => item.id !== record.id)); }} > 删除 , ], }, ]; return ( <> rowKey="id" headerTitle="可编辑表格" maxLength={5} recordCreatorProps={ position !== 'hidden' ? { position: position as 'top', record: () => ({ id: (Math.random() * 1000000).toFixed(0) }), } : false } toolBarRender={() => [ setPosition(e.target.value), }} options={[ { label: '添加到顶部', value: 'top', }, { label: '添加到底部', value: 'bottom', }, { label: '隐藏', value: 'hidden', }, ]} />, ]} columns={columns} request={async () => ({ data: defaultData, total: 3, success: true, })} value={dataSource} onChange={setDataSource} editable={{ type: 'multiple', editableKeys, onSave: async (rowKey, data, row) => { console.log(rowKey, data, row); await waitTime(2000); }, onChange: setEditableRowKeys, }} /> ); };