Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Html exposing (..)
- import Html.App as App
- import Html.Attributes exposing (..)
- import Html.Events exposing (onInput, onClick)
- main =
- App.beginnerProgram
- { model = model
- , view = view
- , update = update
- }
- -- MODEL
- type alias User =
- {
- name : String,
- password : String
- }
- type alias Model =
- {
- form : User,
- users: List User
- }
- model: Model
- model =
- { form = {name = "", password = ""}, users = [] }
- -- UPDATE
- type Msg
- = Name String
- | Password String
- | Submit
- update : Msg -> Model -> Model
- update msg model =
- case msg of
- Name name ->
- { model | form = updateForm msg model.form}
- Password password ->
- { model | form = updateForm msg model.form}
- Submit ->
- submitForm model
- updateForm msg form =
- case msg of
- Name name ->
- { form | name = name }
- Password password ->
- { form | password = password }
- Submit ->
- form
- clearName form =
- {form | name = ""}
- clearPassword form =
- {form | password = ""}
- clearForm form =
- clearPassword (clearName form)
- addFormToList model =
- { model | users = List.append model.users [model.form] }
- updateF model form1 =
- { model | form = form1}
- submitForm model =
- updateF (addFormToList model) (clearForm model.form)
- -- VIEW
- view : Model -> Html Msg
- view model =
- div []
- [ input [ type' "text", placeholder "Name", onInput Name ] []
- , input [ type' "password", placeholder "Password", onInput Password ] []
- , button [ onClick Submit ] [ text "Submit"]
- , ul []
- (List.map (\user -> li [] [text user.name]) model.users)
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement