Skip to content

Commit

Permalink
feat(kit): updated search of available range
Browse files Browse the repository at this point in the history
  • Loading branch information
p-ivannikov committed Aug 13, 2024
1 parent b49a156 commit 0f606c2
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions projects/kit/components/calendar-range/calendar-range.component.ts
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 @@ -245,28 +247,47 @@ export class TuiCalendarRangeComponent implements TuiWithOptionalMinMax<TuiDay>
): boolean {
return (
disabledItemHandler(item) ||
(!!value?.isSingleDay &&
this.isExternalDaysDisabled(disabledItemHandler, value.from, item))
(!!value?.isSingleDay && !this.availableRangeContainsItem(item))
);
}

private isExternalDaysDisabled(
disabledItemHandler: TuiBooleanHandler<TuiDay>,
startDay: TuiDay,
item: TuiDay,
): boolean {
let temp = item;
private availableRangeContainsItem(item: TuiDay): boolean {
if (this.availableRange === null) {
return true;
}

const {from, to} = this.availableRange;

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

while (temp.dayBefore(startDay) || temp.dayAfter(startDay)) {
if (disabledItemHandler(temp)) {
return true;
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});
}

const day = temp.dayBefore(startDay) ? 1 : -1;
rightShift = !disabledItemHandler(to.append({day: 1}));

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

return false;
return new TuiDayRange(from, to);
}
}

0 comments on commit 0f606c2

Please sign in to comment.