Skip to content

Commit

Permalink
[DateRangePicker] Fix not working renderDay prop (mui#1953)
Browse files Browse the repository at this point in the history
* [DateRangePicker] Properly call `renderDayProp`

* Omit base calendar renderDay from props

* Make renderDay optional
  • Loading branch information
dmtrKovalenko authored Jul 10, 2020
1 parent 7d75a2c commit ad3ed4a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 43 deletions.
26 changes: 13 additions & 13 deletions docs/prop-types.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/src/DateRangePicker/DateRangePickerDay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useUtils } from '../_shared/hooks/useUtils';
import { makeStyles, fade } from '@material-ui/core/styles';
import { Day, DayProps, areDayPropsEqual } from '../views/Calendar/Day';

interface DateRangeDayProps extends DayProps {
export interface DateRangeDayProps extends DayProps {
isHighlighting: boolean;
isEndOfHighlighting: boolean;
isStartOfHighlighting: boolean;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/DateRangePicker/DateRangePickerView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
ExportedDesktopDateRangeCalendarProps,
} from './DateRangePickerViewDesktop';

type BaseCalendarPropsToReuse = Omit<ExportedCalendarViewProps, 'onYearChange'>;
type BaseCalendarPropsToReuse = Omit<ExportedCalendarViewProps, 'onYearChange' | 'renderDay'>;

export interface ExportedDateRangePickerViewProps
extends BaseCalendarPropsToReuse,
Expand Down
34 changes: 20 additions & 14 deletions lib/src/DateRangePicker/DateRangePickerViewDesktop.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as React from 'react';
import { DateRange } from './RangeTypes';
import { DateRangeDay } from './DateRangePickerDay';
import { useUtils } from '../_shared/hooks/useUtils';
import { makeStyles } from '@material-ui/core/styles';
import { MaterialUiPickersDate } from '../typings/date';
import { calculateRangePreview } from './date-range-manager';
import { Calendar, CalendarProps } from '../views/Calendar/Calendar';
import { DateRangeDay, DateRangeDayProps } from './DateRangePickerDay';
import { defaultMinDate, defaultMaxDate } from '../constants/prop-types';
import { ArrowSwitcher, ExportedArrowSwitcherProps } from '../_shared/ArrowSwitcher';
import {
Expand All @@ -25,11 +25,16 @@ export interface ExportedDesktopDateRangeCalendarProps {
* @default 2
*/
calendars?: 1 | 2 | 3;
/**
* Custom renderer for `<DateRangePicker />` days.
* @example (date, DateRangeDayProps) => <DateRangePickerDay {...DateRangeDayProps} />
*/
renderDay?: (date: MaterialUiPickersDate, DateRangeDayProps: DateRangeDayProps) => JSX.Element;
}

interface DesktopDateRangeCalendarProps
extends ExportedDesktopDateRangeCalendarProps,
CalendarProps,
Omit<CalendarProps, 'renderDay'>,
DateValidationProps,
ExportedArrowSwitcherProps {
date: DateRange;
Expand Down Expand Up @@ -93,6 +98,7 @@ export const DateRangePickerViewDesktop: React.FC<DesktopDateRangeCalendarProps>
maxDate: __maxDate,
currentlySelectingRangeEnd,
currentMonth,
renderDay = (_, props) => <DateRangeDay {...props} />,
...other
}) => {
const utils = useUtils();
Expand Down Expand Up @@ -175,18 +181,18 @@ export const DateRangePickerViewDesktop: React.FC<DesktopDateRangeCalendarProps>
onChange={handleDayChange}
currentMonth={monthOnIteration}
TransitionProps={CalendarTransitionProps}
renderDay={(day, _, DayProps) => (
<DateRangeDay
isPreviewing={isWithinRange(utils, day, previewingRange)}
isStartOfPreviewing={isStartOfRange(utils, day, previewingRange)}
isEndOfPreviewing={isEndOfRange(utils, day, previewingRange)}
isHighlighting={isWithinRange(utils, day, date)}
isStartOfHighlighting={isStartOfRange(utils, day, date)}
isEndOfHighlighting={isEndOfRange(utils, day, date)}
onMouseEnter={() => handlePreviewDayChange(day)}
{...DayProps}
/>
)}
renderDay={(day, _, DayProps) =>
renderDay(day, {
isPreviewing: isWithinRange(utils, day, previewingRange),
isStartOfPreviewing: isStartOfRange(utils, day, previewingRange),
isEndOfPreviewing: isEndOfRange(utils, day, previewingRange),
isHighlighting: isWithinRange(utils, day, date),
isStartOfHighlighting: isStartOfRange(utils, day, date),
isEndOfHighlighting: isEndOfRange(utils, day, date),
onMouseEnter: () => handlePreviewDayChange(day),
...DayProps,
})
}
/>
</div>
);
Expand Down
29 changes: 16 additions & 13 deletions lib/src/DateRangePicker/DateRangePickerViewMobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import { MaterialUiPickersDate } from '../typings/date';
import { Calendar, CalendarProps } from '../views/Calendar/Calendar';
import { ExportedArrowSwitcherProps } from '../_shared/ArrowSwitcher';
import { defaultMinDate, defaultMaxDate } from '../constants/prop-types';
import { ExportedDesktopDateRangeCalendarProps } from './DateRangePickerViewDesktop';
import {
isWithinRange,
isStartOfRange,
isEndOfRange,
DateValidationProps,
} from '../_helpers/date-utils';

export interface ExportedMobileDateRangeCalendarProps {}
export interface ExportedMobileDateRangeCalendarProps
extends Pick<ExportedDesktopDateRangeCalendarProps, 'renderDay'> {}

interface DesktopDateRangeCalendarProps
extends ExportedMobileDateRangeCalendarProps,
CalendarProps,
Omit<CalendarProps, 'renderDay'>,
DateValidationProps,
ExportedArrowSwitcherProps {
date: DateRange;
Expand All @@ -39,6 +41,7 @@ export const DateRangePickerViewMobile: React.FC<DesktopDateRangeCalendarProps>
rightArrowButtonProps,
rightArrowButtonText,
rightArrowIcon,
renderDay = (_, props) => <DateRangeDay {...props} />,
...other
}) => {
const utils = useUtils();
Expand Down Expand Up @@ -67,17 +70,17 @@ export const DateRangePickerViewMobile: React.FC<DesktopDateRangeCalendarProps>
{...other}
date={date}
onChange={onChange}
renderDay={(day, _, DayProps) => (
<DateRangeDay
isPreviewing={false}
isStartOfPreviewing={false}
isEndOfPreviewing={false}
isHighlighting={isWithinRange(utils, day, date)}
isStartOfHighlighting={isStartOfRange(utils, day, date)}
isEndOfHighlighting={isEndOfRange(utils, day, date)}
{...DayProps}
/>
)}
renderDay={(day, _, DayProps) =>
renderDay(day, {
isPreviewing: false,
isStartOfPreviewing: false,
isEndOfPreviewing: false,
isHighlighting: isWithinRange(utils, day, date),
isStartOfHighlighting: isStartOfRange(utils, day, date),
isEndOfHighlighting: isEndOfRange(utils, day, date),
...DayProps,
})
}
/>
</React.Fragment>
);
Expand Down
17 changes: 16 additions & 1 deletion lib/src/__tests__/DateRangePicker.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { utilsToUse } from './test-utils';
import { DesktopDateRangePicker } from '../';
import { screen, waitFor } from '@testing-library/react';
import { utilsToUse, getAllByMuiTest } from './test-utils';
import { TextField, TextFieldProps } from '@material-ui/core';
import { createClientRender, fireEvent } from './createClientRender';

Expand Down Expand Up @@ -31,4 +31,19 @@ describe('<DateRangePicker />', () => {
await waitFor(() => screen.getByRole('tooltip'));
expect(screen.getByRole('tooltip')).toBeInTheDocument();
});

it('prop – `renderDay` should be called and render days', async () => {
render(
<DesktopDateRangePicker
open
renderInput={defaultRangeRenderInput}
onChange={jest.fn()}
renderDay={day => <div key={String(day)} data-mui-test="renderDayCalled" />}
value={[null, null]}
/>
);

await waitFor(() => screen.getByRole('tooltip'));
expect(getAllByMuiTest('renderDayCalled').length).not.toBe(0);
});
});

0 comments on commit ad3ed4a

Please sign in to comment.