Created
December 28, 2020 05:44
-
-
Save codegirl-007/4f3bb8f0b41e6ad076ca703d764a6904 to your computer and use it in GitHub Desktop.
Solution to Elm text field exercise
This file contains hidden or 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
-- A text input for reversing text. Very useful! | |
-- | |
-- Read how it works: | |
-- https://guide.elm-lang.org/architecture/text_fields.html | |
-- | |
import Browser | |
import Html exposing (Html, Attribute, div, input, text) | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (onInput) | |
-- MAIN | |
main = | |
Browser.sandbox { init = init, update = update, view = view } | |
-- MODEL | |
type alias Model = | |
{ content : String | |
, length : Int | |
} | |
init : Model | |
init = | |
{ content = "" | |
, length = 0 } | |
-- UPDATE | |
type Msg | |
= Change String | |
update : Msg -> Model -> Model | |
update msg model = | |
case msg of | |
Change newContent -> | |
{ model | content = newContent, length = String.length newContent } | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ input [ placeholder "Text to reverse", value model.content, onInput Change ] [] | |
, div [] [ text (String.reverse model.content) ] | |
, div [] [ text (String.fromInt model.length) ] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!