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 customize the month and the year buttons #13321

Merged
merged 14 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
30 changes: 1 addition & 29 deletions docs/data/date-pickers/custom-components/custom-components.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
productId: x-date-pickers
title: Date and Time Pickers - Custom slots and subcomponents
components: DateTimePickerTabs, PickersActionBar, DatePickerToolbar, TimePickerToolbar, DateTimePickerToolbar, PickersCalendarHeader, PickersRangeCalendarHeader, PickersShortcuts, DateRangePickerToolbar
components: DateTimePickerTabs, PickersActionBar, DatePickerToolbar, TimePickerToolbar, DateTimePickerToolbar, PickersShortcuts, DateRangePickerToolbar
---

# Custom slots and subcomponents
Expand Down Expand Up @@ -219,34 +219,6 @@ Each component comes with its own toolbar (`DatePickerToolbar`, `TimePickerToolb

{{"demo": "ToolbarComponent.js"}}

## Calendar header

The calendar header is available on any component that renders a calendar to select a date or a range of dates.
It allows the user to navigate through months and to switch to the month and year views when available.

### Component props

You can pass props to the calendar header as shown below:

{{"demo": "CalendarHeaderComponentProps.js", "defaultCodeOpen": false}}

### Component

You can pass custom components to replace the header, as shown below:

{{"demo": "CalendarHeaderComponent.js", "defaultCodeOpen": false}}

When used with a date range component,
you receive three additional props to let you handle scenarios where multiple months are rendered:

- `calendars`: The number of calendars rendered
- `month`: The month used for the header being rendered
- `monthIndex`: The index of the month used for the header being rendered

The demo below shows how to navigate the months two by two:

{{"demo": "CalendarHeaderComponentRange.js", "defaultCodeOpen": false}}

## Arrow switcher

The following slots let you customize how to render the buttons and icons for an arrow switcher component—the component
Expand Down
26 changes: 26 additions & 0 deletions docs/data/date-pickers/custom-views/MonthButtonComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as React from 'react';
import { styled } from '@mui/material/styles';
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DateCalendar } from '@mui/x-date-pickers/DateCalendar';

const CustomMonthButton = styled('button')({
margin: '8px 0',
height: 36,
width: 72,
});

export default function MonthButtonComponent() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateCalendar']}>
<DateCalendar
slots={{ monthButton: CustomMonthButton }}
views={['month', 'day']}
openTo="month"
/>
</DemoContainer>
</LocalizationProvider>
);
}
26 changes: 26 additions & 0 deletions docs/data/date-pickers/custom-views/MonthButtonComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as React from 'react';
import { styled } from '@mui/material/styles';
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DateCalendar } from '@mui/x-date-pickers/DateCalendar';

const CustomMonthButton = styled('button')({
margin: '8px 0',
height: 36,
width: 72,
});

export default function MonthButtonComponent() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateCalendar']}>
<DateCalendar
slots={{ monthButton: CustomMonthButton }}
views={['month', 'day']}
openTo="month"
/>
</DemoContainer>
</LocalizationProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<DateCalendar
slots={{ monthButton: CustomMonthButton }}
views={['month', 'day']}
openTo="month"
/>
23 changes: 23 additions & 0 deletions docs/data/date-pickers/custom-views/MonthButtonComponentProps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as React from 'react';
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DateCalendar } from '@mui/x-date-pickers/DateCalendar';

export default function MonthButtonComponentProps() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateCalendar']}>
<DateCalendar
slotProps={{
monthButton: {
sx: { border: '1px red solid' },
},
}}
views={['month', 'day']}
openTo="month"
/>
</DemoContainer>
</LocalizationProvider>
);
}
23 changes: 23 additions & 0 deletions docs/data/date-pickers/custom-views/MonthButtonComponentProps.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as React from 'react';
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DateCalendar } from '@mui/x-date-pickers/DateCalendar';

export default function MonthButtonComponentProps() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateCalendar']}>
<DateCalendar
slotProps={{
monthButton: {
sx: { border: '1px red solid' },
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
},
}}
views={['month', 'day']}
openTo="month"
/>
</DemoContainer>
</LocalizationProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<DateCalendar
slotProps={{
monthButton: {
sx: { border: '1px red solid' },
},
}}
views={['month', 'day']}
openTo="month"
/>
22 changes: 22 additions & 0 deletions docs/data/date-pickers/custom-views/YearButtonComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as React from 'react';
import { styled } from '@mui/material/styles';
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DateCalendar } from '@mui/x-date-pickers/DateCalendar';

const CustomYearButton = styled('button')({
margin: '8px 0',
height: 36,
width: 72,
});

export default function YearButtonComponent() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateCalendar']}>
<DateCalendar slots={{ yearButton: CustomYearButton }} openTo="year" />
</DemoContainer>
</LocalizationProvider>
);
}
22 changes: 22 additions & 0 deletions docs/data/date-pickers/custom-views/YearButtonComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as React from 'react';
import { styled } from '@mui/material/styles';
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DateCalendar } from '@mui/x-date-pickers/DateCalendar';

const CustomYearButton = styled('button')({
margin: '8px 0',
height: 36,
width: 72,
});

export default function YearButtonComponent() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateCalendar']}>
<DateCalendar slots={{ yearButton: CustomYearButton }} openTo="year" />
</DemoContainer>
</LocalizationProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<DateCalendar slots={{ yearButton: CustomYearButton }} openTo="year" />
22 changes: 22 additions & 0 deletions docs/data/date-pickers/custom-views/YearButtonComponentProps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as React from 'react';
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DateCalendar } from '@mui/x-date-pickers/DateCalendar';

export default function YearButtonComponentProps() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateCalendar']}>
<DateCalendar
openTo="year"
slotProps={{
yearButton: {
sx: { border: '1px red solid' },
},
}}
/>
</DemoContainer>
</LocalizationProvider>
);
}
22 changes: 22 additions & 0 deletions docs/data/date-pickers/custom-views/YearButtonComponentProps.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as React from 'react';
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DateCalendar } from '@mui/x-date-pickers/DateCalendar';

export default function YearButtonComponentProps() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateCalendar']}>
<DateCalendar
openTo="year"
slotProps={{
yearButton: {
sx: { border: '1px red solid' },
},
}}
/>
</DemoContainer>
</LocalizationProvider>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<DateCalendar
openTo="year"
slotProps={{
yearButton: {
sx: { border: '1px red solid' },
},
}}
/>
73 changes: 73 additions & 0 deletions docs/data/date-pickers/custom-views/custom-views.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
LukasTy marked this conversation as resolved.
Show resolved Hide resolved
productId: x-date-pickers
title: Date and Time Pickers - Custom views
components: PickersCalendarHeader, PickersRangeCalendarHeader, PickersMonth, PickersYear
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
---

# Custom views

<p class="description">Learn how to override the default DOM structure of the views.</p>

## Calendar header

The calendar header is available on any component that renders a calendar to select a date or a range of dates.
It allows the user to navigate through months and to switch to the month and year views when available.

### Component props

You can pass props to the calendar header as shown below:

{{"demo": "CalendarHeaderComponentProps.js"}}

### Component

You can pass a custom component to replace the header, as shown below:

{{"demo": "CalendarHeaderComponent.js"}}

When used with a date range component,
you receive three additional props to let you handle scenarios where multiple months are rendered:

- `calendars`: The number of calendars rendered
- `month`: The month used for the header being rendered
- `monthIndex`: The index of the month used for the header being rendered

The demo below shows how to navigate the months two by two:

{{"demo": "CalendarHeaderComponentRange.js"}}

## Year button

This button allows users to change the selected year in the `year` view.

### Component props

You can pass props to the year button as shown below:

{{"demo": "YearButtonComponentProps.js"}}

### Component

You can pass a custom component to replace the year button, as shown below:

{{"demo": "YearButtonComponent.js"}}

## Month button

This button allows users to change the selected month in the `month` view.

:::success
You can learn more on how to enable the `month` view on the [`DateCalendar` doc page](/x/react-date-pickers/date-calendar/#views).
:::

### Component props

You can pass props to the month button as shown below:

{{"demo": "MonthButtonComponentProps.js"}}

### Component

You can pass a custom component to replace the month button, as shown below:

{{"demo": "MonthButtonComponent.js"}}
1 change: 1 addition & 0 deletions docs/data/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ const pages: MuiPage[] = [
title: 'Custom slots and subcomponents',
},
{ pathname: '/x/react-date-pickers/custom-layout' },
{ pathname: '/x/react-date-pickers/custom-views' },
{ pathname: '/x/react-date-pickers/custom-field' },
{ pathname: '/x/react-date-pickers/custom-opening-button' },
{ pathname: '/x/react-date-pickers/playground', title: 'Customization playground' },
Expand Down
12 changes: 12 additions & 0 deletions docs/pages/x/api/date-pickers/date-calendar.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,18 @@
"description": "Custom component for day.\nCheck the [PickersDay](https://mui.com/x/api/date-pickers/pickers-day/) component.",
"default": "PickersDay",
"class": null
},
{
"name": "monthButton",
"description": "Button displayed to render a single month in the \"month\" view.",
"default": "MonthCalendarButton",
"class": null
},
{
"name": "yearButton",
"description": "Button displayed to render a single year in the \"year\" view.",
"default": "YearCalendarButton",
"class": null
}
],
"classes": [
Expand Down
12 changes: 12 additions & 0 deletions docs/pages/x/api/date-pickers/date-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@
"default": "PickersDay",
"class": null
},
{
"name": "monthButton",
"description": "Button displayed to render a single month in the \"month\" view.",
"default": "MonthCalendarButton",
"class": null
},
{
"name": "yearButton",
"description": "Button displayed to render a single year in the \"year\" view.",
"default": "YearCalendarButton",
"class": null
},
{
"name": "actionBar",
"description": "Custom component for the action bar, it is placed below the picker views.",
Expand Down
Loading