Skip to content

Commit b810965

Browse files
committed
coerce ISO 8601 strings to dates
1 parent bfb084a commit b810965

File tree

5 files changed

+84
-11
lines changed

5 files changed

+84
-11
lines changed

src/lib/datepicker/calendar.ts

+28-4
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,31 @@ export class MdCalendar<D> implements AfterContentInit, OnDestroy {
6060
private _intlChanges: Subscription;
6161

6262
/** A date representing the period (month or year) to start the calendar in. */
63-
@Input() startAt: D;
63+
@Input()
64+
get startAt(): D { return this._startAt; }
65+
set startAt(value: D) { this._startAt = this._coerceDateProperty(value); }
66+
private _startAt: D;
6467

6568
/** Whether the calendar should be started in month or year view. */
6669
@Input() startView: 'month' | 'year' = 'month';
6770

6871
/** The currently selected date. */
69-
@Input() selected: D | null;
72+
@Input()
73+
get selected(): D | null { return this._selected; }
74+
set selected(value: D | null) { this._selected = this._coerceDateProperty(value); }
75+
private _selected: D | null;
7076

7177
/** The minimum selectable date. */
72-
@Input() minDate: D | null;
78+
@Input()
79+
get minDate(): D | null { return this._minDate; }
80+
set minDate(value: D | null) { this._minDate = this._coerceDateProperty(value); }
81+
private _minDate: D | null;
7382

7483
/** The maximum selectable date. */
75-
@Input() maxDate: D | null;
84+
@Input()
85+
get maxDate(): D | null { return this._maxDate; }
86+
set maxDate(value: D | null) { this._maxDate = this._coerceDateProperty(value); }
87+
private _maxDate: D | null;
7688

7789
/** A function used to filter which dates are selectable. */
7890
@Input() dateFilter: (date: D) => boolean;
@@ -351,4 +363,16 @@ export class MdCalendar<D> implements AfterContentInit, OnDestroy {
351363
(this._dateAdapter.getMonth(date) >= 7 ? 5 : 12);
352364
return this._dateAdapter.addCalendarMonths(date, increment);
353365
}
366+
367+
/**
368+
* Attempts to coerce a property to a date by parsing it as a ISO 8601 string. If not a valid
369+
* ISO 8601 string, returns the original vlaue.
370+
*/
371+
private _coerceDateProperty(value: any): any {
372+
if (typeof value === 'string') {
373+
const d = this._dateAdapter.fromISODateString(value);
374+
return d || value;
375+
}
376+
return value;
377+
}
354378
}

src/lib/datepicker/datepicker-input.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
123123
return this._value;
124124
}
125125
set value(value: D | null) {
126+
value = this._coerceDateProperty(value);
126127
if (value != null && !this._dateAdapter.isDateInstance(value)) {
127128
throw Error('Datepicker: value not recognized as a date object by DateAdapter.');
128129
}
@@ -143,7 +144,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
143144
@Input()
144145
get min(): D | null { return this._min; }
145146
set min(value: D | null) {
146-
this._min = value;
147+
this._min = this._coerceDateProperty(value);
147148
this._validatorOnChange();
148149
}
149150
private _min: D | null;
@@ -152,7 +153,7 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
152153
@Input()
153154
get max(): D | null { return this._max; }
154155
set max(value: D | null) {
155-
this._max = value;
156+
this._max = this._coerceDateProperty(value);
156157
this._validatorOnChange();
157158
}
158159
private _max: D | null;
@@ -329,4 +330,16 @@ export class MdDatepickerInput<D> implements AfterContentInit, ControlValueAcces
329330
private _getValidDateOrNull(obj: any): D | null {
330331
return (this._dateAdapter.isDateInstance(obj) && this._dateAdapter.isValid(obj)) ? obj : null;
331332
}
333+
334+
/**
335+
* Attempts to coerce a property to a date by parsing it as a ISO 8601 string. If not a valid
336+
* ISO 8601 string, returns the original vlaue.
337+
*/
338+
private _coerceDateProperty(value: any): any {
339+
if (typeof value === 'string') {
340+
const d = this._dateAdapter.fromISODateString(value);
341+
return d || value;
342+
}
343+
return value;
344+
}
332345
}

