Skip to content

Commit

Permalink
[DateTimeRangePicker] Use time in referenceDate when selecting date
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasTy committed Nov 15, 2024
1 parent beba044 commit 375b769
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ const DateRangeCalendar = React.forwardRef(function DateRangeCalendar(
rangePosition,
allowRangeFlip,
shouldMergeDateAndTime: true,
referenceDate,
});

const isNextSectionAvailable = availableRangePositions.includes(nextSelection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface CalculateRangeChangeOptions {
*/
allowRangeFlip?: boolean;
shouldMergeDateAndTime?: boolean;
referenceDate?: PickerValidDate;
}

interface CalculateRangeChangeResponse {
Expand All @@ -28,6 +29,7 @@ export function calculateRangeChange({
rangePosition,
allowRangeFlip = false,
shouldMergeDateAndTime = false,
referenceDate,
}: CalculateRangeChangeOptions): CalculateRangeChangeResponse {
const [start, end] = range;

Expand All @@ -41,21 +43,26 @@ export function calculateRangeChange({
}
}

const newSelectedDate =
referenceDate && selectedDate && shouldMergeDateAndTime
? mergeDateAndTime(utils, selectedDate, referenceDate)
: selectedDate;

if (rangePosition === 'start') {
const truthyResult: CalculateRangeChangeResponse = allowRangeFlip
? { nextSelection: 'start', newRange: [end!, selectedDate] }
: { nextSelection: 'end', newRange: [selectedDate, null] };
return Boolean(end) && utils.isAfter(selectedDate!, end!)
? { nextSelection: 'start', newRange: [end!, newSelectedDate] }
: { nextSelection: 'end', newRange: [newSelectedDate, null] };
return Boolean(end) && utils.isAfter(newSelectedDate!, end!)
? truthyResult
: { nextSelection: 'end', newRange: [selectedDate, end] };
: { nextSelection: 'end', newRange: [newSelectedDate, end] };
}

const truthyResult: CalculateRangeChangeResponse = allowRangeFlip
? { nextSelection: 'end', newRange: [selectedDate, start!] }
: { nextSelection: 'end', newRange: [selectedDate, null] };
return Boolean(start) && utils.isBeforeDay(selectedDate!, start!)
? { nextSelection: 'end', newRange: [newSelectedDate, start!] }
: { nextSelection: 'end', newRange: [newSelectedDate, null] };
return Boolean(start) && utils.isBeforeDay(newSelectedDate!, start!)
? truthyResult
: { nextSelection: 'start', newRange: [start, selectedDate] };
: { nextSelection: 'start', newRange: [start, newSelectedDate] };
}

export function calculateRangePreview(options: CalculateRangeChangeOptions): PickerRangeValue {
Expand Down

0 comments on commit 375b769

Please sign in to comment.