This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

Form.Field

Read and write field values.

Constructors

type alias Field = Tree FieldValue

A field is a tree node.

type FieldValue = String String | Bool Bool | EmptyField

Form field. Can either be a group of named fields, or a final field.

value : FieldValue -> Field

Build a field from its value.

string : String -> Field

Build a string field, for text inputs, selects, etc.

maybeString : Maybe String -> Field

Build a string field from a Maybe value. Returns EmptyField if Nothing.

bool : Bool -> Field

Build a boolean field, for checkboxes.

maybeBool : Maybe Bool -> Field

Build a boolean field from a Maybe value. Returns EmptyField if Nothing.

group : List ( String, Field ) -> Field

Gather named fields as a group field.

list : List Field -> Field

Gather fields as a list field.

Value readers

asString : Field -> Maybe String

Get field value as string.

asBool : Field -> Maybe Bool

Get field value as boolean.

module Form.Field exposing (Field, FieldValue(..), value, string, maybeString, bool, maybeBool, group, list, asString, asBool)

{-| Read and write field values.


# Constructors

@docs Field, FieldValue, value, string, maybeString, bool, maybeBool, group, list


# Value readers

@docs asString, asBool

-}

import Form.Tree as Tree exposing (Tree)


{-| A field is a tree node.
-}
type alias Field =
    Tree FieldValue


{-| Form field. Can either be a group of named fields, or a final field.
-}
type FieldValue
    = String String
    | Bool Bool
    | EmptyField


{-| Build a field from its value.
-}
value : FieldValue -> Field
value =
    Tree.Value


{-| Build a string field, for text inputs, selects, etc.
-}
string : String -> Field
string =
    String >> Tree.Value


{-| Build a string field from a Maybe value. Returns EmptyField if Nothing.
-}
maybeString : Maybe String -> Field
maybeString =
    Maybe.map String
        >> Maybe.withDefault EmptyField
        >> Tree.Value


{-| Build a boolean field, for checkboxes.
-}
bool : Bool -> Field
bool =
    Bool >> Tree.Value


{-| Build a boolean field from a Maybe value. Returns EmptyField if Nothing.
-}
maybeBool : Maybe Bool -> Field
maybeBool =
    Maybe.map Bool
        >> Maybe.withDefault EmptyField
        >> Tree.Value


{-| Gather named fields as a group field.
-}
group : List ( String, Field ) -> Field
group =
    Tree.group


{-| Gather fields as a list field.
-}
list : List Field -> Field
list =
    Tree.List


{-| Get field value as boolean.
-}
asBool : Field -> Maybe Bool
asBool field =
    case field of
        Tree.Value (Bool b) ->
            Just b

        _ ->
            Nothing


{-| Get field value as string.
-}
asString : Field -> Maybe String
asString field =
    case field of
        Tree.Value (String s) ->
            Just s

        _ ->
            Nothing