-
Notifications
You must be signed in to change notification settings - Fork 3
/
4-multiple-dropdowns.elm
executable file
·113 lines (92 loc) · 2.45 KB
/
4-multiple-dropdowns.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
import Date exposing (Date, Month(..))
import Date.Extra as Date exposing (Interval(Year, Month, Day))
import DateSelectorDropdown
import Html exposing (Html, div, text, h1, label)
import Html.Attributes exposing (class)
import String
main : Program Never Model Msg
main =
Html.beginnerProgram
{ model = init <| Date.fromCalendarDate 2016 Sep 15
, view = view
, update = update
}
-- model
type DateField
= From
| To
| Birthdate
type alias Model =
{ today : Date
, from : Date
, to : Date
, birthdate : Maybe Date
, openDateField : Maybe DateField
}
init : Date -> Model
init today =
Model
today
(Date.floor Month today)
(Date.ceiling Month today |> Date.add Day -1)
Nothing
Nothing
-- update
type Msg
= Select DateField Date
| OpenDropdown DateField
| CloseDropdown
update : Msg -> Model -> Model
update msg model =
case msg of
Select dateField date ->
case dateField of
From -> { model | from = date }
To -> { model | to = date }
Birthdate -> { model | birthdate = Just date }
OpenDropdown dateField ->
{ model | openDateField = Just dateField }
CloseDropdown ->
{ model | openDateField = Nothing }
-- view
view : Model -> Html Msg
view { today, from, to, birthdate, openDateField } =
div []
[ Html.node "style" []
[ text <| String.join " "
[ "@import url(./examples.css);"
, "@import url(./date-selector.css);"
]
]
, div
[ class "columns" ]
[ div []
[ label [] [ text "From" ]
, viewDateSelector From openDateField
(Date.add Year -10 today)
to
(Just from)
]
, div []
[ label [] [ text "To" ]
, viewDateSelector To openDateField
from
(Date.add Year 1 today)
(Just to)
]
]
, label [] [ text "Birthdate" ]
, viewDateSelector Birthdate openDateField
(Date.add Year -110 today)
today
birthdate
]
viewDateSelector : DateField -> (Maybe DateField) -> Date -> Date -> Maybe Date -> Html Msg
viewDateSelector dateField openDateField =
let
isOpen = openDateField |> Maybe.map ((==) dateField) |> Maybe.withDefault False
in
DateSelectorDropdown.view
(if isOpen then CloseDropdown else OpenDropdown dateField)
(Select dateField)
isOpen