import { NativeElements } from '@/lib/NativeElements' ; import { JsonSchema } from '../../types/jsonschema' ; import { Dict , IUIDescriptor , FieldKind , UnknowField , Component , HelperAttributes , LabelAttributes , DescriptorDefinition , IComponents } from '../../types' ; const DESCRIPTORS = { } ; export abstract class UIDescriptor < TField extends UnknowField , TDescriptorDefinition extends DescriptorDefinition , TFieldKind extends FieldKind = FieldKind > implements IUIDescriptor < TDescriptorDefinition > { readonly label : string ; readonly helper : string ; readonly component : Component ; readonly props : Dict ; readonly attrs : Dict < any > ; readonly labelAttrs : LabelAttributes ; readonly helperAttrs : HelperAttributes ; readonly components : IComponents ; readonly definition : TDescriptorDefinition ; readonly visible : boolean ; readonly kind : TFieldKind ; readonly schema : JsonSchema ; static register ( kind , parserClass ) { DESCRIPTORS [ kind ] = parserClass ; } static get < T extends DescriptorDefinition = DescriptorDefinition > ( options , field , components ? ) { if ( ! DESCRIPTORS . hasOwnProperty ( field . kind ) ) { throw new TypeError ( `Unknow descriptor kind '${ field . kind }'` ) ; } if ( typeof options === 'function' ) { options = options ( field ) ; } return new DESCRIPTORS [ field . kind ] ( options , field , components ) ; } constructor ( definition , field , components ? ) { this . visible = typeof definition . visible === 'boolean' ? definition . visible : true ; this . kind = field . kind ; this . attrs = { ... field . attrs , ... ( definition . attrs || { } ) } ; this . props = definition . props || { } ; this . components = components || NativeElements ; this . component = definition . component || this . components . get ( this . kind ) || NativeElements . get ( this . kind ) ; this . schema = field . schema ; this . definition = definition ; this . label = definition . hasOwnProperty ( 'label' ) ? definition . label || '' : field . schema . title || '' ; this . helper = definition . hasOwnProperty ( 'helper' ) ? definition . helper || '' : field . schema . description || '' ; this . labelAttrs = { id : this . label ? `${ this . attrs . id }-label` : undefined , for : this . attrs . id as string } ; this . helperAttrs = { id : this . helper ? `${ this . attrs . id }-helper` : undefined } ; see https this . attrs [ 'aria-labelledby' ] = this . labelAttrs . id ; this . attrs [ 'aria-describedby' ] = this . helperAttrs . id ; } parse ( field ) { Object . assign ( this . attrs , field . attrs , this . definition . attrs || { } ) ; } update ( field ) { } }