This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Input.elm
129 lines (107 loc) · 3.32 KB
/
Input.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
module Form.Input exposing
( Input
, baseInput, textInput, passwordInput, textArea, checkboxInput, selectInput, radioInput
)
{-| Html input view helpers, wired for elm-form validation.
@docs Input
@docs baseInput, textInput, passwordInput, textArea, checkboxInput, selectInput, radioInput
-}
import Form exposing (FieldState, Form, InputType(..), Msg(..))
import Form.Error exposing (ErrorValue(..))
import Form.Field as Field exposing (Field, FieldValue(..))
import Html exposing (..)
import Html.Attributes as HtmlAttr exposing (..)
import Html.Events exposing (..)
import Json.Decode as Json
import Maybe exposing (andThen)
import String
{-| An input renders Html from a field state and list of additional attributes.
All input functions using this type alias are pre-wired with event handlers.
-}
type alias Input e a =
FieldState e a -> List (Attribute Msg) -> Html Msg
{-| Untyped input, first param is `type` attribute.
-}
baseInput : String -> (String -> FieldValue) -> InputType -> Input e String
baseInput t toFieldValue inputType state attrs =
let
formAttrs =
[ type_ t
, value (state.value |> Maybe.withDefault "")
, onInput (toFieldValue >> Input state.path inputType)
, onFocus (Focus state.path)
, onBlur (Blur state.path)
]
in
input (formAttrs ++ attrs) []
{-| Text input.
-}
textInput : Input e String
textInput =
baseInput "text" String Text
{-| Password input.
-}
passwordInput : Input e String
passwordInput =
baseInput "password" String Text
{-| Textarea.
-}
textArea : Input e String
textArea state attrs =
let
formAttrs =
[ value (state.value |> Maybe.withDefault "")
, onInput (String >> Input state.path Textarea)
, onFocus (Focus state.path)
, onBlur (Blur state.path)
]
in
Html.textarea (formAttrs ++ attrs) []
{-| Select input.
-}
selectInput : List ( String, String ) -> Input e String
selectInput options state attrs =
let
formAttrs =
[ on
"change"
(targetValue |> Json.map (String >> Input state.path Select))
, onFocus (Focus state.path)
, onBlur (Blur state.path)
]
buildOption ( k, v ) =
option [ value k, selected (state.value == Just k) ] [ text v ]
in
select (formAttrs ++ attrs) (List.map buildOption options)
{-| Checkbox input.
-}
checkboxInput : Input e Bool
checkboxInput state attrs =
let
formAttrs =
[ type_ "checkbox"
, checked (state.value |> Maybe.withDefault False)
, onCheck (Bool >> Input state.path Checkbox)
, onFocus (Focus state.path)
, onBlur (Blur state.path)
]
in
input (formAttrs ++ attrs) []
{-| Radio input.
-}
radioInput : String -> Input e String
radioInput value state attrs =
let
formAttrs =
[ type_ "radio"
, name state.path
, HtmlAttr.value value
, checked (state.value == Just value)
, onFocus (Focus state.path)
, onBlur (Blur state.path)
, on
"change"
(targetValue |> Json.map (String >> Input state.path Radio))
]
in
input (formAttrs ++ attrs) []