152 lines
3.5 KiB
JavaScript
152 lines
3.5 KiB
JavaScript
import React, { useState } from 'react';
|
|
import {
|
|
Table, Input, Button, Form, Space, Card, Modal, message, Popconfirm
|
|
} from 'antd';
|
|
import { EditOutlined, DeleteOutlined } from '@ant-design/icons';
|
|
import { useTranslation } from 'react-i18next';
|
|
import i18n from '../i18n';
|
|
|
|
const DictManager = ({
|
|
title,
|
|
data,
|
|
fields = [
|
|
{ name: 'storeValue', label: i18n.t('common.storeValue'), required: true },
|
|
{ name: 'displayName', label: i18n.t('common.name'), required: true },
|
|
{ name: 'remark', label: i18n.t('roles.remark'), required: false }
|
|
],
|
|
onAdd,
|
|
onUpdate,
|
|
onDelete,
|
|
refresh,
|
|
loading,
|
|
}) => {
|
|
const { t } = useTranslation();
|
|
const [visible, setVisible] = useState(false);
|
|
const [form] = Form.useForm();
|
|
const [editing, setEditing] = useState(null);
|
|
const [messageApi, contextHolder] = message.useMessage();
|
|
|
|
// 打开新增
|
|
const handleAddNew = () => {
|
|
setEditing(null);
|
|
form.resetFields();
|
|
setVisible(true);
|
|
};
|
|
|
|
// 编辑
|
|
const handleEdit = (record) => {
|
|
setEditing(record);
|
|
form.setFieldsValue(record);
|
|
setVisible(true);
|
|
};
|
|
|
|
const handleSubmit = async () => {
|
|
try {
|
|
const values = await form.validateFields();
|
|
let res;
|
|
|
|
if (editing) {
|
|
res = await onUpdate({ ...editing, ...values });
|
|
} else {
|
|
res = await onAdd(values);
|
|
}
|
|
|
|
if (res?.code == 200) {
|
|
messageApi.success(editing ? t('common.editSuccess') : t('common.addSuccess'));
|
|
setVisible(false);
|
|
form.resetFields();
|
|
setEditing(null);
|
|
refresh?.();
|
|
} else {
|
|
messageApi.error(res?.msg || t('common.operationFailed'));
|
|
}
|
|
} catch (err) {
|
|
messageApi.error(t('common.submitFail'));
|
|
}
|
|
};
|
|
|
|
// 表格列
|
|
const columns = [
|
|
...fields.map(item => ({
|
|
title: item.label,
|
|
dataIndex: item.name,
|
|
key: item.name,
|
|
})),
|
|
{
|
|
title: t('roles.actions'),
|
|
key: 'action',
|
|
render: (_, record) => (
|
|
<Space>
|
|
<Button type="link" icon={<EditOutlined />} onClick={() => handleEdit(record)}>
|
|
{t('common.edit')}
|
|
</Button>
|
|
<Popconfirm
|
|
title={t('common.confirmDeleteRecord')}
|
|
description={t('common.deleteIrreversible')}
|
|
okText={t('common.ok')}
|
|
cancelText={t('common.cancel')}
|
|
onConfirm={async () => {
|
|
try {
|
|
const res = await onDelete(record.id);
|
|
if (res?.code === 200) {
|
|
messageApi.success(t('common.deleteSuccess'));
|
|
refresh?.();
|
|
} else {
|
|
messageApi.error(res?.msg || t('common.deleteFail'));
|
|
}
|
|
} catch (err) {
|
|
messageApi.error(t('common.deleteFail'));
|
|
}
|
|
}}
|
|
>
|
|
<Button type="link" danger icon={<DeleteOutlined />}>
|
|
{t('common.delete')}
|
|
</Button>
|
|
</Popconfirm>
|
|
</Space>
|
|
),
|
|
},
|
|
];
|
|
|
|
return (
|
|
<Card size="small" title={title} style={{ marginBottom: 16 }}>
|
|
{contextHolder}
|
|
<Button type="primary" size="small" onClick={handleAddNew}>
|
|
{t('common.addNew')}
|
|
</Button>
|
|
|
|
<Table
|
|
size="small"
|
|
columns={columns}
|
|
dataSource={data}
|
|
rowKey="id"
|
|
pagination={false}
|
|
style={{ marginTop: 10 }}
|
|
/>
|
|
|
|
<Modal
|
|
open={visible}
|
|
title={editing ? t('roles.edit') : t('common.addNew')}
|
|
onCancel={() => setVisible(false)}
|
|
onOk={handleSubmit}
|
|
confirmLoading={loading}
|
|
>
|
|
<Form form={form} layout="vertical">
|
|
{fields.map(item => (
|
|
<Form.Item
|
|
key={item.name}
|
|
name={item.name}
|
|
label={item.label}
|
|
rules={[{ required: item.required, message: `${t('common.pleaseInput')}${item.label}` }]}
|
|
>
|
|
<Input />
|
|
</Form.Item>
|
|
))}
|
|
</Form>
|
|
</Modal>
|
|
</Card>
|
|
);
|
|
};
|
|
|
|
export default DictManager;
|