|
@@ -4,12 +4,24 @@ import { Button, message } from 'antd'
|
|
|
import { addProject, getProject, updateProject } from '@/services/api/project'
|
|
|
import { delay } from '@/utils/util'
|
|
|
import FormRender, { useForm } from 'form-render'
|
|
|
-import { BaseMenuEnum } from '@/pages/Schema/Base'
|
|
|
+import { BaseMenuEnum, SchemaEnum } from '@/pages/Schema/Base'
|
|
|
import type { SchemaBaseModelState } from '@/pages/Schema/Base/model'
|
|
|
import type { ConnectProps } from 'umi'
|
|
|
import type { ProjectModelState } from '../../model'
|
|
|
import consts from '@/utils/consts'
|
|
|
-import { createForm, onFieldMount } from '@formily/core'
|
|
|
+import { createForm, onFieldMount, onFormInit } from '@formily/core'
|
|
|
+import {
|
|
|
+ Form,
|
|
|
+ FormButtonGroup,
|
|
|
+ Submit,
|
|
|
+ FormLayout,
|
|
|
+ FormItem,
|
|
|
+ Input,
|
|
|
+ Select,
|
|
|
+ Reset
|
|
|
+} from '@formily/antd'
|
|
|
+import { createSchemaField } from '@formily/react'
|
|
|
+import { connectSchema } from '@/utils/schema'
|
|
|
|
|
|
export enum ModalType {
|
|
|
ADD = 0,
|
|
@@ -22,21 +34,36 @@ type ProjectModalProps = ConnectProps & {
|
|
|
setVisible: (visible: boolean) => void
|
|
|
readOnly: boolean
|
|
|
type: ModalType
|
|
|
- defaultFormData?: {
|
|
|
- dataID: string
|
|
|
- }
|
|
|
+ defaultFormData: Record<string, any> | null
|
|
|
pTypeList: API.ProjectTypeList
|
|
|
reload: () => void
|
|
|
- schema?: Record<string, any> | null
|
|
|
+ projectSchema?: Record<string, any> | null
|
|
|
}
|
|
|
const ProjectModal: React.FC<ProjectModalProps> = ({
|
|
|
setVisible,
|
|
|
- schema,
|
|
|
+ projectSchema,
|
|
|
type,
|
|
|
defaultFormData,
|
|
|
pTypeList,
|
|
|
reload
|
|
|
}) => {
|
|
|
+ const formInstance = createForm({
|
|
|
+ validateFirst: true,
|
|
|
+ initialValues: type === ModalType.ADD ? null : defaultFormData,
|
|
|
+ effects() {
|
|
|
+ onFieldMount('projectTypeID', field => (field.dataSource = pTypeList))
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ const SchemaField = createSchemaField({
|
|
|
+ components: {
|
|
|
+ FormLayout,
|
|
|
+ FormItem,
|
|
|
+ Input,
|
|
|
+ Select
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
const { run: tryUpdateProject } = useRequest(updateProject, {
|
|
|
manual: true,
|
|
|
onSuccess: () => {
|
|
@@ -50,40 +77,13 @@ const ProjectModal: React.FC<ProjectModalProps> = ({
|
|
|
}
|
|
|
})
|
|
|
|
|
|
- // const onMount = async () => {
|
|
|
- // if (defaultFormData?.dataID) {
|
|
|
- // const { code = -1, data = {} } = await getProject({ ID: defaultFormData.dataID })
|
|
|
- // if (code === consts.RET_CODE.SUCCESS) {
|
|
|
- // const currentFormData = { ...data }
|
|
|
- // const keys = Object.keys(currentFormData)
|
|
|
- // keys.forEach(key => {
|
|
|
- // if (currentFormData[key] instanceof Object) {
|
|
|
- // const targetMap = currentFormData[key]
|
|
|
- // delete currentFormData[key]
|
|
|
- // currentFormData[`${key}ID`] = targetMap.ID
|
|
|
- // }
|
|
|
- // })
|
|
|
- // form.setValues(type === ModalType.ADD ? {} : { ...currentFormData })
|
|
|
- // delay(80).then(() => {
|
|
|
- // // console.log(pTypeList)
|
|
|
-
|
|
|
- // form.setSchemaByPath('projectTypeID', {
|
|
|
- // enum: pTypeList.map(item => item.value),
|
|
|
- // enumNames: pTypeList.map(item => item.label)
|
|
|
- // })
|
|
|
- // })
|
|
|
- // }
|
|
|
- // }
|
|
|
- // }
|
|
|
-
|
|
|
- const onFinish = async (formData, errors) => {
|
|
|
- if (errors?.length) return
|
|
|
+ const onFinish = async formData => {
|
|
|
try {
|
|
|
// 执行表单提交
|
|
|
if (type === ModalType.ADD) {
|
|
|
await tryAddProject(formData)
|
|
|
} else {
|
|
|
- await tryUpdateProject(formData)
|
|
|
+ await tryUpdateProject({ ...formData, ID: defaultFormData.ID })
|
|
|
}
|
|
|
setVisible(false)
|
|
|
reload()
|
|
@@ -92,53 +92,20 @@ const ProjectModal: React.FC<ProjectModalProps> = ({
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- const renderForm = () => {
|
|
|
- const form = createForm({
|
|
|
- validateFirst: true,
|
|
|
- effects() {
|
|
|
- onFieldMount('projectTypeID', field => (field.dataSource = pTypeList))
|
|
|
- }
|
|
|
- })
|
|
|
-
|
|
|
- const SchemaField = createSchemaField({
|
|
|
- components: {
|
|
|
- FormLayout,
|
|
|
- FormItem,
|
|
|
- Input,
|
|
|
- Select
|
|
|
- }
|
|
|
- })
|
|
|
- return (
|
|
|
- <FormProvider form={form}>
|
|
|
+ return (
|
|
|
+ <div className="max-w-800px mt-20px">
|
|
|
+ <Form form={formInstance} labelCol={6} wrapperCol={8}>
|
|
|
<SchemaField
|
|
|
- schema={connectSchema(SchemaEnum?.[BaseMenuEnum.PROJECT], schema)}
|
|
|
- scope={{ useAsyncDataSource, loadProjectTypeData }}
|
|
|
+ schema={connectSchema(SchemaEnum?.[BaseMenuEnum.PROJECT], projectSchema)}
|
|
|
+ // scope={{ useAsyncDataSource }}
|
|
|
/>
|
|
|
- </FormProvider>
|
|
|
- )
|
|
|
- }
|
|
|
-
|
|
|
- return (
|
|
|
- <div>
|
|
|
- {schema && <FormRender form={form} schema={schema} onFinish={onFinish} onMount={onMount} />}
|
|
|
- <div className="ml-120px">
|
|
|
- {/* * 重置会导致下拉框的options丢失
|
|
|
- <Button onClick={() => form.setValues({})}>重置</Button> */}
|
|
|
- <Button type="primary" onClick={form.submit}>
|
|
|
- 提交
|
|
|
- </Button>
|
|
|
- </div>
|
|
|
- <div className="max-w-800px">{renderForm()}</div>
|
|
|
- {/* <Form>
|
|
|
- {schema && <FormRender form={form} schema={schema} onFinish={onFinish} onMount={onMount} />}
|
|
|
- <div className="ml-120px">
|
|
|
- * 重置会导致下拉框的options丢失
|
|
|
- <Button onClick={() => form.setValues({})}>重置</Button>
|
|
|
- <Button type="primary" onClick={form.submit}>
|
|
|
- 提交
|
|
|
- </Button>
|
|
|
- </div>
|
|
|
- </Form> */}
|
|
|
+ <FormButtonGroup.Sticky>
|
|
|
+ <FormButtonGroup.FormItem>
|
|
|
+ <Submit onSubmit={onFinish}>提交</Submit>
|
|
|
+ <Reset>重置</Reset>
|
|
|
+ </FormButtonGroup.FormItem>
|
|
|
+ </FormButtonGroup.Sticky>
|
|
|
+ </Form>
|
|
|
</div>
|
|
|
)
|
|
|
}
|
|
@@ -146,6 +113,6 @@ const ProjectModal: React.FC<ProjectModalProps> = ({
|
|
|
export default connect(
|
|
|
({ project, schemaBase }: { project: ProjectModelState; schemaBase: SchemaBaseModelState }) => ({
|
|
|
pTypeList: project.projectTypeList.map(item => ({ label: item.name, value: item.ID })),
|
|
|
- schema: schemaBase.base[BaseMenuEnum.PROJECT]?.schema
|
|
|
+ projectSchema: schemaBase.base[BaseMenuEnum.PROJECT]
|
|
|
})
|
|
|
)(ProjectModal)
|