Skip to content

Commit f3c0c78

Browse files
committed
feat(dp): add activeDate property + tests
1 parent bcdb253 commit f3c0c78

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,59 @@ describe('IgxDatePicker', () => {
103103
expect(datePicker['_calendar'].orientation).toEqual(PickerCalendarOrientation.Vertical.toString());
104104
expect(datePicker['_calendar'].wrapper.nativeElement).toHaveClass(CSS_CLASS_CALENDAR_WRAPPER_VERTICAL);
105105
}));
106+
107+
it('should initialize activeDate with current date, when not set', fakeAsync(() => {
108+
const datePicker = fixture.componentInstance.datePicker;
109+
datePicker.value = null;
110+
fixture.detectChanges();
111+
const todayDate = new Date();
112+
const today = new Date(todayDate.setHours(0, 0, 0, 0)).getTime().toString();
113+
114+
expect(datePicker.activeDate).toEqual(todayDate);
115+
116+
datePicker.open();
117+
fixture.detectChanges();
118+
119+
expect(datePicker['_calendar'].activeDate).toEqual(todayDate);
120+
expect(datePicker['_calendar'].value).toBeUndefined();
121+
const wrapper = fixture.debugElement.query(By.css('.igx-calendar__wrapper')).nativeElement;
122+
expect(wrapper.getAttribute('aria-activedescendant')).toEqual(today);
123+
}));
124+
125+
it('should initialize activeDate = value when it is not set, but value is', fakeAsync(() => {
126+
const datePicker = fixture.componentInstance.datePicker;
127+
const date = fixture.componentInstance.date;
128+
129+
expect(datePicker.activeDate).toEqual(date);
130+
datePicker.open();
131+
fixture.detectChanges();
132+
133+
const activeDescendantDate = new Date(date.setHours(0, 0, 0, 0)).getTime().toString();
134+
expect(datePicker['_calendar'].activeDate).toEqual(date);
135+
expect(datePicker['_calendar'].value).toEqual(date);
136+
const wrapper = fixture.debugElement.query(By.css('.igx-calendar__wrapper')).nativeElement;
137+
expect(wrapper.getAttribute('aria-activedescendant')).toEqual(activeDescendantDate);
138+
}));
139+
140+
it('should set activeDate correctly', fakeAsync(() => {
141+
const datePicker = fixture.componentInstance.datePicker;
142+
const targetDate = new Date(2025, 0, 1);
143+
datePicker.activeDate = new Date(targetDate);
144+
fixture.detectChanges();
145+
146+
expect(datePicker.activeDate).toEqual(targetDate);
147+
expect(datePicker.value).toEqual(fixture.componentInstance.date);
148+
149+
datePicker.open();
150+
fixture.detectChanges();
151+
152+
const activeDescendantDate = new Date(targetDate.setHours(0, 0, 0, 0)).getTime().toString();
153+
expect(datePicker['_calendar'].activeDate).toEqual(targetDate);
154+
expect(datePicker['_calendar'].viewDate.getMonth()).toEqual(targetDate.getMonth());
155+
expect(datePicker['_calendar'].value).toEqual(fixture.componentInstance.date);
156+
const wrapper = fixture.debugElement.query(By.css('.igx-calendar__wrapper')).nativeElement;
157+
expect(wrapper.getAttribute('aria-activedescendant')).toEqual(activeDescendantDate);
158+
}));
106159
});
107160

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

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ export class IgxDatePickerComponent extends PickerBaseDirective implements Contr
160160
@Input({ transform: booleanAttribute })
161161
public showWeekNumbers: boolean;
162162

163+
@Input()
164+
public get activeDate(): Date {
165+
const today = new Date(new Date().setHours(0, 0, 0, 0));
166+
const dateValue = DateTimeUtil.isValidDate(this._dateValue) ? new Date(this._dateValue.setHours(0, 0, 0, 0)) : null;
167+
return this._activeDate ?? dateValue ?? this._calendar?.activeDate ?? today;
168+
}
169+
170+
public set activeDate(value: Date) {
171+
this._activeDate = value;
172+
}
173+
163174
/**
164175
* Gets/Sets a custom formatter function on the selected or passed date.
165176
*
@@ -468,6 +479,7 @@ export class IgxDatePickerComponent extends PickerBaseDirective implements Contr
468479
private _calendarContainer?: HTMLElement;
469480
private _specialDates: DateRangeDescriptor[] = null;
470481
private _disabledDates: DateRangeDescriptor[] = null;
482+
private _activeDate: Date = null;
471483
private _overlaySubFilter:
472484
[MonoTypeOperatorFunction<OverlayEventArgs>,
473485
MonoTypeOperatorFunction<OverlayEventArgs | OverlayCancelableEventArgs>] = [
@@ -873,7 +885,10 @@ export class IgxDatePickerComponent extends PickerBaseDirective implements Contr
873885
date.setMilliseconds(this.dateValue.getMilliseconds());
874886
}
875887
this.value = date;
876-
this._calendar.viewDate = date;
888+
if (this._calendar) {
889+
this._calendar.activeDate = this.activeDate;
890+
this._calendar.viewDate = this.activeDate;
891+
}
877892
this.close();
878893
}
879894

@@ -964,7 +979,6 @@ export class IgxDatePickerComponent extends PickerBaseDirective implements Contr
964979
}
965980
}
966981

967-
968982
private _initializeCalendarContainer(componentInstance: IgxCalendarContainerComponent) {
969983
this._calendar = componentInstance.calendar;
970984
this._calendar.hasHeader = !this.isDropdown && !this.hideHeader;

0 commit comments

Comments
 (0)