From 47aff7319694cd71dd19d9ae920e0349893faf26 Mon Sep 17 00:00:00 2001 From: Sergey Andrievskiy Date: Thu, 11 Apr 2019 14:53:48 +0300 Subject: [PATCH] fix(month picker): prevent duplicates (#1377) --- .../calendar-month-picker.component.spec.ts | 15 ++++++++++++++- .../calendar-month-picker.component.ts | 16 ++++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/framework/theme/components/calendar-kit/components/calendar-month-picker/calendar-month-picker.component.spec.ts b/src/framework/theme/components/calendar-kit/components/calendar-month-picker/calendar-month-picker.component.spec.ts index 75da8495f2..0ea32b128c 100644 --- a/src/framework/theme/components/calendar-kit/components/calendar-month-picker/calendar-month-picker.component.spec.ts +++ b/src/framework/theme/components/calendar-kit/components/calendar-month-picker/calendar-month-picker.component.spec.ts @@ -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++; + } + } + }); }); diff --git a/src/framework/theme/components/calendar-kit/components/calendar-month-picker/calendar-month-picker.component.ts b/src/framework/theme/components/calendar-kit/components/calendar-month-picker/calendar-month-picker.component.ts index 93ca02ce2e..eff33a5fd6 100644 --- a/src/framework/theme/components/calendar-kit/components/calendar-month-picker/calendar-month-picker.component.ts +++ b/src/framework/theme/components/calendar-kit/components/calendar-month-picker/calendar-month-picker.component.ts @@ -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'; @@ -75,15 +75,19 @@ export class NbCalendarMonthPickerComponent 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)); - } }