From ae40369c6b4ca73a3da9215a6126cbfc1a005d88 Mon Sep 17 00:00:00 2001 From: Wallace Oliveira Date: Thu, 28 Jul 2022 17:52:11 -0300 Subject: [PATCH] feat(controls): add next and prev years --- src/hooks/use-calendar.tsx | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/hooks/use-calendar.tsx b/src/hooks/use-calendar.tsx index 330ec84..8f70970 100644 --- a/src/hooks/use-calendar.tsx +++ b/src/hooks/use-calendar.tsx @@ -1,6 +1,7 @@ import * as React from 'react' import { addMonths, + addYears, eachDayOfInterval, endOfMonth, endOfWeek, @@ -11,6 +12,7 @@ import { startOfMonth, startOfWeek, subMonths, + subYears, } from 'date-fns' import { Range, RangeSelection, Target } from '../types' @@ -58,6 +60,16 @@ export function useCalendar({ [state] ) + const onPrevYear = React.useCallback( + () => setState(() => subYears(state, 1)), + [state] + ) + + const onNextYear = React.useCallback( + () => setState(() => addYears(state, 1)), + [state] + ) + const dates = React.useMemo(() => { return [...Array(months).keys()].map(index => { const currentMonth = addMonths(state, index) @@ -129,7 +141,9 @@ export function useCalendar({ value: null, onSelectDate, onNextMonth, + onNextYear, onPrevMonth, + onPrevYear, selected: singleDateSelection ? date : range, } }, [ @@ -137,6 +151,8 @@ export function useCalendar({ dates, onNextMonth, onPrevMonth, + onNextYear, + onPrevYear, onSelectDate, range, singleDateSelection, @@ -155,6 +171,8 @@ export function useCalendar({ getMonthProps, onPrevMonth, onNextMonth, + onPrevYear, + onNextYear, months: dates, dates: range, }