11import { useFormKitSchema } from './useFormKitSchema'
2+ import { useInputEditor } from './useInputEditor'
23
34export function useInputEditorSchema ( ) {
45 const { addElement, addList, addListGroup, addComponent } = useFormKitSchema ( )
6+ const { primeInputNames } = useInputEditor ( )
57 function addFlexElement ( children : any [ ] ) {
68 return addElement ( 'div' , children , { style : 'max-width: 40rem;display: flex;gap: 1rem;' } )
79 }
@@ -23,10 +25,6 @@ export function useInputEditorSchema() {
2325 ] , { style : 'padding-top: 1.5rem;' } )
2426 }
2527
26- const primeInputWithOptionNames = [ 'CascadeSelect' , 'Listbox' , 'MultiSelect' , 'RadioButton' , 'Select' , 'SelectButton' , 'TreeSelect' ]
27-
28- const primeInputNames = [ ...primeInputWithOptionNames , 'AutoComplete' , 'Checkbox' , 'ColorPicker' , 'DatePicker' , 'Editor' , 'InputMask' , 'InputNumber' , 'InputOtp' , 'InputText' , 'Knob' , 'Password' , 'Rating' , 'Slider' , 'Textarea' , 'ToggleButton' , 'ToggleSwitch' ] . sort ( )
29-
3028 function primeInputOptions ( list : string [ ] ) {
3129 return list . map ( ( name : string ) => {
3230 return { label : name , value : `prime${ name } ` }
@@ -54,50 +52,6 @@ export function useInputEditorSchema() {
5452
5553 ]
5654
57- function editorDataToSchema ( data : any ) : any {
58- const formkitInput = data ?. _dollar_formkit
59- let tempData = { }
60- if ( data . prime ?. length > 0 ) {
61- const mapped = data . prime ?. map ( ( entry ) => {
62- const key = entry . prime_key
63- let value = entry . prime_value
64- // some inputs require numbers
65- if ( formkitInput === 'primeInputOtp' && key === 'length' )
66- value = + value
67- return [ key , value ]
68- } )
69- tempData = Object . assign ( ...mapped . map ( ( [ key , val ] ) => ( { [ key ] : val } ) ) )
70- }
71-
72- const readonlyValue = data . readonly ? true : undefined
73- const disabledValue = data . disabled ? true : undefined
74- const preserveValue = data . preserve ? true : undefined
75-
76- const defaultObject = { readonly : readonlyValue , disabled : disabledValue , preserve : preserveValue }
77-
78- const undefinedObject = { prime : undefined , schemaResultFormKey : undefined , _dollar_formkit : undefined , slots : undefined , selectButton : undefined }
79-
80- const useOptions = primeInputWithOptionNames . map ( s => `prime${ s } ` ) . includes ( formkitInput )
81-
82- if ( useOptions )
83- return { ...data , $formkit : formkitInput , ...tempData , ...undefinedObject , ...defaultObject , optionLabel : 'label' , optionValue : 'value' }
84- else
85- return { ...data , $formkit : formkitInput , ...tempData , ...undefinedObject , ...defaultObject , options : undefined }
86- }
87-
88- function editorDataToJson ( data : any ) : string {
89- return JSON . stringify ( editorDataToSchema ( data ) )
90- }
91-
92- function editorDataToObject ( data : any ) : string {
93- return `{${ Object . entries ( JSON . parse ( editorDataToJson ( data ) ) ) . map ( ( [ key , value ] ) => `${ key } : '${ value } '` ) . join ( ) } }`
94- }
95-
96- function schemaToEditorData ( schema : any ) : any {
97- const formkitInput = schema ?. $formkit
98- return { ...schema , _dollar_formkit : formkitInput }
99- }
100-
10155 function editorSchema ( inputOptions : any [ ] = primeInputOptions ( primeInputNames ) ) {
10256 return [
10357 addGridElement ( [
@@ -338,5 +292,5 @@ export function useInputEditorSchema() {
338292 ]
339293 }
340294
341- return { editorSchema, primeInputNames , primeInputOptions, editorDataToSchema , editorDataToJson , editorDataToCode : editorDataToObject , schemaToEditorData }
295+ return { editorSchema, primeInputOptions }
342296}
0 commit comments