@@ -60,7 +60,11 @@ objectView : Options -> Path -> SubSchema -> Form -> Html F.Msg
60
60
objectView options path schema form =
61
61
case schema. type_ of
62
62
AnyType ->
63
- fieldView options path schema BooleanType form
63
+ if schema. oneOf /= Nothing || schema. anyOf /= Nothing then
64
+ switch options path schema form
65
+
66
+ else
67
+ fieldView options path schema BooleanType form
64
68
65
69
NullableType singleType ->
66
70
fieldView options path schema singleType form
@@ -98,35 +102,26 @@ fieldView options path schema type_ form =
98
102
in
99
103
case schema. items of
100
104
NoItems ->
101
- field options schema f ( list options path form ( schema. title, blankSchema ))
105
+ field options schema f <|
106
+ list options path form ( schema. title, blankSchema )
102
107
103
108
ItemDefinition item ->
104
- field options schema f ( list options path form ( schema. title, item ))
109
+ field options schema f <|
110
+ list options path form ( schema. title, item )
105
111
106
112
ArrayOfItems items ->
107
- field options schema f ( tuple options path form items)
113
+ field options schema f <|
114
+ tuple options path form ( schema. title, items )
108
115
109
116
ObjectType ->
110
- let
111
- f =
112
- getFieldAsString path form
113
-
114
- schemataItem ( name, subSchema ) =
115
- schemaView options ( path ++ [ name ] ) subSchema form
116
-
117
- fields =
118
- case schema. properties of
119
- Nothing ->
120
- []
117
+ if schema. oneOf /= Nothing || schema. anyOf /= Nothing then
118
+ switch options path schema form
121
119
122
- Just ( Json . Schema . Definitions . Schemata schemata) ->
123
- List . map schemataItem schemata
124
- in
125
- group options schema f fields
120
+ else
121
+ fieldset schema [ group options path schema form ]
126
122
127
123
NullType ->
128
- fieldset []
129
- []
124
+ div [] []
130
125
131
126
132
127
txt : Options -> SubSchema -> F .FieldState ErrorValue String -> Html F .Msg
@@ -155,7 +150,7 @@ txt options schema f =
155
150
field options
156
151
schema
157
152
f
158
- [ fieldTitle schema
153
+ [ fieldTitle schema|> Maybe . withDefault ( text " " )
159
154
, case schema. format of
160
155
Just " email" ->
161
156
Input . textInput f
@@ -194,12 +189,7 @@ checkbox options schema f =
194
189
]
195
190
, text ( schema. title |> Maybe . withDefault " " )
196
191
, liveError options. errors f |> Maybe . withDefault ( text " " )
197
- , case schema. description of
198
- Just str ->
199
- fieldDescription str
200
-
201
- Nothing ->
202
- text " "
192
+ , fieldDescription schema |> Maybe . withDefault ( text " " )
203
193
]
204
194
]
205
195
@@ -215,7 +205,7 @@ select options schema f =
215
205
216
206
items =
217
207
schemata
218
- |> List . map option
208
+ |> List . map ( option constAsString )
219
209
|> List . map
220
210
( \ ( name, schema_ ) ->
221
211
( name
@@ -227,21 +217,20 @@ select options schema f =
227
217
228
218
descriptions =
229
219
schemata
230
- |> List . map option
220
+ |> List . map ( option constAsString )
231
221
|> List . map
232
222
( \ ( name, schema_ ) ->
233
223
( name
234
224
, schema_
235
- |> Maybe . andThen . description
236
- |> Maybe . map fieldDescription
225
+ |> Maybe . andThen fieldDescription
237
226
|> Maybe . withDefault ( text " " )
238
227
)
239
228
)
240
229
in
241
230
field options
242
231
schema
243
232
f
244
- [ fieldTitle schema
233
+ [ fieldTitle schema|> Maybe . withDefault ( text " " )
245
234
, Input . selectInput
246
235
items
247
236
f
@@ -255,14 +244,14 @@ select options schema f =
255
244
]
256
245
257
246
258
- option : Schema -> ( String , Maybe SubSchema )
259
- option schema =
247
+ option : ( SubSchema -> Maybe String ) -> Schema -> ( String , Maybe SubSchema )
248
+ option attr schema =
260
249
case schema of
261
250
BooleanSchema _ ->
262
251
( " " , Nothing )
263
252
264
253
ObjectSchema schema_ ->
265
- ( constAsString schema_ |> Maybe . withDefault " "
254
+ ( attr schema_ |> Maybe . withDefault " "
266
255
, Just schema_
267
256
)
268
257
@@ -331,26 +320,89 @@ tuple :
331
320
Options
332
321
-> Path
333
322
-> Form
334
- -> List Schema
323
+ -> ( Maybe String , List Schema )
335
324
-> List ( Html F . Msg )
336
- tuple options path form schemata =
325
+ tuple options path form ( title , schemata) =
337
326
let
338
327
itemPath idx =
339
328
path ++ [ " tuple" ++ String . fromInt idx ]
340
329
341
- itemView idx schema =
330
+ itemView idx itemSchema =
342
331
div
343
332
[ class " col" ]
344
- [ schemaView options ( itemPath idx) schema form ]
333
+ [ schemaView options ( itemPath idx) itemSchema form ]
334
+ in
335
+ [ case title of
336
+ Just str ->
337
+ div [ class " field-title" ] [ text str ]
338
+
339
+ Nothing ->
340
+ text " "
341
+ , div [ class " form-row" ] ( List . indexedMap itemView schemata)
342
+ ]
343
+
344
+
345
+ radio : F .FieldState ErrorValue String -> ( String , String ) -> Html F .Msg
346
+ radio fieldState ( value, title ) =
347
+ label [ class " form-check-label" ]
348
+ [ Input . radioInput value
349
+ fieldState
350
+ [ class " form-check-input"
351
+ , id fieldState. path
352
+ ]
353
+ , text title
354
+ ]
355
+
356
+
357
+ switch : Options -> Path -> SubSchema -> Form -> Html F .Msg
358
+ switch options path schema form =
359
+ let
360
+ f =
361
+ getFieldAsString ( path ++ [ " switch" ] ) form
362
+
363
+ schemata =
364
+ List . concat
365
+ [ schema. oneOf |> Maybe . withDefault []
366
+ , schema. anyOf |> Maybe . withDefault []
367
+ ]
368
+
369
+ items =
370
+ schemata
371
+ |> List . map ( option . title)
372
+
373
+ itemId idx =
374
+ " option" ++ String . fromInt idx
375
+
376
+ itemButton idx ( title, schema_ ) =
377
+ div
378
+ [ classList
379
+ [ ( " form-check" , True )
380
+ , ( " form-check-inline" , List . length items <= 2 )
381
+ ]
382
+ ]
383
+ [ radio f ( itemId idx, title ) ]
384
+
385
+ itemFields idx ( title, schema_ ) =
386
+ case schema_ of
387
+ Just s ->
388
+ ( itemId idx, group options path s form )
389
+
390
+ Nothing ->
391
+ ( itemId idx, text " " )
345
392
in
346
- [ div [ class " form-row" ] ( List . indexedMap itemView schemata) ]
393
+ field options schema f <|
394
+ [ fieldTitle schema |> Maybe . withDefault ( text " " )
395
+ , div [ class " form-group" , id f. path, tabindex - 1 ]
396
+ ( List . indexedMap itemButton items)
397
+ , conditional f ( List . indexedMap itemFields items)
398
+ ]
347
399
348
400
349
401
field : Options -> SubSchema -> F .FieldState ErrorValue String -> List (Html F .Msg ) -> Html F .Msg
350
402
field options schema f content =
351
403
let
352
404
meta =
353
- [ Maybe . map fieldDescription schema. description ]
405
+ [ fieldDescription schema ]
354
406
|> List . filterMap identity
355
407
356
408
feedback =
@@ -369,12 +421,25 @@ field options schema f content =
369
421
]
370
422
371
423
372
- group : Options -> SubSchema -> F . FieldState ErrorValue String -> List ( Html F . Msg ) -> Html F .Msg
373
- group options schema f content =
424
+ group : Options -> Path -> SubSchema -> Form -> Html F .Msg
425
+ group options path schema form =
374
426
let
427
+ f =
428
+ getFieldAsString path form
429
+
430
+ schemataItem ( name, subSchema ) =
431
+ schemaView options ( path ++ [ name ] ) subSchema form
432
+
433
+ fields =
434
+ case schema. properties of
435
+ Nothing ->
436
+ []
437
+
438
+ Just ( Json . Schema . Definitions . Schemata schemata) ->
439
+ List . map schemataItem schemata
440
+
375
441
meta =
376
- [ Maybe . map ( \ str -> legend [] [ text str ] ) schema. title
377
- , Maybe . map ( \ str -> p [] [ text str ] ) schema. description
442
+ [ Maybe . map ( \ str -> p [] [ text str ] ) schema. description
378
443
]
379
444
|> List . filterMap identity
380
445
@@ -383,27 +448,26 @@ group options schema f content =
383
448
]
384
449
|> List . filterMap identity
385
450
in
386
- fieldset
387
- [ name f. path
388
- , id f. path
389
- , tabindex - 1
451
+ div
452
+ [ classList
453
+ [ ( " form-group" , True )
454
+ , ( " is-invalid" , f. liveError /= Nothing )
455
+ , ( " has-value" , f. value /= Nothing && f. value /= Just " " )
456
+ ]
390
457
]
391
- ( meta ++ content ++ feedback)
458
+ ( meta ++ fields ++ feedback)
392
459
393
460
394
- fieldTitle : SubSchema -> Html F .Msg
461
+ fieldTitle : SubSchema -> Maybe ( Html F .Msg )
395
462
fieldTitle schema =
396
- case schema. title of
397
- Just str ->
398
- span [ class " label-text" ] [ text str ]
399
-
400
- Nothing ->
401
- text " "
463
+ schema. title
464
+ |> Maybe . map ( \ str -> span [ class " label-text" ] [ text str ] )
402
465
403
466
404
- fieldDescription : String -> Html F .Msg
405
- fieldDescription str =
406
- div [ class " form-text text-muted" ] [ text str ]
467
+ fieldDescription : SubSchema -> Maybe (Html F .Msg )
468
+ fieldDescription schema =
469
+ schema. description
470
+ |> Maybe . map ( \ str -> div [ class " form-text text-muted" ] [ text str ] )
407
471
408
472
409
473
liveError : Errors -> F .FieldState ErrorValue a -> Maybe (Html F .Msg )
@@ -434,6 +498,20 @@ inputGroup title content =
434
498
)
435
499
436
500
501
+ fieldset : SubSchema -> List (Html F .Msg ) -> Html F .Msg
502
+ fieldset schema content =
503
+ let
504
+ title =
505
+ case schema. title of
506
+ Just str ->
507
+ [ legend [] [ text str ] ]
508
+
509
+ Nothing ->
510
+ []
511
+ in
512
+ Html . fieldset [ tabindex - 1 ] ( title ++ content)
513
+
514
+
437
515
getFieldAsBool : Path -> F .Form e o -> F .FieldState e Bool
438
516
getFieldAsBool path =
439
517
F . getFieldAsBool ( fieldPath path)
0 commit comments