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

[pickers] Allow to render the months before currentMonth instead of the one after #8592

Merged
merged 11 commits into from
May 15, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as React from 'react';
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
import { LocalizationProvider } from '@mui/x-date-pickers-pro';
import { AdapterDayjs } from '@mui/x-date-pickers-pro/AdapterDayjs';
import { DateRangeCalendar } from '@mui/x-date-pickers-pro/DateRangeCalendar';

export default function DateRangeCalendarCalendarDirectionProp() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateRangeCalendar']}>
<DateRangeCalendar calendarDirection="backward" disableFuture />
</DemoContainer>
</LocalizationProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as React from 'react';
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
import { LocalizationProvider } from '@mui/x-date-pickers-pro';
import { AdapterDayjs } from '@mui/x-date-pickers-pro/AdapterDayjs';
import { DateRangeCalendar } from '@mui/x-date-pickers-pro/DateRangeCalendar';

export default function DateRangeCalendarCalendarDirectionProp() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateRangeCalendar']}>
<DateRangeCalendar calendarDirection="backward" disableFuture />
</DemoContainer>
</LocalizationProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<DateRangeCalendar calendarDirection="backward" disableFuture />
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LocalizationProvider } from '@mui/x-date-pickers-pro';
import { AdapterDayjs } from '@mui/x-date-pickers-pro/AdapterDayjs';
import { DateRangeCalendar } from '@mui/x-date-pickers-pro/DateRangeCalendar';

export default function DateRangeCalendarCalendarProp() {
export default function DateRangeCalendarCalendarsProp() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateRangeCalendar', 'DateRangeCalendar']}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LocalizationProvider } from '@mui/x-date-pickers-pro';
import { AdapterDayjs } from '@mui/x-date-pickers-pro/AdapterDayjs';
import { DateRangeCalendar } from '@mui/x-date-pickers-pro/DateRangeCalendar';

export default function DateRangeCalendarCalendarProp() {
export default function DateRangeCalendarCalendarsProp() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateRangeCalendar', 'DateRangeCalendar']}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ The component can be disabled or read-only.

## Customization

### Render 1 to 3 months
### Choose the months to render

You can render up to 3 months at the same time using the `calendars` prop:

{{"demo": "DateRangeCalendarCalendarProp.js"}}
{{"demo": "DateRangeCalendarCalendarsProp.js"}}

You can render the month in the backward direction using the `calendarDirection` prop.
This can be useful when using `disableFuture` to render the current month and the month before instead of the current month and the month after.

{{"demo": "DateRangeCalendarCalendarDirectionProp.js"}}

### Custom day rendering

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ const DateRangeCalendar = React.forwardRef(function DateRangeCalendar<TDate>(
defaultRangePosition: inDefaultRangePosition,
onRangePositionChange: inOnRangePositionChange,
calendars,
calendarDirection = 'onward',
components,
componentsProps,
slots: innerSlots,
Expand Down Expand Up @@ -463,16 +464,14 @@ const DateRangeCalendar = React.forwardRef(function DateRangeCalendar<TDate>(
[calendars],
);

const visibleMonths = React.useMemo(
() =>
Array.from({ length: calendars }).map((_, index) =>
utils.setMonth(
calendarState.currentMonth,
utils.getMonth(calendarState.currentMonth) + index,
),
),
[utils, calendarState.currentMonth, calendars],
);
const visibleMonths = React.useMemo(() => {
const firstMonth =
calendarDirection === 'onward'
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
? calendarState.currentMonth
: utils.addMonths(calendarState.currentMonth, 1 - calendars);

return Array.from({ length: calendars }).map((_, index) => utils.addMonths(firstMonth, index));
}, [utils, calendarState.currentMonth, calendars, calendarDirection]);

const focusedMonth = React.useMemo(() => {
if (!autoFocus) {
Expand Down Expand Up @@ -580,6 +579,10 @@ DateRangeCalendar.propTypes = {
* - the `input` element if there is a field rendered.
*/
autoFocus: PropTypes.bool,
/**
* @default 'onward'
*/
calendarDirection: PropTypes.oneOf(['backward', 'onward']),
/**
* The number of calendars to render.
* @default 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ export interface ExportedDateRangeCalendarProps<TDate>
* @default 2
*/
calendars?: 1 | 2 | 3;
/**
* @default 'onward'
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
*/
calendarDirection?: 'onward' | 'backward';
/**
* If `true`, editing dates by dragging is disabled.
* @default false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ DateRangePicker.propTypes = {
* - the `input` element if there is a field rendered.
*/
autoFocus: PropTypes.bool,
/**
* @default 'onward'
*/
calendarDirection: PropTypes.oneOf(['backward', 'onward']),
/**
* The number of calendars to render on **desktop**.
* @default 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ DesktopDateRangePicker.propTypes = {
* - the `input` element if there is a field rendered.
*/
autoFocus: PropTypes.bool,
/**
* @default 'onward'
*/
calendarDirection: PropTypes.oneOf(['backward', 'onward']),
/**
* The number of calendars to render on **desktop**.
* @default 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ MobileDateRangePicker.propTypes = {
* - the `input` element if there is a field rendered.
*/
autoFocus: PropTypes.bool,
/**
* @default 'onward'
*/
calendarDirection: PropTypes.oneOf(['backward', 'onward']),
/**
* The number of calendars to render on **desktop**.
* @default 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ StaticDateRangePicker.propTypes = {
* - the `input` element if there is a field rendered.
*/
autoFocus: PropTypes.bool,
/**
* @default 'onward'
*/
calendarDirection: PropTypes.oneOf(['backward', 'onward']),
/**
* The number of calendars to render.
* @default 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const renderDateRangeViewCalendar = <TDate extends unknown>({
defaultRangePosition,
onRangePositionChange,
calendars,
calendarDirection,
components,
componentsProps,
slots,
Expand Down Expand Up @@ -67,6 +68,7 @@ export const renderDateRangeViewCalendar = <TDate extends unknown>({
defaultRangePosition={defaultRangePosition}
onRangePositionChange={onRangePositionChange}
calendars={calendars}
calendarDirection={calendarDirection}
components={components}
componentsProps={componentsProps}
slots={slots}
Expand Down