Skip to content

Commit a77e61f

Browse files
committed
make some tweaks to datepicker docs
1 parent 9673f63 commit a77e61f

File tree

1 file changed

+99
-27
lines changed

1 file changed

+99
-27
lines changed

src/lib/datepicker/datepicker.md

Lines changed: 99 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
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.
33

44
<!-- example(datepicker-overview) -->
55

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-
156
### Connecting a datepicker to an input
7+
168
A datepicker is composed of a text input and a calendar pop-up, connected via the `matDatepicker`
179
property on the text input.
1810

@@ -41,19 +33,44 @@ can easily be used as a prefix or suffix on the material input:
4133
```
4234

4335
### Setting the calendar starting view
36+
4437
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
4639
then proceed to month view after choosing a month.
4740

4841
The month or year that the calendar opens to is determined by first checking if any date is
4942
currently selected, if so it will open to the month or year containing that date. Otherwise it will
5043
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
5245
containing the `startAt` date.
5346

5447
<!-- example(datepicker-start-view) -->
5548

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+
5672
### Date validation
73+
5774
There are three properties that add date validation to the datepicker input. The first two are the
5875
`min` and `max` properties. In addition to enforcing validation on the input, these properties will
5976
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:
84101
* A value that violates the `matDatepickerFilter` property will have a `matDatepickerFilter` error.
85102

86103
### 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 -->
91115

92116
```html
93117
<input [matDatepicker]="d" (dateInput)="onInput($event)" (dateChange)="onChange($event)">
94118
<mat-datepicker #d></mat-datepicker>
95119
```
96120

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+
97131
### Touch UI mode
132+
98133
The datepicker normally opens as a popup under the input. However this is not ideal for touch
99134
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.
102137

103138
<!-- example(datepicker-touch) -->
104139

105140
### Manually opening and closing the calendar
141+
106142
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.
108144

109145
<!-- example(datepicker-api) -->
110146

111147
### Internationalization
148+
112149
In order to support internationalization, the datepicker supports customization of the following
113150
three pieces via injection:
114151
1. The date implementation that the datepicker accepts.
115152
2. The display and parse formats used by the datepicker.
116153
3. The message strings used in the datepicker's UI.
117154

118155
#### Setting the locale code
156+
119157
By default, the `MAT_DATE_LOCALE` injection token will use the existing `LOCALE_ID` locale code
120158
from `@angular/core`. If you want to override it, you can provide a new value for the
121159
`MAT_DATE_LOCALE` token:
@@ -145,14 +183,19 @@ export class FooComponent {
145183
}
146184
```
147185

186+
<!-- TODO - example -->
187+
148188
#### Choosing a date implementation and date format settings
189+
149190
The datepicker was built to be date implementation agnostic. This means that it can be made to work
150191
with a variety of different date implementations. However it also means that developers need to make
151192
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`|
156199

157200
These modules include providers for `DateAdapter` and `MAT_DATE_FORMATS`
158201

@@ -177,10 +220,14 @@ export class MyComponent {
177220

178221
*Please note: `MatNativeDateModule` is based off of the functionality available in JavaScript's
179222
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 -->
182228

183229
#### Customizing the date implementation
230+
184231
The datepicker does all of its interaction with date objects via the `DateAdapter`. Making the
185232
datepicker work with a different date implementation is as easy as extending `DateAdapter`, and
186233
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
197244
export class MyApp {}
198245
```
199246

247+
<!-- TODO guide -->
248+
200249
#### Customizing the parse and display formats
250+
201251
The `MAT_DATE_FORMATS` object is just a collection of formats that the datepicker uses when parsing
202252
and displaying dates. These formats are passed through to the `DateAdapter` so you will want to make
203253
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.
206255

207256
```ts
208257
@NgModule({
@@ -215,7 +264,10 @@ formats.
215264
export class MyApp {}
216265
```
217266

267+
<!-- TODO example -->
268+
218269
#### Localizing labels and messages
270+
219271
The various text strings used by the datepicker are provided through `MatDatepickerIntl`.
220272
Localization of these messages can be done by providing a subclass with translated values in your
221273
application root module.
@@ -229,7 +281,11 @@ application root module.
229281
})
230282
export class MyApp {}
231283
```
284+
285+
<!-- TODO example -->
286+
232287
### Accessibility
288+
233289
The `MatDatepickerInput` directive adds `aria-haspopup` attribute to the native input element, and it
234290
triggers a calendar dialog with `role="dialog"`.
235291

@@ -238,6 +294,7 @@ should have a placeholder or be given a meaningful label via `aria-label`, `aria
238294
`MatDatepickerIntl`.
239295

240296
#### Keyboard shortcuts
297+
241298
The keyboard shortcuts to handle datepicker are:
242299

243300
| Shortcut | Action |
@@ -278,3 +335,18 @@ In year view:
278335
| `PAGE_DOWN` | Go to next year |
279336
| `ALT` + `PAGE_DOWN` | Go to next 10 years |
280337
| `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

Comments
 (0)