Rather than passing the attributes through, render the button and link directly

This commit is contained in:
Tessa Kelly 2019-07-11 14:40:05 -07:00
parent 7397eb24a6
commit c7c56cdf3d
2 changed files with 22 additions and 81 deletions

View File

@ -174,21 +174,6 @@ href url (ButtonOrLink config) =
} }
{-| -}
renderButton : List (Attribute msg) -> ButtonOrLink msg -> Html msg
renderButton attributes (ButtonOrLink config) =
button
{ onClick = config.onClick
, size = config.size
, style = config.style
, width = config.width
}
{ label = config.label
, state = config.state
, icon = config.icon
}
type Link type Link
= Default = Default
| WithTracking | WithTracking
@ -245,8 +230,8 @@ linkExternalWithTracking (ButtonOrLink config) =
{-| -} {-| -}
renderLink : List (Attribute msg) -> ButtonOrLink msg -> Html msg renderLink : ButtonOrLink msg -> Html msg
renderLink attributes (ButtonOrLink config) = renderLink (ButtonOrLink config) =
let let
linkBase linkFunctionName extraAttrs = linkBase linkFunctionName extraAttrs =
Nri.Ui.styled Styled.a Nri.Ui.styled Styled.a
@ -258,18 +243,18 @@ renderLink attributes (ButtonOrLink config) =
case config.linkType of case config.linkType of
Default -> Default ->
linkBase "link" linkBase "link"
(Attributes.target "_self" :: attributes) (Attributes.target "_self" :: config.customAttributes)
SinglePageApp -> SinglePageApp ->
linkBase "linkSpa" linkBase "linkSpa"
(EventExtras.onClickPreventDefaultForLinkWithHref config.onClick (EventExtras.onClickPreventDefaultForLinkWithHref config.onClick
:: attributes :: config.customAttributes
) )
WithMethod method -> WithMethod method ->
linkBase "linkWithMethod" linkBase "linkWithMethod"
(Attributes.attribute "data-method" method (Attributes.attribute "data-method" method
:: attributes :: config.customAttributes
) )
WithTracking -> WithTracking ->
@ -277,12 +262,12 @@ renderLink attributes (ButtonOrLink config) =
"linkWithTracking" "linkWithTracking"
(Events.preventDefaultOn "click" (Events.preventDefaultOn "click"
(Json.Decode.succeed ( config.onClick, True )) (Json.Decode.succeed ( config.onClick, True ))
:: attributes :: config.customAttributes
) )
External -> External ->
linkBase "linkExternal" linkBase "linkExternal"
(Attributes.target "_blank" :: attributes) (Attributes.target "_blank" :: config.customAttributes)
ExternalWithTracking -> ExternalWithTracking ->
linkBase "linkExternalWithTracking" linkBase "linkExternalWithTracking"
@ -290,7 +275,7 @@ renderLink attributes (ButtonOrLink config) =
[ Attributes.target "_blank" [ Attributes.target "_blank"
, EventExtras.onClickForLinkWithHref config.onClick , EventExtras.onClickForLinkWithHref config.onClick
] ]
attributes config.customAttributes
) )
@ -475,73 +460,29 @@ type ButtonState
{-| A delightful button which can trigger an effect when clicked! {-| A delightful button which can trigger an effect when clicked!
This button will trigger the passed-in message if the button state is:
- Enabled
- Unfulfilled
This button will be Disabled if the button state is:
- Disabled
- Error
- Loading
- Success
-} -}
button : renderButton : ButtonOrLink msg -> Html msg
{ onClick : msg renderButton (ButtonOrLink config) =
, size : ButtonSize
, style : ButtonStyle
, width : ButtonWidth
}
->
{ label : String
, state : ButtonState
, icon : Maybe Svg
}
-> Html msg
button config content =
let let
buttonStyle_ = ( buttonStyle_, isDisabled ) =
case content.state of case config.state of
Enabled -> Enabled ->
styleToColorPalette config.style ( styleToColorPalette config.style, False )
Disabled -> Disabled ->
InactiveColors ( InactiveColors, True )
Error -> Error ->
ErrorColors ( ErrorColors, True )
Unfulfilled -> Unfulfilled ->
InactiveColors ( InactiveColors, False )
Loading -> Loading ->
LoadingColors ( LoadingColors, True )
Success -> Success ->
SuccessColors ( SuccessColors, True )
isDisabled =
case content.state of
Enabled ->
False
Disabled ->
True
Error ->
True
Unfulfilled ->
False
Loading ->
True
Success ->
True
in in
Nri.Ui.styled Html.button Nri.Ui.styled Html.button
(styledName "customButton") (styledName "customButton")
@ -550,7 +491,7 @@ button config content =
, Attributes.disabled isDisabled , Attributes.disabled isDisabled
, Attributes.type_ "button" , Attributes.type_ "button"
] ]
[ viewLabel content.icon content.label ] [ viewLabel config.icon config.label ]

View File

@ -153,7 +153,7 @@ viewButtonExamples messages (State control) =
|> Button.secondary |> Button.secondary
|> Button.onClick (messages.showItWorked "linkExternalWithTracking clicked") |> Button.onClick (messages.showItWorked "linkExternalWithTracking clicked")
|> Button.linkExternalWithTracking |> Button.linkExternalWithTracking
|> Button.renderLink [] |> Button.renderLink
] ]
|> div [] |> div []
@ -208,10 +208,10 @@ buttons messages model =
|> Button.onClick (messages.showItWorked "Button clicked!") |> Button.onClick (messages.showItWorked "Button clicked!")
|> (case model.buttonType of |> (case model.buttonType of
Link -> Link ->
Button.renderLink [] Button.renderLink
Button -> Button ->
Button.renderButton [] Button.renderButton
) )
) )
|> List.singleton |> List.singleton