Skip to content

Instantly share code, notes, and snippets.

@markhamburg
Created September 25, 2016 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markhamburg/874a2dc5e92d233415cbe9cbe0e8e5d6 to your computer and use it in GitHub Desktop.
Save markhamburg/874a2dc5e92d233415cbe9cbe0e8e5d6 to your computer and use it in GitHub Desktop.
A demonstration of how text fields need identity continuity
import Html exposing (div, input, text, fieldset, label)
import Html.App exposing (beginnerProgram)
import Html.Events exposing (onInput, onClick)
import Html.Attributes exposing (value, type', style, name)
type alias Model =
{ text : String
, items : List String
, position : BeforeAfter
}
type BeforeAfter
= Before
| After
init =
{ text = ""
, items = []
, position = Before
}
type Msg
= NewText String
| SetPosition BeforeAfter
update : Msg -> Model -> Model
update msg model =
case msg of
NewText string ->
{ model | text = string, items = string :: model.items }
SetPosition position ->
{ model | position = position }
view : Model -> Html.Html Msg
view model =
let
buttons =
fieldset []
[ radio "Before" "position" (SetPosition Before)
, radio "After" "position" (SetPosition After)
]
items =
List.map (\str -> div [] [ text str ]) model.items
textField =
input [ value model.text, onInput NewText ] []
ordered =
case model.position of
Before ->
textField :: items
After ->
List.reverse (textField :: items)
in
div []
[ buttons
, div [] ordered
]
radio : String -> String -> msg -> Html.Html msg
radio value groupName msg =
label
[ style [ ( "padding", "20px" ) ] ]
[ input [ type' "radio", name groupName, onClick msg ] []
, text value
]
main =
beginnerProgram { model = init, view = view, update = update }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment