Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DateTimeRangePicker] Fix selection on same day #12604

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import * as React from 'react';
import { expect } from 'chai';
import { screen } from '@mui-internal/test-utils';
import { createPickerRenderer, adapterToUse } from 'test/utils/pickers';
import { screen, userEvent } from '@mui-internal/test-utils';
import {
createPickerRenderer,
adapterToUse,
openPicker,
getFieldSectionsContainer,
expectFieldValueV7,
} from 'test/utils/pickers';
import { DesktopDateTimeRangePicker } from '../DesktopDateTimeRangePicker';

describe('<DesktopDateTimeRangePicker />', () => {
Expand All @@ -10,6 +16,31 @@ describe('<DesktopDateTimeRangePicker />', () => {
clockConfig: new Date(2018, 0, 10, 10, 16, 0),
});

describe('value selection', () => {
it('should allow to select range within the same day', () => {
render(<DesktopDateTimeRangePicker enableAccessibleFieldDOMStructure />);

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

// select start date range
userEvent.mousePress(screen.getByRole('gridcell', { name: '11' }));
userEvent.mousePress(screen.getByRole('option', { name: '4 hours' }));
userEvent.mousePress(screen.getByRole('option', { name: '5 minutes' }));
userEvent.mousePress(screen.getByRole('option', { name: 'PM' }));

// select end date range on the same day
userEvent.mousePress(screen.getByRole('gridcell', { name: '11' }));
userEvent.mousePress(screen.getByRole('option', { name: '5 hours' }));
userEvent.mousePress(screen.getByRole('option', { name: '10 minutes' }));
userEvent.mousePress(screen.getByRole('option', { name: 'PM' }));

const startSectionsContainer = getFieldSectionsContainer(0);
const endSectionsContainer = getFieldSectionsContainer(1);
expect(expectFieldValueV7(startSectionsContainer, '01/11/2018 04:05 PM'));
expect(expectFieldValueV7(endSectionsContainer, '01/11/2018 05:10 PM'));
});
});

describe('disabled dates', () => {
it('should respect the "disablePast" prop', () => {
render(<DesktopDateTimeRangePicker enableAccessibleFieldDOMStructure disablePast open />);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { calculateRangeChange, calculateRangePreview } from './date-range-manage
import { DateRange } from '../../models';

const start2018 = adapterToUse.date('2018-01-01');
const start2018At4PM = adapterToUse.date('2018-01-01T16:00:00');
const mid2018 = adapterToUse.date('2018-07-01');
const end2019 = adapterToUse.date('2019-01-01');

Expand Down Expand Up @@ -88,6 +89,14 @@ describe('date-range-manager', () => {
allowRangeFlip: true,
expectedNextSelection: 'end' as const,
},
{
range: [start2018At4PM, null],
rangePosition: 'end' as const,
newDate: start2018,
expectedRange: [start2018At4PM, start2018],
allowRangeFlip: false,
expectedNextSelection: 'start' as const,
},
].forEach(
({ range, rangePosition, newDate, expectedRange, allowRangeFlip, expectedNextSelection }) => {
it(`calculateRangeChange should return ${expectedRange} when selecting ${rangePosition} of ${range} with user input ${newDate}`, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function calculateRangeChange<TDate extends PickerValidDate>({
const truthyResult: CalculateRangeChangeResponse<TDate> = allowRangeFlip
? { nextSelection: 'end', newRange: [selectedDate, start!] }
: { nextSelection: 'end', newRange: [selectedDate, null] };
return Boolean(start) && utils.isBefore(selectedDate!, start!)
return Boolean(start) && utils.isBeforeDay(selectedDate!, start!)
? truthyResult
: { nextSelection: 'start', newRange: [start, selectedDate] };
}
Expand Down
Loading