Skip to content

Having a form

Thibaut Assus edited this page Nov 24, 2017 · 2 revisions
type Msg
= FocusField
| Inputs Int
| BlurField
| InputField String

type Field
= Finished
| Typing

type alias Model =
{ field : Maybe Field 
, value : Maybe String
}

update msg model =
Update.Extra.identity <|
    case msg of
    FocusField ->
        if model.field /= Just Finished then
        { model | field = Just Typing }
        else
        model
    BlurField ->
        if model.field /= Just Finished then
        { model | field = Nothing, value = Nothing }
        else
        model
    Inputs x ->
        if x == 13 then
        { model | field = Just Finished }
        else
        model
    InputField value ->
        { model | value = Just value }
        
view model =
Builder.inputText
    [ Events.onFocus FocusField
    , Events.onBlur BlurField
    , Events.onInput InputField
    , Attributes.value <| Maybe.withDefault "" model.value
    ]

subscriptions model =
if model.field == Just Typing then
    Keyboard.downs Inputs
else
    Sub.none

Clone this wiki locally