@@ -15,7 +15,7 @@ import ErrorMessages exposing (stringifyError)
15
15
import Html exposing (..)
16
16
import Html.Attributes exposing (class , classList )
17
17
import Html.Events exposing (onClick )
18
- import Json.Decode as Decode exposing (decodeValue )
18
+ import Json.Decode as Decode exposing (Value , decodeValue )
19
19
import Json.Encode as Encode
20
20
import Json.Form.Config exposing (Config )
21
21
import Json.Form.Definitions as Definitions exposing (EditingMode (..) , Msg (..) , Path )
@@ -51,27 +51,29 @@ view model =
51
51
52
52
viewNode : Model -> Schema -> Bool -> Bool -> Path -> Html Msg
53
53
viewNode model schema isRequired isDisabled path =
54
- case editingMode model schema of
55
- TextField ->
56
- TextField . view model schema False isRequired isDisabled path
54
+ Html . div [ class <| " nesting-level-" ++ String . fromInt ( List . length path) ]
55
+ [ case editingMode model schema of
56
+ TextField ->
57
+ TextField . view model schema False isRequired isDisabled path
57
58
58
- JsonEditor ->
59
- TextField . view model schema True isRequired isDisabled path
59
+ JsonEditor ->
60
+ TextField . view model schema True isRequired isDisabled path
60
61
61
- NumberField ->
62
- TextField . viewNumeric model schema isRequired isDisabled path
62
+ NumberField ->
63
+ TextField . viewNumeric model schema isRequired isDisabled path
63
64
64
- Definitions . Switch ->
65
- Selection . switch model schema isRequired isDisabled path
65
+ Definitions . Switch ->
66
+ Selection . switch model schema isRequired isDisabled path
66
67
67
- Checkbox ->
68
- Selection . checkbox model schema isRequired isDisabled path
68
+ Checkbox ->
69
+ Selection . checkbox model schema isRequired isDisabled path
69
70
70
- Object properties ->
71
- viewObject model schema properties isRequired isDisabled path
71
+ Object properties ->
72
+ viewObject model schema properties isRequired isDisabled path
72
73
73
- Array ->
74
- viewArray model schema isRequired isDisabled path
74
+ Array ->
75
+ viewArray model schema isRequired isDisabled path
76
+ ]
75
77
76
78
77
79
editingMode : Model -> Schema -> EditingMode
@@ -179,7 +181,7 @@ viewArray model schema isRequired isDisabled path =
179
181
)
180
182
|> div []
181
183
, div [ class " array-item-add" ]
182
- [ button [ class " button" , onClick <| AddItem path ( List . length list) ] [ text " ADD ITEM" ]
184
+ [ button [ class " button" , onClick <| AddItem path ( List . length list) itemSchema ] [ text " ADD ITEM" ]
183
185
]
184
186
]
185
187
|> div []
@@ -252,7 +254,7 @@ viewObject model schema properties isRequired isDisabled path =
252
254
text " "
253
255
254
256
else
255
- div [ class " jf-nested- object" ]
257
+ div [ class " jf-object" ]
256
258
[ if title /= " " then
257
259
div
258
260
( [ classList
@@ -297,6 +299,36 @@ updateSchema schema model =
297
299
{ model | schema = schema }
298
300
299
301
302
+ initValue : Schema -> Value -> Value
303
+ initValue schema someValue =
304
+ schema
305
+ |> Json . Schema . validateValue { applyDefaults = True } someValue
306
+ |> ( \ res ->
307
+ case res of
308
+ Ok updValue ->
309
+ updValue
310
+
311
+ Err x ->
312
+ someValue
313
+ )
314
+
315
+
316
+ defaultFor : Schema -> JsonValue
317
+ defaultFor s =
318
+ case s of
319
+ ObjectSchema os ->
320
+ if os. type_ == SingleType ObjectType then
321
+ Encode . object []
322
+ |> initValue s
323
+ |> JsonValue . decodeValue
324
+
325
+ else
326
+ NullValue
327
+
328
+ _ ->
329
+ NullValue
330
+
331
+
300
332
update : Msg -> Model -> ( ( Model , Cmd Msg ), ExternalMsg )
301
333
update msg model =
302
334
case msg of
@@ -306,7 +338,7 @@ update msg model =
306
338
)
307
339
|> withExMsg None
308
340
309
- AddItem path index ->
341
+ AddItem path index schema ->
310
342
let
311
343
newPropPath =
312
344
path ++ [ index |> String . fromInt ]
@@ -325,7 +357,7 @@ update msg model =
325
357
|> Result . toMaybe
326
358
}
327
359
in
328
- editValue updatedModel newPropPath JsonValue . NullValue
360
+ editValue updatedModel newPropPath ( defaultFor schema )
329
361
330
362
DeleteProperty path ->
331
363
let
@@ -378,16 +410,18 @@ update msg model =
378
410
case focused of
379
411
Nothing ->
380
412
if isNumber then
381
- editValue
382
- { model | beingFocused = touch focused model. focused model. beingFocused, focused = Nothing }
383
- ( model. focused |> Maybe . withDefault [] )
384
- ( case model. editedJson |> String . toFloat of
385
- Just num ->
386
- JsonValue . NumericValue num
387
-
388
- _ ->
389
- JsonValue . StringValue model. editedJson
390
- )
413
+ case model. editedJson |> String . toFloat of
414
+ Just num ->
415
+ editValue
416
+ { model | beingFocused = touch focused model. focused model. beingFocused, focused = Nothing }
417
+ ( model. focused |> Maybe . withDefault [] )
418
+ ( JsonValue . NumericValue num)
419
+
420
+ _ ->
421
+ ( model
422
+ , Cmd . none
423
+ )
424
+ |> withExMsg None
391
425
392
426
else
393
427
( { model | beingFocused = touch focused model. focused model. beingFocused, focused = Nothing }
@@ -569,27 +603,27 @@ init config schema v =
569
603
( value, errors ) =
570
604
case v of
571
605
Just something ->
572
- something |> JsonValue . encode |> initValue
606
+ something |> JsonValue . encode |> initVal
573
607
574
608
Nothing ->
575
609
case schema of
576
610
ObjectSchema os ->
577
611
case os. default of
578
612
Just def ->
579
- def |> initValue
613
+ def |> initVal
580
614
581
615
Nothing ->
582
616
case os. type_ of
583
617
SingleType ObjectType ->
584
- Encode . object [] |> initValue
618
+ Encode . object [] |> initVal
585
619
586
620
_ ->
587
621
( Nothing , Dict . empty )
588
622
589
623
_ ->
590
624
( Nothing , Dict . empty )
591
625
592
- initValue someValue =
626
+ initVal someValue =
593
627
schema
594
628
|> Json . Schema . validateValue { applyDefaults = True } someValue
595
629
|> ( \ res ->
0 commit comments