Skip to content

Commit

Permalink
fix: range not selected when missing the from value (#1876)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam-Apostel authored Aug 16, 2023
1 parent b291218 commit 748dca4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 35 deletions.
28 changes: 14 additions & 14 deletions src/contexts/Modifiers/utils/isDateInRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import { DateRange } from 'types/Matchers';
/** Return `true` whether `date` is inside `range`. */
export function isDateInRange(date: Date, range: DateRange): boolean {
let { from, to } = range;
if (!from) {
return false;
if (from && to) {
const isRangeInverted = differenceInCalendarDays(to, from) < 0;
if (isRangeInverted) {
[from, to] = [to, from];
}
const isInRange =
differenceInCalendarDays(date, from) >= 0 &&
differenceInCalendarDays(to, date) >= 0;
return isInRange;
}
if (!to && isSameDay(from, date)) {
return true;
if (to) {
return isSameDay(to, date);
}
if (!to) {
return false;
if (from) {
return isSameDay(from, date);
}
const isRangeInverted = differenceInCalendarDays(to, from) < 0;
if (isRangeInverted) {
[from, to] = [to, from];
}
const isInRange =
differenceInCalendarDays(date, from) >= 0 &&
differenceInCalendarDays(to, date) >= 0;
return isInRange;
return false;
}
20 changes: 20 additions & 0 deletions src/contexts/SelectRange/SelectRangeContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ describe('when only the "from" day is selected', () => {
});
});

describe('when only the "to" day is selected', () => {
const selected = { from: undefined, to };
const dayPickerProps: DayPickerRangeProps = {
...initialProps,
selected
};
test('should return the "range_start" modifiers with the "to" day', () => {
const result = renderHook(dayPickerProps);
expect(result.current.modifiers.range_start).toEqual([to]);
});
test('should return the "range_end" modifiers with the "to" day', () => {
const result = renderHook(dayPickerProps);
expect(result.current.modifiers.range_end).toEqual([to]);
});
test('should not return any "range_middle" modifiers', () => {
const result = renderHook(dayPickerProps);
expect(result.current.modifiers.range_middle).toEqual([]);
});
});

describe('when a complete range of days is selected', () => {
const selected = { from, to };
const dayPickerProps: DayPickerRangeProps = {
Expand Down
17 changes: 17 additions & 0 deletions src/contexts/SelectRange/SelectRangeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ export function SelectRangeProviderInternal({
];
}
}
} else if (selectedTo) {
modifiers.range_start = [selectedTo];
modifiers.range_end = [selectedTo];
}

if (min) {
Expand All @@ -134,6 +137,12 @@ export function SelectRangeProviderInternal({
before: addDays(selectedFrom, min - 1)
});
}
if (!selectedFrom && selectedTo) {
modifiers.disabled.push({
after: subDays(selectedTo, min - 1),
before: addDays(selectedTo, min - 1)
});
}
}
if (max) {
if (selectedFrom && !selectedTo) {
Expand All @@ -155,6 +164,14 @@ export function SelectRangeProviderInternal({
after: addDays(selectedTo, offset)
});
}
if (!selectedFrom && selectedTo) {
modifiers.disabled.push({
before: addDays(selectedTo, -max + 1)
});
modifiers.disabled.push({
after: addDays(selectedTo, max - 1)
});
}
}

return (
Expand Down
45 changes: 24 additions & 21 deletions src/contexts/SelectRange/utils/addToRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,32 @@ export function addToRange(
range?: DateRange
): DateRange | undefined {
const { from, to } = range || {};
if (!from) {
return { from: day, to: undefined };
}
if (!to && isSameDay(from, day)) {
return { from: from, to: day };
}
if (!to && isBefore(day, from)) {
return { from: day, to: from };
}
if (!to) {
if (from && to) {
if (isSameDay(to, day) && isSameDay(from, day)) {
return undefined;
}
if (isSameDay(to, day)) {
return { from: to, to: undefined };
}
if (isSameDay(from, day)) {
return undefined;
}
if (isAfter(from, day)) {
return { from: day, to };
}
return { from, to: day };
}
if (isSameDay(to, day) && isSameDay(from, day)) {
return undefined;
}
if (isSameDay(to, day)) {
return { from: to, to: undefined };
}
if (isSameDay(from, day)) {
return undefined;
}
if (isAfter(from, day)) {
if (to) {
if (isAfter(day, to)) {
return { from: to, to: day };
}
return { from: day, to };
}
return { from, to: day };
if (from) {
if (isBefore(day, from)) {
return { from: day, to: from };
}
return { from, to: day };
}
return { from: day, to: undefined };
}

0 comments on commit 748dca4

Please sign in to comment.