Skip to content

Commit cfad709

Browse files
fix(caledar): enable setting ISO format dates 17.1.x (#14115)
Co-authored-by: Hristo <[email protected]>
1 parent 5306367 commit cfad709

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

projects/igniteui-angular/src/lib/calendar/calendar-base.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,12 @@ export class IgxCalendarBaseDirective implements ControlValueAccessor {
366366
* a single `Date` object.
367367
* Otherwise it is an array of `Date` objects.
368368
*/
369-
public set value(value: Date | Date[]) {
369+
public set value(value: Date | Date[] | string) {
370+
// Validate the date if it is of type string and it is IsoDate
371+
if (typeof value === 'string') {
372+
value = DateTimeUtil.parseIsoDate(value);
373+
}
374+
370375
if (!value || !!value && (value as Date[]).length === 0) {
371376
this.selectedDatesWithoutFocus = new Date();
372377
return;
@@ -392,11 +397,15 @@ export class IgxCalendarBaseDirective implements ControlValueAccessor {
392397
/**
393398
* Sets the date that will be presented in the default view when the component renders.
394399
*/
395-
public set viewDate(value: Date) {
400+
public set viewDate(value: Date | string) {
396401
if (Array.isArray(value)) {
397402
return;
398403
}
399404

405+
if (typeof value === 'string') {
406+
value = DateTimeUtil.parseIsoDate(value);
407+
}
408+
400409
const validDate = this.validateDate(value);
401410
if (this._viewDate) {
402411
this.selectedDatesWithoutFocus = validDate;
@@ -528,18 +537,26 @@ export class IgxCalendarBaseDirective implements ControlValueAccessor {
528537
*
529538
* @hidden
530539
*/
531-
public isDateDisabled(date: Date) {
540+
public isDateDisabled(date: Date | string) {
532541
if (this.disabledDates === null) {
533542
return false;
534543
}
535544

545+
if (typeof date === 'string') {
546+
date = DateTimeUtil.parseIsoDate(date);
547+
}
548+
536549
return isDateInRanges(date, this.disabledDates);
537550
}
538551

539552
/**
540553
* Selects date(s) (based on the selection type).
541554
*/
542-
public selectDate(value: Date | Date[]) {
555+
public selectDate(value: Date | Date[] | string) {
556+
if (typeof value === 'string') {
557+
value = DateTimeUtil.parseIsoDate(value);
558+
}
559+
543560
if (value === null || value === undefined || (Array.isArray(value) && value.length === 0)) {
544561
return;
545562
}
@@ -562,11 +579,15 @@ export class IgxCalendarBaseDirective implements ControlValueAccessor {
562579
/**
563580
* Deselects date(s) (based on the selection type).
564581
*/
565-
public deselectDate(value?: Date | Date[]) {
582+
public deselectDate(value?: Date | Date[] | string) {
566583
if (!this.selectedDates || this.selectedDates.length === 0) {
567584
return;
568585
}
569586

587+
if (typeof value === 'string') {
588+
value = DateTimeUtil.parseIsoDate(value);
589+
}
590+
570591
if (value === null || value === undefined) {
571592
this.selectedDates = this.selection === CalendarSelection.SINGLE ? null : [];
572593
this.rangeStarted = false;

projects/igniteui-angular/src/lib/calendar/calendar.component.spec.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,21 @@ describe('IgxCalendar - ', () => {
238238
);
239239

240240
expect(() => (calendar.selection = 'non-existant')).toThrow();
241+
242+
const todayIsoDate = new Date(Date.now()).toISOString();
243+
244+
calendar.viewDate = todayIsoDate;
245+
fixture.detectChanges();
246+
247+
calendar.value = new Date(todayIsoDate);
248+
fixture.detectChanges();
249+
250+
expect(
251+
(fixture.componentInstance.model as Date).toDateString(),
252+
).toMatch(new Date(todayIsoDate).toDateString());
253+
expect((calendar.value as Date).toDateString()).toMatch(
254+
new Date(todayIsoDate).toDateString(),
255+
);
241256
});
242257

243258
it('Should properly set formatOptions and formatViews', () => {
@@ -320,6 +335,20 @@ describe('IgxCalendar - ', () => {
320335
fixture.detectChanges();
321336

322337
expect(calendar.viewDate.getMonth()).toEqual(date.getMonth());
338+
339+
const isoStringDate = new Date(2020, 10, 10).toISOString();
340+
calendar.viewDate = isoStringDate;
341+
fixture.detectChanges();
342+
expect(calendar.viewDate.getMonth()).toEqual(
343+
new Date(isoStringDate).getMonth(),
344+
);
345+
346+
calendar.value = new Date(2020, 11, 15).toISOString();
347+
fixture.detectChanges();
348+
349+
expect(calendar.viewDate.getMonth()).toEqual(
350+
new Date(isoStringDate).getMonth(),
351+
);
323352
});
324353

325354
it('Should properly set locale', () => {
@@ -606,7 +635,7 @@ describe('IgxCalendar - ', () => {
606635
const target = dom.query(By.css(HelperTestFunctions.SELECTED_DATE_CSSCLASS));
607636
const weekDiv = target.parent;
608637
const weekDays = weekDiv.queryAll(By.css(HelperTestFunctions.DAY_CSSCLASS));
609-
const nextDay = new Date(2017, 5, 14);
638+
let nextDay = new Date(2017, 5, 14);
610639

611640
expect((calendar.value as Date).toDateString()).toMatch(
612641
new Date(2017, 5, 13).toDateString()
@@ -623,6 +652,14 @@ describe('IgxCalendar - ', () => {
623652
(fixture.componentInstance.model as Date).toDateString()
624653
).toMatch(nextDay.toDateString());
625654
HelperTestFunctions.verifyDateNotSelected(target);
655+
656+
nextDay = new Date(2017, 6, 15);
657+
calendar.selectDate(new Date(2017, 6, 15).toISOString());
658+
fixture.detectChanges();
659+
660+
expect((calendar.value as Date).toDateString()).toMatch(
661+
nextDay.toDateString(),
662+
);
626663
});
627664

628665
it('Calendar selection - multiple with event', () => {
@@ -1412,6 +1449,25 @@ describe('IgxCalendar - ', () => {
14121449

14131450
selectedDate = calendar.value;
14141451
expect(selectedDate).toEqual(date);
1452+
1453+
// Select date with ISOString as value
1454+
const isoDate = new Date(2024, 10, 10).toISOString();
1455+
calendar.selectDate(isoDate);
1456+
fixture.detectChanges();
1457+
expect(calendar.value).toEqual(new Date(isoDate));
1458+
1459+
calendar.deselectDate(isoDate);
1460+
fixture.detectChanges();
1461+
expect(calendar.value).toBeNull();
1462+
1463+
// Deselect with date different than selected date
1464+
calendar.selectDate(isoDate);
1465+
fixture.detectChanges();
1466+
expect(calendar.value).toEqual(new Date(isoDate));
1467+
1468+
calendar.deselectDate(new Date(2024, 10, 11).toISOString());
1469+
fixture.detectChanges();
1470+
expect(calendar.value).toEqual(new Date(isoDate));
14151471
});
14161472

14171473
it('Deselect using API. Should deselect in "multi" selection mode.', () => {

projects/igniteui-angular/src/lib/calendar/calendar.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ export class IgxCalendarComponent extends IgxMonthPickerBaseDirective implements
870870
* this.calendar.deselectDate(new Date(`2018-06-12`));
871871
* ````
872872
*/
873-
public override deselectDate(value?: Date | Date[]) {
873+
public override deselectDate(value?: Date | Date[] | string) {
874874
super.deselectDate(value);
875875

876876
this.monthViews.forEach((view) => {

0 commit comments

Comments
 (0)