7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【React + MUI】令和最新版 DatePicker 日本語化

Last updated at Posted at 2024-06-30

blog-hirono-038-001r.png

MUI の DatePicker を日本人向けにカスタマイズしていきます。
https://mui.com/x/react-date-pickers/date-picker/
※ 日付ライブラリはdayjsを使用します。

完成イメージ

001.gif

変更点

ざっくり変更点は以下になります。

  • テキストエリア内のフォーマット
  • カレンダーヘッダーのフォーマット
  • カレンダー内の年一覧のフォーマット
  • カレンダーの曜日のフォーマット

テキストエリア内のフォーマット

DatePickerコンポーネントのformatを設定します。

DateComponents.tsx
import { DatePicker, LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';

export const DateComponents = (): JSX.Element => {
  return (
    <LocalizationProvider dateAdapter={AdapterDayjs}>
      <DatePicker format="YYYY/MM/DD" />
    </LocalizationProvider>
  );
};

カレンダーヘッダーのフォーマット

DatePickerコンポーネントのslotProps.calendarHeader.formatを設定します。

DateComponents.tsx
import { DatePicker, LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';

export const DateComponents = (): JSX.Element => {
  return (
    <LocalizationProvider dateAdapter={AdapterDayjs}>
      <DatePicker
          format="YYYY/MM/DD"
          slotProps={{ calendarHeader: { format: 'YYYY年MM月' } }}
        />
    </LocalizationProvider>
  );
};

カレンダー内の年一覧のフォーマット

LocalizationProviderコンポーネントのdateFormats.yearを設定します。

DateComponents.tsx
import { DatePicker, LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';

export const DateComponents = (): JSX.Element => {
  return (
    <LocalizationProvider dateAdapter={AdapterDayjs} dateFormats={{ year: 'YYYY年' }}>
      <DatePicker
          format="YYYY/MM/DD"
          slotProps={{ calendarHeader: { format: 'YYYY年MM月' } }}
        />
    </LocalizationProvider>
  );
};

カレンダーの曜日

dayjsに日本語プラグインを追加します。

DateComponents.tsx
import { DatePicker, LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import dayjs from 'dayjs';
import 'dayjs/locale/ja';

dayjs.locale('ja');

export const DateComponents = (): JSX.Element => {
  return (
    <LocalizationProvider dateAdapter={AdapterDayjs} dateFormats={{ year: 'YYYY年' }}>
      <DatePicker
          format="YYYY/MM/DD"
          slotProps={{ calendarHeader: { format: 'YYYY年MM月' } }}
        />
    </LocalizationProvider>
  );
};

完成系コード

DateComponents.tsx
import { Box } from '@mui/material';
import { DatePicker, LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import dayjs from 'dayjs';
import 'dayjs/locale/ja';

dayjs.locale('ja'); // カレンダーの曜日のフォーマット

export const DateComponents = (): JSX.Element => {
  return (
    <Box sx={{ p: 10, display: 'flex', justifyContent: 'center' }}>
      <LocalizationProvider
        dateAdapter={AdapterDayjs}
        dateFormats={{ year: 'YYYY年' }} // カレンダー内の年一覧のフォーマット
      >
        <DatePicker
          label="日本語"
          format="YYYY/MM/DD" // テキストエリア内のフォーマット
          slotProps={{ calendarHeader: { format: 'YYYY年MM月' } }} // カレンダーヘッダーのフォーマット
        />
      </LocalizationProvider>
    </Box>
  );
};

ここまで変更出来れば、最低限日本人でも使いやすい入力フォームになるかなと思います。

余談

カレンダーヘッダーのフォーマットは以前まで、
LocalizationProviderコンポーネントのdateFormatsmonthAndYearを設定すればよかったのですが非推奨な形式として削除されてました...
https://github.com/mui/mui-x/issues/10774
https://github.com/mui/mui-x/pull/10776

代わりにDatePickerコンポーネントにカレンダーヘッダーを上書きできるプロパティが追加されたという経緯みたいです。
https://github.com/mui/mui-x/pull/10990

7
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?