Skip to content

Instantly share code, notes, and snippets.

@boiyama
Created April 27, 2017 02:20
Show Gist options
  • Save boiyama/b1549132ddf029cc11e139a0f2e83bb2 to your computer and use it in GitHub Desktop.
Save boiyama/b1549132ddf029cc11e139a0f2e83bb2 to your computer and use it in GitHub Desktop.
Simple form example in Elm
import Html exposing (Html, beginnerProgram, div, button, input, li, text, ul)
import Html.Attributes exposing (value)
import Html.Events exposing (onClick, onInput)
import List exposing (map)
type alias Model =
{ value : String
, posts : List String
}
model : Model
model =
Model "" []
view : Model -> Html Msg
view model =
div []
[ div []
[ input [ value model.value, onInput ChangeValue ] []
, button [ onClick Send ] [ text "Send" ]
]
, ul [] (map (\post -> li [] [ text post ]) model.posts)
]
type Msg
= ChangeValue String
| Send
update : Msg -> Model -> Model
update msg model =
case msg of
ChangeValue value ->
{ model | value = value }
Send ->
Model "" (model.posts ++ [ model.value ])
main : Program Never Model Msg
main =
beginnerProgram
{ model = model
, view = view
, update = update
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment