iTranslated by AI
Making Birthday Input as Easy as Possible with MUI DatePicker
Introduction
Creating a form where birthdays are easy to enter is surprisingly difficult. On the other hand, since I wanted to avoid extra implementation work as much as possible, I was looking into the MUI DatePicker options and found that it could be implemented with almost no modifications. In this article, I will introduce that small piece of know-how.
Final Result
import { AdapterDateFns } from "@mui/x-date-pickers/AdapterDateFns";
import { DatePicker } from "@mui/x-date-pickers/DatePicker";
import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider";
import ja from "date-fns/locale/ja";
<LocalizationProvider
dateAdapter={AdapterDateFns}
adapterLocale={ja}
localeText={{
previousMonth: "前月",
nextMonth: "次月"
}}
>
<DatePicker
label="誕生日"
format="yyyy年MM月dd日"
openTo="year"
views={["year", "month", "day"]}
maxDate={new Date()}
value={value}
onOpen={() => {
if (value === null) {
setValue(new Date(1990, 0, 1));
}
}}
onChange={(newValue) => {
setValue(newValue);
}}
/>
</LocalizationProvider>
Demo: https://codesandbox.io/s/birthdaypicker-demo-3sdekh?file=/demo.tsx
Regarding the CodeSandbox demo, errors such as being unable to import date-fns/locale/ja may occur during the initial load. In such cases, you can work around it by editing a single line and saving it.
What I Did
Displaying YearPicker First
DatePicker includes a YearPicker for selecting years, but by default, it displays the CalendarPicker for selecting days.
To avoid this, specify openTo="year" to make the YearPicker open first.
Displaying MonthPicker
MUI provides a MonthPicker for selecting months, but it is not displayed by default, requiring users to repeatedly click the left and right arrows in the calendar view.
To avoid this, specify the views={["year", "month", "day"]} option to display the MonthPicker as well. This alone makes it practical enough.
Hiding Future Dates
You can hide them with maxDate={new Date()}. Note that disableFuture={true} only makes them unselectable while still keeping them visible.
Localizing
You can localize using the LocalizationProvider. By specifying the localeText option in LocalizationProvider, you can also localize aria-labels.
Also, by specifying mask="____年__月__日", inputFormat="yyyy年MM月dd日"format="yyyy年MM月dd日", the display of the input result can also be localized. During keyboard input, it will auto-complete the year, month, and day suffixes just by entering numbers.
Specifying the Initial Year to Display
For example, if many users were born around 1990, you might want 1990 to be selected when the YearPicker opens. Although it's a bit straightforward, this can be achieved by passing a function to onOpen that sets an initial value.
onOpen={() => {
if (value === null) {
setValue(new Date(1990, 0, 1));
}
}}
Things that didn't work
Disabling today's date highlight
It seems that disableHighlightToday={true} can disable the highlight for today's date, but it didn't work well for me. This is because once the year is selected, today's month and day are automatically selected, resulting in today's date being highlighted. Also, since the year is displayed starting from 1900, it ends up being a DatePicker geared quite a bit toward older users. If you know a better way, please let me know.
2024/12/06
In an MUI update, it has been changed so that January 1st is automatically selected when a year is chosen. Additionally, disableHighlightToday={true} is a feature to disable the white circle on today's date and was not intended to avoid automatic selection.
Reference
MUI DatePicker API Reference: https://mui.com/x/api/date-pickers/date-picker/
Discussion
素朴にテキストフィールドでチャレンジしてみました
確かに素朴に実装してしまうのもいいですね、コード例が充実してとてもありがたいです!