🎨 add value helper replacing current value config

This commit is contained in:
Tessa Kelly 2021-11-04 10:44:47 -07:00
parent 8f70b6d6f0
commit fff5a4c621
2 changed files with 31 additions and 18 deletions

View File

@ -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

View File

@ -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)
{-| -}