primait/pyxis-components/5.0.0

NOTE: the latest version is 9.0.3

Prima.Pyxis.Form

Allows to create a Form and it's fields using predefined Html syntax.

Form Configuration

type Form model msg

Represents the Form configuration.

Represents the type of group which can wrap a form field. Used to add a boxed icon in a form field (for instance the calendar icon of the datepicker).

type alias FormRenderer model msg =
( FormField model msg -> List (Html msg)
, List (FormField model msg)
)

A list in which each item represents a row of the form. Each row has is own list of fields (FormField model msg) which will be rendered by the mapper function (FormField model msg -> List (Html msg)).

type alias Label =
String

Alias for String. Useful to have easy-to-read signatures.

type alias Slug =
String

Alias for String. Useful to have easy-to-read signatures.

type alias Value =
String

Alias for String. Useful to have easy-to-read signatures.

(FormField model msg -> List (Html msg))
-> List (FormField model msg)
-> FormRenderer model msg

Utility function to create a FormRenderer instance

Form Configuration Helpers

init : Form model msg

Creates an empty, pristine form.

addFields : List (FormRenderer model msg) -> Form model msg -> Form model msg

Add rows of fields to the form.

--
import Prima.Pyxis.Form as Form
import Prima.Pyxis.Form.Event as Event


...


type alias Model =
    { data : FormData
    , form : Form FormData Msg
    }


type alias FormData =
    { username : Maybe String
    }


type Msg
    = UpdateUsername (Maybe String)


...


usernameConfig : FormField FormData Msg
usernameConfig =
    Form.textConfig
        "username"
        (Just "Username")
        [ minlength 3, maxlength 12 ]
        .username
        [ Event.onInput UpdateUsername ]
        [ NotEmpty "Username must not be blank."
        ]


...


view : Model -> Html Msg
view ({ form, data } as model) =
    form
        |> Form.addFields [( Form.renderField form data, usernameConfig )]
        |> Form.render
setAsPristine : Form model msg -> Form model msg

Sets the form to Pristine state.

setAsTouched : Form model msg -> Form model msg

Sets the form to Touched state.

setAsSubmitted : Form model msg -> Form model msg

Sets the form to Submitted state. When submitted the form will eventually show errors.

Fields Configuration

type FormField model msg
= FormField (FormFieldConfig model msg)

Represents the configuration of a single form field.

Input

Slug
-> Maybe Label
-> List (Attribute msg)
-> (model -> Maybe Value)
-> List (Event msg)
-> List (Validation model)
-> FormField model msg

Creates an input text field. This field can handle only onInput, onFocus, onBlur events. Other events will be ignored.

--
import Prima.Pyxis.Form as Form exposing (FormField)
import Prima.Pyxis.Form.Event as Event


...


type alias Msg
    = OnInput (Maybe String)
    | OnFocus
    | OnBlur


type alias Model =
    { username : Maybe String }


...


usernameConfig : FormField Model Msg
usernameConfig =
    Form.textConfig
        "username"
        (Just "Username")
        [ minlength 3, maxlength 12 ]
        .username
        [ Event.onInput OnInput
        , Event.onFocus OnFocus
        , Event.onBlur OnBlur
        ]
        [ NotEmpty "Empty value is not acceptable."
        ]
Slug
-> Maybe Label
-> List (Attribute msg)
-> (model -> Maybe Value)
-> List (Event msg)
-> List (Validation model)
-> FormField model msg

Creates a password text field. Same configuration as textConfig.

Slug
-> Maybe Label
-> List (Attribute msg)
-> (model -> Maybe Value)
-> List (Event msg)
-> List (Validation model)
-> FormField model msg

Creates a texarea field. Same configuration as textConfig.

Checkbox

Slug
-> Maybe Label
-> List (Attribute msg)
-> (model -> List ( Slug, Bool ))
-> List (Event msg)
-> List CheckboxOption
-> List (Validation model)
-> FormField model msg

Creates a checkbox field. This field can handle only onCheck event. Other events will be ignored.

--
import Prima.Pyxis.Form as Form exposing (FormField, Label, Slug, CheckboxOption)
import Prima.Pyxis.Form.Event as Event


...


type alias Msg
    = OnCheck ( Slug, Bool )


type alias Model =
    { country : Maybe String
    , countries: List (Label, Slug, Bool)
    }


