Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2a267af

Browse files
committed
Init array item
Closes #8
1 parent 984e556 commit 2a267af

File tree

4 files changed

+72
-41
lines changed

4 files changed

+72
-41
lines changed

‎src/Json/Form.elm‎

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import ErrorMessages exposing (stringifyError)
1515
import Html exposing (..)
1616
import Html.Attributes exposing (class, classList)
1717
import Html.Events exposing (onClick)
18-
import Json.Decode as Decode exposing (decodeValue)
18+
import Json.Decode as Decode exposing (Value, decodeValue)
1919
import Json.Encode as Encode
2020
import Json.Form.Config exposing (Config)
2121
import Json.Form.Definitions as Definitions exposing (EditingMode(..), Msg(..), Path)
@@ -51,27 +51,29 @@ view model =
5151

5252
viewNode : Model -> Schema -> Bool -> Bool -> Path -> Html Msg
5353
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
5758

58-
JsonEditor ->
59-
TextField.view model schema True isRequired isDisabled path
59+
JsonEditor ->
60+
TextField.view model schema True isRequired isDisabled path
6061

61-
NumberField ->
62-
TextField.viewNumeric model schema isRequired isDisabled path
62+
NumberField ->
63+
TextField.viewNumeric model schema isRequired isDisabled path
6364

64-
Definitions.Switch ->
65-
Selection.switch model schema isRequired isDisabled path
65+
Definitions.Switch ->
66+
Selection.switch model schema isRequired isDisabled path
6667

67-
Checkbox ->
68-
Selection.checkbox model schema isRequired isDisabled path
68+
Checkbox ->
69+
Selection.checkbox model schema isRequired isDisabled path
6970

70-
Object properties ->
71-
viewObject model schema properties isRequired isDisabled path
71+
Object properties ->
72+
viewObject model schema properties isRequired isDisabled path
7273

73-
Array ->
74-
viewArray model schema isRequired isDisabled path
74+
Array ->
75+
viewArray model schema isRequired isDisabled path
76+
]
7577

7678

7779
editingMode : Model -> Schema -> EditingMode
@@ -179,7 +181,7 @@ viewArray model schema isRequired isDisabled path =
179181
)
180182
|> div []
181183
, 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" ]
183185
]
184186
]
185187
|> div []
@@ -252,7 +254,7 @@ viewObject model schema properties isRequired isDisabled path =
252254
text ""
253255

254256
else
255-
div [ class "jf-nested-object" ]
257+
div [ class "jf-object" ]
256258
[ if title /= "" then
257259
div
258260
([ classList
@@ -297,6 +299,36 @@ updateSchema schema model =
297299
{ model | schema = schema }
298300

299301

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+
300332
update : Msg -> Model -> ( ( Model, Cmd Msg ), ExternalMsg )
301333
update msg model =
302334
case msg of
@@ -306,7 +338,7 @@ update msg model =
306338
)
307339
|> withExMsg None
308340

309-
AddItem path index ->
341+
AddItem path index schema ->
310342
let
311343
newPropPath =
312344
path ++ [ index |> String.fromInt ]
@@ -325,7 +357,7 @@ update msg model =
325357
|> Result.toMaybe
326358
}
327359
in
328-
editValue updatedModel newPropPath JsonValue.NullValue
360+
editValue updatedModel newPropPath (defaultFor schema)
329361

330362
DeleteProperty path ->
331363
let
@@ -378,16 +410,18 @@ update msg model =
378410
case focused of
379411
Nothing ->
380412
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
391425

392426
else
393427
( { model | beingFocused = touch focused model.focused model.beingFocused, focused = Nothing }
@@ -569,27 +603,27 @@ init config schema v =
569603
( value, errors ) =
570604
case v of
571605
Just something ->
572-
something |> JsonValue.encode |> initValue
606+
something |> JsonValue.encode |> initVal
573607

574608
Nothing ->
575609
case schema of
576610
ObjectSchema os ->
577611
case os.default of
578612
Just def ->
579-
def |> initValue
613+
def |> initVal
580614

581615
Nothing ->
582616
case os.type_ of
583617
SingleType ObjectType ->
584-
Encode.object [] |> initValue
618+
Encode.object [] |> initVal
585619

586620
_ ->
587621
( Nothing, Dict.empty )
588622

589623
_ ->
590624
( Nothing, Dict.empty )
591625

592-
initValue someValue =
626+
initVal someValue =
593627
schema
594628
|> Json.Schema.validateValue { applyDefaults = True } someValue
595629
|> (\res ->

‎src/Json/Form/Definitions.elm‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type Msg
3737
| EditNumber String
3838
| EditJson Path Float String
3939
| EditMultiline Path Float String
40-
| AddItem Path Int
40+
| AddItem Path IntSchema
4141
| ToggleShowPassword
4242
| DeleteProperty Path
4343
| GetViewport Path (Result Browser.Dom.Error Viewport)

‎src/Showcase.elm‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,10 @@ getShowcase ds =
297297

298298
Validation ->
299299
[ buildSchema
300-
|> withType "string"
300+
|> withType "number"
301301
|> withTitle "Name"
302302
|> withDescription "Enter name between 2 and 10 characters"
303-
|> withMaxLength 10
304-
|> withMinLength 2
305-
|> withPattern "^\\D"
303+
|> withMinimum 2
306304
|> makeExample "Single field validation"
307305
, buildSchema
308306
|> withType "string"

‎stylesheets/json-form.css‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
font-size: 16px;
1212
}
1313

14-
.jf-nested-object {
15-
/* padding: var(--nested-object-padding); */
14+
.jf-object {
1615
}
1716

1817
.jf-json-form {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /