@@ -22,25 +22,40 @@ import '@shoelace-style/shoelace/dist/components/range/range.js';
22
22
import '@shoelace-style/shoelace/dist/components/switch/switch.js' ;
23
23
import '@shoelace-style/shoelace/dist/components/color-picker/color-picker.js' ;
24
24
25
- import type { Jsf , Path , UiOptions } from '../json-schema-form.js' ;
25
+ import type { Jsf , Path , UiSchema } from '../json-schema-form.js' ;
26
26
27
27
export const field = (
28
28
schema : JSONSchema7 ,
29
29
value : any ,
30
30
path : Path ,
31
- uiOptions : UiOptions ,
31
+ uiOptions : UiSchema ,
32
32
required : boolean ,
33
33
handleChange : Jsf [ '_handleChange' ] ,
34
34
) => {
35
- const label = schema . title
36
- ? schema . title
37
- : isNaN ( Number ( path . at ( - 1 ) ) )
38
- ? path . at ( - 1 )
39
- : undefined ;
35
+ let label : string | undefined ;
36
+
37
+ if ( schema . title ) label = schema . title ;
38
+ else if ( Number . isNaN ( Number ( path . at ( - 1 ) ) ) ) {
39
+ label = path . at ( - 1 ) ;
40
+ }
40
41
41
42
const helpText = schema . description ?? uiOptions ?. [ 'ui:help' ] ;
42
43
const placeholder = uiOptions ?. [ 'ui:placeholder' ] ;
43
44
45
+ let baseValue : string | number | boolean | undefined ;
46
+
47
+ if ( value ) {
48
+ baseValue = value ;
49
+ } else if (
50
+ schema . default &&
51
+ ( typeof schema . default === 'string' ||
52
+ typeof schema . default === 'number' ||
53
+ schema . default == null ||
54
+ typeof schema . default === 'boolean' )
55
+ ) {
56
+ baseValue = schema . default ;
57
+ }
58
+
44
59
if (
45
60
schema . type === 'array' &&
46
61
typeof schema . items === 'object' &&
@@ -57,7 +72,7 @@ export const field = (
57
72
${ schema . items . enum . map (
58
73
( stringEnum , _index ) =>
59
74
html ` < sl-checkbox
60
- .checked =${ value ?. includes ( stringEnum ) }
75
+ .checked =${ baseValue ?. includes ( stringEnum ) }
61
76
@sl-change =${ ( event : CustomEvent ) => {
62
77
const { checked } = event . target as SlCheckbox ;
63
78
@@ -69,15 +84,15 @@ export const field = (
69
84
70
85
const newData : any [ ] = [ ] ;
71
86
72
- value ?. map ( ( val : any ) => {
73
- if ( val === stringEnum && ! checked ) {
87
+ baseValue ?. map ( ( newValue : any ) => {
88
+ if ( newValue === stringEnum && ! checked ) {
74
89
} else {
75
- newData . push ( val ) ;
90
+ newData . push ( newValue ) ;
76
91
}
77
92
} ) ;
78
- schema . items . enum ?. forEach ( ( val ) => {
79
- if ( val === stringEnum && checked ) {
80
- newData . push ( val ) ;
93
+ schema . items . enum ?. forEach ( ( newValue ) => {
94
+ if ( newValue === stringEnum && checked ) {
95
+ newData . push ( newValue ) ;
81
96
}
82
97
} ) ;
83
98
@@ -101,21 +116,22 @@ export const field = (
101
116
size ="medium "
102
117
.label =${ label ?? '' }
103
118
.helpText =${ helpText ?? '' }
104
- value=${ ifDefined ( ( schema . default ?? value ) || undefined ) }
119
+ value=${ baseValue ? String ( baseValue ) : '' }
105
120
.name=${ path . join ( '.' ) }
106
121
.required=${ required }
107
122
@sl-change=${ ( event : CustomEvent ) => {
108
- let value : number | string | null = ( event . target as SlRadioGroup )
109
- . value ;
123
+ let newValue : number | string | null = (
124
+ event . target as SlRadioGroup
125
+ ) . value ;
110
126
111
127
if ( schema . type ?. includes ( 'number' ) ) {
112
- value = Number ( ( event . target as HTMLInputElement ) . value ) ;
128
+ newValue = Number ( ( event . target as HTMLInputElement ) . value ) ;
113
129
}
114
- if ( schema . type ?. [ 1 ] === 'null' && ! value ) {
115
- value = null ;
130
+ if ( schema . type ?. [ 1 ] === 'null' && ! newValue ) {
131
+ newValue = null ;
116
132
}
117
133
118
- handleChange ( path , value ) ;
134
+ handleChange ( path , newValue ) ;
119
135
} }
120
136
>
121
137
${ schema . enum ?. map (
@@ -133,28 +149,31 @@ export const field = (
133
149
return html `
134
150
< sl-select
135
151
.label =${ label ?? '' }
136
- value =${ schema . default ?? value }
152
+ value =${ baseValue ? String ( baseValue ) : '' }
137
153
.helpText=${ helpText ?? '' }
138
154
@sl-change=${ ( event : Event ) => {
139
- let value : string | null | number | string [ ] = (
155
+ let newValue : string | null | number | string [ ] = (
140
156
event . target as SlSelect
141
157
) . value ;
142
158
143
159
if (
144
160
schema . type ?. includes ( 'number' ) ||
145
161
schema . type ?. includes ( 'integer' )
146
162
) {
147
- value = Number ( value ) ;
163
+ newValue = Number ( newValue ) ;
148
164
}
149
- if ( schema . type ?. [ 1 ] === 'null' && ! value ) {
150
- value = null ;
165
+ if ( schema . type ?. [ 1 ] === 'null' && ! newValue ) {
166
+ newValue = null ;
151
167
}
152
168
153
- handleChange ( path , value ) ;
169
+ handleChange ( path , newValue ) ;
154
170
} }
155
171
>
156
172
${ schema . enum . map (
157
- ( val ) => html ` < sl-option .value =${ String ( val ) } > ${ val } </ sl-option > ` ,
173
+ ( enumValue ) =>
174
+ html ` < sl-option .value =${ String ( enumValue ) } >
175
+ ${ enumValue }
176
+ </ sl-option > ` ,
158
177
) }
159
178
</ sl-select >
160
179
` ;
@@ -191,38 +210,24 @@ export const field = (
191
210
step = 1 ;
192
211
}
193
212
194
- let val : string | number | boolean | undefined ;
195
-
196
- if ( value ) {
197
- val = String ( value ) ;
198
- } else if (
199
- schema . default &&
200
- ( typeof schema . default === 'string' ||
201
- typeof schema . default === 'number' ||
202
- schema . default == null ||
203
- typeof schema . default === 'boolean' )
204
- ) {
205
- val = schema . default ;
206
- }
207
-
208
213
if ( uiOptions ?. [ 'ui:widget' ] === 'textarea' ) {
209
214
return html ` < sl-textarea
210
215
.label =${ label ?? '' }
211
216
.helpText =${ helpText ?? '' }
212
217
placeholder=${ placeholder ?? '' }
213
- value=${ ifDefined ( value ? String ( value ) : undefined ) }
218
+ value=${ baseValue ? String ( baseValue ) : '' }
214
219
.name=${ path . join ( '.' ) }
215
220
.id=${ path . join ( '.' ) }
216
221
.required=${ required }
217
222
@sl-change=${ ( event : CustomEvent ) => {
218
- let value : null | string = ( event . target as HTMLTextAreaElement )
223
+ let newValue : null | string = ( event . target as HTMLTextAreaElement )
219
224
. value ;
220
225
221
- if ( schema . type ?. [ 1 ] === 'null' && ! value ) {
222
- value = null ;
226
+ if ( schema . type ?. [ 1 ] === 'null' && ! newValue ) {
227
+ newValue = null ;
223
228
}
224
229
225
- handleChange ( path , value ) ;
230
+ handleChange ( path , newValue ) ;
226
231
} }
227
232
> </ sl-textarea > ` ;
228
233
}
@@ -232,16 +237,16 @@ export const field = (
232
237
< label > ${ label } </ label >
233
238
< sl-color-picker
234
239
.label =${ label ?? '' }
235
- value =${ ifDefined ( String ( val ) ) }
240
+ value =${ baseValue ? String ( baseValue ) : '' }
236
241
@sl-change=${ ( event : CustomEvent ) => {
237
- let value : string | null =
242
+ let newValue : string | null =
238
243
( event . target as HTMLInputElement ) . value ?? schema . default ;
239
244
240
- if ( schema . type ?. [ 1 ] === 'null' && ! value ) {
241
- value = null ;
245
+ if ( schema . type ?. [ 1 ] === 'null' && ! newValue ) {
246
+ newValue = null ;
242
247
}
243
248
244
- handleChange ( path , value ) ;
249
+ handleChange ( path , newValue ) ;
245
250
} }
246
251
> </ sl-color-picker >
247
252
@@ -255,18 +260,18 @@ export const field = (
255
260
< sl-range
256
261
.label =${ label ?? '' }
257
262
.helpText =${ helpText ?? '' }
258
- value=${ ifDefined ( val ? Number ( val ) : undefined ) }
263
+ value=${ ifDefined ( baseValue ? Number ( baseValue ) : undefined ) }
259
264
.type=${ type }
260
265
step=${ ifDefined ( step ) }
261
266
min=${ ifDefined ( schema . minimum ) }
262
267
max=${ ifDefined ( schema . maximum ) }
263
268
.name=${ path . join ( '.' ) }
264
269
.required=${ required }
265
270
@sl-change=${ ( event : CustomEvent ) => {
266
- let { value } = event . target as SlRange ;
271
+ const { value : newValue } = event . target as SlRange ;
267
272
268
- let val : number | null | undefined = value ;
269
- if ( schema . type ?. [ 1 ] === 'null' && ! value ) {
273
+ let val : number | null | undefined = newValue ;
274
+ if ( schema . type ?. [ 1 ] === 'null' && ! newValue ) {
270
275
val = null ;
271
276
}
272
277
@@ -284,7 +289,7 @@ export const field = (
284
289
.type =${ type }
285
290
.helpText=${ helpText ?? '' }
286
291
placeholder=${ placeholder ?? '' }
287
- value=${ ifDefined ( val ) }
292
+ value=${ ifDefined ( baseValue ) }
288
293
.name=${ path . join ( '.' ) }
289
294
.id=${ path . join ( '.' ) }
290
295
.required=${ required }
@@ -295,16 +300,19 @@ export const field = (
295
300
maxLength=${ ifDefined ( schema . maxLength ) }
296
301
pattern=${ ifDefined ( schema . pattern ) }
297
302
@sl-change=${ ( event : CustomEvent ) => {
298
- let { value, type, valueAsNumber } =
299
- event . target as HTMLInputElement ;
300
-
301
- let val : number | string | null | undefined = value ;
302
- if ( type === 'number' ) {
303
+ const {
304
+ value : newValue ,
305
+ type : inputType ,
306
+ valueAsNumber,
307
+ } = event . target as HTMLInputElement ;
308
+
309
+ let val : number | string | null | undefined = newValue ;
310
+ if ( inputType === 'number' ) {
303
311
val = valueAsNumber ;
304
312
}
305
313
if (
306
314
schema . type ?. [ 1 ] === 'null' &&
307
- ( typeof value === 'undefined' || value === '' )
315
+ ( typeof newValue === 'undefined' || newValue === '' )
308
316
) {
309
317
val = null ;
310
318
}
@@ -335,21 +343,15 @@ export const field = (
335
343
size ="medium "
336
344
.label =${ label ?? '' }
337
345
.helpText =${ helpText ?? '' }
338
- value=${ ifDefined (
339
- typeof schema . default !== undefined
340
- ? String ( schema . default )
341
- : typeof value !== undefined
342
- ? String ( value )
343
- : undefined ,
344
- ) }
346
+ value=${ ifDefined ( baseValue ? String ( baseValue ) : '' ) }
345
347
.name=${ path . join ( '.' ) }
346
348
.required=${ required }
347
349
@sl-change=${ ( event : CustomEvent ) => {
348
- let { value } = event . target as HTMLInputElement ;
350
+ const { value : newValue } = event . target as HTMLInputElement ;
349
351
350
352
let val : boolean | null | undefined ;
351
- val = value === 'true' ;
352
- if ( schema . type ?. [ 1 ] === 'null' && ! value ) {
353
+ val = newValue === 'true' ;
354
+ if ( schema . type ?. [ 1 ] === 'null' && ! newValue ) {
353
355
val = null ;
354
356
}
355
357
@@ -369,12 +371,12 @@ export const field = (
369
371
return html `
370
372
< div >
371
373
< sl-switch
372
- .checked =${ schema . default ?? value }
374
+ .checked =${ Boolean ( baseValue ) }
373
375
.name =${ path . join ( '.' ) }
374
376
@sl-change=${ ( event : CustomEvent ) => {
375
- const value = ( event . target as SlSwitch ) . checked ;
377
+ const newValue = ( event . target as SlSwitch ) . checked ;
376
378
377
- handleChange ( path , value ) ;
379
+ handleChange ( path , newValue ) ;
378
380
} }
379
381
> ${ label } </ sl-switch
380
382
>
@@ -387,9 +389,9 @@ export const field = (
387
389
.checked =${ schema . default ?? value }
388
390
.name =${ path . join ( '.' ) }
389
391
@sl-change=${ ( event : CustomEvent ) => {
390
- const value = ( event . target as SlCheckbox ) . checked ;
392
+ const newValue = ( event . target as SlCheckbox ) . checked ;
391
393
392
- handleChange ( path , value ) ;
394
+ handleChange ( path , newValue ) ;
393
395
} }
394
396
> ${ label } </ sl-checkbox
395
397
>
0 commit comments