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 8460e2c

Browse files
committed
Add switch control for oneOf/anyOf.
1 parent d2592d2 commit 8460e2c

File tree

4 files changed

+153
-66
lines changed

4 files changed

+153
-66
lines changed

‎elm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
"test-dependencies": {
2424
"elm-explorations/test": "1.2.0 <= v < 2.0.0"
2525
}
26-
}
26+
}

‎src/Json/Schema/Form.elm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ update msg state =
8888
msg
8989
state.form
9090
in
91-
{ state | form = form }
91+
{ state | form = Debug.log "form"form }
9292

9393

9494
{-| The form fields as HTML. Use together with `submit` to submit the form.

‎src/Json/Schema/Form/Fields.elm

Lines changed: 139 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ objectView : Options -> Path -> SubSchema -> Form -> Html F.Msg
6060
objectView options path schema form =
6161
case schema.type_ of
6262
AnyType ->
63-
fieldView options path schema BooleanType form
63+
if schema.oneOf /= Nothing || schema.anyOf /= Nothing then
64+
switch options path schema form
65+
66+
else
67+
fieldView options path schema BooleanType form
6468

6569
NullableType singleType ->
6670
fieldView options path schema singleType form
@@ -98,35 +102,26 @@ fieldView options path schema type_ form =
98102
in
99103
case schema.items of
100104
NoItems ->
101-
field options schema f (list options path form ( schema.title, blankSchema ))
105+
field options schema f <|
106+
list options path form ( schema.title, blankSchema )
102107

103108
ItemDefinition item ->
104-
field options schema f (list options path form ( schema.title, item ))
109+
field options schema f <|
110+
list options path form ( schema.title, item )
105111

106112
ArrayOfItems items ->
107-
field options schema f (tuple options path form items)
113+
field options schema f <|
114+
tuple options path form ( schema.title, items )
108115

109116
ObjectType ->
110-
let
111-
f =
112-
getFieldAsString path form
113-
114-
schemataItem ( name, subSchema ) =
115-
schemaView options (path ++ [ name ]) subSchema form
116-
117-
fields =
118-
case schema.properties of
119-
Nothing ->
120-
[]
117+
if schema.oneOf /= Nothing || schema.anyOf /= Nothing then
118+
switch options path schema form
121119

122-
Just (Json.Schema.Definitions.Schemata schemata) ->
123-
List.map schemataItem schemata
124-
in
125-
group options schema f fields
120+
else
121+
fieldset schema [ group options path schema form ]
126122

127123
NullType ->
128-
fieldset []
129-
[]
124+
div [] []
130125

131126

132127
txt : Options -> SubSchema -> F.FieldState ErrorValue String -> Html F.Msg
@@ -155,7 +150,7 @@ txt options schema f =
155150
field options
156151
schema
157152
f
158-
[ fieldTitle schema
153+
[ fieldTitle schema|>Maybe.withDefault (text "")
159154
, case schema.format of
160155
Just "email" ->
161156
Input.textInput f
@@ -194,12 +189,7 @@ checkbox options schema f =
194189
]
195190
, text (schema.title |> Maybe.withDefault "")
196191
, liveError options.errors f |> Maybe.withDefault (text "")
197-
, case schema.description of
198-
Just str ->
199-
fieldDescription str
200-
201-
Nothing ->
202-
text ""
192+
, fieldDescription schema |> Maybe.withDefault (text "")
203193
]
204194
]
205195

@@ -215,7 +205,7 @@ select options schema f =
215205

