Skip to content

Commit b6f1807

Browse files
committed
Introduce checkbox
1 parent 49f9f6d commit b6f1807

File tree

1 file changed

+67
-2
lines changed

1 file changed

+67
-2
lines changed

src/Json/Form.elm

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Json.Schema.Definitions exposing (..)
1515
import Json.Schema
1616
import Json.Schema.Validation exposing (Error)
1717
import JsonValue exposing (JsonValue(..))
18-
import Json.Decode exposing (decodeValue)
18+
import Json.Decode as Decode exposing (decodeValue)
1919
import ErrorMessages exposing (stringifyError)
2020
import Dict exposing (Dict)
2121

@@ -36,6 +36,7 @@ type EditingMode
3636
= TextField
3737
| NumberField
3838
| Switch
39+
| Checkbox
3940
| JsonEditor
4041
| Object
4142

@@ -82,6 +83,9 @@ viewNode model schema path =
8283
Switch ->
8384
viewSwitch model schema path
8485

86+
Checkbox ->
87+
viewCheckbox model schema path
88+
8589
Object ->
8690
viewObject model schema path
8791

@@ -101,7 +105,7 @@ editingMode model schema =
101105
TextField
102106

103107
SingleType BooleanType ->
104-
Switch
108+
getBooleanUiWidget schema
105109

106110
SingleType ObjectType ->
107111
Object
@@ -113,6 +117,28 @@ editingMode model schema =
113117
JsonEditor
114118

115119

120+
getBooleanUiWidget : Schema -> EditingMode
121+
getBooleanUiWidget schema =
122+
schema
123+
|> getCustomKeywordValue "ui"
124+
|> Maybe.andThen
125+
(\settings ->
126+
settings
127+
|> Decode.decodeValue
128+
(Decode.field "booleanWidget" Decode.string
129+
|> Decode.map
130+
(\widget ->
131+
if widget == "switch" then
132+
Switch
133+
else
134+
Checkbox
135+
)
136+
)
137+
|> Result.toMaybe
138+
)
139+
|> Maybe.withDefault Checkbox
140+
141+
116142
viewSwitch : Model -> Schema -> Path -> Html Msg
117143
viewSwitch model schema path =
118144
let
@@ -151,6 +177,45 @@ viewSwitch model schema path =
151177
]
152178

153179

180+
viewCheckbox : Model -> Schema -> Path -> Html Msg
181+
viewCheckbox model schema path =
182+
let
183+
isChecked =
184+
case model.value |> Maybe.andThen (JsonValue.getIn path >> Result.toMaybe) of
185+
Just (BoolValue x) ->
186+
x
187+
188+
_ ->
189+
False
190+
191+
( hasError, helperText ) =
192+
renderHelper model schema path
193+
in
194+
label
195+
[ classList
196+
[ ( "jf-checkbox", True )
197+
, ( "jf-checkbox--on", isChecked )
198+
, ( "jf-checkbox--focused", model.focused |> Maybe.map ((==) path) |> Maybe.withDefault False )
199+
, ( "jf-checkbox--invalid", hasError )
200+
]
201+
]
202+
[ input
203+
[ type_ "checkbox"
204+
, class "jf-checkbox__input"
205+
, checked isChecked
206+
, onFocus <| FocusInput (Just path)
207+
, onBlur <| FocusInput Nothing
208+
, onCheck <| (JsonValue.BoolValue >> EditValue path)
209+
]
210+
[]
211+
, span [ class "jf-checkbox__label" ] [ schema |> getTitle |> text ]
212+
, div [ class "jf-checkbox__box-outline" ]
213+
[ div [ class "jf-checkbox__tick-outline" ] []
214+
]
215+
, div [ class "jf-checkbox__helper-text" ] [ helperText ]
216+
]
217+
218+
154219
jsonValueToString : JsonValue -> String
155220
jsonValueToString jv =
156221
case jv of

0 commit comments

Comments
 (0)