...


countries : Model -> FormField Model Msg
countries model =
    let
        reader : (Model -> List (Slug, Bool))
        reader =
            (List.map (\( _, slug, checked ) -> ( slug, checked )) << .countries)


        options : List CheckboxOption
        options =
            List.map (( label, slug, isChecked ) -> Form.checkboxOption label slug isChecked) model.countries
    in
    Form.checkboxConfig
        "countries"
        (Just "Countries")
        []
        reader
        [ Event.onCheck OnCheck ]
        options
        []
checkboxOption : Label -> Slug -> Bool -> CheckboxOption

Creates a radio option.

Radio

Slug
-> Maybe Label
-> List (Attribute msg)
-> (model -> Maybe Value)
-> List (Event msg)
-> List RadioOption
-> List (Validation model)
-> FormField model msg

Creates a radio field. This field can handle only onSelect event. Other events will be ignored.

--
import Prima.Pyxis.Form as Form exposing (FormField, RadioOption)
import Prima.Pyxis.Form.Event as Event


...


type alias Msg
    = OnSelect (Maybe String)


type alias Model =
    { gender : Maybe String }


...


genderConfig : FormField Model Msg
genderConfig =
    let
        options : List RadioOption
        options =
            [ Form.radioOption "Male" "male", Form.radioOption "Female", "female" ]
    in
    Form.radioConfig
        "gender"
        (Just "Gender")
        []
        .gender
        [ Event.onSelect OnSelect ]
        options
        []
radioOption : Label -> Slug -> RadioOption

Creates a radio option.

Select

Slug
-> Maybe Label
-> Bool
-> Bool
-> Maybe String
-> List (Attribute msg)
-> (model -> Maybe Value)
-> List (Event msg)
-> List SelectOption
-> List (Validation model)
-> FormField model msg

Creates a select field. This field is a custom component on desktop devices. It fallbacks to a native Html Select tag on mobile devices. Infact we need to express isDisabled flag as an Html Attribute and also as a parameter for this method.

This field can handle only onToggle, onInput, onSelect, onFocus and onBlur events. Other events will be ignored.

--
import Prima.Pyxis.Form as Form exposing (FormField, Label, Slug, SelectOption)
import Prima.Pyxis.Form.Event as Event
import Html.Attributes
...


type alias Msg
    = OnInput (Maybe String)
    | OnToggle
    | OnSelect (Maybe String)
    | OnFocus
    | OnBlur


type alias Model =
    { city : Maybe String
    , isOpen : Bool
    , isDisabled : Bool
    }


...


cities : Model -> FormField Model Msg
cities model =
    let
        options : List SelectOption
        options =
            [ Form.selectOption "Milan" "MI", Form.selectOption "Turin" "TO", Form.selectOption "Rome" "RO" ]
    in
    Form.selectConfig
        "city"
        (Just "City")
        model.isDisabled
        model.isOpen
        (Just "Select a city")
        [ Html.Attributes.disabled model.isDisabled ]
        .city
        [ Event.onToggle OnToggle, Event.onInput OnInput, Event.onSelect OnSelect, Event.onFocus, Event.onBlur ]
        options
        [ NotEmpty "Empty value is not acceptable." ]
selectOption : Label -> Slug -> SelectOption

Creates a select option.

Datepicker

Slug
-> Maybe Label
-> List (Attribute msg)
-> (model -> Maybe Value)
-> (Msg -> msg)
-> List (Event msg)
-> Model
-> Bool
-> List (Validation model)
-> FormField model msg

Creates a datepicker field. This field can handle only onInput event. Other events will be ignored.

--
import Prima.Pyxis.Form as Form exposing (FormField)
import Prima.Pyxis.Form.Event as Event
import Prima.Pyxis.DatePicker as DatePicker
...


type alias Msg
    = OnInput (Maybe String)
    | OnDatePickerChange DatePicker.Msg


type alias Model =
    { datePicker : DatePicker.Model
    , birthDate : Maybe String
    , isDatePickerOpen : Bool
    }


...


dateOfBirth : Model -> FormField Model Msg
dateOfBirth model =
    Form.datepickerConfig
        "birthdate"
        (Just "Date of Birth")
        []
        .birthDate
        OnDatePickerChange
        [ Event.onInput OnInput ]
        model.datePicker
        model.isDatePickerOpen
        []

