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 33fd131

Browse files
committed
Change to use oneOf instead of enum for select boxes.
1 parent ba44db1 commit 33fd131

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

‎example/Main.elm‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,29 @@ init =
5757
, string "blue"
5858
]
5959
)
60+
, ( "airport"
61+
, buildSchema
62+
|> withTitle "Select an airport"
63+
|> withNullableType "string"
64+
|> withDefault (string "ARN")
65+
|> withOneOf
66+
[ buildSchema
67+
|> withTitle "Stockholm Arlanda"
68+
|> withConst (string "ARN")
69+
, buildSchema
70+
|> withTitle "London Heathrow"
71+
|> withConst (string "LHR")
72+
, buildSchema
73+
|> withTitle "Dubai International Airport"
74+
|> withConst (string "DXB")
75+
, buildSchema
76+
|> withTitle "Paris Charles de Gaulle"
77+
|> withConst (string "CDG")
78+
, buildSchema
79+
|> withTitle "Other"
80+
|> withDescription "Enter airport IATA code"
81+
]
82+
)
6083
, ( "contact"
6184
, buildSchema
6285
|> withType "object"

‎src/Schema/Form.elm‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fieldView path schema type_ form =
7474
txt schema (getFieldAsString path form)
7575

7676
StringType ->
77-
case schema.enum of
77+
case schema.oneOf of
7878
Just _ ->
7979
select schema (getFieldAsString path form)
8080

@@ -159,12 +159,9 @@ select : SubSchema -> F.FieldState ValidationError String -> Html F.Msg
159159
select schema f =
160160
let
161161
options =
162-
case schema.enum of
162+
case schema.oneOf of
163163
Just values ->
164-
values
165-
|> List.map (Json.Decode.decodeValue Json.Decode.string)
166-
|> List.map (Result.withDefault "")
167-
|> List.map (\str -> ( str, str ))
164+
List.map option values
168165

169166
Nothing ->
170167
[]
@@ -177,6 +174,21 @@ select schema f =
177174
]
178175

179176

177+
option : Schema -> ( String, String )
178+
option schema =
179+
case schema of
180+
BooleanSchema _ ->
181+
( "", "" )
182+
183+
ObjectSchema schema_ ->
184+
( schema_.const
185+
|> Maybe.map (Json.Decode.decodeValue Json.Decode.string)
186+
|> Maybe.map (Result.withDefault "")
187+
|> Maybe.withDefault ""
188+
, schema_.title |> Maybe.withDefault ""
189+
)
190+
191+
180192
field : SubSchema -> F.FieldState ValidationError String -> Html F.Msg -> Html F.Msg
181193
field schema f content =
182194
fieldset

0 commit comments

Comments
(0)

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