Combine events and values and config, since we're not taking advantage of having both fields anymore

This commit is contained in:
Tessa Kelly 2021-11-22 16:07:58 -08:00
parent 8d96764996
commit 4f5378a7f2

View File

@ -54,23 +54,21 @@ import Svg.Styled.Attributes as SvgAttributes
-} -}
disabled : Attribute value msg disabled : Attribute value msg
disabled = disabled =
Attribute emptyEventsAndValues <| Attribute <| \config -> { config | isDisabled = True }
\config -> { config | isDisabled = True }
{-| This enables the input, this is the default behavior {-| This enables the input, this is the default behavior
-} -}
enabled : Attribute value msg enabled : Attribute value msg
enabled = enabled =
Attribute emptyEventsAndValues <| Attribute <| \config -> { config | isDisabled = False }
\config -> { config | isDisabled = False }
{-| Fire a message parameterized by the value type when selecting a radio option {-| Fire a message parameterized by the value type when selecting a radio option
-} -}
onSelect : (value -> msg) -> Attribute value msg onSelect : (value -> msg) -> Attribute value msg
onSelect onSelect_ = onSelect onSelect_ =
Attribute { emptyEventsAndValues | onSelect = Just onSelect_ } identity Attribute <| \config -> { config | onSelect = Just onSelect_ }
{-| Lock Premium content if the user does not have Premium. {-| Lock Premium content if the user does not have Premium.
@ -81,7 +79,7 @@ premium :
} }
-> Attribute value msg -> Attribute value msg
premium { teacherPremiumLevel, contentPremiumLevel } = premium { teacherPremiumLevel, contentPremiumLevel } =
Attribute emptyEventsAndValues <| Attribute <|
\config -> \config ->
{ config { config
| teacherPremiumLevel = Just teacherPremiumLevel | teacherPremiumLevel = Just teacherPremiumLevel
@ -98,38 +96,35 @@ For RadioButton.V4, consider removing `showPennant` from the API.
-} -}
showPennant : msg -> Attribute value msg showPennant : msg -> Attribute value msg
showPennant premiumMsg = showPennant premiumMsg =
Attribute { emptyEventsAndValues | premiumMsg = Just premiumMsg } identity Attribute <| \config -> { config | premiumMsg = Just premiumMsg }
{-| Content that shows when this RadioButton is selected {-| Content that shows when this RadioButton is selected
-} -}
disclosure : List (Html msg) -> Attribute value msg disclosure : List (Html msg) -> Attribute value msg
disclosure childNodes = disclosure childNodes =
Attribute { emptyEventsAndValues | disclosedContent = childNodes } identity Attribute <| \config -> { config | disclosedContent = childNodes }
{-| Adds CSS to the element containing the input. {-| Adds CSS to the element containing the input.
-} -}
containerCss : List Css.Style -> Attribute value msg containerCss : List Css.Style -> Attribute value msg
containerCss styles = containerCss styles =
Attribute emptyEventsAndValues <| Attribute <| \config -> { config | containerCss = config.containerCss ++ styles }
\config -> { config | containerCss = config.containerCss ++ styles }
{-| Hides the visible label. (There will still be an invisible label for screen readers.) {-| Hides the visible label. (There will still be an invisible label for screen readers.)
-} -}
hiddenLabel : Attribute value msg hiddenLabel : Attribute value msg
hiddenLabel = hiddenLabel =
Attribute emptyEventsAndValues <| Attribute <| \config -> { config | hideLabel = True }
\config -> { config | hideLabel = True }
{-| Shows the visible label. This is the default behavior {-| Shows the visible label. This is the default behavior
-} -}
visibleLabel : Attribute value msg visibleLabel : Attribute value msg
visibleLabel = visibleLabel =
Attribute emptyEventsAndValues <| Attribute <| \config -> { config | hideLabel = False }
\config -> { config | hideLabel = False }
{-| Set a custom ID for this text input and label. If you don't set this, {-| Set a custom ID for this text input and label. If you don't set this,
@ -139,8 +134,7 @@ the page. You might also use this helper if you're manually managing focus.
-} -}
id : String -> Attribute value msg id : String -> Attribute value msg
id id_ = id id_ =
Attribute emptyEventsAndValues <| Attribute <| \config -> { config | id = Just id_ }
\config -> { config | id = Just id_ }
{-| Use this helper to add custom attributes. {-| Use this helper to add custom attributes.
@ -152,8 +146,7 @@ Instead, please use the `css` helper.
-} -}
custom : List (Html.Attribute Never) -> Attribute value msg custom : List (Html.Attribute Never) -> Attribute value msg
custom attributes = custom attributes =
Attribute emptyEventsAndValues <| Attribute <| \config -> { config | custom = config.custom ++ attributes }
\config -> { config | custom = config.custom ++ attributes }
{-| -} {-| -}
@ -171,27 +164,12 @@ testId id_ =
{-| Customizations for the RadioButton. {-| Customizations for the RadioButton.
-} -}
type Attribute value msg type Attribute value msg
= Attribute (EventsAndValues value msg) (Config -> Config) = Attribute (Config value msg -> Config value msg)
type alias EventsAndValues value msg =
{ onSelect : Maybe (value -> msg)
, premiumMsg : Maybe msg
, disclosedContent : List (Html msg)
}
emptyEventsAndValues : EventsAndValues value msg
emptyEventsAndValues =
{ onSelect = Nothing
, premiumMsg = Nothing
, disclosedContent = []
}
{-| This is private. The public API only exposes `Attribute`. {-| This is private. The public API only exposes `Attribute`.
-} -}
type alias Config = type alias Config value msg =
{ name : Maybe String { name : Maybe String
, id : Maybe String , id : Maybe String
, teacherPremiumLevel : Maybe PremiumLevel , teacherPremiumLevel : Maybe PremiumLevel
@ -201,10 +179,13 @@ type alias Config =
, hideLabel : Bool , hideLabel : Bool
, containerCss : List Css.Style , containerCss : List Css.Style
, custom : List (Html.Attribute Never) , custom : List (Html.Attribute Never)
, onSelect : Maybe (value -> msg)
, premiumMsg : Maybe msg
, disclosedContent : List (Html msg)
} }
emptyConfig : Config emptyConfig : Config value msg
emptyConfig = emptyConfig =
{ name = Nothing { name = Nothing
, id = Nothing , id = Nothing
@ -215,38 +196,19 @@ emptyConfig =
, hideLabel = False , hideLabel = False
, containerCss = [] , containerCss = []
, custom = [] , custom = []
, onSelect = Nothing
, premiumMsg = Nothing
, disclosedContent = []
} }
applyConfig : List (Attribute value msg) -> Config -> Config applyConfig : List (Attribute value msg) -> Config value msg -> Config value msg
applyConfig attributes beginningConfig = applyConfig attributes beginningConfig =
List.foldl (\(Attribute _ update) config -> update config) List.foldl (\(Attribute update) config -> update config)
beginningConfig beginningConfig
attributes attributes
orExisting : (acc -> Maybe a) -> acc -> acc -> Maybe a
orExisting f new previous =
case f previous of
Just just ->
Just just
Nothing ->
f new
applyEvents : List (Attribute value msg) -> EventsAndValues value msg
applyEvents =
List.foldl
(\(Attribute eventsAndValues _) existing ->
{ onSelect = orExisting .onSelect eventsAndValues existing
, premiumMsg = orExisting .premiumMsg eventsAndValues existing
, disclosedContent = eventsAndValues.disclosedContent ++ existing.disclosedContent
}
)
emptyEventsAndValues
maybeAttr : (a -> Html.Attribute msg) -> Maybe a -> Html.Attribute msg maybeAttr : (a -> Html.Attribute msg) -> Maybe a -> Html.Attribute msg
maybeAttr attr maybeValue = maybeAttr attr maybeValue =
maybeValue maybeValue
@ -270,9 +232,6 @@ view { label, name, value, valueToString, selectedValue } attributes =
config = config =
applyConfig attributes emptyConfig applyConfig attributes emptyConfig
eventsAndValues =
applyEvents attributes
stringValue = stringValue =
valueToString value valueToString value
@ -293,7 +252,7 @@ view { label, name, value, valueToString, selectedValue } attributes =
|> not |> not
( disclosureId, disclosureElement ) = ( disclosureId, disclosureElement ) =
case ( eventsAndValues.disclosedContent, isChecked ) of case ( config.disclosedContent, isChecked ) of
( [], _ ) -> ( [], _ ) ->
( Nothing, text "" ) ( Nothing, text "" )
@ -329,7 +288,7 @@ view { label, name, value, valueToString, selectedValue } attributes =
isChecked isChecked
([ Attributes.id id_ ([ Attributes.id id_
, Widget.disabled (isLocked || config.isDisabled) , Widget.disabled (isLocked || config.isDisabled)
, case ( eventsAndValues.onSelect, config.isDisabled ) of , case ( config.onSelect, config.isDisabled ) of
( Just onSelect_, False ) -> ( Just onSelect_, False ) ->
onClick (onSelect_ value) onClick (onSelect_ value)
@ -411,7 +370,7 @@ view { label, name, value, valueToString, selectedValue } attributes =
[] []
] ]
[ Html.text label ] [ Html.text label ]
, case ( config.contentPremiumLevel, eventsAndValues.premiumMsg ) of , case ( config.contentPremiumLevel, config.premiumMsg ) of
( Nothing, _ ) -> ( Nothing, _ ) ->
text "" text ""