Skip to content

Instantly share code, notes, and snippets.

@avh4
Last active July 29, 2016 08:44
Show Gist options
  • Save avh4/821112ae7e218ac3899e54e4b02b9df0 to your computer and use it in GitHub Desktop.
Save avh4/821112ae7e218ac3899e54e4b02b9df0 to your computer and use it in GitHub Desktop.
Intro to Elm - SF Elm meetup 2016-07-27
module LightButton exposing (view)
import Html
import Html.Attributes
import Html.Events
colorForBool : Bool -> String
colorForBool bool =
if bool then
"red"
else
"blue"
view : Int -> Bool -> Html.Html Int
view index isOn =
Html.div
[ Html.Attributes.style
[ ( "background-color", colorForBool isOn )
, ( "width", "150px" )
, ( "height", "150px" )
]
, Html.Events.onClick index
]
[]
module Main exposing (..)
import Html
import Html.App
import Html.Attributes
import Html.Events
import LightButton
type alias Model =
{ isOn : List Bool
, numberOfMoves : Int
}
initialModel : Model
initialModel =
{ isOn = [ False, False, False ]
, numberOfMoves = 0
}
type Msg
= Toggle Int
| Reset
toggleButton : Int -> List Bool -> List Bool
toggleButton indexToToggle list =
List.indexedMap
(\index bool ->
if index == indexToToggle then
not bool
else
bool
)
list
update : Msg -> Model -> Model
update msg model =
case msg of
Toggle indexToToggle ->
{ model
| isOn =
toggleButton
indexToToggle
model.isOn
, numberOfMoves =
model.numberOfMoves + 1
}
Reset ->
initialModel
view : Model -> Html.Html Msg
view model =
Html.div []
[ Html.App.map Toggle
(Html.div [] (List.indexedMap LightButton.view model.isOn))
, Html.button
[ Html.Events.onClick Reset ]
[ Html.text "Reset" ]
, Html.hr [] []
, Html.text (toString model)
]
main : Program Never
main =
Html.App.beginnerProgram
{ model = initialModel
, update = update
, view = view
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment