@@ -15,7 +15,7 @@ import Json.Schema.Definitions exposing (..)
15
15
import Json.Schema
16
16
import Json.Schema.Validation exposing (Error )
17
17
import JsonValue exposing (JsonValue (..) )
18
- import Json.Decode exposing (decodeValue )
18
+ import Json.Decode as Decode exposing (decodeValue )
19
19
import ErrorMessages exposing (stringifyError )
20
20
import Dict exposing (Dict )
21
21
@@ -36,6 +36,7 @@ type EditingMode
36
36
= TextField
37
37
| NumberField
38
38
| Switch
39
+ | Checkbox
39
40
| JsonEditor
40
41
| Object
41
42
@@ -82,6 +83,9 @@ viewNode model schema path =
82
83
Switch ->
83
84
viewSwitch model schema path
84
85
86
+ Checkbox ->
87
+ viewCheckbox model schema path
88
+
85
89
Object ->
86
90
viewObject model schema path
87
91
@@ -101,7 +105,7 @@ editingMode model schema =
101
105
TextField
102
106
103
107
SingleType BooleanType ->
104
- Switch
108
+ getBooleanUiWidget schema
105
109
106
110
SingleType ObjectType ->
107
111
Object
@@ -113,6 +117,28 @@ editingMode model schema =
113
117
JsonEditor
114
118
115
119
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
+
116
142
viewSwitch : Model -> Schema -> Path -> Html Msg
117
143
viewSwitch model schema path =
118
144
let
@@ -151,6 +177,45 @@ viewSwitch model schema path =
151
177
]
152
178
153
179
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
+
154
219
jsonValueToString : JsonValue -> String
155
220
jsonValueToString jv =
156
221
case jv of
0 commit comments