diff --git a/.changeset/weak-spies-happen.md b/.changeset/weak-spies-happen.md new file mode 100644 index 000000000..779c94504 --- /dev/null +++ b/.changeset/weak-spies-happen.md @@ -0,0 +1,5 @@ +--- +"ingred-ui": minor +--- + +Feat i18n support for DatePicker and DateRangePicker diff --git a/src/components/Calendar/Calendar/Calendar.tsx b/src/components/Calendar/Calendar/Calendar.tsx index e7fba8fc3..26823fb81 100644 --- a/src/components/Calendar/Calendar/Calendar.tsx +++ b/src/components/Calendar/Calendar/Calendar.tsx @@ -13,6 +13,15 @@ export type CalendarProps = React.HTMLAttributes & { * @default dayjs() */ date: Dayjs; + /** + * カレンダーに表示する年月のフォーマット + */ + monthFormat?: string; + /** + * カレンダーに表示する曜日のリスト + * @memo dayjs().format("ddd") で対応したいが、階層が深くなったりするので一旦静的な値で対処 + */ + weekList?: string[]; /** * カレンダーの左に表示するアクション */ @@ -37,6 +46,8 @@ export type CalendarProps = React.HTMLAttributes & { const Calendar = forwardRef(function Calendar( { date, + monthFormat, + weekList, actions, onClickCloseButton, isOutsideRange = () => false, @@ -74,6 +85,8 @@ const Calendar = forwardRef(function Calendar( & { * @default dayjs() */ endDate: Dayjs; + /** + * カレンダーに表示する年月のフォーマット + */ + monthFormat?: string; + /** + * カレンダーに表示する曜日のリスト + * @memo dayjs().format("ddd") で対応したいが、階層が深くなったりするので一旦静的な値で対処 + */ + weekList?: string[]; /** * カレンダーの左に表示するアクション */ @@ -60,6 +69,8 @@ export const CalendarRange = forwardRef( { startDate, endDate, + monthFormat, + weekList, actions, onClose, isOutsideRange = () => false, @@ -131,6 +142,8 @@ export const CalendarRange = forwardRef( ` export const HEIGHT = "400px"; - -export const weekList = { - en: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], - ja: ["日", "月", "火", "水", "木", "金", "土"], -}; - -export const monthList = { - en: [ - "Jan", - "Feb", - "Mar", - "Apr", - "May", - "June", - "July", - "Aug", - "Sept", - "Oct", - "Nov", - "Dec", - ], - ja: [ - "1月", - "2月", - "3月", - "4月", - "5月", - "6月", - "7月", - "8月", - "9月", - "10月", - "11月", - "12月", - ], -}; diff --git a/src/components/Calendar/internal/InnerCalendar/InnerCalendar.tsx b/src/components/Calendar/internal/InnerCalendar/InnerCalendar.tsx index d019e516a..bfdae65b0 100644 --- a/src/components/Calendar/internal/InnerCalendar/InnerCalendar.tsx +++ b/src/components/Calendar/internal/InnerCalendar/InnerCalendar.tsx @@ -3,7 +3,7 @@ import dayjs, { Dayjs } from "dayjs"; import { Icon, ScrollArea } from "../../.."; import { useTheme } from "styled-components"; import { useScrollCalendar } from "../../hooks/useScrollCalendar"; -import { HEIGHT, weekList } from "../../constants"; +import { HEIGHT } from "../../constants"; import { CalendarContainer, CalendarMonth, @@ -16,6 +16,8 @@ import { Day } from "../../Calendar/internal/Day"; type Props = { date: Dayjs; + monthFormat?: string; + weekList?: string[]; current: Dayjs; yearIsOpen: boolean; onYearIsOpen: (yearIsOpen: boolean) => void; @@ -24,6 +26,8 @@ type Props = { }; export const InnerCalendar: React.FC = ({ date, + weekList, + monthFormat, current, yearIsOpen, onYearIsOpen, @@ -51,7 +55,7 @@ export const InnerCalendar: React.FC = ({ {/* 年月の表示 */} - {m.format("YYYY年MM月")} + {m.format(monthFormat)} = ({ {/* カレンダーの表示 */} {/* 曜日の表示 */} - {weekList["ja"].map((week) => ( - {week} - ))} + {weekList?.map((week) => {week})} {/* 開始曜日まで空白をセット */} {Array.from(new Array(m.startOf("month").day()), (_, i) => ( diff --git a/src/components/Calendar/internal/InnerCalendarRange/InnerCalendarRange.tsx b/src/components/Calendar/internal/InnerCalendarRange/InnerCalendarRange.tsx index dd2603c80..c83b806ec 100644 --- a/src/components/Calendar/internal/InnerCalendarRange/InnerCalendarRange.tsx +++ b/src/components/Calendar/internal/InnerCalendarRange/InnerCalendarRange.tsx @@ -1,5 +1,5 @@ import React, { useRef } from "react"; -import { HEIGHT, weekList } from "../../constants"; +import { HEIGHT } from "../../constants"; import { useTheme } from "../../../../themes"; import { useScrollCalendar } from "../../hooks/useScrollCalendar"; import { Icon, ScrollArea } from "../../../"; @@ -18,6 +18,8 @@ import { Day } from "../../CalendarRange/internal/Day"; type Props = { startDate: Dayjs; endDate: Dayjs; + monthFormat?: string; + weekList?: string[]; current: Dayjs; yearIsOpen: boolean; onYearIsOpen: (yearIsOpen: boolean) => void; @@ -28,6 +30,8 @@ type Props = { export const InnerCalendarRange: React.FC = ({ startDate, endDate, + monthFormat, + weekList, current, yearIsOpen, onYearIsOpen, @@ -54,7 +58,7 @@ export const InnerCalendarRange: React.FC = ({ > - {m.format("YYYY年MM月")} + {m.format(monthFormat)} = ({ - {weekList["ja"].map((week) => ( - {week} - ))} + {weekList?.map((week) => {week})} {Array.from(new Array(m.startOf("month").day()), (_, i) => ( ))} diff --git a/src/components/LocaleProvider/LocaleProvider.stories.tsx b/src/components/LocaleProvider/LocaleProvider.stories.tsx index f7c007dba..d2f910416 100644 --- a/src/components/LocaleProvider/LocaleProvider.stories.tsx +++ b/src/components/LocaleProvider/LocaleProvider.stories.tsx @@ -6,6 +6,7 @@ import { ConfirmModal, DatePicker, MultipleFilter, + NewDatePicker, OptionType, Select, Spacer, @@ -184,6 +185,11 @@ export const Example: StoryObj = { {}} /> + +

NewDatePicker

+ + {}} /> + ); }, diff --git a/src/components/NewDatePicker/NewDatePicker.tsx b/src/components/NewDatePicker/NewDatePicker.tsx index e6e72fcce..76e821c9b 100644 --- a/src/components/NewDatePicker/NewDatePicker.tsx +++ b/src/components/NewDatePicker/NewDatePicker.tsx @@ -10,6 +10,7 @@ import { useInteractions, useRole, } from "@floating-ui/react"; +import { useLocaleProps } from "../../hooks/useLocaleProps"; export type NewDatePickerProps = { /** @@ -35,6 +36,15 @@ export type NewDatePickerProps = { * @default false */ disabled?: boolean; + /** + * カレンダーに表示する年月のフォーマット + */ + monthFormat?: string; + /** + * カレンダーに表示する曜日のリスト + * @memo dayjs().format("ddd") で対応したいが、階層が深くなったりするので一旦静的な値で対処 + */ + weekList?: string[]; /** * 選択可能なカレンダーの領域を制限する * true が返る場合は、選択不可となる @@ -51,18 +61,20 @@ export type NewDatePickerProps = { * @memo 次のメジャーリリースで DatePicker に変更。現行の DatePicker は削除。 */ export const NewDatePicker = forwardRef( - function DatePicker( - { + function DatePicker(inProps, ref) { + const props = useLocaleProps({ props: inProps, name: "NewDatePicker" }); + const { date, format = "YYYY-MM-DD", disabled = false, + monthFormat = "MMM YYYY", + weekList = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], isOutsideRange, errorText, actions, onDateChange, - }, - ref, - ) { + } = props; + const [open, setOpen] = useState(false); const { context, refs, strategy, x, y } = useFloating({ placement: "right-start", @@ -112,6 +124,8 @@ export const NewDatePicker = forwardRef( (function DateRangePicker( - { +>(function DateRangePicker(inProps, propRef) { + const props = useLocaleProps({ + props: inProps, + name: "NewDateRangePicker", + }); + const { startDate, endDate, errorText, disabled = false, + monthFormat = "MMM YYYY", + weekList = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], isOutsideRange = () => false, actions, onDatesChange, - }, - propRef, -) { + } = props; + const [open, setOpen] = useState(false); const { context, refs, strategy, x, y } = useFloating({ placement: "right-start", @@ -133,6 +148,8 @@ export const DateRangePicker = forwardRef< startDate={startDate} endDate={endDate} actions={actions} + monthFormat={monthFormat} + weekList={weekList} style={{ position: strategy, top: y ?? 0, diff --git a/src/constants/locale.ts b/src/constants/locale.ts index 9aac6a0c7..21d7508c1 100644 --- a/src/constants/locale.ts +++ b/src/constants/locale.ts @@ -7,7 +7,7 @@ import { MultipleFilterProps, ToggleButtonProps, } from ".."; -import { SelectProps } from "../components"; +import { NewDatePickerProps, SelectProps } from "../components"; import { CreatableSelectProps } from "../components/CreatableSelect"; import { FullSizeConfirmModalProps } from "../components/FullSizeConfirmModal"; import { EditFilterCardProps } from "../components/MultipleFilter/internal/EditFilterCard/EditFilterCard"; @@ -75,6 +75,12 @@ export interface Localization { DateRangePicker?: { defaultProps: Pick; }; + NewDatePicker?: { + defaultProps: Pick; + }; + NewDateRangePicker?: { + defaultProps: Pick; + }; }; } @@ -124,6 +130,18 @@ export const jaJP: Localization = { DateRangePicker: { defaultProps: { displayFormat: "YYYY/MM/DD", monthFormat: "YYYY年 M月" }, }, + NewDatePicker: { + defaultProps: { + monthFormat: "YYYY年MM月", + weekList: ["日", "月", "火", "水", "木", "金", "土"], + }, + }, + NewDateRangePicker: { + defaultProps: { + monthFormat: "YYYY年MM月", + weekList: ["日", "月", "火", "水", "木", "金", "土"], + }, + }, }, };