11<script context =" module" lang =" ts" >
2+ import { getContext , setContext } from " svelte" ;
3+ import * as controls from " ./controls" ;
4+
5+ const controlNames = Object .keys (controls );
6+ const ControlPropsContextKey = Symbol (" control-props" );
7+
28 function useConst(schema : JSONSchema7 | undefined ) {
39 return (schema != null ) && ! isBoolean (schema ) && (" const" in schema );
410 }
1319 : schema ?.type ;
1420 return (Array .isArray (type ) ? type [0 ] : type ) ?? " object" ;
1521 }
22+
23+ export function setControlProps(props : Record <string , any >) {
24+ const context = Object .keys (props ).reduce ((controlsProps , name ) => {
25+ const [controlName, propName] = name .split (' $' , 2 );
26+ if ((controlNames .includes (controlName )) && (propName != null )) {
27+ (controlsProps [controlName ] ?? = {})[propName ] = props [name ];
28+ }
29+ return controlsProps ;
30+ }, {} as { [control : string ]: { [prop : string ]: any } });
31+ return setContext (ControlPropsContextKey , context );
32+ }
33+
34+ export function getControlProps() {
35+ return getContext <{[control : string ]: Record <string , any >}>(ControlPropsContextKey );
36+ }
1637 </script >
1738
1839<script lang =" ts" >
1940 import type { JSONSchema7 } from " json-schema" ;
2041 import type UISchema from " ./UISchema" ;
2142 import { isBoolean } from " ./utilities" ;
22- import * as controls from " ./controls" ;
2343
2444 export let schema: JSONSchema7 | undefined ;
2545 export let data: any = undefined ;
2646 export let uischema: UISchema = {};
2747 export let force: boolean = false ;
2848
49+ const allControlProps = getControlProps ();
2950 let control: any ;
51+ let controlProps: Record <string , any > = {};
3052
3153 $ : updateControlType (schema );
3254
3557 const updatedControl = controls [singleType as keyof typeof controls ] as any ;
3658 if (updatedControl != control ) {
3759 control = updatedControl ;
60+ controlProps = allControlProps [singleType ];
3861 }
3962 }
4063 </script >
4164
42- <svelte:component this ={control } {...schema } bind:data {uischema } {force } />
65+ <svelte:component this ={control } {...controlProps } {... schema } bind:data {uischema } {force } />
0 commit comments