fix(client): schema render

This commit is contained in:
chenos 2022-03-20 09:04:27 +08:00
parent 28a7cdbefb
commit 4422324980

View File

@ -1,8 +1,10 @@
import { createForm } from '@formily/core';
import { Schema } from '@formily/react'; import { Schema } from '@formily/react';
import { Spin } from 'antd'; import { Spin } from 'antd';
import React from 'react'; import React, { useMemo } from 'react';
import { useRequest } from '../../api-client'; import { useRequest } from '../../api-client';
import { useSchemaComponentContext } from '../hooks'; import { useSchemaComponentContext } from '../hooks';
import { FormProvider } from './FormProvider';
import { SchemaComponent } from './SchemaComponent'; import { SchemaComponent } from './SchemaComponent';
export interface RemoteSchemaComponentProps { export interface RemoteSchemaComponentProps {
@ -23,6 +25,7 @@ const RequestSchemaComponent: React.FC<RemoteSchemaComponentProps> = (props) =>
const conf = { const conf = {
url: `/uiSchemas:${onlyRenderProperties ? 'getProperties' : 'getJsonSchema'}/${uid}`, url: `/uiSchemas:${onlyRenderProperties ? 'getProperties' : 'getJsonSchema'}/${uid}`,
}; };
const form = useMemo(() => createForm(), [uid]);
const { data, loading } = useRequest(conf, { const { data, loading } = useRequest(conf, {
refreshDeps: [uid], refreshDeps: [uid],
onSuccess(data) { onSuccess(data) {
@ -30,14 +33,17 @@ const RequestSchemaComponent: React.FC<RemoteSchemaComponentProps> = (props) =>
reset && reset(); reset && reset();
}, },
}); });
if (loading) { if (loading) {
return <Spin />; return <Spin />;
} }
if (hidden) { if (hidden) {
return <Spin />; return <Spin />;
} }
return <SchemaComponent memoized scope={scope} schema={schemaTransform(data?.data || {})} />; return (
<FormProvider form={form}>
<SchemaComponent memoized scope={scope} schema={schemaTransform(data?.data || {})} />;
</FormProvider>
);
}; };
export const RemoteSchemaComponent: React.FC<RemoteSchemaComponentProps> = (props) => { export const RemoteSchemaComponent: React.FC<RemoteSchemaComponentProps> = (props) => {