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