Skip to content

Commit

Permalink
[DateTimePicker] Allow same date selection (#2016)
Browse files Browse the repository at this point in the history
* [DateTimePicker] Allow same date selection

* Override all props in the DateRangePickerDay
  • Loading branch information
dmtrKovalenko authored Jul 22, 2020
1 parent c235589 commit 32b7483
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
6 changes: 3 additions & 3 deletions lib/src/DateRangePicker/DateRangePickerDay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ export const PureDateRangeDay = ({
})}
>
<Day
allowKeyboardControl={false}
disableMargin
{...other}
disableMargin
allowSameDateSelection
allowKeyboardControl={false}
day={day}
selected={selected}
allowSameDateSelection={true}
inCurrentMonth={inCurrentMonth}
data-mui-test="DateRangeDay"
className={clsx(
Expand Down
1 change: 1 addition & 0 deletions lib/src/DateTimePicker/DateTimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function useInterceptProps({
orientation,
showToolbar: true,
showTabs: true,
allowSameDateSelection: true,
minDate: minDateTime || minDate,
minTime: minDateTime || minTime,
maxDate: maxDateTime || maxDate,
Expand Down
18 changes: 17 additions & 1 deletion lib/src/__tests__/DateTimePickerTestingLib.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import TextField from '@material-ui/core/TextField';
import { utilsToUse } from './test-utils';
import { createClientRender } from './createClientRender';
import { DesktopDateTimePicker } from '../DateTimePicker';
import { DesktopDateTimePicker, StaticDateTimePicker } from '../DateTimePicker';
import { fireEvent, screen, waitFor } from '@testing-library/react';

describe('<DateTimePicker />', () => {
Expand Down Expand Up @@ -96,4 +96,20 @@ describe('<DateTimePicker />', () => {
fireEvent.click(screen.getByLabelText('open next view'));
expect(screen.getByLabelText('open next view')).toBeDisabled();
});

it('allows to select the same day and move to the next view', () => {
const onChangeMock = jest.fn()
render(
<StaticDateTimePicker
onChange={onChangeMock}
renderInput={(props) => <TextField {...props} />}
value={utilsToUse.date('2018-01-01T00:00:00.000Z')}
/>
);

fireEvent.click(screen.getByLabelText("Jan 1, 2018"))
expect(onChangeMock).toHaveBeenCalled()

expect(screen.getByLabelText(/Selected time/)).toBeInTheDocument()
})
});
19 changes: 12 additions & 7 deletions lib/src/views/Calendar/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { useGlobalKeyDown, keycode } from '../../_shared/hooks/useKeyDown';
import { SlideTransition, SlideDirection, SlideTransitionProps } from './SlideTransition';

export interface ExportedCalendarProps
extends Pick<DayProps, 'disableHighlightToday' | 'showDaysOutsideCurrentMonth'> {
extends Pick<
DayProps,
'disableHighlightToday' | 'showDaysOutsideCurrentMonth' | 'allowSameDateSelection'
> {
/**
* Calendar onChange.
*/
Expand Down Expand Up @@ -55,7 +58,7 @@ export interface CalendarProps extends ExportedCalendarProps {
}

const muiComponentConfig = { name: 'MuiPickersCalendar' };
export const useStyles = makeStyles((theme) => {
export const useStyles = makeStyles(theme => {
const weeksContainerHeight = (DAY_SIZE + DAY_MARGIN * 4) * 6;
return {
root: {
Expand Down Expand Up @@ -119,6 +122,7 @@ export const Calendar: React.FC<CalendarProps> = withDefaultProps(
showDaysOutsideCurrentMonth,
className,
loading,
allowSameDateSelection,
renderLoading = () => <span data-mui-test="loading-progress">...</span>,
TransitionProps,
}) => {
Expand Down Expand Up @@ -156,7 +160,7 @@ export const Calendar: React.FC<CalendarProps> = withDefaultProps(
const currentMonthNumber = utils.getMonth(currentMonth);
const selectedDates = (Array.isArray(date) ? date : [date])
.filter(Boolean)
.map((selectedDateItem) => utils.startOfDay(selectedDateItem));
.map(selectedDateItem => utils.startOfDay(selectedDateItem));

return (
<React.Fragment>
Expand Down Expand Up @@ -184,9 +188,9 @@ export const Calendar: React.FC<CalendarProps> = withDefaultProps(
{...TransitionProps}
>
<div role="grid" className={classes.weekContainer}>
{utils.getWeekArray(currentMonth).map((week) => (
{utils.getWeekArray(currentMonth).map(week => (
<div role="row" key={`week-${week[0]}`} className={classes.week}>
{week.map((day) => {
{week.map(day => {
const disabled = isDateDisabled(day);
const isDayInCurrentMonth = utils.getMonth(day) === currentMonthNumber;

Expand All @@ -196,14 +200,15 @@ export const Calendar: React.FC<CalendarProps> = withDefaultProps(
role: 'cell',
isAnimating: isMonthSwitchingAnimating,
disabled: disabled,
allowKeyboardControl: allowKeyboardControl,
allowKeyboardControl,
allowSameDateSelection,
focused:
allowKeyboardControl &&
Boolean(focusedDay) &&
utils.isSameDay(day, focusedDay),
today: utils.isSameDay(day, now),
inCurrentMonth: isDayInCurrentMonth,
selected: selectedDates.some((selectedDate) =>
selected: selectedDates.some(selectedDate =>
utils.isSameDay(selectedDate, day)
),
disableHighlightToday,
Expand Down

1 comment on commit 32b7483

@vercel
Copy link

@vercel vercel bot commented on 32b7483 Jul 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.