diff --git a/packages/core/client/src/schema-component/core/SchemaComponent.tsx b/packages/core/client/src/schema-component/core/SchemaComponent.tsx index bb1a2e11e..91270a609 100644 --- a/packages/core/client/src/schema-component/core/SchemaComponent.tsx +++ b/packages/core/client/src/schema-component/core/SchemaComponent.tsx @@ -1,7 +1,12 @@ import { IRecursionFieldProps, ISchemaFieldProps, RecursionField, Schema } from '@formily/react'; -import React, { useMemo } from 'react'; +import React, { useContext, useMemo } from 'react'; +import { SchemaComponentContext } from '../context'; import { SchemaComponentOptions } from './SchemaComponentOptions'; +type SchemaComponentOnChange = { + onChange?: (s: Schema) => void; +}; + function toSchema(schema?: any) { if (Schema.isSchemaInstance(schema)) { return schema; @@ -21,22 +26,36 @@ const useMemoizedSchema = (schema) => { return useMemo(() => toSchema(schema), []); }; -const RecursionSchemaComponent = (props: ISchemaFieldProps) => { +const RecursionSchemaComponent = (props: ISchemaFieldProps & SchemaComponentOnChange) => { const { components, scope, schema, ...others } = props; + const ctx = useContext(SchemaComponentContext); + const s = useMemo(() => toSchema(schema), [schema]); return ( - - - + { + ctx.refresh?.(); + props.onChange?.(s); + }, + }} + > + + + + ); }; -const MemoizedSchemaComponent = (props: ISchemaFieldProps) => { +const MemoizedSchemaComponent = (props: ISchemaFieldProps & SchemaComponentOnChange) => { const { schema, ...others } = props; const s = useMemoizedSchema(schema); return ; }; -export const SchemaComponent = (props: (ISchemaFieldProps | IRecursionFieldProps) & { memoized?: boolean }) => { +export const SchemaComponent = ( + props: (ISchemaFieldProps | IRecursionFieldProps) & { memoized?: boolean } & SchemaComponentOnChange, +) => { const { memoized, ...others } = props; if (memoized) { return ;