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 authored Nov 15, 2024
1 parent 21fec19 commit a204305
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 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 @@ -39,6 +39,22 @@ describe('<DesktopDateTimeRangePicker />', () => {
expect(expectFieldValueV7(startSectionsContainer, '01/11/2018 04:05 PM'));
expect(expectFieldValueV7(endSectionsContainer, '01/11/2018 05:10 PM'));
});

it('should use time from `referenceDate` when selecting the day', () => {
render(
<DesktopDateTimeRangePicker referenceDate={adapterToUse.date('2022-04-14T14:15:00')} />,
);

openPicker({ type: 'date-time-range', variant: 'desktop', initialFocus: 'start' });

fireEvent.click(screen.getByRole('gridcell', { name: '11' }));

expect(screen.getByRole('option', { name: '2 hours', selected: true })).not.to.equal(null);
expect(screen.getByRole('option', { name: '15 minutes', selected: true })).not.to.equal(null);
expect(screen.getByRole('option', { name: 'PM', selected: true })).not.to.equal(null);
const startSectionsContainer = getFieldSectionsContainer(0);
expect(expectFieldValueV7(startSectionsContainer, '04/11/2022 02:15 PM'));
});
});

describe('disabled dates', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ describe('date-range-manager', () => {
expectedRange: [start2018, null],
expectedNextSelection: 'end' as const,
},
{
range: [null, null],
rangePosition: 'start' as const,
newDate: start2018,
expectedRange: [start2018At4PM, null],
expectedNextSelection: 'end' as const,
shouldMergeDateAndTime: true,
referenceDate: start2018At4PM,
},
{
range: [start2018, null],
rangePosition: 'start' as const,
Expand Down Expand Up @@ -99,7 +108,16 @@ describe('date-range-manager', () => {
expectedNextSelection: 'start' as const,
},
].forEach(
({ range, rangePosition, newDate, expectedRange, allowRangeFlip, expectedNextSelection }) => {
({
range,
rangePosition,
newDate,
expectedRange,
allowRangeFlip,
expectedNextSelection,
shouldMergeDateAndTime,
referenceDate,
}) => {
it(`calculateRangeChange should return ${expectedRange} when selecting ${rangePosition} of ${range} with user input ${newDate}`, () => {
expect(
calculateRangeChange({
Expand All @@ -108,6 +126,8 @@ describe('date-range-manager', () => {
newDate,
rangePosition,
allowRangeFlip,
shouldMergeDateAndTime,
referenceDate,
}),
).to.deep.equal({
nextSelection: expectedNextSelection,
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 a204305

Please sign in to comment.