src/lib/datepicker/datepicker.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export class MdDatepicker<D> implements OnDestroy {
133133
// selected value is.
134134
return this._startAt || (this._datepickerInput ? this._datepickerInput.value : null);
135135
}
136-
set startAt(date: D | null) { this._startAt = date; }
136+
set startAt(date: D | null) { this._startAt = this._coerceDateProperty(date); }
137137
private _startAt: D | null;
138138

139139
/** The view that the calendar should start in. */
@@ -364,4 +364,16 @@ export class MdDatepicker<D> implements OnDestroy {
364364
{ overlayX: 'end', overlayY: 'bottom' }
365365
);
366366
}
367+
368+
/**
369+
* Attempts to coerce a property to a date by parsing it as a ISO 8601 string. If not a valid
370+
* ISO 8601 string, returns the original vlaue.
371+
*/
372+
private _coerceDateProperty(value: any): any {
373+
if (typeof value === 'string') {
374+
const d = this._dateAdapter.fromISODateString(value);
375+
return d || value;
376+
}
377+
return value;
378+
}
367379
}

src/lib/datepicker/month-view.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class MdMonthView<D> implements AfterContentInit {
4545
get activeDate(): D { return this._activeDate; }
4646
set activeDate(value: D) {
4747
let oldActiveDate = this._activeDate;
48-
this._activeDate = value || this._dateAdapter.today();
48+
this._activeDate = this._coerceDateProperty(value) || this._dateAdapter.today();
4949
if (!this._hasSameMonthAndYear(oldActiveDate, this._activeDate)) {
5050
this._init();
5151
}
@@ -56,7 +56,7 @@ export class MdMonthView<D> implements AfterContentInit {
5656
@Input()
5757
get selected(): D { return this._selected; }
5858
set selected(value: D) {
59-
this._selected = value;
59+
this._selected = this._coerceDateProperty(value);
6060
this._selectedDate = this._getDateInCurrentMonth(this.selected);
6161
}
6262
private _selected: D;
@@ -182,4 +182,16 @@ export class MdMonthView<D> implements AfterContentInit {
182182
return !!(d1 && d2 && this._dateAdapter.getMonth(d1) == this._dateAdapter.getMonth(d2) &&
183183
this._dateAdapter.getYear(d1) == this._dateAdapter.getYear(d2));
184184
}
185+
186+
/**
187+
* Attempts to coerce a property to a date by parsing it as a ISO 8601 string. If not a valid
188+
* ISO 8601 string, returns the original vlaue.
189+
*/
190+
private _coerceDateProperty(value: any): any {
191+
if (typeof value === 'string') {
192+
const d = this._dateAdapter.fromISODateString(value);
193+
return d || value;
194+
}
195+
return value;
196+
}
185197
}

src/lib/datepicker/year-view.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class MdYearView<D> implements AfterContentInit {
4040
get activeDate(): D { return this._activeDate; }
4141
set activeDate(value: D) {
4242
let oldActiveDate = this._activeDate;
43-
this._activeDate = value || this._dateAdapter.today();
43+
this._activeDate = this._coerceDateProperty(value) || this._dateAdapter.today();
4444
if (this._dateAdapter.getYear(oldActiveDate) != this._dateAdapter.getYear(this._activeDate)) {
4545
this._init();
4646
}
@@ -51,7 +51,7 @@ export class MdYearView<D> implements AfterContentInit {
5151
@Input()
5252
get selected(): D { return this._selected; }
5353
set selected(value: D) {
54-
this._selected = value;
54+
this._selected = this._coerceDateProperty(value);
5555
this._selectedMonth = this._getMonthInCurrentYear(this.selected);
5656
}
5757
private _selected: D;
@@ -151,4 +151,16 @@ export class MdYearView<D> implements AfterContentInit {
151151

152152
return false;
153153
}
154+
155+
/**
156+
* Attempts to coerce a property to a date by parsing it as a ISO 8601 string. If not a valid
157+
* ISO 8601 string, returns the original vlaue.
158+
*/
159+
private _coerceDateProperty(value: any): any {
160+
if (typeof value === 'string') {
161+
const d = this._dateAdapter.fromISODateString(value);
162+
return d || value;
163+
}
164+
return value;
165+
}
154166
}

0 commit comments

Comments
 (0)