From fff5a4c62145f130d2edf33b4c4b5b265c27db13 Mon Sep 17 00:00:00 2001 From: Tessa Kelly Date: Thu, 4 Nov 2021 10:44:47 -0700 Subject: [PATCH] :art: add value helper replacing current value config --- src/Nri/Ui/Select/V8.elm | 32 ++++++++++++++++++++---------- styleguide-app/Examples/Select.elm | 17 +++++++++------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/Nri/Ui/Select/V8.elm b/src/Nri/Ui/Select/V8.elm index ac64db91..a5787431 100644 --- a/src/Nri/Ui/Select/V8.elm +++ b/src/Nri/Ui/Select/V8.elm @@ -1,5 +1,6 @@ module Nri.Ui.Select.V8 exposing ( view, generateId + , value , Attribute , id , errorIf, errorMessage @@ -22,6 +23,8 @@ module Nri.Ui.Select.V8 exposing ### Input content +@docs value + ### Input handlers @@ -58,17 +61,23 @@ we'll automatically generate one from the label you pass in, but this can cause problems if you have more than one text input with the same label on the page. Use this to be more specific and avoid issues with duplicate IDs! -} -id : String -> Attribute +id : String -> Attribute value id id_ = Attribute (\config -> { config | id = Just id_ }) +{-| -} +value : Maybe value -> Attribute value +value value_ = + Attribute (\config -> { config | value = value_ }) + + {-| Sets whether or not the field will be highlighted as having a validation error. If you have an error message to display, use `errorMessage` instead. -} -errorIf : Bool -> Attribute +errorIf : Bool -> Attribute value errorIf = Attribute << InputErrorInternal.setErrorIf @@ -76,33 +85,35 @@ errorIf = {-| If `Just`, the field will be highlighted as having a validation error, and the given error message will be shown. -} -errorMessage : Maybe String -> Attribute +errorMessage : Maybe String -> Attribute value errorMessage = Attribute << InputErrorInternal.setErrorMessage {-| Customizations for the Select. -} -type Attribute - = Attribute (Config -> Config) +type Attribute value + = Attribute (Config value -> Config value) -applyConfig : List Attribute -> Config +applyConfig : List (Attribute value) -> Config value applyConfig attributes = List.foldl (\(Attribute update) config -> update config) defaultConfig attributes -type alias Config = +type alias Config value = { id : Maybe String + , value : Maybe value , error : ErrorState } -defaultConfig : Config +defaultConfig : Config value defaultConfig = { id = Nothing + , value = Nothing , error = InputErrorInternal.init } @@ -119,11 +130,10 @@ view : String -> { choices : List (Choice a) - , current : Maybe a , valueToString : a -> String , defaultDisplayText : Maybe String } - -> List Attribute + -> List (Attribute a) -> Html a view label config attributes = let @@ -152,7 +162,7 @@ view label config attributes = [ Html.text label ] , viewSelect { choices = config.choices - , current = config.current + , current = config_.value , id = id_ , valueToString = config.valueToString , defaultDisplayText = config.defaultDisplayText diff --git a/styleguide-app/Examples/Select.elm b/styleguide-app/Examples/Select.elm index 95093f48..2a5f526e 100644 --- a/styleguide-app/Examples/Select.elm +++ b/styleguide-app/Examples/Select.elm @@ -31,14 +31,14 @@ example = , view = \state -> let + attributes : Settings attributes = Control.currentValue state.attributes in [ Control.view UpdateAttributes state.attributes |> Html.Styled.fromUnstyled , Select.view "Tortilla Selector" - { current = Nothing - , choices = + { choices = [ { label = "Tacos", value = "Tacos" } , { label = "Burritos", value = "Burritos" } , { label = "Enchiladas", value = "Enchiladas" } @@ -51,8 +51,7 @@ example = , Html.Styled.div [ Html.Styled.Attributes.css [ Css.maxWidth (Css.px 400) ] ] [ Select.view "Selector with Overflowed Text" - { current = Nothing - , choices = [] + { choices = [] , valueToString = identity , defaultDisplayText = Just "Look at me, I design coastlines, I got an award for Norway. Where's the sense in that?" } @@ -65,7 +64,7 @@ example = {-| -} type alias State = - { attributes : Control (List Select.Attribute) + { attributes : Control Settings } @@ -76,7 +75,11 @@ init = } -initControls : Control (List Select.Attribute) +type alias Settings = + List (Select.Attribute String) + + +initControls : Control Settings initControls = ControlExtra.list |> ControlExtra.optionalListItem "errorIf" @@ -88,7 +91,7 @@ initControls = {-| -} type Msg = ConsoleLog String - | UpdateAttributes (Control (List Select.Attribute)) + | UpdateAttributes (Control Settings) {-| -}