import React, { useRef, useState } from 'react';
import type { ProColumns } from '@ant-design/pro-table';
import { EditableProTable } from '@ant-design/pro-table';
import type { ActionType } from '@ant-design/pro-table';
import ProField from '@ant-design/pro-field';
import ProCard from '@ant-design/pro-card';
import { Button, Input, Space, Tag, Form } from 'antd';
import { PlusOutlined } from '@ant-design/icons';
const waitTime = (time: number = 100) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, time);
});
};
const TagList: React.FC<{
value?: {
key: string;
label: string;
}[];
onChange?: (
value: {
key: string;
label: string;
}[],
) => void;
}> = ({ value, onChange }) => {
const ref = useRef(null);
const [newTags, setNewTags] = useState<
{
key: string;
label: string;
}[]
>([]);
const [inputValue, setInputValue] = useState('');
const handleInputChange = (e: React.ChangeEvent) => {
setInputValue(e.target.value);
};
const handleInputConfirm = () => {
let tempsTags = [...(value || [])];
if (inputValue && tempsTags.filter((tag) => tag.label === inputValue).length === 0) {
tempsTags = [...tempsTags, { key: `new-${tempsTags.length}`, label: inputValue }];
}
onChange?.(tempsTags);
setNewTags([]);
setInputValue('');
};
return (
{(value || []).concat(newTags).map((item) => (
{item.label}
))}
);
};
type DataSourceType = {
id: React.Key;
title?: string;
labels?: {
key: string;
label: string;
}[];
state?: string;
created_at?: string;
children?: DataSourceType[];
};
const defaultData: DataSourceType[] = [
{
id: 624748504,
title: '活动名称一',
labels: [{ key: 'woman', label: '川妹子' }],
state: 'open',
created_at: '2020-05-26T09:42:56Z',
},
{
id: 624691229,
title: '活动名称二',
labels: [{ key: 'man', label: '西北汉子' }],
state: 'closed',
created_at: '2020-05-26T08:19:22Z',
},
];
const columns: ProColumns[] = [
{
title: '活动名称',
dataIndex: 'title',
formItemProps: {
rules: [
{
required: true,
message: '此项为必填项',
},
],
},
width: '30%',
},
{
title: '状态',
key: 'state',
dataIndex: 'state',
valueType: 'select',
valueEnum: {
all: { text: '全部', status: 'Default' },
open: {
text: '未解决',
status: 'Error',
},
closed: {
text: '已解决',
status: 'Success',
},
},
},
{
title: '标签',
dataIndex: 'labels',
width: '20%',
formItemProps: {
rules: [
{
required: true,
message: '此项为必填项',
},
],
},
renderFormItem: (_, { isEditable }) => {
return isEditable ? : ;
},
render: (_, row) => row?.labels?.map((item) => {item.label}),
},
{
title: '操作',
valueType: 'option',
width: 250,
render: (text, record, _, action) => [
{
action?.startEditable?.(record.id);
}}
>
编辑
,
复制此行到末尾
,
],
},
];
export default () => {
const actionRef = useRef();
const [editableKeys, setEditableRowKeys] = useState([]);
const [dataSource, setDataSource] = useState([]);
const [form] = Form.useForm();
return (
<>
rowKey="id"
actionRef={actionRef}
headerTitle="可编辑表格"
maxLength={5}
// 关闭默认的新建按钮
recordCreatorProps={false}
columns={columns}
request={async () => ({
data: defaultData,
total: 3,
success: true,
})}
value={dataSource}
onChange={setDataSource}
editable={{
form,
editableKeys,
onSave: async () => {
await waitTime(2000);
},
onChange: setEditableRowKeys,
actionRender: (row, config, dom) => [dom.save, dom.cancel],
}}
/>
>
);
};