Autocomplete

String
-> Maybe String
-> Bool
-> Maybe String
-> List (Attribute msg)
-> (model -> Maybe Value)
-> (model -> Maybe Value)
-> List (Event msg)
-> List AutocompleteOption
-> List (Validation model)
-> FormField model msg

Creates an autocomplete field. This field can handle only onSelect, onAutocompleteFilter, onFocus and onBlur events. Other events will be ignored.

--
import Prima.Pyxis.Form as Form exposing (FormField)
import Prima.Pyxis.Form.Event as Event
import Tuple
...


type alias Msg
    = OnSelect (Maybe String)
    | OnFilter (Maybe String)


type alias Model =
    { empireFilter : Maybe String
    , empire : Maybe String
    , isOpen : Bool
    }


...


empire : Model -> FormField Model Msg
empire model =
    let
        filter : String
        filter =
            Maybe.withDefault "" model.empireFilter


        options : List AutocompleteOption
        options =
            [ ("Roman Empire", "roman")
            , ("Ottoman Empire", "ottoman")
            , ("French Empire", "french")
            , ("British Empire", "british")
            ]
            |> List.filter (String.contains filter << Tuple.first)
            |> List.map (\(label, slug) -> Form.autocompleteOption label slug)
    in
    Form.autocompleteConfig
        "empire"
        (Just "empire")
        model.isOpen
        (Just "No results")
        []
        .empireFilter
        .empire
        [ Event.onAutocompleteFilter OnFilter, Event.onSelect OnSelect ]
        options
        []
autocompleteOption : Label -> Slug -> AutocompleteOption

Creates an autocomplete option.

Pure Html

pureHtmlConfig : List (Html msg) -> FormField model msg

Creates a pure html field. No events accepeted.

--
import Prima.Pyxis.Form as Form exposing (FormField)


...


loremIpsum : FormField Model Msg
loremIpsum =
    Form.pureHtmlConfig [ text "Lorem ipsum dolor sit amet" ]

Fields Helpers

isValid : model -> FormField model msg -> Bool

Check if a FormField is valid

isPristine : model -> FormField model msg -> Bool

Check if a FormField is pristine

Render

render : Form model msg -> Html msg

Renders a form with all it's fields. Requires a Form model msg created via Form.init and Form.addFields.

renderField : Form model msg -> model -> FormField model msg -> List (Html msg)

Renders a field by receiving the Form and the FormField configuration.

--
import Html exposing (Html)
import Prima.Pyxis.Form as Form exposing (Form, FormField)
import FieldConfig exposing (usernameConfig)


...


type Msg
    = OnInput (Maybe String)


type alias Model =
    { form : Form FormData Msg
    , data : FormData
    }


type alias FormData =
    { username: Maybe String
    }


...


view : Model -> Html Msg
view model =
    ( Form.renderField model.form model.data, [ usernameConfig ] )
        |> Form.render model.form
Form model msg
-> model
-> FormField model msg
-> List (Html msg)

Renders a field by receiving the Form, the FormFieldGroup, and the FormField configuration. Useful to build a field with an icon to the left (prepend), or to the right (append). You can pass any html to this function, but be careful, UI can be broken.

--
import Html exposing (Html, i)
import Html.Attributes exposing (class)
import Html.Events exposing (onClick)
import Prima.Pyxis.Form as Form exposing (Form, FormField)
import FieldConfig exposing (datePickerConfig)


...


type Msg
    = ToggleDatePicker


type alias Model =
    { form : Form FormData Msg
    , data : FormData
    }


type alias FormData =
    { birthDate: Maybe String
    }


...


datePickerIcon : Html Msg
datePickerIcon =
    i
        [ class "a-icon a-icon-calendar cBrandAltDark"
        , onClick ToggleDatePicker
        ]


view : Model -> Html Msg
view model =
    ( Form.renderFieldWithGroup model.form model.data <| Form.appendGroup [ datePickerIcon ], [ datePickerConfig ] )
        |> Form.render model.form

Render Helpers

prependGroup : List (Html msg) -> FormFieldGroup msg

Represents an html which prepends to the form field.

appendGroup : List (Html msg) -> FormFieldGroup msg

Represents an html which appends to the form field.