11import type { ISchema , SchemaTypes } from '@formily/json-schema'
2+ import type { VNode } from 'vue'
23import type {
3- ISchemaFieldProps ,
44 ISchemaFieldVueFactoryOptions ,
55 SchemaExpressionScope ,
66 SchemaVueComponents ,
77} from '../types'
8- import type { MarkupSchemaProps } from '../utils/schemaFieldProps'
98import { Schema } from '@formily/json-schema'
109import { lazyMerge } from '@formily/shared'
1110import { computed , defineComponent , Fragment , h , inject , provide , shallowRef , watch } from 'vue'
@@ -14,6 +13,9 @@ import { resolveSchemaProps } from '../utils/resolveSchemaProps'
1413import { markupSchemaProps , schemaFieldProps } from '../utils/schemaFieldProps'
1514import RecursionField from './RecursionField'
1615
16+ type SchemaFieldProps = import ( '../utils/schemaFieldProps' ) . SchemaFieldProps
17+ type MarkupSchemaProps = import ( '../utils/schemaFieldProps' ) . MarkupSchemaProps
18+
1719const env = {
1820 nonameId : 0 ,
1921}
@@ -29,8 +31,8 @@ export function createSchemaField<Components extends SchemaVueComponents = Schem
2931 name : 'SchemaField' ,
3032 inheritAttrs : false ,
3133 props : schemaFieldProps ,
32- setup ( props : ISchemaFieldProps , { slots } ) {
33- const schemaRef = computed ( ( ) =>
34+ setup ( props : SchemaFieldProps , { slots } ) {
35+ const schemaRef = computed < Schema > ( ( ) =>
3436 Schema . isSchemaInstance ( props . schema )
3537 ? props . schema
3638 : new Schema ( {
@@ -43,7 +45,7 @@ export function createSchemaField<Components extends SchemaVueComponents = Schem
4345 lazyMerge ( { } as SchemaExpressionScope , options . scope ?? { } , props . scope ?? { } ) ,
4446 )
4547
46- const optionsRef = computed ( ( ) => ( {
48+ const optionsRef = computed < ISchemaFieldVueFactoryOptions > ( ( ) => ( {
4749 ...options ,
4850 components : {
4951 ...options . components ,
@@ -58,12 +60,7 @@ export function createSchemaField<Components extends SchemaVueComponents = Schem
5860 return ( ) => {
5961 env . nonameId = 0
6062
61- const slotContent = slots . default ?.( )
62- const normalizedSlots = Array . isArray ( slotContent )
63- ? slotContent
64- : slotContent
65- ? [ slotContent ]
66- : [ ]
63+ const normalizedSlots = slots . default ?.( ) ?? [ ]
6764
6865 const recursionNode = h ( RecursionField , {
6966 ...props ,
@@ -83,37 +80,40 @@ export function createSchemaField<Components extends SchemaVueComponents = Schem
8380 } ,
8481 setup ( props : MarkupSchemaProps , { slots } ) {
8582 const parentRef = inject ( SchemaMarkupSymbol , null )
86- if ( ! parentRef || ! parentRef . value )
87- return ( ) => null
88-
89- const name = props . name || getRandomName ( )
90- const appendArraySchema = ( schema : ISchema ) => {
91- if ( parentRef . value . items ) {
92- return parentRef . value . addProperty ( name , schema )
93- }
94- else {
95- return parentRef . value . setItems ( resolveSchemaProps ( props ) )
96- }
97- }
98-
99- const schemaRef = shallowRef ( )
83+ let render : ( ) => VNode | null = ( ) => null
10084
101- watch (
102- parentRef ,
103- ( ) => {
104- if ( parentRef . value . type === 'object' || parentRef . value . type === 'void' ) {
105- schemaRef . value = parentRef . value . addProperty ( name , resolveSchemaProps ( props ) )
85+ if ( parentRef ?. value ) {
86+ const name = props . name || getRandomName ( )
87+ const appendArraySchema = ( schema : ISchema ) => {
88+ if ( parentRef . value . items ) {
89+ return parentRef . value . addProperty ( name , schema )
10690 }
107- else if ( parentRef . value . type === 'array' ) {
108- const schema = appendArraySchema ( resolveSchemaProps ( props ) )
109- schemaRef . value = Array . isArray ( schema ) ? schema [ 0 ] : schema
91+ else {
92+ return parentRef . value . setItems ( resolveSchemaProps ( props ) )
11093 }
111- } ,
112- { immediate : true , flush : 'sync' } ,
113- )
114- provide ( SchemaMarkupSymbol , schemaRef )
94+ }
95+
96+ const schemaRef = shallowRef < Schema > ( parentRef . value )
97+
98+ watch (
99+ parentRef ,
100+ ( ) => {
101+ if ( parentRef . value . type === 'object' || parentRef . value . type === 'void' ) {
102+ schemaRef . value = parentRef . value . addProperty ( name , resolveSchemaProps ( props ) )
103+ }
104+ else if ( parentRef . value . type === 'array' ) {
105+ const schema = appendArraySchema ( resolveSchemaProps ( props ) )
106+ schemaRef . value = Array . isArray ( schema ) ? schema [ 0 ] : schema
107+ }
108+ } ,
109+ { immediate : true , flush : 'sync' } ,
110+ )
111+ provide ( SchemaMarkupSymbol , schemaRef )
112+
113+ render = ( ) => h ( Fragment , null , slots . default ?.( ) ?? [ ] )
114+ }
115115
116- return ( ) => h ( Fragment , null , slots . default ?. ( ) ?? [ ] )
116+ return render
117117 } ,
118118 } )
119119
0 commit comments