Skip to content

Commit

Permalink
feat(kit): prevent disabled date selection for calendar-range in 3x v…
Browse files Browse the repository at this point in the history
…ersion
  • Loading branch information
p-ivannikov committed Jul 20, 2024
1 parent 276879b commit 2c144d0
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions projects/kit/components/calendar-range/calendar-range.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,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 +232,37 @@ 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 {
if (disabledItemHandler(item)) {
return true;
}

if (!value || !value.isSingleDay) {
return false;
}

let temp = item;

while (temp.dayBefore(value.from) || temp.dayAfter(value.from)) {
if (disabledItemHandler(temp)) {
return true;
}

const day = temp.dayBefore(value.from) ? 1 : -1;

temp = temp.append({day});
}

return false;
}
}

0 comments on commit 2c144d0

Please sign in to comment.