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] How to customize the DatePicker calendar header? #13439

Closed
sirius829 opened this issue Jun 10, 2024 · 3 comments
Closed

[pickers] How to customize the DatePicker calendar header? #13439

sirius829 opened this issue Jun 10, 2024 · 3 comments
Labels
component: DatePicker The React component. component: pickers This is the name of the generic UI component, not the React module! customization: extend Logic customizability status: waiting for author Issue with insufficient information

Comments

@sirius829
Copy link

sirius829 commented Jun 10, 2024

Hi there,
Currently I am trying to make MD3 Datepicker with MUI X Date picker.
image
I want to add custom year select and month select on calendar header.
Here is my code for it.

function CustomCalendarHeader(props: PickersCalendarHeaderProps<Dayjs>) {
    const { currentMonth, onMonthChange, onViewChange } = props;
    const [selectedMonth, setSelectedMonth] = useState(currentMonth.month());
    const [selectedYear, setSelectedYear] = useState(currentMonth.year());
    const [view, setView] = useState<DateView>('month');
    useEffect(() => {
        setSelectedMonth(currentMonth.month());
        setSelectedYear(currentMonth.year());
    }, [currentMonth]);

    useEffect(() => {
        if (onViewChange)
            onViewChange(view);
    }, [view]);

    const selectNextMonth = () => onMonthChange(currentMonth.add(1, 'month'), 'left');
    const selectNextYear = () => onMonthChange(currentMonth.add(1, 'year'), 'left');
    const selectPreviousMonth = () => onMonthChange(currentMonth.subtract(1, 'month'), 'right');
    const selectPreviousYear = () => onMonthChange(currentMonth.subtract(1, 'year'), 'right');

    const months = Array.from({ length: 12 }, (_, index) => currentMonth.month(index).format('MMMM'));
    const years = Array.from({ length: 20 }, (_, index) => currentMonth.year() - 10 + index);

    return (
        <CustomCalendarHeaderRoot>
            <Stack spacing={1} direction="row" alignItems="center">
                <IconButton onClick={selectPreviousMonth} title="Previous month">
                    <ChevronLeft />
                </IconButton>
                <Button variant="text" color="primary" onClick={() => {onViewChange && onViewChange("month")}}>
                    {selectedMonth}
                </Button>
                <IconButton onClick={selectNextMonth} title="Next month">
                    <ChevronRight />
                </IconButton>
            </Stack>
            <Stack spacing={1} direction="row" alignItems="center">
                <IconButton onClick={selectPreviousYear} title="Previous year">
                    <KeyboardDoubleArrowLeftIcon />
                </IconButton>
                <Button variant="text" color="primary" onClick={() => {onViewChange && onViewChange("year")}}>
                    {selectedYear}
                </Button>
                <IconButton onClick={selectNextYear} title="Next year">
                    <KeyboardDoubleArrowRightIcon />
                </IconButton>
            </Stack>
        </CustomCalendarHeaderRoot>
    );
}

It shows UI but how can I go to Year select view when I click year, and go back to day view when I click the specific year.
Of course should be same for month too.

Search keywords:

Search keywords:

@github-actions github-actions bot added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jun 10, 2024
@oliviertassinari oliviertassinari transferred this issue from mui/material-ui Jun 10, 2024
@michelengelen michelengelen changed the title How to customize the MUI X Datepicker calendar header? [pickers] How to customize the DatePicker calendar header? Jun 11, 2024
@michelengelen michelengelen added component: pickers This is the name of the generic UI component, not the React module! component: DatePicker The React component. customization: extend Logic customizability labels Jun 11, 2024
@michelengelen
Copy link
Member

Hey @sirius829 we do have a section about customizing the parts of the pickers in the docs. This one is specifically about the Toolbar (the calendar header)

@michelengelen michelengelen added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jun 11, 2024
@LukasTy
Copy link
Member

LukasTy commented Jun 11, 2024

@michelengelen I think you wanted to refer to the calendarHeader slot customization, didn't you? 🤔

In regards to the problem itself, I think that your best bet @sirius829 is to create your own year and month viewRenderers, which you'd pass to your Picker component.
That is not a small feat. 🙈
That's why we did not rush implementing the MD3 behavior.

Currently the DateCalendar is used to render all the date views (year, month, and day), but in your case, you'd need to have different views for the year and month.

Copy link

The issue has been inactive for 7 days and has been automatically closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: DatePicker The React component. component: pickers This is the name of the generic UI component, not the React module! customization: extend Logic customizability status: waiting for author Issue with insufficient information
Projects
None yet
Development

No branches or pull requests

3 participants