Skip to content

Commit

Permalink
fix(month picker): prevent duplicates (#1377)
Browse files Browse the repository at this point in the history
  • Loading branch information
yggg committed Apr 11, 2019
1 parent e20eb6d commit 47aff73
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,18 @@ describe('Component: NbCalendarMonthPicker', () => {
componentEl.query(By.css('nb-calendar-picker'))
.nativeElement
.dispatchEvent(new CustomEvent('select'));
})
});

it('should not have duplicate months', () => {
component.month = new Date(2019, 0, 30);
component.initMonths();

let expectedMonthIndex = 0;
for (const monthRow of component.months) {
for (const month of monthRow) {
expect(month.getMonth()).toEqual(expectedMonthIndex);
expectedMonthIndex++;
}
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
Output,
Type,
} from '@angular/core';
import { batch, range } from '../../helpers';
import { batch } from '../../helpers';
import { NbCalendarCell, NbCalendarSize } from '../../model';
import { NbCalendarMonthCellComponent } from './calendar-month-cell.component';
import { NbDateService } from '../../services';
Expand Down Expand Up @@ -75,15 +75,19 @@ export class NbCalendarMonthPickerComponent<D, T> implements OnInit {
}

initMonths() {
const months: D[] = range(MONTHS_IN_VIEW).map(i => this.createMonthDateByIndex(i));
const date = this.dateService.getDate(this.month);
const year = this.dateService.getYear(this.month);
const firstMonth = this.dateService.createDate(year, 0, date);
const months = [ firstMonth ];

for (let monthIndex = 1; monthIndex < MONTHS_IN_VIEW; monthIndex++) {
months.push(this.dateService.addMonth(firstMonth, monthIndex));
}

this.months = batch(months, MONTHS_IN_COLUMN);
}

onSelect(month: D) {
this.monthChange.emit(month);
}

private createMonthDateByIndex(i: number): D {
return this.dateService.createDate(this.dateService.getYear(this.month), i, this.dateService.getDate(this.month));
}
}

0 comments on commit 47aff73

Please sign in to comment.