Skip to content

Commit

Permalink
fix(kit): CalendarRange delete code for availableRange property (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Aug 26, 2024
1 parent a666b53 commit 18d9b07
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,39 +168,6 @@ test.describe('InputDateRange', () => {
'07-item-and-calendar-interactions.png',
);
});

test('Prevent selection of range with disabled days', async ({page}) => {
const calendar = new TuiCalendarPO(
inputDateRange.calendarRange.locator('tui-calendar'),
);

const getCellSelectors = async (cell: Locator): Promise<string | null> =>
cell.getAttribute('class');

const daysSelectors = async (): Promise<Array<string | null>> =>
Promise.all((await calendar.getDays()).map(getCellSelectors));

await tuiGoto(page, 'components/input-date-range/API?disabledItemHandler$=1');

await inputDateRange.textfield.click();

// check disabled items length before day selection
expect(
(await daysSelectors()).filter((selectors) =>
selectors?.includes('t-cell_disabled'),
),
).toHaveLength(20);

await calendar.clickOnCalendarDay(7);

// check range which includes disabled days
// range should have only 2 enabled items
expect(
(await daysSelectors()).filter(
(selectors) => !selectors?.includes('t-cell_disabled'),
),
).toHaveLength(2);
});
});

test.describe('Examples', () => {
Expand Down
60 changes: 2 additions & 58 deletions projects/kit/components/calendar-range/calendar-range.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export class TuiCalendarRange implements OnInit, OnChanges {
protected previousValue: TuiDayRange | null = null;
protected hoveredItem: TuiDay | null = null;
protected readonly capsMapper = TUI_DAY_CAPS_MAPPER;
protected availableRange: TuiDayRange | null = null;

@Input()
public defaultViewedMonth: TuiMonth = TuiMonth.currentLocal();
Expand Down Expand Up @@ -206,8 +205,6 @@ export class TuiCalendarRange implements OnInit, OnChanges {
this.updateValue(sortedDayRange);
this.itemChange.emit(this.findItemByDayRange(sortedDayRange));
}

this.availableRange = this.findAvailableRange();
}

protected updateValue(value: TuiDayRange | null): void {
Expand Down Expand Up @@ -240,7 +237,7 @@ export class TuiCalendarRange implements OnInit, OnChanges {
): TuiBooleanHandler<TuiDay> {
return (item) => {
if (!value?.isSingleDay || !minLength) {
return this.isDisabledItem(disabledItemHandler, value, item);
return disabledItemHandler(item);
}

const negativeMinLength = Object.fromEntries(
Expand All @@ -251,63 +248,10 @@ export class TuiCalendarRange implements OnInit, OnChanges {
const inDisabledRange =
disabledBefore.dayBefore(item) && disabledAfter.dayAfter(item);

return (
inDisabledRange || this.isDisabledItem(disabledItemHandler, value, item)
);
return inDisabledRange || disabledItemHandler(item);
};
}

private isDisabledItem(
disabledItemHandler: TuiBooleanHandler<TuiDay>,
value: TuiDayRange | null,
item: TuiDay,
): boolean {
return (
disabledItemHandler(item) ||
(!!value?.isSingleDay && !this.availableRangeContainsItem(item))
);
}

private availableRangeContainsItem(item: TuiDay): boolean {
if (this.availableRange === null) {
return true;
}

const {from, to} = this.availableRange;

return from.daySameOrBefore(item) && to.daySameOrAfter(item);
}

private findAvailableRange(): TuiDayRange | null {
const {disabledItemHandler, value} = this;

if (!value?.isSingleDay || disabledItemHandler === TUI_FALSE_HANDLER) {
return null;
}

let from = value.from;
let to = value.from;

let leftShift = true;
let rightShift = true;

while (leftShift || rightShift) {
leftShift = !disabledItemHandler(from.append({day: -1}));

if (leftShift) {
from = from.append({day: -1});
}

rightShift = !disabledItemHandler(to.append({day: 1}));

if (rightShift) {
to = to.append({day: 1});
}
}

return new TuiDayRange(from, to);
}

private updateDefaultViewedMonth(): void {
if (this.max && this.defaultViewedMonth.monthSameOrAfter(this.max)) {
this.defaultViewedMonth = this.max.append({month: -1});
Expand Down

0 comments on commit 18d9b07

Please sign in to comment.