import { defineComponent , computed , toRaw } from 'vue' ; import { SchemaTypes , CommonFieldPropsDefine , Schema , UISchema } from './types' ; import { getSchemaType , isSelect , retrieveSchema } from './utils' ; import { StringField , NumberField , ObjectField , ArrayField , BooleanField , UnsupportedField , MultiSchemaField , } from './fields' ; const COMPONENT_TYPES = { array : 'ArrayField' , boolean : 'BooleanField' , integer : 'NumberField' , number : 'NumberField' , object : 'ObjectField' , string : 'StringField' , null : 'NullField' , } ; const FIELDS = { ArrayField , StringField , NumberField , IntegerField : NumberField , ObjectField , BooleanField , } ; function getFieldComponent ( schema , uiSchema ) { const componentName = COMPONENT_TYPES [ getSchemaType ( schema ) as string ] ; if ( ! componentName && ( schema . anyOf || schema . oneOf ) ) { return ( ) => null ; } return componentName in FIELDS ? FIELDS [ componentName ] : ( ) => { return ( < UnsupportedField schema = { schema } reason = { `Unknown field type ${ schema . type }` } />); } ; } export default defineComponent ( { name : 'SchemaItem' , props : CommonFieldPropsDefine , setup ( props ) { const schemaRef = computed ( ( ) => { return retrieveSchema ( props . schema , props . rootSchema , props . value ) ; } ) ; return ( ) => { const { uiSchema } = props ; const schema = schemaRef . value ; if ( ! schema . type ) { console . warn ( `it's better to give every schema a type:` , schema ) ; } const FiledComponent = getFieldComponent ( schema , uiSchema ) ; return ( < > < FiledComponent { ... props } schema = { schema } / > { schema . anyOf && ! isSelect ( schema ) && ( < MultiSchemaField { ... props } options = { schema . anyOf } baseType = { schema . type as any } / > ) } { schema . oneOf && ! isSelect ( schema ) && ( < MultiSchemaField { ... props } options = { schema . oneOf } baseType = { schema . type as any } / > ) } < />); } ; } , } ) ;