From b5d328cb41a0e8c663c5fe875ef2db9a8f929036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=98=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2?= Date: Wed, 18 Dec 2024 11:49:52 +0300 Subject: [PATCH] fix(core): dont allow to select disabled dates in calendar (#10007) --- .../calendar/calendar-sheet.component.ts | 24 +++++++++++++++++ .../calendar-range/calendar-range.pw.spec.ts | 27 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/projects/core/components/calendar/calendar-sheet.component.ts b/projects/core/components/calendar/calendar-sheet.component.ts index 8c886bae8a05..bb4cdb129b4c 100644 --- a/projects/core/components/calendar/calendar-sheet.component.ts +++ b/projects/core/components/calendar/calendar-sheet.component.ts @@ -149,6 +149,10 @@ export class TuiCalendarSheet { } protected onItemClick(item: TuiDay): void { + if (this.rangeHasDisabledDay(item)) { + return; + } + this.dayClick.emit(item); } @@ -167,4 +171,24 @@ export class TuiCalendarSheet { this.hoveredItem = day; this.hoveredItemChange.emit(day); } + + private rangeHasDisabledDay(item: TuiDay): boolean { + if (this.value instanceof TuiDayRange) { + const range = this.getRange(this.value, item); + + for ( + const day = range.from.toUtcNativeDate(); + day <= range.to.toUtcNativeDate(); + day.setDate(day.getDate() + 1) + ) { + const tuiDay = TuiDay.fromLocalNativeDate(day); + + if (this.disabledItemHandler(tuiDay)) { + return true; + } + } + } + + return false; + } } diff --git a/projects/demo-playwright/tests/kit/calendar-range/calendar-range.pw.spec.ts b/projects/demo-playwright/tests/kit/calendar-range/calendar-range.pw.spec.ts index a83cccee0bd4..96d539ab38bc 100644 --- a/projects/demo-playwright/tests/kit/calendar-range/calendar-range.pw.spec.ts +++ b/projects/demo-playwright/tests/kit/calendar-range/calendar-range.pw.spec.ts @@ -92,5 +92,32 @@ test.describe('CalendarRange', () => { await expect(example).toHaveScreenshot('06-maximum-month-with-items.png'); }); + + test('Dont allow to select disabled dates in calendar', async ({page}) => { + await tuiGoto(page, `${DemoRoute.CalendarRange}/API?disabledItemHandler$=1`); + + await expect(example).toHaveScreenshot('08-disabled-dates-1-default.png'); + + const getCells = (): Locator => + page.locator('[automation-id="tui-calendar-sheet__cell"]'); + + await getCells().nth(1).click(); + + await expect(example).toHaveScreenshot('08-disabled-dates-2-select-from.png'); + + await getCells().nth(9).hover(); + + await expect(example).toHaveScreenshot('08-disabled-dates-3-hover-to.png'); + + await getCells().nth(9).click(); + await page.mouse.click(100, 100); // clear focus + + await expect(example).toHaveScreenshot('08-disabled-dates-4-click-to.png'); + + await getCells().nth(0).click(); + await page.mouse.click(100, 100); // clear focus + + await expect(example).toHaveScreenshot('08-disabled-dates-5-click-to.png'); + }); }); });