Files
shihezi-admin/src/pages/Classify/Industry/edit.tsx

189 lines
5.0 KiB
TypeScript
Raw Normal View History

2025-03-28 15:30:35 +08:00
import React, { useEffect } from 'react';
import {
ModalForm,
ProForm,
ProFormDigit,
ProFormRadio,
ProFormTreeSelect,
2025-03-28 15:30:35 +08:00
ProFormText,
2025-03-31 17:43:40 +08:00
ProDescriptions,
2025-03-28 15:30:35 +08:00
} from '@ant-design/pro-components';
import { Form } from 'antd';
import { DictValueEnumObj } from '@/components/DictTag';
import { FormattedMessage } from '@@/exports';
import { getCmsIndustryTreeList } from '@/services/classify/industry'; // 修改导入为 getCmsIndustryTreeList
2025-03-31 17:43:40 +08:00
interface IndustryDetail {
id?: number;
industryName?: string;
orderNum?: number;
status?: string;
parentId?: number;
parentName?: string;
2025-03-31 17:43:40 +08:00
}
2025-03-28 15:30:35 +08:00
export type ListFormProps = {
onCancel: (flag?: boolean, formVars?: unknown) => void;
onSubmit: (values: any) => Promise<void>;
2025-03-28 15:30:35 +08:00
open: boolean;
values?: Partial<IndustryDetail>;
2025-03-28 15:30:35 +08:00
industryStatusEnum: DictValueEnumObj;
2025-03-31 17:43:40 +08:00
mode?: 'view' | 'edit' | 'create';
2025-03-28 15:30:35 +08:00
};
const listEdit: React.FC<ListFormProps> = (props) => {
const [form] = Form.useForm();
const { industryStatusEnum, mode = props.values ? 'edit' : 'create', values } = props;
2025-03-28 15:30:35 +08:00
useEffect(() => {
form.resetFields();
2025-03-31 17:43:40 +08:00
if (values) {
form.setFieldsValue({
...values,
parentId: values?.parentId ?? 0
});
2025-03-28 15:30:35 +08:00
}
}, [form, values?.id]);
2025-03-31 17:43:40 +08:00
const getSafeDetailData = (data?: Partial<IndustryDetail>): IndustryDetail => {
return {
id: data?.id ?? 0,
industryName: data?.industryName ?? '',
orderNum: data?.orderNum ?? 0,
parentId: data?.parentId ?? 0,
parentName: data?.parentName ?? '顶级节点',
status: data?.status ?? '0',
};
2025-03-31 17:43:40 +08:00
};
2025-03-28 15:30:35 +08:00
const handleCancel = () => {
props.onCancel();
form.resetFields();
};
const handleFinish = async (values: Record<string, any>) => {
const submitValues = {
...values,
parentId: values.parentId === 0 ? undefined : values.parentId
};
props.onSubmit(submitValues);
2025-03-28 15:30:35 +08:00
};
// 获取行业树数据
const fetchIndustryTree = async () => {
const res = await getCmsIndustryTreeList();
return [
{
id: 0,
label: '顶级节点',
value: 0,
children: res.data || []
}
];
};
if (mode === 'view') {
return (
<ModalForm
title="行业详情"
open={props.open}
width={800}
modalProps={{
destroyOnClose: true,
onCancel: () => handleCancel(),
footer: null,
}}
submitter={false}
2025-03-31 17:43:40 +08:00
>
<ProDescriptions<IndustryDetail>
2025-03-31 17:43:40 +08:00
column={1}
dataSource={getSafeDetailData(values)}
loading={!values}
>
<ProDescriptions.Item label="行业ID" dataIndex="id" />
2025-03-31 17:43:40 +08:00
<ProDescriptions.Item label="行业名称" dataIndex="industryName" />
<ProDescriptions.Item label="显示顺序" dataIndex="orderNum" />
<ProDescriptions.Item
label="父行业"
dataIndex="parentName"
renderText={(text, record) => record.parentId === 0 ? '顶级节点' : text}
2025-03-31 17:43:40 +08:00
/>
<ProDescriptions.Item
label="行业状态"
dataIndex="status"
valueEnum={industryStatusEnum}
/>
</ProDescriptions>
</ModalForm>
);
}
2025-03-28 15:30:35 +08:00
return (
2025-03-31 17:43:40 +08:00
<ModalForm
title={`${values ? '编辑' : '新增'}行业`}
2025-03-28 15:30:35 +08:00
form={form}
autoFocusFirstInput
open={props.open}
modalProps={{
destroyOnClose: true,
onCancel: () => handleCancel(),
}}
submitTimeout={2000}
onFinish={handleFinish}
>
<ProFormDigit name="id" disabled hidden={true} />
2025-03-28 15:30:35 +08:00
<ProForm.Group>
<ProFormText
width="md"
name="industryName"
label="行业名称"
placeholder="请输入行业名称"
rules={[{ required: true, message: '请输入行业名称' }]}
/>
2025-03-28 15:30:35 +08:00
<ProFormDigit
label="显示顺序"
name="orderNum"
width="md"
min={0}
placeholder="请输入显示顺序"
rules={[{ required: true, message: '请输入显示顺序' }]}
2025-03-28 15:30:35 +08:00
/>
</ProForm.Group>
<ProForm.Group>
<ProFormTreeSelect
2025-03-28 15:30:35 +08:00
name="parentId"
label="父行业"
placeholder="请选择父行业"
2025-03-28 15:30:35 +08:00
allowClear
width="md"
request={fetchIndustryTree}
2025-03-28 15:30:35 +08:00
fieldProps={{
treeDefaultExpandAll: true,
2025-03-28 15:30:35 +08:00
showSearch: true,
filterTreeNode: true,
2025-03-28 15:30:35 +08:00
fieldNames: {
label: 'label',
value: 'id',
children: 'children',
},
dropdownStyle: {
maxHeight: 400,
overflow: 'auto',
2025-03-28 15:30:35 +08:00
},
}}
rules={[{ required: true, message: '请选择父行业' }]}
2025-03-28 15:30:35 +08:00
/>
<ProFormRadio.Group
valueEnum={industryStatusEnum}
name="status"
label="行业状态"
width="md"
initialValue="0"
rules={[{ required: true, message: '请选择行业状态' }]}
2025-03-28 15:30:35 +08:00
/>
</ProForm.Group>
</ModalForm>
);
};
export default listEdit;