top of page
Search

Anatomy of an Elm Form Component

jattueyi
Nov 17, 2022
2 min read

This will be an intro to the Elm Architecture through a concrete example of a form component.


A form component allows users to input data into a system, an example could be a sign up form with username, email & password fields.


Configuration:

It's useful for our form component to have configurable options like fields (e.g username or email), custom validation errors (e.g you passwords should have at least one of symbol, lowercase, uppercase and numeric characters) etc.


We could make users of this component have to declare all configuration options upfront, but when we add new configuration options in the future, we would have to update the call site for every part of the codebase that calls the config, otherwise it wouldn't work.


Instead we can make the config initialization function take a minimal number of config options and then have higher level functions that take a config and return a new config that handle specific options and make our configurations composable


Example.

type alias Config = { }

type FieldType = String | Int


config: Config


withField : FieldType -> Config -> Config


Model :

The Model defines all the data involved in the Form component at any point in time. Data like fields, errors, form state etc are stored here and are generated from the Config


Example.


type alias Model = { }


init: Config -> Model


Msg:

The Msg type is an enum / sum type that defines all events & associated data that happens within our component, it is closely associated with the update function.


Example.


type Msg = SubmitButtonClicked | FieldDisableToggled String Bool


Update:

The Update function acts as the main event handler for the component, it takes a Model & Msg and returns a new Model and a side effecting Cmd. Since the Msg is an enum / sum type, this function is a big case statement over all the Msg types with each arm responsible for a specific Msg type, the update to the Model for that Msg type as well as any side effecting commands


Example.


update: Msg -> Model -> (Model, Cmd msg)

update msg model =

case msg of

SubmitButtonClicked ->

( model, Cmd.perform (postForm model.fields))

FieldDisableToggled id bool ->

( { model | field =

case Dict.get id model.fields of

Just _ -> Dict.insert id model.fields value

_ -> model.fields

},

Cmd.none

)


View:

The view function is responsible for showing the Form component in the UI, it takes updated model from the Update function and renders the new Form based on the models current state.


Example.


view: Model -> Html msg



The lifecycle of the form component begins with an initial view that is rendered on the UI with some initial Model. Every time the user interacts with the form by inputting fields or clicking buttons, the update function gets called with the current Model and a new Model is produced as a result.


That new model get rendered in the UI, so that the users can see their interactions represented in the UI. This idea should be familiar to people with experience with Immediated Mode Graphics UIs like imgui or with React in Javascript.




 
 
 

Comments


design log

©2022 by design log. Proudly created with Wix.com

bottom of page