From d2cfd83801e1ec236ea71e915a800fdb431a1229 Mon Sep 17 00:00:00 2001 From: Niklas Holmgren Date: Thu, 3 Jan 2019 17:28:35 +0100 Subject: [PATCH] Add conditional descriptions on select boxes. --- example/Main.elm | 2 ++ src/Schema/Form.elm | 86 +++++++++++++++++++++++++++++++-------------- 2 files changed, 62 insertions(+), 26 deletions(-) diff --git a/example/Main.elm b/example/Main.elm index 6f1a70f..5613df7 100644 --- a/example/Main.elm +++ b/example/Main.elm @@ -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") ] ) diff --git a/src/Schema/Form.elm b/src/Schema/Form.elm index 83c23bb..7b09582 100644 --- a/src/Schema/Form.elm +++ b/src/Schema/Form.elm @@ -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 @@ -16,6 +17,7 @@ import Json.Schema.Definitions , SingleType(..) , SubSchema , Type(..) + , blankSchema ) import Schema.Error exposing (ValidationError, errorString) import Schema.Validation exposing (validation) @@ -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 )) @@ -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 "" @@ -175,21 +177,42 @@ 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 ) @@ -197,21 +220,20 @@ select schema f = ] , 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 : @@ -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 @@ -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 @@ -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