Skip to content

Commit

Permalink
fix(kit): CalendarRange show actual defaultViewedMonth
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlufy committed Oct 8, 2024
1 parent 20b1c61 commit 4f79289
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {
EventEmitter,
Inject,
Input,
OnChanges,
OnInit,
Optional,
Output,
Self,
SimpleChanges,
} from '@angular/core';
import {
ALWAYS_FALSE_HANDLER,
Expand Down Expand Up @@ -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<TuiDay> = ALWAYS_FALSE_HANDLER;

Expand Down Expand Up @@ -99,6 +101,19 @@ export class TuiPrimitiveCalendarRangeComponent implements OnInit {
monthOffset: TuiTypedMapper<[TuiMonth, number], TuiMonth> = (value, offset) =>
value.append({month: offset});

ngOnChanges({
defaultViewedMonthFirst,
defaultViewedMonthSecond,
value,
}: SimpleChanges): void {
if (!value) {
this.updateViewedMonths(
defaultViewedMonthFirst?.currentValue,
defaultViewedMonthSecond?.currentValue,
);
}
}

ngOnInit(): void {
this.setInitialMonths();
}
Expand Down Expand Up @@ -157,10 +172,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,
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
});
});

0 comments on commit 4f79289

Please sign in to comment.