Skip to content

Commit 864462b

Browse files
committed
feat(month-view): add activeDay input to override the active day
Closes #885
1 parent f67afc6 commit 864462b

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

projects/angular-calendar/src/modules/month/calendar-month-view.component.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ export class CalendarMonthViewComponent
142142
*/
143143
@Input() activeDayIsOpen: boolean = false;
144144

145+
/**
146+
* If set will be used to determine the day that should be open. If not set, the `viewDate` is used
147+
*/
148+
@Input() activeDay: Date;
149+
145150
/**
146151
* An observable that when emitted on will re-render the current view
147152
*/
@@ -322,7 +327,8 @@ export class CalendarMonthViewComponent
322327
changes.activeDayIsOpen ||
323328
changes.viewDate ||
324329
changes.events ||
325-
changes.excludeDays
330+
changes.excludeDays ||
331+
changes.activeDay
326332
) {
327333
this.checkActiveDayIsOpen();
328334
}
@@ -411,8 +417,9 @@ export class CalendarMonthViewComponent
411417

412418
private checkActiveDayIsOpen(): void {
413419
if (this.activeDayIsOpen === true) {
420+
const activeDay = this.activeDay || this.viewDate;
414421
this.openDay = this.view.days.find(day =>
415-
this.dateAdapter.isSameDay(day.date, this.viewDate)
422+
this.dateAdapter.isSameDay(day.date, activeDay)
416423
);
417424
const index: number = this.view.days.indexOf(this.openDay);
418425
this.openRowIndex =

projects/angular-calendar/test/calendar-month-view.component.spec.ts

+36-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('calendarMonthView component', () => {
7777
fixture.destroy();
7878
});
7979

80-
it('should emit on the columnHeaderClicked output', (done) => {
80+
it('should emit on the columnHeaderClicked output', done => {
8181
const fixture: ComponentFixture<
8282
CalendarMonthViewComponent
8383
> = TestBed.createComponent(CalendarMonthViewComponent);
@@ -187,6 +187,41 @@ describe('calendarMonthView component', () => {
187187
fixture.destroy();
188188
});
189189

190+
it('should use the activeDay input instead of the viewDate to determine the active day', () => {
191+
const fixture: ComponentFixture<
192+
CalendarMonthViewComponent
193+
> = TestBed.createComponent(CalendarMonthViewComponent);
194+
expect(fixture.componentInstance.openRowIndex).to.equal(undefined);
195+
expect(fixture.componentInstance.openDay).to.equal(undefined);
196+
fixture.componentInstance.viewDate = moment()
197+
.startOf('month')
198+
.startOf('week')
199+
.add(14, 'days')
200+
.toDate();
201+
fixture.componentInstance.activeDay = moment()
202+
.startOf('month')
203+
.startOf('week')
204+
.add(8, 'days')
205+
.toDate();
206+
fixture.componentInstance.activeDayIsOpen = true;
207+
fixture.componentInstance.ngOnChanges({
208+
viewDate: {},
209+
activeDayIsOpen: {}
210+
});
211+
expect(fixture.componentInstance.openRowIndex).to.equal(7);
212+
expect(fixture.componentInstance.openDay).to.equal(
213+
fixture.componentInstance.view.days[8]
214+
);
215+
fixture.componentInstance.activeDayIsOpen = false;
216+
fixture.componentInstance.ngOnChanges({
217+
viewDate: {},
218+
activeDayIsOpen: {}
219+
});
220+
expect(!!fixture.componentInstance.openRowIndex).to.equal(false);
221+
expect(!!fixture.componentInstance.openDay).to.equal(false);
222+
fixture.destroy();
223+
});
224+
190225
it('should add a custom CSS class to events', () => {
191226
const fixture: ComponentFixture<
192227
CalendarMonthViewComponent

0 commit comments

Comments
 (0)