hecrj/composable-form/1.0.1

NOTE: this package is not compatible with Elm 0.19.1

Form.Field

This module contains a type that represents a generic form field.

Note: You should not need to care about this unless you are creating your own custom fields or writing your own form renderer.

type alias Field attributes value values =
{ value : Value value
, update : value -> values
, attributes : attributes
}

Represents a form field.

It contains:

  • the current value of the field
  • an update function that takes a new field value and returns updated form values
  • the attributes of the field

These record fields are normally used in renderers to set up the value and onInput attributes. For example, you could render a TextField like this:

view : (values -> msg) -> Form values output -> values -> Html output
view onChange form values =
    let
        { fields, result } =
            Form.fill form values


        fieldsHtml =
            List.map (viewField onChange) fields


        -- ...
    in
    Html.form
        [-- ...
        ]
        [ Html.div [] fieldsHtml
        , submitButton
        ]


viewField : (values -> msg) -> ( Form.Field values, Maybe Error ) -> Html msg
viewField onChange ( field, maybeError ) =
    case field of
        Form.Text TextField.Raw { attributes, state } ->
            Html.input
                [ Attributes.type_ "text"
                , Attributes.value
                    (state.value
                        |> Value.raw
                        |> Maybe.withDefault ""
                    )
                , Attributes.onInput (state.update >> onChange)
                ]
                []


        _ ->
            -- ...