1
1
The datepicker allows users to enter a date either through text input, or by choosing a date from
2
- the calendar. It is made up of several components and directives that work together:
2
+ the calendar. It is made up of several components and directives that work together.
3
3
4
4
<!-- example(datepicker-overview) -->
5
5
6
- ### Current state
7
- Currently the datepicker is in the beginning stages and supports basic date selection functionality.
8
- There are many more features that will be added in future iterations, including:
9
- * Support for datetimes (e.g. May 2, 2017 at 12:30pm) and month + year only (e.g. May 2017)
10
- * Support for selecting and displaying date ranges
11
- * Support for custom time zones
12
- * Infinite scrolling through calendar months
13
- * Built in support for [ Moment.js] ( https://momentjs.com/ ) dates
14
-
15
6
### Connecting a datepicker to an input
7
+
16
8
A datepicker is composed of a text input and a calendar pop-up, connected via the ` matDatepicker `
17
9
property on the text input.
18
10
@@ -41,19 +33,44 @@ can easily be used as a prefix or suffix on the material input:
41
33
```
42
34
43
35
### Setting the calendar starting view
36
+
44
37
By default the calendar will open in month view, this can be changed by setting the ` startView `
45
- property of ` mat-datepicker ` to ` " year" ` . In year view the user will see all months of the year and
38
+ property of ` < mat-datepicker> ` to ` year ` . In year view the user will see all months of the year and
46
39
then proceed to month view after choosing a month.
47
40
48
41
The month or year that the calendar opens to is determined by first checking if any date is
49
42
currently selected, if so it will open to the month or year containing that date. Otherwise it will
50
43
open to the month or year containing today's date. This behavior can be overridden by using the
51
- ` startAt ` property of ` mat-datepicker ` . In this case the calendar will open to the month or year
44
+ ` startAt ` property of ` < mat-datepicker> ` . In this case the calendar will open to the month or year
52
45
containing the ` startAt ` date.
53
46
54
47
<!-- example(datepicker-start-view) -->
55
48
49
+ ### Setting the selected date
50
+
51
+ The type of values that the datepicker expects actually depends on the particular ` DateAdapter ` that
52
+ you're using in your app. For example when using the ` NativeDateAdapter ` the datepicker expects
53
+ ` Date ` objects, and when using the ` MomentDateAdapter ` it expects ` Moment ` objects. You can also
54
+ create your own ` DateAdapter ` that works with whatever date type you want. (For more information
55
+ about this see the section on
56
+ [ choosing a date implementation] ( #choosing-a-date-implementation-and-date-format-settings ) ).
57
+
58
+ Depending on the ` DateAdapter ` you're using, the datepicker may automatically deserialize certain
59
+ date formats for you as well. For example, both the ` NativeDateAdapter ` and ` MomentDateAdapter `
60
+ allow [ ISO 8601] ( https://tools.ietf.org/html/rfc3339 ) strings to be passed to the datepicker and
61
+ automatically converted to the proper object type. This can be convenient when binding data directly
62
+ from your backend to the datepicker. However, when possible, you should pass the appropriate object
63
+ type. The datepicker will not accept date strings formatted in user format such as ` "1/2/2017" ` as
64
+ this is ambiguous and will mean different things depending on the locale of the browser running the
65
+ code.
66
+
67
+ As with other types of ` <input> ` , the datepicker works with ` @angular/forms ` directives such as
68
+ ` formGroup ` , ` formControl ` , ` ngModel ` , etc.
69
+
70
+ <!-- TODO - example -->
71
+
56
72
### Date validation
73
+
57
74
There are three properties that add date validation to the datepicker input. The first two are the
58
75
` min ` and ` max ` properties. In addition to enforcing validation on the input, these properties will
59
76
disable all dates on the calendar popup before or after the respective values and prevent the user
@@ -84,38 +101,59 @@ Each validation property has a different error that can be checked:
84
101
* A value that violates the ` matDatepickerFilter ` property will have a ` matDatepickerFilter ` error.
85
102
86
103
### Input and change events
87
- The input's native ` input ` and ` change ` events will only trigger due to user interaction with the
88
- input element; they will not fire when the user selects a date from the calendar popup. Because of
89
- this limitation, the datepicker input also has support for ` dateInput ` and ` dateChange ` events.
90
- These trigger when the user interacts with either the input or the popup.
104
+
105
+ The input's native ` (input) ` and ` (change) ` events will only trigger due to user interaction with
106
+ the input element; they will not fire when the user selects a date from the calendar popup. Because
107
+ of this limitation, the datepicker input also has support for ` (dateInput) ` and ` (dateChange) `
108
+ events. These trigger when the user interacts with either the input or the popup.
109
+
110
+ The ` (dateInput) ` event will fire whenever the value changes due to the user typing or selecting a
111
+ date from the calendar. The ` (dateChange) ` event will fire whenever the user finishes typing input
112
+ (on ` <input> ` blur), or when the user chooses a date from the calendar.
113
+
114
+ <!-- TODO - example -->
91
115
92
116
``` html
93
117
<input [matDatepicker] =" d" (dateInput) =" onInput($event)" (dateChange) =" onChange($event)" >
94
118
<mat-datepicker #d ></mat-datepicker >
95
119
```
96
120
121
+ ### Disabling parts of the datepicker
122
+
123
+ As with any standard ` <input> ` , it is possible to disable the datepicker input by adding the
124
+ ` disabled ` property. By default the ` <mat-datepicker> ` and ` <mat-datepicker-toggle> ` will inherit
125
+ their disabled state from the ` <input> ` , but this can be overridden by setting the ` disabled `
126
+ property on the datepicker or toggle elements. This can be useful if you want to disable text input
127
+ but allow selection via the calendar or vice-versa.
128
+
129
+ <!-- TODO example -->
130
+
97
131
### Touch UI mode
132
+
98
133
The datepicker normally opens as a popup under the input. However this is not ideal for touch
99
134
devices that don't have as much screen real estate and need bigger click targets. For this reason
100
- ` mat-datepicker ` has a ` touchUi ` property that can be set to ` true ` in order to enable a more touch
101
- friendly UI where the calendar opens in a large dialog.
135
+ ` < mat-datepicker> ` has a ` touchUi ` property that can be set to ` true ` in order to enable a more
136
+ touch friendly UI where the calendar opens in a large dialog.
102
137
103
138
<!-- example(datepicker-touch) -->
104
139
105
140
### Manually opening and closing the calendar
141
+
106
142
The calendar popup can be programmatically controlled using the ` open ` and ` close ` methods on the
107
- ` mat-datepicker ` . It also has an ` opened ` property that reflects the status of the popup.
143
+ ` < mat-datepicker> ` . It also has an ` opened ` property that reflects the status of the popup.
108
144
109
145
<!-- example(datepicker-api) -->
110
146
111
147
### Internationalization
148
+
112
149
In order to support internationalization, the datepicker supports customization of the following
113
150
three pieces via injection:
114
151
1 . The date implementation that the datepicker accepts.
115
152
2 . The display and parse formats used by the datepicker.
116
153
3 . The message strings used in the datepicker's UI.
117
154
118
155
#### Setting the locale code
156
+
119
157
By default, the ` MAT_DATE_LOCALE ` injection token will use the existing ` LOCALE_ID ` locale code
120
158
from ` @angular/core ` . If you want to override it, you can provide a new value for the
121
159
` MAT_DATE_LOCALE ` token:
@@ -145,14 +183,19 @@ export class FooComponent {
145
183
}
146
184
```
147
185
186
+ <!-- TODO - example -->
187
+
148
188
#### Choosing a date implementation and date format settings
189
+
149
190
The datepicker was built to be date implementation agnostic. This means that it can be made to work
150
191
with a variety of different date implementations. However it also means that developers need to make
151
192
sure to provide the appropriate pieces for the datepicker to work with their chosen implementation.
152
- The easiest way to ensure this is just to import one of the pre-made modules (currently
153
- ` MatNativeDateModule ` is the only implementation that ships with material, but there are plans to add
154
- a module for Moment.js support):
155
- * ` MatNativeDateModule ` - support for native JavaScript Date object
193
+ The easiest way to ensure this is just to import one of the pre-made modules:
194
+
195
+ | Module | Date type| Supported locales | Dependencies | Import from |
196
+ | ---------------------| ---------| -----------------------------------------------------------------------| ----------------------------------| ----------------------------------|
197
+ | ` MatNativeDateModule ` | ` Date ` | en-US | None | ` @angular/material ` |
198
+ | ` MatMomentDateModule ` | ` Moment ` | [ See project] ( https://github.com/moment/moment/tree/develop/src/locale ) | [ Moment.js] ( https://momentjs.com/ ) | ` @angular/material-moment-adapter ` |
156
199
157
200
These modules include providers for ` DateAdapter ` and ` MAT_DATE_FORMATS `
158
201
@@ -177,10 +220,14 @@ export class MyComponent {
177
220
178
221
* Please note: ` MatNativeDateModule ` is based off of the functionality available in JavaScript's
179
222
native ` Date ` object, and is thus not suitable for many locales. One of the biggest shortcomings of
180
- the native ` Date ` object is the inability to set the parse format. We highly recommend using a
181
- custom ` DateAdapter ` that works with the formatting/parsing library of your choice.*
223
+ the native ` Date ` object is the inability to set the parse format. We highly recommend using the
224
+ ` MomentDateAdapter ` or a custom ` DateAdapter ` that works with the formatting/parsing library of your
225
+ choice.*
226
+
227
+ <!-- TODO - example -->
182
228
183
229
#### Customizing the date implementation
230
+
184
231
The datepicker does all of its interaction with date objects via the ` DateAdapter ` . Making the
185
232
datepicker work with a different date implementation is as easy as extending ` DateAdapter ` , and
186
233
using your subclass as the provider. You will also want to make sure that the ` MAT_DATE_FORMATS `
@@ -197,12 +244,14 @@ provided in your app are formats that can be understood by your date implementat
197
244
export class MyApp {}
198
245
```
199
246
247
+ <!-- TODO guide -->
248
+
200
249
#### Customizing the parse and display formats
250
+
201
251
The ` MAT_DATE_FORMATS ` object is just a collection of formats that the datepicker uses when parsing
202
252
and displaying dates. These formats are passed through to the ` DateAdapter ` so you will want to make
203
253
sure that the format objects you're using are compatible with the ` DateAdapter ` used in your app.
204
- This example shows how to use the native ` Date ` implementation from material, but with custom
205
- formats.
254
+ This example shows how to use the ` NativeDateAdapter ` , but with custom formats.
206
255
207
256
``` ts
208
257
@NgModule ({
@@ -215,7 +264,10 @@ formats.
215
264
export class MyApp {}
216
265
```
217
266
267
+ <!-- TODO example -->
268
+
218
269
#### Localizing labels and messages
270
+
219
271
The various text strings used by the datepicker are provided through ` MatDatepickerIntl ` .
220
272
Localization of these messages can be done by providing a subclass with translated values in your
221
273
application root module.
@@ -229,7 +281,11 @@ application root module.
229
281
})
230
282
export class MyApp {}
231
283
```
284
+
285
+ <!-- TODO example -->
286
+
232
287
### Accessibility
288
+
233
289
The ` MatDatepickerInput ` directive adds ` aria-haspopup ` attribute to the native input element, and it
234
290
triggers a calendar dialog with ` role="dialog" ` .
235
291
@@ -238,6 +294,7 @@ should have a placeholder or be given a meaningful label via `aria-label`, `aria
238
294
` MatDatepickerIntl ` .
239
295
240
296
#### Keyboard shortcuts
297
+
241
298
The keyboard shortcuts to handle datepicker are:
242
299
243
300
| Shortcut | Action |
@@ -278,3 +335,18 @@ In year view:
278
335
| ` PAGE_DOWN ` | Go to next year |
279
336
| ` ALT ` + ` PAGE_DOWN ` | Go to next 10 years |
280
337
| ` ENTER ` | Select current month |
338
+
339
+ ### Future work
340
+
341
+ Currently the datepicker supports basic date selection functionality. There are more features that
342
+ will be added in future iterations, including:
343
+ * Support for datetimes (e.g. May 2, 2017 at 12:30pm) and month + year only (e.g. May 2017)
344
+ * Support for selecting and displaying date ranges
345
+ * Support for custom time zones
346
+ * Infinite scrolling through calendar months
347
+ * Easier year selection
348
+ * Custom views for the calendar popup
349
+
350
+ ### Troubleshooting
351
+
352
+ <!-- TODO: fill in this section -->
0 commit comments