Skip to content

Commit

Permalink
fix(core): dont allow to select disabled dates in calendar (#10007)
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Dec 18, 2024
1 parent c56cfbf commit b5d328c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
24 changes: 24 additions & 0 deletions projects/core/components/calendar/calendar-sheet.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ export class TuiCalendarSheet {
}

protected onItemClick(item: TuiDay): void {
if (this.rangeHasDisabledDay(item)) {
return;
}

this.dayClick.emit(item);
}

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});

0 comments on commit b5d328c

Please sign in to comment.