Learn HalogenGHC loads in the background

3. Properties and events

The browser stores text typed into an <input>. To keep component state and the field in sync, connect them in both directions:

  • HE.onValueInput SetName produces a SetName action containing the current text. handleAction saves it in state.
  • HP.value state.name sets the displayed value from state. When Submit clears name, the field clears too.

In a field the reader types text into (a text input like this one, or a textarea), a render writes value only when it differs from the value rendered last, so a render that comes before the action of a key leaves the key, and the caret, where the reader put them. A component that refuses an edit while keeping the same name therefore leaves the edit in the field: to take it out, render a different value or set the element's value yourself. Other controls, such as a select or a range, are different: the page can change what they show without the reader typing, so their rendered value is compared with the element on every render.

HP is Halogen.HTML.Properties. It provides HP.value, HP.placeholder, HP.type_ and HP.checked. Input types such as InputCheckbox and button types such as ButtonSubmit come from DOM.HTML.Indexed.

Checkboxes use HP.checked and HE.onChecked, which supplies a Bool. Properties are checked against the element type: HP.checked on a button does not compile. Reading records with state.name requires OverloadedRecordDot.

Submitting

Pressing Enter in the field or clicking a ButtonSubmit button submits the form. HE.onSubmit converts that event into Submit.

The browser would normally submit the form and navigate, reloading this page. Wrap the handler in HE.preventingDefault to prevent that behaviour.

Calling preventDefault from Web.Event.Event through H.sync inside an action depends on the action running before browser dispatch ends. If the action is queued, it may be too late. HE.preventingDefault acts in the event listener before handing the action to Halogen, so it also works when the component is busy.

Exercise

Add submitted :: [Text] to State, initially []. Make Submit append the current name, using T.toUpper when Shout is checked. Import Data.Text qualified as T.

Display submitted names below the form in an HH.ul_. Build its children with [HH.li_ [HH.text item] | item <- state.submitted].

Lesson.hs
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}

module Lesson (component) where

import Control.Monad.IO.Class (MonadIO)
import Control.Monad.State (modify)
import DOM.HTML.Indexed (ButtonType (..), InputType (..))
import Data.Text (Text)
import Halogen qualified as H
import Halogen.HTML qualified as HH
import Halogen.HTML.Events qualified as HE
import Halogen.HTML.Properties qualified as HP

data State = State {name :: Text, shout :: Bool}

data Action = SetName Text | SetShout Bool | Submit

component :: forall q o m. (MonadIO m) => H.Component q () o m
component =
  H.component initialState render [H.onAction handleAction]
  where
    initialState _ = pure State {name = "", shout = False}
    render state =
      HH.form
        [HE.preventingDefault (HE.onSubmit (\_ -> Submit))]
        [ HH.input [HP.value state.name, HE.onValueInput SetName, HP.placeholder "A name"]
        , HH.label_
            [HH.input [HP.type_ InputCheckbox, HP.checked state.shout, HE.onChecked SetShout], HH.text " Shout"]
        , HH.button [HP.type_ ButtonSubmit] [HH.text "Add"]
        , HH.p_ [HH.text ("You are typing: " <> state.name)]
        ]
    handleAction action = case action of
      SetName text -> modify (\s -> s {name = text})
      SetShout ticked -> modify (\s -> s {shout = ticked})
      Submit -> modify (\s -> s {name = ""})

The compiler loads once the page has started.

Your component will appear here after compilation.