@@ -10,25 +10,33 @@ export function useInputEditor() {
1010 return { }
1111
1212 const formkitInput = data ?. _dollar_formkit
13- let tempData = { }
14- if ( data . prime && data . prime . length > 0 ) {
15- const mapped = data . prime . map ( ( entry : { prime_key : string , prime_value : any } ) => {
16- const key : string = entry . prime_key
17- let value : any = entry . prime_value
18- // some inputs require numbers
19- if ( formkitInput === 'primeInputOtp' && key === 'length' ) {
20- value = + value
21- }
22- return [ key , value ] as [ string , any ]
23- } )
13+ let tempData : Record < string , any > = { }
14+
15+ if ( data . prime && Array . isArray ( data . prime ) && data . prime . length > 0 ) {
16+ const mapped = data . prime
17+ . filter ( ( entry : any ) => entry && typeof entry === 'object' && 'prime_key' in entry && 'prime_value' in entry )
18+ . map ( ( entry : { prime_key : string , prime_value : any } ) => {
19+ const key : string = entry . prime_key || ''
20+ let value : any = entry . prime_value
21+ // some inputs require numbers
22+ if ( formkitInput === 'primeInputOtp' && key === 'length' && value !== undefined ) {
23+ value = Number ( value )
24+ }
25+ return [ key , value ] as [ string , any ]
26+ } )
27+
2428 tempData = Object . assign ( { } , ...mapped . map ( ( [ key , val ] : [ string , any ] ) => ( { [ key ] : val } ) ) )
2529 }
2630
27- const readonlyValue = data . readonly ? true : undefined
28- const disabledValue = data . disabled ? true : undefined
29- const preserveValue = data . preserve ? true : undefined
31+ const readonlyValue = data . readonly === true ? true : undefined
32+ const disabledValue = data . disabled === true ? true : undefined
33+ const preserveValue = data . preserve === true ? true : undefined
3034
31- const defaultObject = { readonly : readonlyValue , disabled : disabledValue , preserve : preserveValue }
35+ const defaultObject = {
36+ readonly : readonlyValue ,
37+ disabled : disabledValue ,
38+ preserve : preserveValue ,
39+ }
3240
3341 // outer class
3442 let outerClass : string | undefined = ''
@@ -45,20 +53,55 @@ export function useInputEditor() {
4553 if ( data . innerClass )
4654 innerClass = `${ innerClass } ${ data . innerClass } ` . trim ( )
4755
48- const undefinedObject = { prime : undefined , schemaResultFormKey : undefined , _dollar_formkit : undefined , slots : undefined , selectButton : undefined }
49-
50- const useOptions = primeInputWithOptionNames . map ( s => `prime${ s } ` ) . includes ( formkitInput )
56+ const undefinedObject = {
57+ prime : undefined ,
58+ schemaResultFormKey : undefined ,
59+ _dollar_formkit : undefined ,
60+ slots : undefined ,
61+ selectButton : undefined ,
62+ }
5163
52- let result : { [ key : string ] : any } = { }
53- if ( useOptions )
54- result = { ...data , $formkit : formkitInput , ...tempData , ...undefinedObject , ...defaultObject , outerClass, wrapperClass, innerClass, optionLabel : 'label' , optionValue : 'value' }
55- else
56- result = { ...data , $formkit : formkitInput , ...tempData , ...undefinedObject , ...defaultObject , outerClass, wrapperClass, innerClass, options : undefined }
64+ const useOptions = formkitInput
65+ ? primeInputWithOptionNames
66+ . map ( s => `prime${ s } ` )
67+ . includes ( formkitInput )
68+ : false
69+
70+ let result : Record < string , any > = { }
71+
72+ if ( useOptions ) {
73+ result = {
74+ ...data ,
75+ $formkit : formkitInput ,
76+ ...tempData ,
77+ ...undefinedObject ,
78+ ...defaultObject ,
79+ outerClass,
80+ wrapperClass,
81+ innerClass,
82+ optionLabel : 'label' ,
83+ optionValue : 'value' ,
84+ }
85+ }
86+ else {
87+ result = {
88+ ...data ,
89+ $formkit : formkitInput ,
90+ ...tempData ,
91+ ...undefinedObject ,
92+ ...defaultObject ,
93+ outerClass,
94+ wrapperClass,
95+ innerClass,
96+ options : undefined ,
97+ }
98+ }
5799
58100 // cleanup empty values
59101 for ( const key in result ) {
60102 const value = result [ key ]
61- if ( ( typeof value === 'string' || typeof value === 'string' ) ) {
103+ if ( value !== null && value !== undefined
104+ && ( typeof value === 'string' ) ) {
62105 if ( value . trim ( ) . length === 0 )
63106 result [ key ] = undefined
64107 }
@@ -68,9 +111,14 @@ export function useInputEditor() {
68111 }
69112
70113 function dataToSchema ( data : any ) : any {
114+ if ( ! data )
115+ return { }
116+
71117 const schema = editorDataToSchema ( data )
72- if ( schema . options ) {
73- const options = schema . options . map ( ( o : object ) => JSON . parse ( JSON . stringify ( o ) ) )
118+
119+ if ( schema ?. options && Array . isArray ( schema . options ) ) {
120+ const options = schema . options . map ( ( o : object ) =>
121+ o ? JSON . parse ( JSON . stringify ( o ) ) : { } )
74122 return { ...schema , options }
75123 }
76124 else {
@@ -79,14 +127,23 @@ export function useInputEditor() {
79127 }
80128
81129 function editorDataToJson ( data : any ) : string {
130+ if ( ! data )
131+ return '{}'
82132 return JSON . stringify ( dataToSchema ( data ) )
83133 }
84134
85135 function objectToString ( data : Record < string , any > ) : string {
136+ if ( ! data )
137+ return '{}'
138+
86139 return `{${ Object . entries ( data ) . map ( ( [ key , value ] : [ string , any ] ) => {
87140 if ( key === 'options' && Array . isArray ( value ) && value . length > 0 ) {
88141 let result = '['
89- value . forEach ( ( o : any ) => result = `${ result + objectToString ( o ) } , ` )
142+ value . forEach ( ( o : any ) => {
143+ if ( o && typeof o === 'object' ) {
144+ result = `${ result + objectToString ( o ) } , `
145+ }
146+ } )
90147 return `${ key } : ${ result . substring ( 0 , result . length - 2 ) } ]`
91148 }
92149 else if ( key === 'primeInputOtp' ) {
@@ -99,13 +156,33 @@ export function useInputEditor() {
99156 }
100157
101158 function editorDataToObject ( data : any ) : string {
102- return objectToString ( JSON . parse ( editorDataToJson ( data ) ) )
159+ if ( ! data )
160+ return '{}'
161+
162+ try {
163+ const jsonData = editorDataToJson ( data )
164+ return objectToString ( JSON . parse ( jsonData ) )
165+ }
166+ catch ( error ) {
167+ console . error ( 'Error in editorDataToObject:' , error )
168+ return '{}'
169+ }
103170 }
104171
105172 function schemaToEditorData ( schema : any ) : any {
173+ if ( ! schema )
174+ return { }
175+
106176 const formkitInput = schema ?. $formkit
107177 return { ...schema , _dollar_formkit : formkitInput }
108178 }
109179
110- return { primeInputNames, primeOutputNames, editorDataToSchema, editorDataToJson, editorDataToCode : editorDataToObject , schemaToEditorData }
180+ return {
181+ primeInputNames,
182+ primeOutputNames,
183+ editorDataToSchema,
184+ editorDataToJson,
185+ editorDataToCode : editorDataToObject ,
186+ schemaToEditorData,
187+ }
111188}
0 commit comments