Skip to content

Commit c42417a

Browse files
committed
add locale switching to demo
1 parent e5deec0 commit c42417a

File tree

8 files changed

+43
-7
lines changed

8 files changed

+43
-7
lines changed

src/demo-app/datepicker/datepicker-demo.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,9 @@ <h2>Input disabled, datepicker popup enabled</h2>
107107
</p>
108108

109109
<h2>Moment.js Datepicker</h2>
110-
<p><demo-moment-datepicker></demo-moment-datepicker></p>
110+
<p>
111+
<md-select placeholder="Locale" [(ngModel)]="locale">
112+
<md-option *ngFor="let l of locales" [value]="l">{{l}}</md-option>
113+
</md-select>
114+
</p>
115+
<p><demo-moment-datepicker [locale]="locale"></demo-moment-datepicker></p>

src/demo-app/datepicker/datepicker-demo.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export class DatepickerDemo {
2121
date: Date;
2222
lastDateInput: Date | null;
2323
lastDateChange: Date | null;
24+
locales = ['en', 'fr', 'ja'];
25+
locale = 'en';
2426

2527
dateFilter = (date: Date) => date.getMonth() % 2 == 1 && date.getDate() % 2 == 0;
2628

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
<input mdInput [mdDatepicker]="picker" [(ngModel)]="date" placeholder="Moment.js datepicker">
2-
<md-datepicker-toggle mdSuffix [for]="picker"></md-datepicker-toggle>
3-
<md-datepicker #picker></md-datepicker>
1+
<md-form-field>
2+
<input mdInput [mdDatepicker]="picker" [(ngModel)]="date" placeholder="Moment.js datepicker">
3+
<md-datepicker-toggle mdSuffix [for]="picker"></md-datepicker-toggle>
4+
<md-datepicker #picker></md-datepicker>
5+
</md-form-field>

src/demo-app/datepicker/moment-datepicker.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {ChangeDetectionStrategy, Component} from '@angular/core';
1+
import {ChangeDetectionStrategy, ChangeDetectorRef, Component, Input} from '@angular/core';
22
import {DateAdapter, MAT_DATE_LOCALE_PROVIDER, MD_DATE_FORMATS} from '@angular/material';
33
import {MomentDateAdapter, MD_MOMENT_DATE_FORMATS} from '@angular/material-moment-adapter';
4-
import moment from 'moment';
4+
import * as moment from 'moment';
55

66

77
@Component({
@@ -16,5 +16,12 @@ import moment from 'moment';
1616
],
1717
})
1818
export class DemoMomentDatepicker {
19+
@Input()
20+
set locale(value: string) {
21+
this._dateAdapter.setLocale(value);
22+
}
23+
1924
date = moment();
25+
26+
constructor(private _dateAdapter: DateAdapter<any>) {}
2027
}

src/lib/core/datetime/date-adapter.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
*/
88

99
import {InjectionToken, LOCALE_ID} from '@angular/core';
10+
import {Observable} from 'rxjs/Observable';
11+
import {Subject} from 'rxjs/Subject';
12+
1013

1114
/** InjectionToken for datepicker that can be used to override default locale code. */
1215
export const MAT_DATE_LOCALE = new InjectionToken<string>('MAT_DATE_LOCALE');
@@ -19,6 +22,10 @@ export abstract class DateAdapter<D> {
1922
/** The locale to use for all dates. */
2023
protected locale: any;
2124

25+
/** A stream that emits when the locale changes. */
26+
get localeChanges(): Observable<void> { return this._localeChanges; }
27+
protected _localeChanges= new Subject<void>();
28+
2229
/**
2330
* Gets the year component of the given date.
2431
* @param date The date to extract the year from.
@@ -184,6 +191,7 @@ export abstract class DateAdapter<D> {
184191
*/
185192
setLocale(locale: any) {
186193
this.locale = locale;
194+
this._localeChanges.next();
187195
}
188196

189197
/**

src/lib/datepicker/datepicker-input.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
182182

183183
private _datepickerSubscription = Subscription.EMPTY;
184184

185+
private _localeSubscription = Subscription.EMPTY;
186+
185187
/** The form control validator for whether the input parses. */
186188
private _parseValidator: ValidatorFn = (): ValidationErrors | null => {
187189
return this._lastValueValid ?
@@ -228,6 +230,11 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
228230
if (!this._dateFormats) {
229231
throw createMissingDateImplError('MD_DATE_FORMATS');
230232
}
233+
234+
// Update the displayed date when the locale changes.
235+
this._localeSubscription = _dateAdapter.localeChanges.subscribe(() => {
236+
this.value = this.value;
237+
});
231238
}
232239

233240
ngAfterContentInit() {
@@ -245,6 +252,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
245252

246253
ngOnDestroy() {
247254
this._datepickerSubscription.unsubscribe();
255+
this._localeSubscription.unsubscribe();
248256
this._valueChange.complete();
249257
this._disabledChange.complete();
250258
}

src/material-moment-adapter/adapter/moment-date-adapter.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@ describe('MomentDateAdapter', () => {
330330
expect(parsedDate).not.toBe(date);
331331
});
332332

333+
it('should parse empty string as null', () => {
334+
expect(adapter.parse('', 'MM/DD/YYYY')).toBeNull();
335+
});
336+
333337
it('should parse invalid value as invalid', () => {
334338
let d = adapter.parse('hello', 'MM/DD/YYYY');
335339
expect(d).not.toBeNull();

src/material-moment-adapter/adapter/moment-date-adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export class MomentDateAdapter extends DateAdapter<moment.Moment> {
138138
}
139139

140140
parse(value: any, parseFormat: string | string[]): moment.Moment | null {
141-
if (typeof value == 'string') {
141+
if (value && typeof value == 'string') {
142142
return moment(value, parseFormat, this.locale);
143143
}
144144
return value ? moment(value).locale(this.locale) : null;

0 commit comments

Comments
 (0)