Skip to content

Commit f212619

Browse files
committed
add examples
1 parent a77e61f commit f212619

37 files changed

+340
-67
lines changed

src/demo-app/system-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ System.config({
2929

3030
// TODO(devversion): replace once the index.ts file for the Material package has been added.
3131
'@angular/material': 'dist/packages/material/public-api.js',
32+
'@angular/material-moment-adapter': 'dist/packages/material-moment-adapter/public-api.js',
3233
'@angular/cdk': 'dist/packages/cdk/index.js',
3334
'@angular/cdk/a11y': 'dist/packages/cdk/a11y/index.js',
3435
'@angular/cdk/accordion': 'dist/packages/cdk/accordion/index.js',

src/demo-app/tsconfig-aot.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
{
55
"extends": "./tsconfig-build",
66
"compilerOptions": {
7+
// Needed for Moment.js since it doesn't have a default export.
8+
"allowSyntheticDefaultImports": true,
79
"experimentalDecorators": true,
810
// TODO(paul): Remove once Angular has been upgraded and supports noUnusedParameters in AOT.
911
"noUnusedParameters": false,

src/demo-app/tsconfig-build.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// since the demo-app will be served in the browser.
33
{
44
"compilerOptions": {
5+
// Needed for Moment.js since it doesn't have a default export.
6+
"allowSyntheticDefaultImports": true,
57
"declaration": false,
68
"emitDecoratorMetadata": true,
79
"experimentalDecorators": true,

src/demo-app/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
{
33
"extends": "../../tsconfig.json",
44
"compilerOptions": {
5+
// Needed for Moment.js since it doesn't have a default export.
6+
"allowSyntheticDefaultImports": true,
57
"rootDir": "..",
68
"baseUrl": ".",
79
"paths": {
810
"@angular/cdk/*": ["../cdk/*"],
911
"@angular/material/*": ["../lib/*"],
10-
"@angular/material": ["../lib/public-api.ts"]
12+
"@angular/material": ["../lib/public-api.ts"],
13+
"@angular/material-moment-adapter": ["../material-moment-adapter/public-api.ts"]
1114
}
1215
},
1316
"include": ["./**/*.ts"]

src/lib/datepicker/datepicker-errors.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
export function createMissingDateImplError(provider: string) {
1111
return Error(
1212
`MatDatepicker: No provider found for ${provider}. You must import one of the following ` +
13-
`modules at your application root: MatNativeDateModule, or provide a custom implementation.`);
13+
`modules at your application root: MatNativeDateModule, MatMomentDateModule, or provide a ` +
14+
`custom implementation.`);
1415
}

src/lib/datepicker/datepicker.md

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ code.
6767
As with other types of `<input>`, the datepicker works with `@angular/forms` directives such as
6868
`formGroup`, `formControl`, `ngModel`, etc.
6969

70-
<!-- TODO - example -->
70+
<!-- example(datepicker-value) -->
7171

7272
### Date validation
7373

@@ -111,12 +111,7 @@ The `(dateInput)` event will fire whenever the value changes due to the user typ
111111
date from the calendar. The `(dateChange)` event will fire whenever the user finishes typing input
112112
(on `<input>` blur), or when the user chooses a date from the calendar.
113113

114-
<!-- TODO - example -->
115-
116-
```html
117-
<input [matDatepicker]="d" (dateInput)="onInput($event)" (dateChange)="onChange($event)">
118-
<mat-datepicker #d></mat-datepicker>
119-
```
114+
<!-- example(datepicker-events) -->
120115

121116
### Disabling parts of the datepicker
122117

@@ -126,7 +121,7 @@ their disabled state from the `<input>`, but this can be overridden by setting t
126121
property on the datepicker or toggle elements. This can be useful if you want to disable text input
127122
but allow selection via the calendar or vice-versa.
128123

129-
<!-- TODO example -->
124+
<!-- example(datepicker-disabled) -->
130125

131126
### Touch UI mode
132127

@@ -169,21 +164,7 @@ export class MyApp {}
169164

170165
It's also possible to set the locale at runtime using the `setLocale` method of the `DateAdapter`.
171166

172-
```ts
173-
import { DateAdapter, NativeDateAdapter } from '@angular/material';
174-
175-
@Component({
176-
selector: 'foo',
177-
template: ''
178-
})
179-
export class FooComponent {
180-
constructor(dateAdapter: DateAdapter<NativeDateAdapter>) {
181-
dateAdapter.setLocale('de-DE');
182-
}
183-
}
184-
```
185-
186-
<!-- TODO - example -->
167+
<!-- example(datepicker-locale) -->
187168

188169
#### Choosing a date implementation and date format settings
189170

@@ -224,10 +205,12 @@ the native `Date` object is the inability to set the parse format. We highly rec
224205
`MomentDateAdapter` or a custom `DateAdapter` that works with the formatting/parsing library of your
225206
choice.*
226207

227-
<!-- TODO - example -->
208+
<!-- example(datepicker-moment) -->
228209

229210
#### Customizing the date implementation
230211

212+
<!-- TODO(mmalerba): Add a guide about this -->
213+
231214
The datepicker does all of its interaction with date objects via the `DateAdapter`. Making the
232215
datepicker work with a different date implementation is as easy as extending `DateAdapter`, and
233216
using your subclass as the provider. You will also want to make sure that the `MAT_DATE_FORMATS`
@@ -244,27 +227,28 @@ provided in your app are formats that can be understood by your date implementat
244227
export class MyApp {}
245228
```
246229

247-
<!-- TODO guide -->
248-
249230
#### Customizing the parse and display formats
250231

251232
The `MAT_DATE_FORMATS` object is just a collection of formats that the datepicker uses when parsing
252233
and displaying dates. These formats are passed through to the `DateAdapter` so you will want to make
253234
sure that the format objects you're using are compatible with the `DateAdapter` used in your app.
254-
This example shows how to use the `NativeDateAdapter`, but with custom formats.
235+
236+
If you want use one of the `DateAdapters` that ships with Angular Material, but use your own
237+
`MAT_DATE_FORMATS` you can import the `NativeDateModule` or `MomentDateModule`. These modules are
238+
identical to the "Mat"-prefixed versions (`MatNativeDateModule` and `MatMomentDateModule`) except
239+
they do not include the default formats. For example:
255240

256241
```ts
257242
@NgModule({
258-
imports: [MatDatepickerModule],
243+
imports: [MatDatepickerModule, NativeDateModule],
259244
providers: [
260-
{provide: DateAdapter, useClass: NativeDateAdapter},
261245
{provide: MAT_DATE_FORMATS, useValue: MY_NATIVE_DATE_FORMATS},
262246
],
263247
})
264248
export class MyApp {}
265249
```
266250

267-
<!-- TODO example -->
251+
<!-- example(datepicker-formats) -->
268252

269253
#### Localizing labels and messages
270254

@@ -282,8 +266,6 @@ application root module.
282266
export class MyApp {}
283267
```
284268

285-
<!-- TODO example -->
286-
287269
### Accessibility
288270

289271
The `MatDatepickerInput` directive adds `aria-haspopup` attribute to the native input element, and it
@@ -349,4 +331,27 @@ will be added in future iterations, including:
349331

350332
### Troubleshooting
351333

352-
<!-- TODO: fill in this section -->
334+
#### Error: MatDatepicker: No provider found for DateAdapter/MAT_DATE_FORMATS
335+
336+
This error is thrown if you have not provided all of the injectables the datepicker needs to work.
337+
The easiest way to resolve this is to import the `MatNativeDateModule` or `MatMomentDateModule` in
338+
your application's root module. See the section on
339+
[choosing a date implementation](#choosing-a-date-implementation-and-date-format-settings)) for more
340+
information.
341+
342+
#### Error: A MatDatepicker can only be associated with a single input
343+
344+
This error is thrown if more than one `<input>` tries to claim ownership over the same
345+
`<mat-datepicker>` (via the `matDatepicker` attribute on the input). A datepicker can only be
346+
associated with a single input.
347+
348+
#### Error: Attempted to open an MatDatepicker with no associated input.
349+
350+
This error occurs if your `<mat-datepicker>` is not associated with any `<input>`. To associate an
351+
input with your datepicker, create a template reference for the datepicker and assign it to the
352+
`matDatepicker` attribute on the input:
353+
354+
```html
355+
<input [matDatepicker]="picker">
356+
<mat-datepicker #picker></mat-datepicker>
357+
```

src/lib/datepicker/datepicker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export class MatDatepicker<D> implements OnDestroy {
250250
*/
251251
_registerInput(input: MatDatepickerInput<D>): void {
252252
if (this._datepickerInput) {
253-
throw Error('An MatDatepicker can only be associated with a single input.');
253+
throw Error('A MatDatepicker can only be associated with a single input.');
254254
}
255255
this._datepickerInput = input;
256256
this._inputSubscription =
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import {Component} from '@angular/core';
22

3-
/**
4-
* @title Datepicker API
5-
*/
3+
/** @title Datepicker open method */
64
@Component({
75
selector: 'datepicker-api-example',
86
templateUrl: 'datepicker-api-example.html',
97
styleUrls: ['datepicker-api-example.css'],
108
})
11-
export class DatepickerApiExample {
12-
}
9+
export class DatepickerApiExample {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/** No CSS for this example */
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<p>
2+
<mat-form-field>
3+
<input matInput [matDatepicker]="dp1" placeholder="Completely disabled" disabled>
4+
<mat-datepicker-toggle matSuffix [for]="dp1"></mat-datepicker-toggle>
5+
<mat-datepicker #dp1></mat-datepicker>
6+
</mat-form-field>
7+
</p>
8+
9+
<p>
10+
<mat-form-field>
11+
<input matInput [matDatepicker]="dp2" placeholder="Popup disabled">
12+
<mat-datepicker-toggle matSuffix [for]="dp2" disabled></mat-datepicker-toggle>
13+
<mat-datepicker #dp2></mat-datepicker>
14+
</mat-form-field>
15+
</p>
16+
17+
<p>
18+
<mat-form-field>
19+
<input matInput [matDatepicker]="dp3" placeholder="Input disabled" disabled>
20+
<mat-datepicker-toggle matSuffix [for]="dp3"></mat-datepicker-toggle>
21+
<mat-datepicker #dp3 disabled="false"></mat-datepicker>
22+
</mat-form-field>
23+
</p>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {Component} from '@angular/core';
2+
import {FormBuilder, FormGroup} from '@angular/forms';
3+
4+
/** @title Disabled datepicker */
5+
@Component({
6+
selector: 'datepicker-disabled-example',
7+
templateUrl: 'datepicker-disabled-example.html',
8+
styleUrls: ['datepicker-disabled-example.css'],
9+
})
10+
export class DatepickerDisabledExample {
11+
disabled: FormGroup;
12+
13+
constructor(fb: FormBuilder) {
14+
this.disabled = fb.group({
15+
'input': null,
16+
'popup': null,
17+
'toggle': null,
18+
});
19+
}
20+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.example-events {
2+
width: 400px;
3+
height: 200px;
4+
border: 1px solid #555;
5+
overflow: auto;
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<mat-form-field>
2+
<input matInput [matDatepicker]="picker" placeholder="Input & change events"
3+
(dateInput)="addEvent('input', $event)" (dateChange)="addEvent('change', $event)">
4+
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
5+
<mat-datepicker #picker></mat-datepicker>
6+
</mat-form-field>
7+
8+
<div class="example-events">
9+
<div *ngFor="let e of events">{{e}}</div>
10+
</div>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {Component} from '@angular/core';
2+
import {MatDatepickerInputEvent} from '@angular/material/datepicker';
3+
4+
/** @title Datepicker input and change events */
5+
@Component({
6+
selector: 'datepicker-events-example',
7+
templateUrl: 'datepicker-events-example.html',
8+
styleUrls: ['datepicker-events-example.css'],
9+
})
10+
export class DatepickerEventsExample {
11+
events: string[] = [];
12+
13+
addEvent(type: string, event: MatDatepickerInputEvent<Date>) {
14+
this.events.push(`${type}: ${event.value}`);
15+
}
16+
}

src/material-examples/datepicker-filter/datepicker-filter-example.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import {Component} from '@angular/core';
22

3-
/**
4-
* @title Datepicker Filter
5-
*/
3+
/** @title Datepicker with filter validation */
64
@Component({
75
selector: 'datepicker-filter-example',
86
templateUrl: 'datepicker-filter-example.html',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/** No CSS for this example */
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<mat-form-field>
2+
<input matInput [matDatepicker]="dp" placeholder="Verbose datepicker" [formControl]="date">
3+
<mat-datepicker-toggle matSuffix [for]="dp"></mat-datepicker-toggle>
4+
<mat-datepicker #dp></mat-datepicker>
5+
</mat-form-field>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {Component} from '@angular/core';
2+
import {FormControl} from '@angular/forms';
3+
import {MomentDateAdapter} from '@angular/material-moment-adapter';
4+
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';
5+
6+
// Depending on whether rollup is used, moment needs to be imported differently.
7+
// Since Moment.js doesn't have a default export, we normally need to import using the `* as`
8+
// syntax. However, rollup creates a synthetic default module and we thus need to import it using
9+
// the `default as` syntax.
10+
import * as _moment from 'moment';
11+
import {default as _rollupMoment} from 'moment';
12+
const moment = _rollupMoment || _moment;
13+
14+
// See the Moment.js docs for the meaning of these formats:
15+
// https://momentjs.com/docs/#/displaying/format/
16+
export const MY_FORMATS = {
17+
parse: {
18+
dateInput: 'LL',
19+
},
20+
display: {
21+
dateInput: 'LL',
22+
monthYearLabel: 'MMM YYYY',
23+
dateA11yLabel: 'LL',
24+
monthYearA11yLabel: 'MMMM YYYY',
25+
},
26+
};
27+
28+
/** @title Datepicker with custom formats */
29+
@Component({
30+
selector: 'datepicker-formats-example',
31+
templateUrl: 'datepicker-formats-example.html',
32+
styleUrls: ['datepicker-formats-example.css'],
33+
providers: [
34+
// `MomentDateAdapter` can be automatically provided by importing `MomentDateModule` in your
35+
// application's root module. We provide it at the component level here, due to limitations of
36+
// our example generation script.
37+
{provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},
38+
39+
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
40+
],
41+
})
42+
export class DatepickerFormatsExample {
43+
date = new FormControl(moment());
44+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/** No CSS for this example */
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<mat-form-field>
2+
<input matInput [matDatepicker]="dp" placeholder="Different locale">
3+
<mat-datepicker-toggle matSuffix [for]="dp"></mat-datepicker-toggle>
4+
<mat-datepicker #dp></mat-datepicker>
5+
</mat-form-field>
6+
7+
<button mat-button (click)="french()">Dynamically switch to French</button>

0 commit comments

Comments
 (0)