Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kit): CalendarRange not update selectedActivePeriod, when value updates #8536

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {AsyncPipe, NgForOf, NgIf} from '@angular/common';
import type {OnChanges, OnInit} from '@angular/core';
import type {OnChanges, OnInit, SimpleChanges} from '@angular/core';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Expand Down Expand Up @@ -87,12 +87,14 @@ export class TuiCalendarRange implements OnInit, OnChanges {
inject<Observable<TuiDayRange | null>>(TUI_CALENDAR_DATE_STREAM, {optional: true})
?.pipe(tuiWatch(this.cdr), takeUntilDestroyed())
.subscribe((value) => {
this.resetSelectedActivePeriod(value);
this.value = value;
});
}

public ngOnChanges(): void {
public ngOnChanges({value}: SimpleChanges): void {
this.defaultViewedMonth = this.value?.from || this.defaultViewedMonth;
this.resetSelectedActivePeriod(value?.currentValue);
}

public ngOnInit(): void {
Expand Down Expand Up @@ -227,6 +229,12 @@ export class TuiCalendarRange implements OnInit, OnChanges {
};
}

private resetSelectedActivePeriod(value: TuiDayRange | null): void {
if (value?.toString() !== this.selectedActivePeriod?.range.toString()) {
this.selectedActivePeriod = null;
}
}

private isDisabledItem(
disabledItemHandler: TuiBooleanHandler<TuiDay>,
value: TuiDayRange | null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,36 +245,30 @@ describe('rangeCalendarComponent', () => {
expect(component.defaultViewedMonth).toEqual(minDate);
});

it('isItemActive returns true when value is set to today after being changed to yesterday', () => {
it('when value updates, displays appropriate checkbox', () => {
const today = TuiDay.currentLocal();
const yesterday = today.append({day: -1});
const yesterday = TuiDay.currentLocal().append({day: -1});
const previousMonth = today.append({month: -1});

testComponent.value = new TuiDayRange(today, today);
testComponent.items = [
new TuiDayRangePeriod(new TuiDayRange(previousMonth, today), '1'),
new TuiDayRangePeriod(new TuiDayRange(previousMonth, yesterday), '2'),
];
fixture.detectChanges();

expect(
component['isItemActive'](
new TuiDayRangePeriod(new TuiDayRange(today, today), 'Today'),
),
).toBe(true);

testComponent.value = new TuiDayRange(yesterday, yesterday);
component['onItemSelect'](component.items[1]);
fixture.detectChanges();

expect(
component['isItemActive'](
new TuiDayRangePeriod(new TuiDayRange(today, today), 'Today'),
),
).toBe(false);
const items = getItems();

testComponent.value = new TuiDayRange(today, today);
expect(items[0].nativeElement.contains(getCheckmark())).toBe(false);
expect(items[1].nativeElement.contains(getCheckmark())).toBe(true);

testComponent.value = new TuiDayRange(previousMonth, today);
fixture.detectChanges();

expect(
component['isItemActive'](
new TuiDayRangePeriod(new TuiDayRange(today, today), 'Today'),
),
).toBe(true);
expect(items[0].nativeElement.contains(getCheckmark())).toBe(true);
expect(items[1].nativeElement.contains(getCheckmark())).toBe(false);
});
});

Expand Down
Loading