@@ -9,8 +9,7 @@ module Json.Form
9
9
)
10
10
11
11
import Html exposing (..)
12
- import Html.Attributes exposing (..)
13
- import Html.Events exposing (..)
12
+ import Json.Form.UiSpec as UiSpec
14
13
import Json.Form.Definitions as Definitions exposing (Path , EditingMode (..) , Msg (..) )
15
14
import Json.Schema.Definitions exposing (..)
16
15
import Json.Schema
@@ -20,7 +19,7 @@ import Json.Decode as Decode exposing (decodeValue)
20
19
import ErrorMessages exposing (stringifyError )
21
20
import Dict exposing (Dict )
22
21
import Json.Form.TextField as TextField
23
- import Json.Form.Helper as Helper
22
+ import Json.Form.Selection as Selection
24
23
import Util exposing (..)
25
24
26
25
@@ -44,26 +43,26 @@ init =
44
43
45
44
view : Model -> Html Msg
46
45
view model =
47
- viewNode model model. schema []
46
+ viewNode model model. schema False []
48
47
49
48
50
- viewNode : Model -> Schema -> Path -> Html Msg
51
- viewNode model schema path =
49
+ viewNode : Model -> Schema -> Bool -> Path -> Html Msg
50
+ viewNode model schema isRequired path =
52
51
case editingMode model schema of
53
52
TextField ->
54
- TextField . view model schema path
53
+ TextField . view model schema isRequired path
55
54
56
55
NumberField ->
57
- viewNumericTextField model schema path
56
+ TextField . viewNumeric model schema isRequired path
58
57
59
58
Switch ->
60
- viewSwitch model schema path
59
+ Selection . switch model schema isRequired path
61
60
62
61
Checkbox ->
63
- viewCheckbox model schema path
62
+ Selection . checkbox model schema isRequired path
64
63
65
64
Object ->
66
- viewObject model schema path
65
+ viewObject model schema isRequired path
67
66
68
67
_ ->
69
68
text " Not implemented"
@@ -95,154 +94,22 @@ editingMode model schema =
95
94
96
95
getBooleanUiWidget : Schema -> EditingMode
97
96
getBooleanUiWidget schema =
98
- schema
99
- |> getCustomKeywordValue " ui"
100
- |> Maybe . andThen
101
- ( \ settings ->
102
- settings
103
- |> Decode . decodeValue
104
- ( Decode . field " widget" Decode . string
105
- |> Decode . map
106
- ( \ widget ->
107
- if widget == " switch" then
108
- Switch
109
- else
110
- Checkbox
111
- )
112
- )
113
- |> Result . toMaybe
114
- )
115
- |> Maybe . withDefault Checkbox
116
-
117
-
118
- viewSwitch : Model -> Schema -> Path -> Html Msg
119
- viewSwitch model schema path =
120
- let
121
- isChecked =
122
- case model. value |> Maybe . andThen ( JsonValue . getIn path >> Result . toMaybe) of
123
- Just ( BoolValue x) ->
124
- x
125
-
126
- _ ->
127
- False
97
+ case schema |> getUiSpec of
98
+ UiSpec . Switch ->
99
+ Switch
128
100
129
- ( hasError, helperText ) =
130
- Helper . view model schema path
131
- in
132
- label
133
- [ classList
134
- [ ( " jf-switch" , True )
135
- , ( " jf-switch--on" , isChecked )
136
- , ( " jf-switch--focused" , model. focused |> Maybe . map ( (==) path) |> Maybe . withDefault False )
137
- , ( " jf-switch--invalid" , hasError )
138
- ]
139
- ]
140
- [ input
141
- [ type_ " checkbox"
142
- , class " jf-switch__input"
143
- , checked isChecked
144
- , onFocus <| FocusInput ( Just path)
145
- , onBlur <| FocusInput Nothing
146
- , onCheck <| ( JsonValue . BoolValue >> EditValue path)
147
- ]
148
- []
149
- , span [ class " jf-switch__label" ] [ schema |> getTitle |> text ]
150
- , div [ class " jf-switch__track" ] []
151
- , div [ class " jf-switch__thumb" ] []
152
- , div [ class " jf-switch__helper-text" ] [ helperText ]
153
- ]
154
-
155
-
156
- viewCheckbox : Model -> Schema -> Path -> Html Msg
157
- viewCheckbox model schema path =
158
- let
159
- isChecked =
160
- case model. value |> Maybe . andThen ( JsonValue . getIn path >> Result . toMaybe) of
161
- Just ( BoolValue x) ->
162
- x
101
+ _ ->
102
+ Checkbox
163
103
164
- _ ->
165
- False
166
104
167
- ( hasError, helperText ) =
168
- Helper . view model schema path
169
- in
170
- label
171
- [ classList
172
- [ ( " jf-checkbox" , True )
173
- , ( " jf-checkbox--on" , isChecked )
174
- , ( " jf-checkbox--focused" , model. focused |> Maybe . map ( (==) path) |> Maybe . withDefault False )
175
- , ( " jf-checkbox--invalid" , hasError )
176
- ]
177
- ]
178
- [ input
179
- [ type_ " checkbox"
180
- , class " jf-checkbox__input"
181
- , checked isChecked
182
- , onFocus <| FocusInput ( Just path)
183
- , onBlur <| FocusInput Nothing
184
- , onCheck <| ( JsonValue . BoolValue >> EditValue path)
185
- ]
186
- []
187
- , span [ class " jf-checkbox__label" ] [ schema |> getTitle |> text ]
188
- , div [ class " jf-checkbox__box-outline" ]
189
- [ div [ class " jf-checkbox__tick-outline" ] []
190
- ]
191
- , div [ class " jf-checkbox__helper-text" ] [ helperText ]
192
- ]
193
-
194
-
195
- viewNumericTextField : Model -> Schema -> Path -> Html Msg
196
- viewNumericTextField model schema path =
197
- let
198
- isFocused =
199
- model. focused
200
- |> Maybe . map ( (==) path)
201
- |> Maybe . withDefault False
202
-
203
- editedValue =
204
- if isFocused then
205
- model. editedNumber
206
- else
207
- model. value
208
- |> Maybe . map ( JsonValue . getIn path)
209
- |> Maybe . andThen Result . toMaybe
210
- |> Maybe . map Util . jsonValueToString
211
- |> Maybe . withDefault " "
212
-
213
- ( hasError, helperText ) =
214
- Helper . view model schema path
215
- in
216
- div
217
- [ classList
218
- [ ( " jf-textfield" , True )
219
- , ( " jf-textfield--focused" , isFocused )
220
- , ( " jf-textfield--empty" , editedValue == " " )
221
- , ( " jf-textfield--invalid" , hasError )
222
- ]
223
- ]
224
- [ input
225
- [ class " jf-textfield__input"
226
- , onFocus <| FocusNumericInput ( Just path)
227
- , onBlur <| FocusNumericInput Nothing
228
- , onInput <| EditNumber
229
- , value <| editedValue
230
- , type_ " number"
231
- ]
232
- []
233
- , label [ class " jf-textfield__label" ] [ schema |> getTitle |> text ]
234
- , div [ class " jf-textfield__helper-text" ] [ helperText ]
235
- ]
236
-
237
-
238
- viewObject : Model -> Schema -> Path -> Html Msg
239
- viewObject model schema path =
105
+ viewObject : Model -> Schema -> Bool -> Path -> Html Msg
106
+ viewObject model schema isRequired path =
240
107
let
241
108
iterateOverSchemata propsDict required ( Schemata schemata) =
242
109
schemata
243
110
|> List . map
244
111
( \ ( propName, subSchema ) ->
245
- viewNode model subSchema ( path ++ [ propName ] )
112
+ viewNode model subSchema ( required |> Maybe . withDefault [] |> List . member propName ) ( path ++ [ propName ] )
246
113
)
247
114
in
248
115
case schema of
@@ -373,8 +240,3 @@ dictFromListErrors list =
373
240
)
374
241
)
375
242
Dict . empty
376
-
377
-
378
- (=>) : a -> b -> ( a , b )
379
- (=>) a b =
380
- ( a, b )
0 commit comments