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 341afcb

Browse files
committed
Add support for number/int select fields.
1 parent ef1920a commit 341afcb

File tree

3 files changed

+154
-5
lines changed

3 files changed

+154
-5
lines changed

‎example/Main.elm

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,48 @@ schema =
190190
|> withDescription "Enter a natural number between 5.5-10.1 (inclusive)."
191191
|> withMinimum 5.5
192192
|> withMaximum 10.1
193+
, buildSchema
194+
|> withType "integer"
195+
|> withTitle "Month"
196+
|> withOneOf
197+
[ boolSchema False
198+
, buildSchema
199+
|> withConst (int 1)
200+
|> withTitle "Jan"
201+
, buildSchema
202+
|> withConst (int 2)
203+
|> withTitle "Feb"
204+
, buildSchema
205+
|> withConst (int 3)
206+
|> withTitle "Mar"
207+
, buildSchema
208+
|> withConst (int 4)
209+
|> withTitle "Apr"
210+
, buildSchema
211+
|> withConst (int 5)
212+
|> withTitle "May"
213+
, buildSchema
214+
|> withConst (int 6)
215+
|> withTitle "Jun"
216+
, buildSchema
217+
|> withConst (int 7)
218+
|> withTitle "Jul"
219+
, buildSchema
220+
|> withConst (int 8)
221+
|> withTitle "Aug"
222+
, buildSchema
223+
|> withConst (int 9)
224+
|> withTitle "Sep"
225+
, buildSchema
226+
|> withConst (int 10)
227+
|> withTitle "Oct"
228+
, buildSchema
229+
|> withConst (int 11)
230+
|> withTitle "Nov"
231+
, buildSchema
232+
|> withConst (int 12)
233+
|> withTitle "Dec"
234+
]
193235
]
194236
)
195237
, ( "color"

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,18 @@ fieldView : Options -> Path -> SubSchema -> SingleType -> Form -> Html F.Msg
8080
fieldView options path schema type_ form =
8181
case type_ of
8282
IntegerType ->
83-
txt options schema (getFieldAsString path form)
83+
if schema.oneOf /= Nothing || schema.anyOf /= Nothing then
84+
select options schema (getFieldAsString path form)
85+
86+
else
87+
txt options schema (getFieldAsString path form)
8488

8589
NumberType ->
86-
txt options schema (getFieldAsString path form)
90+
if schema.oneOf /= Nothing || schema.anyOf /= Nothing then
91+
select options schema (getFieldAsString path form)
92+
93+
else
94+
txt options schema (getFieldAsString path form)
8795

8896
StringType ->
8997
if schema.oneOf /= Nothing || schema.anyOf /= Nothing then
@@ -534,8 +542,16 @@ fieldPath =
534542

535543
constAsString : SubSchema -> Maybe String
536544
constAsString schema =
545+
let
546+
decoder =
547+
Json.Decode.oneOf
548+
[ Json.Decode.string
549+
, Json.Decode.int |> Json.Decode.map String.fromInt
550+
, Json.Decode.float |> Json.Decode.map String.fromFloat
551+
]
552+
in
537553
schema.const
538-
|> Maybe.map (Json.Decode.decodeValue Json.Decode.string)
554+
|> Maybe.map (Json.Decode.decodeValue decoder)
539555
|> Maybe.andThen Result.toMaybe
540556

541557

‎tests/FieldsTest.elm

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,62 @@ singleTypes =
114114
buildSchema
115115
|> withType "integer"
116116
|> isTextField
117+
, describe "oneOf"
118+
[ test "should be a select" <|
119+
\_ ->
120+
buildSchema
121+
|> withType "integer"
122+
|> isNumberSelect
123+
]
124+
, describe "anyOf"
125+
[ test "should be a select" <|
126+
\_ ->
127+
buildSchema
128+
|> withType "integer"
129+
|> isNumberSelect
130+
]
117131
]
118132
, describe "number"
119133
[ test "should be a text field" <|
120134
\_ ->
121135
buildSchema
122136
|> withType "number"
123137
|> isTextField
138+
, describe "oneOf"
139+
[ test "should be a select" <|
140+
\_ ->
141+
buildSchema
142+
|> withType "number"
143+
|> isNumberSelect
144+
]
145+
, describe "anyOf"
146+
[ test "should be a select" <|
147+
\_ ->
148+
buildSchema
149+
|> withType "number"
150+
|> isNumberSelect
151+
]
124152
]
125153
, describe "string"
126154
[ test "should be a text field" <|
127155
\_ ->
128156
buildSchema
129157
|> withType "string"
130158
|> isTextField
159+
, describe "oneOf"
160+
[ test "should be a select" <|
161+
\_ ->
162+
buildSchema
163+
|> withType "string"
164+
|> isSelect
165+
]
166+
, describe "anyOf"
167+
[ test "should be a select" <|
168+
\_ ->
169+
buildSchema
170+
|> withType "string"
171+
|> isSelect
172+
]
131173
]
132174
, describe "boolean"
133175
[ test "should be a checkbox" <|
@@ -379,10 +421,59 @@ isSelect schema =
379421
>> Query.count (Expect.equal 2)
380422
, Query.findAll [ tag "option" ]
381423
>> Query.index 0
382-
>> Query.has [ text "One" ]
424+
>> Query.has
425+
[ text "One"
426+
, selected False
427+
, attribute
428+
(Html.Attributes.attribute "value" "one")
429+
]
383430
, Query.findAll [ tag "option" ]
384431
>> Query.index 1
385-
>> Query.has [ text "Two" ]
432+
>> Query.has
433+
[ text "Two"
434+
, selected False
435+
, attribute
436+
(Html.Attributes.attribute "value" "two")
437+
]
438+
]
439+
)
440+
]
441+
442+
443+
isNumberSelect schema =
444+
schema
445+
|> withAnyOf
446+
[ buildSchema
447+
|> withTitle "One"
448+
|> withConst (Json.Encode.int 1)
449+
, buildSchema
450+
|> withTitle "Two"
451+
|> withConst (Json.Encode.int 2)
452+
]
453+
|> Expect.all
454+
[ isField
455+
, view
456+
(Expect.all
457+
[ Query.find
458+
[ tag "select"
459+
, classes [ "form-control", "custom-select" ]
460+
]
461+
>> Query.children [ tag "option" ]
462+
>> Query.count (Expect.equal 2)
463+
, Query.findAll [ tag "option" ]
464+
>> Query.index 0
465+
>> Query.has
466+
[ text "One"
467+
, selected False
468+
, attribute (Html.Attributes.attribute "value" "1")
469+
]
470+
, Query.findAll [ tag "option" ]
471+
>> Query.index 1
472+
>> Query.has
473+
[ text "Two"
474+
, selected False
475+
, attribute (Html.Attributes.attribute "value" "2")
476+
]
386477
]
387478
)
388479
]

0 commit comments

Comments
(0)

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