From 16f26db852ccd7be15e257a3d5bb7c21cf9db649 Mon Sep 17 00:00:00 2001 From: mdlufy Date: Tue, 8 Oct 2024 17:10:31 +0300 Subject: [PATCH] fix(kit): `CalendarRange` show actual `defaultViewedMonth` --- .../primitive-calendar-range.component.ts | 30 +++++++++++++++---- ...primitive-calendar-range.component.spec.ts | 16 ++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/projects/kit/internal/primitive-calendar-range/primitive-calendar-range.component.ts b/projects/kit/internal/primitive-calendar-range/primitive-calendar-range.component.ts index e05159e0a046..22f0674938bd 100644 --- a/projects/kit/internal/primitive-calendar-range/primitive-calendar-range.component.ts +++ b/projects/kit/internal/primitive-calendar-range/primitive-calendar-range.component.ts @@ -5,10 +5,12 @@ import { EventEmitter, Inject, Input, + OnChanges, OnInit, Optional, Output, Self, + SimpleChanges, } from '@angular/core'; import { ALWAYS_FALSE_HANDLER, @@ -37,7 +39,7 @@ import {takeUntil} from 'rxjs/operators'; changeDetection: ChangeDetectionStrategy.OnPush, providers: [TuiDestroyService], }) -export class TuiPrimitiveCalendarRangeComponent implements OnInit { +export class TuiPrimitiveCalendarRangeComponent implements OnInit, OnChanges { @Input() disabledItemHandler: TuiBooleanHandler = ALWAYS_FALSE_HANDLER; @@ -99,6 +101,16 @@ export class TuiPrimitiveCalendarRangeComponent implements OnInit { monthOffset: TuiTypedMapper<[TuiMonth, number], TuiMonth> = (value, offset) => value.append({month: offset}); + ngOnChanges({ + defaultViewedMonthFirst, + defaultViewedMonthSecond, + }: SimpleChanges): void { + this.updateViewedMonths( + defaultViewedMonthFirst?.currentValue, + defaultViewedMonthSecond?.currentValue, + ); + } + ngOnInit(): void { this.setInitialMonths(); } @@ -157,10 +169,18 @@ export class TuiPrimitiveCalendarRangeComponent implements OnInit { return month; } - private updateViewedMonths(): void { - this.userViewedMonthFirst = - this.value === null ? this.defaultViewedMonthFirst : this.value.from; + private updateViewedMonths(firstMonth?: TuiMonth, secondMonth?: TuiMonth): void { + if (this.value) { + this.userViewedMonthFirst = this.value.from; + this.userViewedMonthSecond = this.userViewedMonthFirst.append({month: 1}); + } else { + this.userViewedMonthSecond = this.updatedViewedMonthSecond( + secondMonth ?? this.userViewedMonthSecond, + ); - this.userViewedMonthSecond = this.userViewedMonthFirst.append({month: 1}); + this.userViewedMonthFirst = this.updatedViewedMonthFirst( + firstMonth ?? this.userViewedMonthFirst, + ); + } } } diff --git a/projects/kit/internal/primitive-calendar-range/test/primitive-calendar-range.component.spec.ts b/projects/kit/internal/primitive-calendar-range/test/primitive-calendar-range.component.spec.ts index d8c8c3e9f32e..89171c221365 100644 --- a/projects/kit/internal/primitive-calendar-range/test/primitive-calendar-range.component.spec.ts +++ b/projects/kit/internal/primitive-calendar-range/test/primitive-calendar-range.component.spec.ts @@ -141,4 +141,20 @@ describe('PrimitiveRangeCalendar component', () => { expect(component.cappedUserViewedMonthSecond).toBe(day); }); }); + + it('When handle any changes, current viewed month do not updates', () => { + const date = TuiMonth.currentLocal().append({month: 3}); + + component.userViewedMonthFirst = date; + component.userViewedMonthSecond = date.append({month: 1}); + + component.markerHandler = (day: TuiDay) => + day.day % 2 === 0 ? ['first'] : ['second']; + component.ngOnChanges({}); + + expect(component.userViewedMonthFirst.toString()).toBe(date.toString()); + expect(component.userViewedMonthSecond.toString()).toBe( + date.append({month: 1}).toString(), + ); + }); });