Course map
miso // 22

Forms and inputs

Read

Controlled inputs

An input is controlled when its value comes from the model (P.value_) and every change goes back through an action (E.onInput). The model is the truth; the DOM just shows it.

H.input_ [ P.type_ "text", P.value_ (_name m), E.onInput SetName ]

H.input_ [ P.type_ "checkbox", P.checked_ (_agree m), E.onChecked (\(Checked b) -> SetAgree b) ]

H.select_ [ E.onChange SetSize ]
  [ H.option_ [ P.value_ "s" ] [ "small" ]
  , H.option_ [ P.value_ "l" ] [ "large" ]
  ]

H.form_ [ E.onSubmit Submit ] [ ... ]

E.onChecked gives a Bool wrapped in the Checked newtype, so the handler unwraps it; E.onChange gives the selected value. E.onSubmit prevents the browser's page reload and dispatches your action, so the enter key works as users expect.

Validation is just a function from the model to a Maybe error, called in the view. No separate form state to keep in step.

01 / 04