Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Structuring Views in Elm

Structuring Views in Elm

Given at the Boston Elm meetup, this talk walks through ways of structuring Elm views to go from HTML soup to a view built entirely out of domain terms.

Joël Quenneville

January 28, 2020
Tweet

More Decks by Joël Quenneville

Other Decks in Programming

Transcript

  1. div [ class "survey" ] [ div [class "survey-section" ]

    [ h2 [ class "section-title" ] [ text "Food" ] , div [ class "question" ] [ div [ class "question-text" ] [ text "Pineapple on Pizza?" ] , div [] [ label [ for "pop-yes" ] [ text "Yes"] , input [ type_ "radio" , id "pop-yes" , name "pop" , onCheck (always <| PoP True) ] [] , label [ for "pop-no" ] [ text "No"] , input [ type_ "radio" , id "pop-no" , name "pop" , onCheck (always <| PoP False) ] [] ] ] ] -- repeat for anchovies ]
  2. radio : Msg -> String -> String -> String ->

    Html Msg radio msg groupName idSuffix labelText = ...
  3. div [ class "survey" ] [ div [class "survey-section" ]

    [ h2 [ class "section-title" ] [ text "Food" ] , div [ class "question" ] [ div [ class "question-text" ] [ text "Pineapple on Pizza?" ] , div [] [ radio (PoP True) "pop" "-yes" "Yes" , radio (PoP False) "pop" "-no" "No" ] ] ] -- repeat for anchovies ]
  4. div : List (Attribute msg) -> List (Html msg) ->

    Html msg div attributes children = node "div" attributes children
  5. yesNoQuestion : String -> (Bool -> Msg) -> String ->

    Html Msg yesNoQuestion questionText tagger groupName = ...
  6. div [ class "survey" ] [ div [class "survey-section" ]

    [ h2 [ class "section-title" ] [ text "Food" ] , yesNoQuestion "Pineaple on Pizza?" PoP "pop" ] -- repeat for anchovies ]
  7. surveySection : String -> List (Html a) -> Html a

    surveySection sectionTitle questions = ...
  8. survey [ surveySection "Toppings" [ yesNoQuestion "Pineaple on Pizza?" PoP

    "pop" , yesNoQuestion "Anchovies?" Anchovies "ancho" ] , surveySection "Crust" [ pickOne [ ("Thin Crust", Thin) , ("Thick Crust", Thick) , ("Chicago Style", Chicago) ] ] ]
  9. viewMessage : Message -> Html a viewMessage message div [

    if message.isError then (class "red") else (class "green") ] [ text message.text ]
  10. viewMessage : Message -> Html a viewMessage message div [

    messageClass message.isError ] [ text message.text ]
  11. viewMessage : Message -> Html a viewMessage message div [

    if message.isError then (class "red") else (class "green") ] (if message.isError then [ errorIcon, text message.text ] else [ text message.text ] )
  12. viewMessage : Message -> Html a viewMessage message div [

    messageClass message.isError ] (messageContent message)
  13. viewMessage : Message -> Html a viewMessage message if message.isError

    then errorMessage message.text else successMessage message.text
  14. errorMessage : String -> Html a errorMessage content = div

    [ class "red" ] [ errorIcon, text content ]