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

feat(kit): prevent disabled days selection for calendar-range #8328

Merged
Merged
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 @@ -160,6 +160,35 @@
'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 getCellState = async (cell: Locator): Promise<string | null> =>
cell.getAttribute('data-state');

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

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

await inputDateRange.textfield.click();

// check disabled items length before day selection
expect(
(await getDaysState()).filter(state => state === 'disabled'),
).toHaveLength(20);

await calendar.clickOnCalendarDay(7);

// check range which includes disabled days
// range should have only 2 enabled items
expect(
(await getDaysState()).filter(state => state !== 'disabled'),
).toHaveLength(2);

Check failure on line 190 in projects/demo-playwright/tests/kit/input-date-range/input-date-range.spec.ts

View workflow job for this annotation

GitHub Actions / playwright / (3 of 4)

[chromium] › tests/kit/input-date-range/input-date-range.spec.ts:164:13 › InputDateRange › API › Prevent selection of range with disabled days

1) [chromium] › tests/kit/input-date-range/input-date-range.spec.ts:164:13 › InputDateRange › API › Prevent selection of range with disabled days Error: expect(received).toHaveLength(expected) Expected length: 2 Received length: 41 Received array: [null, null, null, null, "hover", null, null, null, null, null, …] 188 | expect( 189 | (await getDaysState()).filter(state => state !== 'disabled'), > 190 | ).toHaveLength(2); | ^ 191 | }); 192 | }); 193 | at /home/runner/work/taiga-ui/taiga-ui/projects/demo-playwright/tests/kit/input-date-range/input-date-range.spec.ts:190:15

Check failure on line 190 in projects/demo-playwright/tests/kit/input-date-range/input-date-range.spec.ts

View workflow job for this annotation

GitHub Actions / playwright / (3 of 4)

[chromium] › tests/kit/input-date-range/input-date-range.spec.ts:164:13 › InputDateRange › API › Prevent selection of range with disabled days

1) [chromium] › tests/kit/input-date-range/input-date-range.spec.ts:164:13 › InputDateRange › API › Prevent selection of range with disabled days Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toHaveLength(expected) Expected length: 2 Received length: 41 Received array: [null, null, null, null, "hover", null, null, null, null, null, …] 188 | expect( 189 | (await getDaysState()).filter(state => state !== 'disabled'), > 190 | ).toHaveLength(2); | ^ 191 | }); 192 | }); 193 | at /home/runner/work/taiga-ui/taiga-ui/projects/demo-playwright/tests/kit/input-date-range/input-date-range.spec.ts:190:15
});
});

test.describe('Examples', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class TuiCalendarRangeComponent implements TuiWithOptionalMinMax<TuiDay>
@Output()
readonly valueChange = new EventEmitter<TuiDayRange | null>();

availableRange: TuiDayRange | null = null;
previousValue: TuiDayRange | null = null;

selectedActivePeriod: TuiDayRangePeriod | null = null;
Expand Down Expand Up @@ -171,6 +172,7 @@ export class TuiCalendarRangeComponent implements TuiWithOptionalMinMax<TuiDay>

if (value === null || !value.isSingleDay) {
this.value = new TuiDayRange(day, day);
this.availableRange = this.findAvailableRange();

return;
}
Expand Down Expand Up @@ -221,7 +223,7 @@ export class TuiCalendarRangeComponent implements TuiWithOptionalMinMax<TuiDay>
): TuiBooleanHandler<TuiDay> {
return item => {
if (!value?.isSingleDay || !minLength) {
return disabledItemHandler(item);
return this.isDisabledItem(disabledItemHandler, value, item);
}

const negativeMinLength = tuiObjectFromEntries(
Expand All @@ -232,7 +234,60 @@ export class TuiCalendarRangeComponent implements TuiWithOptionalMinMax<TuiDay>
const inDisabledRange =
disabledBefore.dayBefore(item) && disabledAfter.dayAfter(item);

return inDisabledRange || disabledItemHandler(item);
return (
inDisabledRange || this.isDisabledItem(disabledItemHandler, value, 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 === ALWAYS_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);
}
}
Loading