216206
items =
217207
schemata
218-
|> List.map option
208+
|> List.map (option constAsString)
219209
|> List.map
220210
(\( name, schema_ ) ->
221211
( name
@@ -227,21 +217,20 @@ select options schema f =
227217

228218
descriptions =
229219
schemata
230-
|> List.map option
220+
|> List.map (option constAsString)
231221
|> List.map
232222
(\( name, schema_ ) ->
233223
( name
234224
, schema_
235-
|> Maybe.andThen .description
236-
|> Maybe.map fieldDescription
225+
|> Maybe.andThen fieldDescription
237226
|> Maybe.withDefault (text "")
238227
)
239228
)
240229
in
241230
field options
242231
schema
243232
f
244-
[ fieldTitle schema
233+
[ fieldTitle schema|>Maybe.withDefault (text "")
245234
, Input.selectInput
246235
items
247236
f
@@ -255,14 +244,14 @@ select options schema f =
255244
]
256245

257246

258-
option : Schema -> ( String, Maybe SubSchema )
259-
option schema =
247+
option : (SubSchema->MaybeString) ->Schema -> ( String, Maybe SubSchema )
248+
option attr schema =
260249
case schema of
261250
BooleanSchema _ ->
262251
( "", Nothing )
263252

264253
ObjectSchema schema_ ->
265-
( constAsString schema_ |> Maybe.withDefault ""
254+
( attr schema_ |> Maybe.withDefault ""
266255
, Just schema_
267256
)
268257

@@ -331,26 +320,89 @@ tuple :
331320
Options
332321
-> Path
333322
-> Form
334-
-> List Schema
323+
-> (MaybeString,List Schema)
335324
-> List (Html F.Msg)
336-
tuple options path form schemata =
325+
tuple options path form ( title,schemata) =
337326
let
338327
itemPath idx =
339328
path ++ [ "tuple" ++ String.fromInt idx ]
340329

341-
itemView idx schema =
330+
itemView idx itemSchema =
342331
div
343332
[ class "col" ]
344-
[ schemaView options (itemPath idx) schema form ]
333+
[ schemaView options (itemPath idx) itemSchema form ]
334+
in
335+
[ case title of
336+
Just str ->
337+
div [ class "field-title" ] [ text str ]
338+
339+
Nothing ->
340+
text ""
341+
, div [ class "form-row" ] (List.indexedMap itemView schemata)
342+
]
343+
344+
345+
radio : F.FieldState ErrorValue String -> ( String, String ) -> Html F.Msg
346+
radio fieldState ( value, title ) =
347+
label [ class "form-check-label" ]
348+
[ Input.radioInput value
349+
fieldState
350+
[ class "form-check-input"
351+
, id fieldState.path
352+
]
353+
, text title
354+
]
355+
356+
357+
switch : Options -> Path -> SubSchema -> Form -> Html F.Msg
358+
switch options path schema form =
359+
let
360+
f =
361+
getFieldAsString (path ++ [ "switch" ]) form
362+
363+
schemata =
364+
List.concat
365+
[ schema.oneOf |> Maybe.withDefault []
366+
, schema.anyOf |> Maybe.withDefault []
367+
]
368+
369+
items =
370+
schemata
371+
|> List.map (option .title)
372+
373+
itemId idx =
374+
"option" ++ String.fromInt idx
375+
376+
itemButton idx ( title, schema_ ) =
377+
div
378+
[ classList
379+
[ ( "form-check", True )
380+
, ( "form-check-inline", List.length items <= 2 )
381+
]
382+
]
383+
[ radio f ( itemId idx, title ) ]
384+
385+
itemFields idx ( title, schema_ ) =
386+
case schema_ of
387+
Just s ->
388+
( itemId idx, group options path s form )
389+
390+
Nothing ->
391+
( itemId idx, text "" )
345392
in
346-
[ div [ class "form-row" ] (List.indexedMap itemView schemata) ]
393+
field options schema f <|
394+
[ fieldTitle schema |> Maybe.withDefault (text "")
395+
, div [ class "form-group", id f.path, tabindex -1 ]
396+
(List.indexedMap itemButton items)
397+
, conditional f (List.indexedMap itemFields items)
398+
]
347399

348400

349401
field : Options -> SubSchema -> F.FieldState ErrorValue String -> List (Html F.Msg) -> Html F.Msg
350402
field options schema f content =
351403
let
352404
meta =
353-
[ Maybe.map fieldDescription schema.description ]
405+
[ fieldDescription schema ]
354406
|> List.filterMap identity
355407

356408
feedback =
@@ -369,12 +421,25 @@ field options schema f content =
369421
]
370422

371423

372-
group : Options -> SubSchema -> F.FieldStateErrorValueString-> List (HtmlF.Msg) -> Html F.Msg
373-
group options schema f content =
424+
group : Options -> Path -> SubSchema-> Form -> Html F.Msg
425+
group options path schema form =
374426
let
427+
f =
428+
getFieldAsString path form
429+
430+
schemataItem ( name, subSchema ) =
431+
schemaView options (path ++ [ name ]) subSchema form
432+
433+
fields =
434+
case schema.properties of
435+
Nothing ->
436+
[]
437+
438+
Just (Json.Schema.Definitions.Schemata schemata) ->
439+
List.map schemataItem schemata
440+
375441
meta =
376-
[ Maybe.map (\str -> legend [] [ text str ]) schema.title
377-
, Maybe.map (\str -> p [] [ text str ]) schema.description
442+
[ Maybe.map (\str -> p [] [ text str ]) schema.description
378443
]
379444
|> List.filterMap identity
380445

@@ -383,27 +448,26 @@ group options schema f content =
383448
]
384449
|> List.filterMap identity
385450
in
386-
fieldset
387-
[ name f.path
388-
, id f.path
389-
, tabindex -1
451+
div
452+
[ classList
453+
[ ( "form-group", True )
454+
, ( "is-invalid", f.liveError /= Nothing )
455+
, ( "has-value", f.value /= Nothing && f.value /= Just "" )
456+
]
390457
]
391-
(meta ++ content ++ feedback)
458+
(meta ++ fields ++ feedback)
392459

393460

394-
fieldTitle : SubSchema -> Html F.Msg
461+
fieldTitle : SubSchema -> Maybe (Html F.Msg)
395462
fieldTitle schema =
396-
case schema.title of
397-
Just str ->
398-
span [ class "label-text" ] [ text str ]
399-
400-
Nothing ->
401-
text ""
463+
schema.title
464+
|> Maybe.map (\str -> span [ class "label-text" ] [ text str ])
402465

403466

404-
fieldDescription : String -> Html F.Msg
405-
fieldDescription str =
406-
div [ class "form-text text-muted" ] [ text str ]
467+
fieldDescription : SubSchema -> Maybe (Html F.Msg)
468+
fieldDescription schema =
469+
schema.description
470+
|> Maybe.map (\str -> div [ class "form-text text-muted" ] [ text str ])
407471

408472

409473
liveError : Errors -> F.FieldState ErrorValue a -> Maybe (Html F.Msg)
@@ -434,6 +498,20 @@ inputGroup title content =
434498
)
435499

436500

501+
fieldset : SubSchema -> List (Html F.Msg) -> Html F.Msg
502+
fieldset schema content =
503+
let
504+
title =
505+
case schema.title of
506+
Just str ->
507+
[ legend [] [ text str ] ]
508+
509+
Nothing ->
510+
[]
511+
in
512+
Html.fieldset [ tabindex -1 ] (title ++ content)
513+
514+
437515
getFieldAsBool : Path -> F.Form e o -> F.FieldState e Bool
438516
getFieldAsBool path =
439517
F.getFieldAsBool (fieldPath path)

‎src/Json/Schema/Form/Validation.elm

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Dict
44
import Form.Error exposing (ErrorValue(..))
55
import Form.Field exposing (Field)
66
import Form.Validate exposing (..)
7+
import Json.Decode
78
import Json.Encode
89
import Json.Schema.Definitions
910
exposing
@@ -202,12 +203,20 @@ constFloat constValue value =
202203

203204

204205
constString : Json.Encode.Value -> String -> Validation ErrorValue String
205-
constString constValue value =
206+
constString constValue value field =
206207
if Json.Encode.string value == constValue then
207-
succeed value
208+
succeed value field
209+
210+
else if field == Form.Field.value Form.Field.EmptyField then
211+
case Json.Decode.decodeValue Json.Decode.string constValue of
212+
Ok str ->
213+
succeed str field
214+
215+
Err error ->
216+
fail (Form.Error.value InvalidString) field
208217

209218
else
210-
fail (Form.Error.value InvalidString)
219+
fail (Form.Error.value InvalidString) field
211220

212221

213222
constBool : Json.Encode.Value -> Bool -> Validation ErrorValue Bool

0 commit comments

Comments
(0)

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