105 lines
2.2 KiB
React
105 lines
2.2 KiB
React
|
|
import React, { useState } from 'react';
|
||
|
|
import {
|
||
|
|
Table, Input, Button, Form, Space, Card, Modal
|
||
|
|
} from 'antd';
|
||
|
|
import { EditOutlined, DeleteOutlined } from '@ant-design/icons';
|
||
|
|
|
||
|
|
|
||
|
|
// 🔥 组件
|
||
|
|
const DictManager = ({
|
||
|
|
title,
|
||
|
|
data,
|
||
|
|
onAdd,
|
||
|
|
onUpdate,
|
||
|
|
onDelete,
|
||
|
|
loading,
|
||
|
|
}) => {
|
||
|
|
const [visible, setVisible] = useState(false);
|
||
|
|
const [form] = Form.useForm();
|
||
|
|
const [editing, setEditing] = useState(null);
|
||
|
|
|
||
|
|
// 编辑
|
||
|
|
const handleEdit = (record) => {
|
||
|
|
setEditing(record);
|
||
|
|
form.setFieldsValue(record);
|
||
|
|
setVisible(true);
|
||
|
|
};
|
||
|
|
|
||
|
|
// 提交
|
||
|
|
const handleSubmit = () => {
|
||
|
|
form.validateFields().then(values => {
|
||
|
|
if (editing) {
|
||
|
|
onUpdate({ ...editing, ...values });
|
||
|
|
} else {
|
||
|
|
onAdd({ id: Date.now(), ...values });
|
||
|
|
}
|
||
|
|
setVisible(false);
|
||
|
|
form.resetFields();
|
||
|
|
setEditing(null);
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
// 表格列
|
||
|
|
const columns = [
|
||
|
|
{ title: '存储值', dataIndex: 'value', key: 'value' },
|
||
|
|
{ title: '显示名', dataIndex: 'label', key: 'label' },
|
||
|
|
{
|
||
|
|
title: '操作',
|
||
|
|
key: 'action',
|
||
|
|
render: (_, record) => (
|
||
|
|
<Space>
|
||
|
|
<Button type="link" icon={<EditOutlined />} onClick={() => handleEdit(record)}>
|
||
|
|
编辑
|
||
|
|
</Button>
|
||
|
|
<Button type="link" danger icon={<DeleteOutlined />} onClick={() => onDelete(record.id)}>
|
||
|
|
删除
|
||
|
|
</Button>
|
||
|
|
</Space>
|
||
|
|
),
|
||
|
|
},
|
||
|
|
];
|
||
|
|
|
||
|
|
return (
|
||
|
|
<Card size="small" title={title} style={{ marginBottom: 16 }}>
|
||
|
|
<Button
|
||
|
|
type="primary"
|
||
|
|
size="small"
|
||
|
|
onClick={() => {
|
||
|
|
setEditing(null);
|
||
|
|
form.resetFields();
|
||
|
|
setVisible(true);
|
||
|
|
}}
|
||
|
|
>
|
||
|
|
新增
|
||
|
|
</Button>
|
||
|
|
|
||
|
|
<Table
|
||
|
|
size="small"
|
||
|
|
columns={columns}
|
||
|
|
dataSource={data}
|
||
|
|
rowKey="id"
|
||
|
|
pagination={false}
|
||
|
|
style={{ marginTop: 10 }}
|
||
|
|
/>
|
||
|
|
|
||
|
|
<Modal
|
||
|
|
open={visible}
|
||
|
|
title={editing ? '编辑' : '新增'}
|
||
|
|
onCancel={() => setVisible(false)}
|
||
|
|
onOk={handleSubmit}
|
||
|
|
confirmLoading={loading}
|
||
|
|
>
|
||
|
|
<Form form={form} layout="vertical">
|
||
|
|
<Form.Item name="value" label="存储值" rules={[{ required: true, message: '请输入' }]}>
|
||
|
|
<Input />
|
||
|
|
</Form.Item>
|
||
|
|
<Form.Item name="label" label="显示名称" rules={[{ required: true, message: '请输入' }]}>
|
||
|
|
<Input />
|
||
|
|
</Form.Item>
|
||
|
|
</Form>
|
||
|
|
</Modal>
|
||
|
|
</Card>
|
||
|
|
);
|
||
|
|
};
|
||
|
|
export default DictManager;
|