import { FormLayout } from '@formily/antd'; import { createForm } from '@formily/core'; import { FieldContext, FormContext, observer, RecursionField, useField, useFieldSchema } from '@formily/react'; import { Options, Result } from 'ahooks/lib/useRequest/src/types'; import { Spin } from 'antd'; import React, { createContext, useContext, useMemo } from 'react'; import { useAttach, useComponent } from '../..'; import { useRequest } from '../../../api-client'; import { useCollection } from '../../../collection-manager'; import { GeneralSchemaDesigner, SchemaSettings } from '../../../schema-settings'; import { useSchemaTemplate } from '../../../schema-templates'; type Opts = Options & { uid?: string }; export interface FormProps { [key: string]: any; } export type FormUseValues = (opts?: Opts, props?: FormProps) => Result; const FormComponent: React.FC = (props) => { const { form, children, ...others } = props; const field = useField(); const fieldSchema = useFieldSchema(); // TODO: component 里 useField 会与当前 field 存在偏差 const f = useAttach(form.createVoidField({ ...field.props, basePath: '' })); return ( ); }; const Def = (props: any) => props.children; const FormDecorator: React.FC = (props) => { const { form, children, ...others } = props; const field = useField(); const fieldSchema = useFieldSchema(); // TODO: component 里 useField 会与当前 field 存在偏差 const f = useAttach(form.createVoidField({ ...field.props, basePath: '' })); const Component = useComponent(fieldSchema['x-component'], Def); return ( {/* {children} */} ); }; const useRequestProps = (props: any) => { const { request, initialValue } = props; if (request) { return request; } return () => { return Promise.resolve({ data: initialValue, }); }; }; const useDef = (opts: any = {}, props: FormProps = {}) => { return useRequest(useRequestProps(props), opts); }; const FormBlockContext = createContext(null); export const Form: React.FC & { Designer?: any } = observer((props) => { const { request, effects, initialValue, useValues = useDef, ...others } = props; const fieldSchema = useFieldSchema(); const field = useField(); const form = useMemo(() => createForm({ effects }), []); const result = useValues( { uid: fieldSchema['x-uid'], async onSuccess(data) { await form.reset(); form.setValues(data?.data); }, }, props, ); const parent = useContext(FormBlockContext); return ( {fieldSchema['x-decorator'] === 'Form' ? ( ) : ( )} ); }); Form.Designer = () => { const { name, title } = useCollection(); const template = useSchemaTemplate(); return ( ); };