♻️(react) refactor dupplicated formatter
Make sure every date formatter has the same time zone by encapsulating it in a factored custom hook. Reorganize component to gain in readability and improve maintainability.
This commit is contained in:
committed by
aleb_the_flash
parent
76ad5621c6
commit
be87c97f31
@@ -6,7 +6,11 @@ import {
|
|||||||
GregorianCalendar,
|
GregorianCalendar,
|
||||||
toCalendar,
|
toCalendar,
|
||||||
} from "@internationalized/date";
|
} from "@internationalized/date";
|
||||||
import { useDateFormatter, useLocale } from "@react-aria/i18n";
|
import {
|
||||||
|
DateFormatterOptions,
|
||||||
|
useDateFormatter,
|
||||||
|
useLocale,
|
||||||
|
} from "@react-aria/i18n";
|
||||||
import { useCalendarState } from "@react-stately/calendar";
|
import { useCalendarState } from "@react-stately/calendar";
|
||||||
import { useCalendar } from "@react-aria/calendar";
|
import { useCalendar } from "@react-aria/calendar";
|
||||||
import {
|
import {
|
||||||
@@ -82,33 +86,29 @@ export const Calendar = ({
|
|||||||
...props
|
...props
|
||||||
}: CalendarSubProps) => {
|
}: CalendarSubProps) => {
|
||||||
const { locale } = useLocale();
|
const { locale } = useLocale();
|
||||||
|
const { t } = useCunningham();
|
||||||
|
const ref = useRef(null);
|
||||||
|
|
||||||
const state = useCalendarState({
|
const state = useCalendarState({
|
||||||
...props,
|
...props,
|
||||||
locale,
|
locale,
|
||||||
createCalendar,
|
createCalendar,
|
||||||
});
|
});
|
||||||
|
|
||||||
const ref = useRef(null);
|
|
||||||
const { calendarProps, prevButtonProps, nextButtonProps } = useCalendar(
|
const { calendarProps, prevButtonProps, nextButtonProps } = useCalendar(
|
||||||
props,
|
props,
|
||||||
state
|
state
|
||||||
);
|
);
|
||||||
|
|
||||||
const monthItemsFormatter = useDateFormatter({
|
const useTimeZoneFormatter = (formatOptions: DateFormatterOptions) => {
|
||||||
month: "long",
|
return useDateFormatter({
|
||||||
timeZone: state.timeZone,
|
...formatOptions,
|
||||||
});
|
timeZone: state.timeZone,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const selectedMonthItemFormatter = useDateFormatter({
|
const monthItemsFormatter = useTimeZoneFormatter({ month: "long" });
|
||||||
month: "short",
|
const selectedMonthItemFormatter = useTimeZoneFormatter({ month: "short" });
|
||||||
timeZone: state.timeZone,
|
const yearItemsFormatter = useTimeZoneFormatter({ year: "numeric" });
|
||||||
});
|
|
||||||
|
|
||||||
const yearItemsFormatter = useDateFormatter({
|
|
||||||
year: "numeric",
|
|
||||||
timeZone: state.timeZone,
|
|
||||||
});
|
|
||||||
|
|
||||||
const monthItems: Array<Option> = useMemo(() => {
|
const monthItems: Array<Option> = useMemo(() => {
|
||||||
// Note that in some calendar systems, such as the Hebrew, the number of months may differ between years.
|
// Note that in some calendar systems, such as the Hebrew, the number of months may differ between years.
|
||||||
@@ -151,7 +151,6 @@ export const Calendar = ({
|
|||||||
});
|
});
|
||||||
}, [state.focusedDate, state.timeZone, state.maxValue, state.minValue]);
|
}, [state.focusedDate, state.timeZone, state.maxValue, state.minValue]);
|
||||||
|
|
||||||
const { t } = useCunningham();
|
|
||||||
const useDownshiftSelect = (
|
const useDownshiftSelect = (
|
||||||
key: string,
|
key: string,
|
||||||
items: Array<Option>
|
items: Array<Option>
|
||||||
|
|||||||
Reference in New Issue
Block a user