Skip to content

Commit

Permalink
[pickers] Allow to render the months before currentMonth instead of t…
Browse files Browse the repository at this point in the history
…he one after
  • Loading branch information
flaviendelangle committed Apr 12, 2023
1 parent adc6db1 commit 06145ab
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 20 deletions.
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 />

This file was deleted.

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 rendered months

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'
? 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'
*/
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

0 comments on commit 06145ab

Please sign in to comment.