|
1 | | -module Ui.DatePicker exposing |
2 | | - ( Model, Msg, init, update, subscriptions, onChange, view, render, setValue |
3 | | - , closeOnSelect ) |
| 1 | +module Ui.DatePicker |
| 2 | + exposing |
| 3 | + ( Model |
| 4 | + , Msg |
| 5 | + , init |
| 6 | + , update |
| 7 | + , subscriptions |
| 8 | + , onChange |
| 9 | + , view |
| 10 | + , render |
| 11 | + , setValue |
| 12 | + , closeOnSelect |
| 13 | + ) |
4 | 14 |
|
5 | 15 | {-| An input component that displays a **Calendar** (in a dropdown) when |
6 | 16 | focused, allowing the user to manipulate the selected date. |
7 | 17 |
|
| 18 | +
|
8 | 19 | # Model |
| 20 | +
|
9 | 21 | @docs Model, Msg, init, subscriptions, update |
10 | 22 |
|
| 23 | +
|
11 | 24 | # DSL |
| 25 | +
|
12 | 26 | @docs closeOnSelect |
13 | 27 |
|
| 28 | +
|
14 | 29 | # Events |
| 30 | +
|
15 | 31 | @docs onChange |
16 | 32 |
|
| 33 | +
|
17 | 34 | # View |
| 35 | +
|
18 | 36 | @docs view, render |
19 | 37 |
|
20 | | -# Functions |
21 | 38 | @docs setValue |
| 39 | +
|
| 40 | +
|
| 41 | +# Functions |
| 42 | +
|
22 | 43 | -} |
23 | 44 |
|
24 | 45 | import Html.Events.Extra exposing (onPreventDefault) |
25 | 46 | import Html exposing (node, text) |
26 | 47 | import Html.Lazy |
27 | | - |
28 | 48 | import Date.Extra.Format exposing (isoDateFormat, format) |
29 | 49 | import Date.Extra.Config.Configs as DateConfigs |
30 | 50 | import Time |
31 | 51 | import Date |
32 | | - |
33 | 52 | import Ui.Helpers.Dropdown as Dropdown exposing (Dropdown) |
34 | 53 | import Ui.Helpers.Picker as Picker |
35 | 54 | import Ui.Native.Uid as Uid |
36 | 55 | import Ui.Calendar |
37 | 56 | import Ui.Icons |
38 | 57 | import Ui |
39 | | - |
40 | 58 | import Ui.Styles.DatePicker exposing (defaultStyle) |
41 | 59 | import Ui.Styles |
42 | 60 |
|
| 61 | + |
43 | 62 | {-| Representation of a date picker: |
| 63 | +
|
44 | 64 | - **closeOnSelect** - Whether or not to close the dropdown after selecting |
45 | 65 | - **format** - The format of the date to render in the input |
46 | 66 | - **readonly** - Whether or not the date picker is readonly |
47 | 67 | - **disabled** - Whether or not the date picker is disabled |
48 | 68 | - **uid** - The unique identifier of the date picker |
49 | 69 | - **calendar** - The model of the calendar |
50 | 70 | - **dropdown** - The model of the dropdown |
| 71 | +
|
51 | 72 | -} |
52 | 73 | type alias Model = |
53 | | - { calendar : Ui.Calendar.Model |
54 | | - , closeOnSelect : Bool |
55 | | - , dropdown : Dropdown |
56 | | - , format : String |
57 | | - , disabled : Bool |
58 | | - , readonly : Bool |
59 | | - , uid : String |
60 | | - } |
| 74 | + { calendar : Ui.Calendar.Model |
| 75 | + , closeOnSelect : Bool |
| 76 | + , dropdown : Dropdown |
| 77 | + , format : String |
| 78 | + , disabled : Bool |
| 79 | + , readonly : Bool |
| 80 | + , uid : String |
| 81 | + } |
61 | 82 |
|
62 | 83 |
|
63 | 84 | {-| Messages that a date picker can receive. |
64 | 85 | -} |
65 | 86 | type Msg |
66 | | - = Calendar Ui.Calendar.Msg |
67 | | - | Picker Picker.Msg |
68 | | - | Select Time.Time |
69 | | - | Increment |
70 | | - | Decrement |
71 | | - | NoOp |
| 87 | + = Calendar Ui.Calendar.Msg |
| 88 | + | Picker Picker.Msg |
| 89 | + | Select Time.Time |
| 90 | + | Increment |
| 91 | + | Decrement |
| 92 | + | NoOp |
72 | 93 |
|
73 | 94 |
|
74 | 95 | {-| Initializes a date picker with the given date. |
75 | 96 |
|
76 | 97 | datePicker = |
77 | | - Ui.DatePicker.init () |
78 | | - |> Ui.DatePicker.closeOnSelect true |
| 98 | + Ui.DatePicker.init () |
| 99 | + |> Ui.DatePicker.closeOnSelect true |
| 100 | +
|
79 | 101 | -} |
80 | 102 | init : () -> Model |
81 | 103 | init _ = |
82 | | - { calendar = Ui.Calendar.init () |
83 | | - , dropdown = Dropdown.init |
84 | | - , format = isoDateFormat |
85 | | - , closeOnSelect = False |
86 | | - , disabled = False |
87 | | - , readonly = False |
88 | | - , uid = Uid.uid () |
89 | | - } |
90 | | - |> Dropdown.offset 5 |
| 104 | + { calendar = Ui.Calendar.init () |
| 105 | + , dropdown = Dropdown.init |
| 106 | + , format = isoDateFormat |
| 107 | + , closeOnSelect = False |
| 108 | + , disabled = False |
| 109 | + , readonly = False |
| 110 | + , uid = Uid.uid () |
| 111 | + } |
| 112 | + |> Dropdown.offset 5 |
91 | 113 |
|
92 | 114 |
|
93 | 115 | {-| Subscribe to the changes of a date picker. |
94 | 116 |
|
95 | | - subscriptions = Ui.DatePicker.onChange DatePickerChanged datePicker |
| 117 | + subscriptions = |
| 118 | + Ui.DatePicker.onChange DatePickerChanged datePicker |
| 119 | +
|
96 | 120 | -} |
97 | 121 | onChange : (Time.Time -> msg) -> Model -> Sub msg |
98 | 122 | onChange msg model = |
99 | | - Ui.Calendar.onChange msg model.calendar |
| 123 | + Ui.Calendar.onChange msg model.calendar |
100 | 124 |
|
101 | 125 |
|
102 | 126 | {-| Subscriptions for a date picker. |
103 | 127 |
|
104 | | - subscriptions = Sub.map DatePicker (Ui.DatePicker.subscriptions datePicker) |
| 128 | + subscriptions = |
| 129 | + Sub.map DatePicker (Ui.DatePicker.subscriptions datePicker) |
| 130 | +
|
105 | 131 | -} |
106 | 132 | subscriptions : Model -> Sub Msg |
107 | 133 | subscriptions model = |
108 | | - Sub.batch |
109 | | - [ Ui.Calendar.onChange Select model.calendar |
110 | | - , Sub.map Picker (Picker.subscriptions model) |
111 | | - ] |
| 134 | + Sub.batch |
| 135 | + [ Ui.Calendar.onChange Select model.calendar |
| 136 | + , Sub.map Picker (Picker.subscriptions model) |
| 137 | + ] |
112 | 138 |
|
113 | 139 |
|
114 | 140 | {-| Sets whether or not to close the dropdown when selecting an other date. |
115 | 141 | -} |
116 | 142 | closeOnSelect : Bool -> Model -> Model |
117 | 143 | closeOnSelect value model = |
118 | | - { model | closeOnSelect = value } |
| 144 | + { model | closeOnSelect = value } |
119 | 145 |
|
120 | 146 |
|
121 | 147 | {-| Updates a date picker. |
122 | 148 |
|
123 | | - ( updatedDatePicker, cmd ) = Ui.DatePicker.update msg datePicker |
| 149 | + ( updatedDatePicker, cmd ) = |
| 150 | + Ui.DatePicker.update msg datePicker |
| 151 | +
|
124 | 152 | -} |
125 | 153 | update : Msg -> Model -> ( Model, Cmd Msg ) |
126 | 154 | update action model = |
127 | | - case action of |
128 | | - NoOp -> |
129 | | - ( model, Cmd.none ) |
130 | | - |
131 | | - Calendar act -> |
132 | | - let |
133 | | - ( calendar, effect ) = |
134 | | - Ui.Calendar.update act model.calendar |
135 | | - in |
136 | | - ( { model | calendar = calendar }, Cmd.map Calendar effect ) |
137 | | - |
138 | | - Select time -> |
139 | | - let |
140 | | - updatedModel = |
141 | | - if model.closeOnSelect then |
142 | | - Dropdown.close model |
143 | | - else |
144 | | - model |
145 | | - in |
146 | | - ( updatedModel, Cmd.none ) |
147 | | - |
148 | | - Picker act -> |
149 | | - ( Picker.update act model, Cmd.none ) |
150 | | - |
151 | | - Decrement -> |
152 | | - ( { model | calendar = Ui.Calendar.previousDay model.calendar } |
153 | | - |> Dropdown.open |
154 | | - , Cmd.none |
155 | | - ) |
156 | | - |
157 | | - Increment -> |
158 | | - ( { model | calendar = Ui.Calendar.nextDay model.calendar } |
159 | | - |> Dropdown.open |
160 | | - , Cmd.none |
161 | | - ) |
| 155 | + case action of |
| 156 | + NoOp -> |
| 157 | + ( model, Cmd.none ) |
| 158 | + |
| 159 | + Calendar act -> |
| 160 | + let |
| 161 | + ( calendar, effect ) = |
| 162 | + Ui.Calendar.update act model.calendar |
| 163 | + in |
| 164 | + ( { model | calendar = calendar }, Cmd.map Calendar effect ) |
| 165 | + |
| 166 | + Select time -> |
| 167 | + let |
| 168 | + updatedModel = |
| 169 | + if model.closeOnSelect then |
| 170 | + Dropdown.close model |
| 171 | + else |
| 172 | + model |
| 173 | + in |
| 174 | + ( updatedModel, Cmd.none ) |
| 175 | + |
| 176 | + Picker act -> |
| 177 | + ( Picker.update act model, Cmd.none ) |
| 178 | + |
| 179 | + Decrement -> |
| 180 | + ( { model | calendar = Ui.Calendar.previousDay model.calendar } |
| 181 | + |> Dropdown.open |
| 182 | + , Cmd.none |
| 183 | + ) |
| 184 | + |
| 185 | + Increment -> |
| 186 | + ( { model | calendar = Ui.Calendar.nextDay model.calendar } |
| 187 | + |> Dropdown.open |
| 188 | + , Cmd.none |
| 189 | + ) |
162 | 190 |
|
163 | 191 |
|
164 | 192 | {-| Lazily renders a date picker in the given locale. |
165 | 193 |
|
166 | 194 | Ui.DatePicker.view "en_us" model |
| 195 | +
|
167 | 196 | -} |
168 | 197 | view : String -> Model -> Html.Html Msg |
169 | 198 | view locale model = |
170 | | - Html.Lazy.lazy2 render locale model |
| 199 | + Html.Lazy.lazy2 render locale model |
171 | 200 |
|
172 | 201 |
|
173 | 202 | {-| Renders a date picker in the given locale. |
174 | 203 |
|
175 | 204 | Ui.DatePicker.render "en_us" model |
| 205 | +
|
176 | 206 | -} |
177 | 207 | render : String -> Model -> Html.Html Msg |
178 | 208 | render locale model = |
179 | | - let |
180 | | - dateText = |
181 | | - (format (DateConfigs.getConfig locale) model.format model.calendar.value) |
182 | | - in |
183 | | - Picker.view |
184 | | - { attributes = Ui.Styles.apply defaultStyle |
185 | | - , address = Picker |
186 | | - , keyActions = |
187 | | - [ ( 40, Increment ) |
188 | | - , ( 38, Decrement ) |
189 | | - , ( 39, Increment ) |
190 | | - , ( 37, Decrement ) |
191 | | - ] |
192 | | - , contents = |
193 | | - [ node "ui-date-picker-content" [] [ text dateText ] |
194 | | - , Ui.Icons.calendar [] |
195 | | - ] |
196 | | - , dropdownContents = |
197 | | - [ node "ui-date-picker-calendar" |
198 | | - [ onPreventDefault "mousedown" NoOp ] |
199 | | - [ Html.map Calendar (Ui.Calendar.view locale model.calendar) |
200 | | - ] |
201 | | - ] |
202 | | - } model |
| 209 | + let |
| 210 | + dateText = |
| 211 | + (format (DateConfigs.getConfig locale) model.format model.calendar.value) |
| 212 | + in |
| 213 | + Picker.view |
| 214 | + { attributes = Ui.Styles.apply defaultStyle |
| 215 | + , address = Picker |
| 216 | + , keyActions = |
| 217 | + [ ( 40, Increment ) |
| 218 | + , ( 38, Decrement ) |
| 219 | + , ( 39, Increment ) |
| 220 | + , ( 37, Decrement ) |
| 221 | + ] |
| 222 | + , contents = |
| 223 | + [ node "ui-date-picker-content" [] [ text dateText ] |
| 224 | + , Ui.Icons.calendar [] |
| 225 | + ] |
| 226 | + , dropdownContents = |
| 227 | + [ node "ui-date-picker-calendar" |
| 228 | + [ onPreventDefault "mousedown" NoOp ] |
| 229 | + [ Html.map Calendar (Ui.Calendar.view locale model.calendar) |
| 230 | + ] |
| 231 | + ] |
| 232 | + } |
| 233 | + model |
203 | 234 |
|
204 | 235 |
|
205 | 236 | {-| Sets the value of a date picker |
206 | 237 |
|
207 | 238 | ( updatedDatePicker, cmd ) = |
208 | | - Ui.DatePicker.setValue (Ext.Date.create 1980 5 17) datePicker |
| 239 | + Ui.DatePicker.setValue (Ext.Date.create 1980 5 17) datePicker |
| 240 | +
|
209 | 241 | -} |
210 | 242 | setValue : Date.Date -> Model -> Model |
211 | 243 | setValue date model = |
212 | | - { model | calendar = Ui.Calendar.setValue date model.calendar } |
| 244 | + { model | calendar = Ui.Calendar.setValue date model.calendar } |
0 commit comments