From bfeb25404fb4f8c9b65bb7d04b3a36bc45862f3c Mon Sep 17 00:00:00 2001 From: Vlad Date: Wed, 20 Nov 2024 10:06:42 +0200 Subject: [PATCH] fix(kit): fix calendar range presets filtration (#9777) Co-authored-by: Vladyslav Bondar Co-authored-by: mdlufy --- .../calendar-range.component.ts | 5 +++- .../test/calendar-range.component.spec.ts | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/projects/kit/components/calendar-range/calendar-range.component.ts b/projects/kit/components/calendar-range/calendar-range.component.ts index fd1e8de3aa2e..37942240e629 100644 --- a/projects/kit/components/calendar-range/calendar-range.component.ts +++ b/projects/kit/components/calendar-range/calendar-range.component.ts @@ -170,7 +170,10 @@ export class TuiCalendarRange implements OnInit, OnChanges { ...items.filter( (item) => (minLength === null || - item.range.from.append(minLength).daySameOrBefore(item.range.to)) && + item.range.from + .append(minLength) + .append({day: -1}) + .daySameOrBefore(item.range.to)) && (min === null || item.range.to.daySameOrAfter(min)) && (max === null || item.range.from.daySameOrBefore(max)), ), diff --git a/projects/kit/components/calendar-range/test/calendar-range.component.spec.ts b/projects/kit/components/calendar-range/test/calendar-range.component.spec.ts index 647c0d4a2206..a78a1196ceda 100644 --- a/projects/kit/components/calendar-range/test/calendar-range.component.spec.ts +++ b/projects/kit/components/calendar-range/test/calendar-range.component.spec.ts @@ -10,6 +10,7 @@ import type {ComponentFixture} from '@angular/core/testing'; import {TestBed} from '@angular/core/testing'; import {FormControl, NgControl} from '@angular/forms'; import {By} from '@angular/platform-browser'; +import type {TuiDayLike} from '@taiga-ui/cdk'; import { TUI_LAST_DAY, tuiControlValue, @@ -40,7 +41,9 @@ describe('rangeCalendarComponent', () => { [items]="items" [markerHandler]="markerHandler" [max]="max" + [maxLength]="maxLength" [min]="min" + [minLength]="minLength" [value]="value" (valueChange)="onRangeChange($event)" /> @@ -72,6 +75,10 @@ describe('rangeCalendarComponent', () => { public max = TUI_LAST_DAY; + public minLength: TuiDayLike | null = null; + + public maxLength: TuiDayLike | null = null; + public value: TuiDayRange | null = null; public defaultViewedMonth = TuiMonth.currentLocal(); @@ -254,6 +261,23 @@ describe('rangeCalendarComponent', () => { expect(items[1]?.nativeElement.contains(getCheckmark())).toBe(true); }); + it('show item if it matches with minLength and maxLength', () => { + const today = TuiDay.currentLocal(); + const length = {day: 1}; + const title = 'Период'; + + testComponent.minLength = length; + testComponent.maxLength = length; + + testComponent.items = [ + new TuiDayRangePeriod(new TuiDayRange(today, today), title), + ]; + + fixture.detectChanges(); + + expect(getItems()[0]?.nativeElement.textContent.trim()).toBe(title); + }); + it('should update selectedActivePeriod after onItemSelect', () => { if (component.items[1]) { component['onItemSelect'](component.items[1]);