/** * Copyright (c) Paymium. * * This source code is licensed under the MIT license found in the * LICENSE file in the root of this projects source tree. */ import { XBox, YBox, YBoxProps } from '../../layout'; import { forwardRef, memo, RefAttributes, useId, useMemo, useState, } from 'react'; import { useCalendar } from '@crossed/use-calendar'; import { IUseCalendarOptions } from '@crossed/use-calendar'; import { DayButton } from './DayButton'; import { capFirstLetter } from './utils'; import { WeekDay } from './WeekDay'; import { IDay } from '@crossed/use-calendar/src'; import { SelectYear } from './SelectYear'; import { SelectMonth } from './SelectMonth'; import { View } from 'react-native'; export type CalendarProps = Partial> & Pick & { locale?: string; selectedDate?: Date; }; export const Calendar = memo>( forwardRef(({ locale = 'default', style, ...props }, ref) => { const { months, getDayProps, setMonth, setYear, monthsByYear } = useCalendar(props); const id = useId(); const [yearSelected, setYearSelected] = useState( (props.selectedDate || new Date()).getFullYear() ); const itemsMonth = useMemo(() => { const formatter = new Intl.DateTimeFormat(locale, { month: 'long', }); let months = monthsByYear.get(yearSelected); if (!months) months = new Set(Array.from(Array(12).keys())); return Array.from(months).map((month) => { const d = new Date(yearSelected, month); const nameMonth = formatter.format(d); return { label: capFirstLetter(nameMonth), value: month.toString(), }; }); }, [monthsByYear, yearSelected]); return ( {months.map(({ year, month, weeks }) => ( { setYearSelected(e); setYear(e); }} years={Array.from(monthsByYear).map(([year]) => year)} /> 0 ? weeks[0] : weeks[1]) as IDay[]} locale={locale} /> {weeks.map((week, i) => ( {week.map((day, j) => { const { onClick, ...props } = getDayProps({ day }); return ( ); })} ))} ))} ); }) );