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 #8529

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
Expand Up @@ -6,9 +6,11 @@ import {
HostListener,
Inject,
Input,
OnChanges,
Optional,
Output,
Self,
SimpleChanges,
} from '@angular/core';
import {
ALWAYS_FALSE_HANDLER,
Expand Down Expand Up @@ -47,7 +49,9 @@ import {takeUntil} from 'rxjs/operators';
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [TuiDestroyService],
})
export class TuiCalendarRangeComponent implements TuiWithOptionalMinMax<TuiDay> {
export class TuiCalendarRangeComponent
implements TuiWithOptionalMinMax<TuiDay>, OnChanges
{
@Input()
defaultViewedMonth: TuiMonth = TuiMonth.currentLocal();

Expand Down Expand Up @@ -107,6 +111,7 @@ export class TuiCalendarRangeComponent implements TuiWithOptionalMinMax<TuiDay>
}

valueChanges.pipe(tuiWatch(cdr), takeUntil(destroy$)).subscribe(value => {
this.resetSelectedActivePeriod(value);
this.value = value;
});
}
Expand All @@ -121,6 +126,10 @@ export class TuiCalendarRangeComponent implements TuiWithOptionalMinMax<TuiDay>
this.value = this.previousValue;
}

ngOnChanges({value}: SimpleChanges): void {
this.resetSelectedActivePeriod(value?.currentValue);
}

readonly monthOffset: TuiTypedMapper<[TuiMonth, number], TuiMonth> = (value, month) =>
value.append({month});

Expand Down Expand Up @@ -247,6 +256,12 @@ export class TuiCalendarRangeComponent implements TuiWithOptionalMinMax<TuiDay>
};
}

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 @@ -226,6 +226,32 @@ describe('rangeCalendarComponent', () => {
expect(items[0].nativeElement.contains(getCheckmark())).toBe(false);
expect(items[1].nativeElement.contains(getCheckmark())).toBe(true);
});

it('when value updates, displays appropriate checkbox', () => {
const today = TuiDay.currentLocal();
const yesterday = TuiDay.currentLocal().append({day: -1});
const previousMonth = today.append({month: -1});

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

component.onItemSelect(component.items[1]);
fixture.detectChanges();

const items = getItems();

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(items[0].nativeElement.contains(getCheckmark())).toBe(true);
expect(items[1].nativeElement.contains(getCheckmark())).toBe(false);
});
});

function getCalendar(): DebugElement | null {
Expand Down
Loading