|
@@ -0,0 +1,46 @@
|
|
|
|
|
+import { DEPT_TYPE_OPTIONS } from '@/defines';
|
|
|
|
|
+import { fetchCreateDept, fetchUpdateDept } from '@/services/dept';
|
|
|
|
|
+import { DrawerForm, ProFormDigit, ProFormSelect, ProFormText } from '@ant-design/pro-components';
|
|
|
|
|
+
|
|
|
|
|
+interface DeptFormProps {
|
|
|
|
|
+ mode: EditorFormMode;
|
|
|
|
|
+ initialValues?: Partial<API.DeptItem>;
|
|
|
|
|
+ onSuccess: () => void;
|
|
|
|
|
+ trigger: React.ReactElement;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export const DeptForm = ({ mode, initialValues, onSuccess, trigger }: DeptFormProps) => {
|
|
|
|
|
+ const title = mode === 'edit' ? '编辑部门' : mode === 'copy' ? '复制部门' : '新建部门';
|
|
|
|
|
+
|
|
|
|
|
+ const handleFinish = async (values: API.CreateDeptReq) => {
|
|
|
|
|
+ if (mode === 'edit' && initialValues?.id) {
|
|
|
|
|
+ await fetchUpdateDept({ ...values, id: initialValues.id } as API.UpdateDeptReq);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ await fetchCreateDept(values);
|
|
|
|
|
+ }
|
|
|
|
|
+ onSuccess();
|
|
|
|
|
+ return true;
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const formValues =
|
|
|
|
|
+ mode === 'copy'
|
|
|
|
|
+ ? { ...initialValues, id: undefined }
|
|
|
|
|
+ : mode === 'edit'
|
|
|
|
|
+ ? initialValues
|
|
|
|
|
+ : { parentId: initialValues?.parentId ?? 0 };
|
|
|
|
|
+
|
|
|
|
|
+ return (
|
|
|
|
|
+ <DrawerForm
|
|
|
|
|
+ title={title}
|
|
|
|
|
+ trigger={trigger}
|
|
|
|
|
+ initialValues={formValues}
|
|
|
|
|
+ onFinish={handleFinish}
|
|
|
|
|
+ drawerProps={{ destroyOnClose: true }}
|
|
|
|
|
+ >
|
|
|
|
|
+ <ProFormText name="name" label="部门名称" rules={[{ required: true }]} />
|
|
|
|
|
+ <ProFormDigit name="sort" label="排序" min={0} fieldProps={{ precision: 0 }} />
|
|
|
|
|
+ <ProFormSelect name="deptType" label="部门类型" options={DEPT_TYPE_OPTIONS} />
|
|
|
|
|
+ <ProFormText name="remark" label="备注" />
|
|
|
|
|
+ </DrawerForm>
|
|
|
|
|
+ );
|
|
|
|
|
+};
|