-
-
Save pablohirafuji/1fcfec3418c0c17bff0b2fb776d92fe2 to your computer and use it in GitHub Desktop.
Elm defaulValue vs. value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Html exposing (..) | |
import Html.App exposing (beginnerProgram) | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (onInput, onClick) | |
main = | |
beginnerProgram { model = Model "" "", view = view, update = update } | |
type alias Model = | |
{ defaultValueEg : String | |
, valueEg : String | |
} | |
view model = | |
div [] | |
[ h1 [] [ text "Using defaultValue" ] | |
, textarea [ onInput Text1Input, defaultValue model.defaultValueEg ] [] | |
, button [ onClick ResetText1 ] [ text "Reset" ] | |
, h1 [] [ text "Using value" ] | |
, textarea [ onInput Text2Input, value model.valueEg ] [] | |
, button [ onClick ResetText2 ] [ text "Reset" ] | |
, div [] [ text (toString model) ] | |
] | |
type Msg | |
= Text1Input String | |
| ResetText1 | |
| Text2Input String | |
| ResetText2 | |
update msg model = | |
case msg of | |
Text1Input str -> | |
{ model | defaultValueEg = str } | |
ResetText1 -> | |
{ model | defaultValueEg = "" } | |
Text2Input str -> | |
{ model | valueEg = str } | |
ResetText2 -> | |
{ model | valueEg = "" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The github issue where you link to this gist is at the top of google, so here's my fork that makes the one-liner change so that it works with Elm 0.18: https://gist.github.com/danneu/958d6f39a6e33721dda98b5dddc3a2be