Skip to content

Commit

Permalink
Add conditional descriptions on select boxes.
Browse files Browse the repository at this point in the history
  • Loading branch information
niho committed Jan 3, 2019
1 parent 8ebadd9 commit d2cfd83
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 26 deletions.
2 changes: 2 additions & 0 deletions example/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ schema =
, buildSchema
|> withTitle "London Heathrow"
|> withConst (string "LHR")
|> withDescription "Heathrow Airport is a major international airport in London, United Kingdom."
, buildSchema
|> withTitle "Dubai International Airport"
|> withConst (string "DXB")
, buildSchema
|> withTitle "Paris Charles de Gaulle"
|> withDescription "Paris Charles de Gaulle Airport is the largest international airport in France and the second largest in Europe."
|> withConst (string "CDG")
]
)
Expand Down
86 changes: 60 additions & 26 deletions src/Schema/Form.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Form.Validate exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Html.Keyed
import Json.Decode
import Json.Schema
import Json.Schema.Definitions
Expand All @@ -16,6 +17,7 @@ import Json.Schema.Definitions
, SingleType(..)
, SubSchema
, Type(..)
, blankSchema
)
import Schema.Error exposing (ValidationError, errorString)
import Schema.Validation exposing (validation)
Expand Down Expand Up @@ -92,7 +94,7 @@ fieldView path schema type_ form =
in
case schema.items of
NoItems ->
group schema f []
field schema f (list path form ( schema.title, blankSchema ))

ItemDefinition item ->
field schema f (list path form ( schema.title, item ))
Expand Down Expand Up @@ -164,7 +166,7 @@ checkbox schema f =
text ""
, case schema.description of
Just str ->
div [ class "form-text text-muted" ] [ text str ]
fieldDescription str

Nothing ->
text ""
Expand All @@ -175,43 +177,63 @@ checkbox schema f =
select : SubSchema -> F.FieldState ValidationError String -> Html F.Msg
select schema f =
let
options schemata =
case schemata of
Just values ->
List.map option values
schemata =
List.concat
[ schema.oneOf |> Maybe.withDefault []
, schema.anyOf |> Maybe.withDefault []
]

Nothing ->
[]
options =
schemata
|> List.map option
|> List.map
(\( name, schema_ ) ->
( name
, schema_
|> Maybe.andThen .title
|> Maybe.withDefault name
)
)

descriptions =
schemata
|> List.map option
|> List.map
(\( name, schema_ ) ->
( name
, schema_
|> Maybe.andThen .description
|> Maybe.map fieldDescription
|> Maybe.withDefault (text "")
)
)
in
field schema
f
[ fieldTitle schema
, Input.selectInput
(options schema.oneOf
++ options schema.anyOf
)
options
f
[ classList
[ ( "form-control custom-select", True )
, ( "is-invalid", f.liveError /= Nothing )
]
, id f.path
]
, conditional f descriptions
]


option : Schema -> ( String, String )
option : Schema -> ( String, Maybe SubSchema )
option schema =
case schema of
BooleanSchema _ ->
( "", "" )
( "", Nothing )

ObjectSchema schema_ ->
let
value =
constAsString schema_ |> Maybe.withDefault ""
in
( value, schema_.title |> Maybe.withDefault value )
( constAsString schema_ |> Maybe.withDefault ""
, Just schema_
)


set :
Expand Down Expand Up @@ -294,17 +316,11 @@ field : SubSchema -> F.FieldState ValidationError String -> List (Html F.Msg) ->
field schema f content =
let
meta =
[ Maybe.map
(\str ->
div [ class "form-text text-muted" ] [ text str ]
)
schema.description
]
[ Maybe.map fieldDescription schema.description ]
|> List.filterMap identity

feedback =
[ Maybe.map (error errorString) f.liveError
]
[ Maybe.map (error errorString) f.liveError ]
|> List.filterMap identity
in
div
Expand Down Expand Up @@ -351,6 +367,11 @@ fieldTitle schema =
text ""


fieldDescription : String -> Html F.Msg
fieldDescription str =
div [ class "form-text text-muted" ] [ text str ]


error : ErrorString ValidationError -> ErrorValue ValidationError -> Html F.Msg
error func err =
div
Expand Down Expand Up @@ -398,3 +419,16 @@ onClickPreventDefault msg =
alwaysPreventDefault : msg -> ( msg, Bool )
alwaysPreventDefault msg =
( msg, True )


conditional : F.FieldState e String -> List ( String, Html F.Msg ) -> Html F.Msg
conditional f conditions =
let
cond ( value, html ) =
if f.value == Just value then
Just ( value, html )

else
Nothing
in
Html.Keyed.node "div" [] <| List.filterMap cond conditions

0 comments on commit d2cfd83

Please sign in to comment.