Skip to content

Commit

Permalink
feat(controls): add next and prev years
Browse files Browse the repository at this point in the history
  • Loading branch information
hiwllc committed Jul 28, 2022
1 parent f265ef3 commit ae40369
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/hooks/use-calendar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react'
import {
addMonths,
addYears,
eachDayOfInterval,
endOfMonth,
endOfWeek,
Expand All @@ -11,6 +12,7 @@ import {
startOfMonth,
startOfWeek,
subMonths,
subYears,
} from 'date-fns'
import { Range, RangeSelection, Target } from '../types'

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -129,14 +141,18 @@ export function useCalendar({
value: null,
onSelectDate,
onNextMonth,
onNextYear,
onPrevMonth,
onPrevYear,
selected: singleDateSelection ? date : range,
}
}, [
state,
dates,
onNextMonth,
onPrevMonth,
onNextYear,
onPrevYear,
onSelectDate,
range,
singleDateSelection,
Expand All @@ -155,6 +171,8 @@ export function useCalendar({
getMonthProps,
onPrevMonth,
onNextMonth,
onPrevYear,
onNextYear,
months: dates,
dates: range,
}
Expand Down

0 comments on commit ae40369

Please sign in to comment.