Skip to content

Commit 0cb144b

Browse files
committed
refactor(dp): align the calendar activeDate with WC
1 parent f3c0c78 commit 0cb144b

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

projects/igniteui-angular/src/lib/date-picker/date-picker.component.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,29 @@ describe('IgxDatePicker', () => {
156156
const wrapper = fixture.debugElement.query(By.css('.igx-calendar__wrapper')).nativeElement;
157157
expect(wrapper.getAttribute('aria-activedescendant')).toEqual(activeDescendantDate);
158158
}));
159+
160+
it('should set activeDate of the calendar to value of picker even when it is outside the enabled range, i.e. > maxValue', fakeAsync(() => {
161+
const datePicker = fixture.componentInstance.datePicker;
162+
const maxDate = new Date(2025, 7, 1);
163+
datePicker.maxValue = maxDate;
164+
fixture.detectChanges();
165+
166+
const valueGreaterThanMax = new Date(2025, 10, 1);
167+
datePicker.value = valueGreaterThanMax;
168+
fixture.detectChanges();
169+
170+
expect(datePicker.activeDate).toEqual(valueGreaterThanMax);
171+
172+
datePicker.open();
173+
fixture.detectChanges();
174+
175+
const activeDescendantDate = new Date(valueGreaterThanMax.setHours(0, 0, 0, 0)).getTime().toString();
176+
expect(datePicker['_calendar'].activeDate).toEqual(valueGreaterThanMax);
177+
expect(datePicker['_calendar'].viewDate.getMonth()).toEqual(valueGreaterThanMax.getMonth());
178+
expect(datePicker['_calendar'].value).toEqual(valueGreaterThanMax);
179+
const wrapper = fixture.debugElement.query(By.css('.igx-calendar__wrapper')).nativeElement;
180+
expect(wrapper.getAttribute('aria-activedescendant')).toEqual(activeDescendantDate);
181+
}));
159182
});
160183

161184
describe('Events', () => {

projects/igniteui-angular/src/lib/date-picker/date-picker.component.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,6 @@ export class IgxDatePickerComponent extends PickerBaseDirective implements Contr
472472
private _dateValue: Date;
473473
private _overlayId: string;
474474
private _value: Date | string;
475-
private _targetViewDate: Date;
476475
private _ngControl: NgControl = null;
477476
private _statusChanges$: Subscription;
478477
private _calendar: IgxCalendarComponent;
@@ -926,13 +925,6 @@ export class IgxDatePickerComponent extends PickerBaseDirective implements Contr
926925
this.opened.emit({ owner: this });
927926

928927
this._calendar.wrapper?.nativeElement?.focus();
929-
930-
if (this._targetViewDate) {
931-
this._targetViewDate.setHours(0, 0, 0, 0);
932-
// INFO: We need to set the active date to the target view date so there's something to
933-
// navigate when the calendar is opened.
934-
this._calendar.activeDate = this._targetViewDate;
935-
}
936928
});
937929

938930
this._overlayService.closing.pipe(...this._overlaySubFilter).subscribe((e: OverlayCancelableEventArgs) => {
@@ -1002,7 +994,8 @@ export class IgxDatePickerComponent extends PickerBaseDirective implements Contr
1002994
// calendar will throw if the picker's value is InvalidDate #9208
1003995
this._calendar.value = this.dateValue;
1004996
}
1005-
this.setCalendarViewDate();
997+
this._calendar.activeDate = this.activeDate;
998+
this._calendar.viewDate = this.activeDate;
1006999

10071000
componentInstance.mode = this.mode;
10081001
componentInstance.closeButtonLabel = this.cancelButtonLabel;
@@ -1012,18 +1005,4 @@ export class IgxDatePickerComponent extends PickerBaseDirective implements Contr
10121005
componentInstance.calendarClose.pipe(takeUntil(this._destroy$)).subscribe(() => this.close());
10131006
componentInstance.todaySelection.pipe(takeUntil(this._destroy$)).subscribe(() => this.selectToday());
10141007
}
1015-
1016-
private setCalendarViewDate() {
1017-
const { minValue, maxValue } = this.getMinMaxDates();
1018-
const dateValue = DateTimeUtil.isValidDate(this.dateValue) ? this.dateValue : new Date();
1019-
if (minValue && DateTimeUtil.lessThanMinValue(dateValue, minValue)) {
1020-
this._calendar.viewDate = this._targetViewDate = minValue;
1021-
return;
1022-
}
1023-
if (maxValue && DateTimeUtil.greaterThanMaxValue(dateValue, maxValue)) {
1024-
this._calendar.viewDate = this._targetViewDate = maxValue;
1025-
return;
1026-
}
1027-
this._calendar.viewDate = this._targetViewDate = dateValue;
1028-
}
10291008
}

0 commit comments

Comments
 (0)