Skip to content

Commit 15ac2b6

Browse files
committed
feat(useDatepicker): update goToPreviousYear and goToNextYear methods
affects: @datepicker-react/hooks ISSUES CLOSED: #35
1 parent 02484b8 commit 15ac2b6

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

packages/hooks/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ Updates `activeMonths` (next months) in accordance with the `numberOfMonths` pro
198198

199199
Updates `activeMonths` (previous months) in accordance with the `numberOfMonths` prop.
200200

201+
#### `goToPreviousYear: (numYears: number = 1) => void`
202+
203+
Go to the previous year by default.
204+
205+
#### `goToNextYear: (numYears: number = 1) => void`
206+
207+
Go to the next year by default.
208+
201209
## `useMonth`
202210

203211
The `useMonth` returns all days of the month, weekday labels and month label.

packages/hooks/src/useDatepicker/useDatepicker.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('useDatepicker', () => {
6464
expect(result.current.activeMonths[1].year).toBe(2020)
6565
expect(result.current.activeMonths[1].month).toBe(3)
6666

67-
// next year
67+
// previous year
6868
act(() => {
6969
result.current.goToPreviousYear()
7070
})
@@ -73,6 +73,22 @@ describe('useDatepicker', () => {
7373
expect(result.current.activeMonths[1].year).toBe(2019)
7474
expect(result.current.activeMonths[1].month).toBe(3)
7575

76+
act(() => {
77+
result.current.goToNextYear(2)
78+
})
79+
expect(result.current.activeMonths[0].year).toBe(2021)
80+
expect(result.current.activeMonths[0].month).toBe(2)
81+
expect(result.current.activeMonths[1].year).toBe(2021)
82+
expect(result.current.activeMonths[1].month).toBe(3)
83+
84+
act(() => {
85+
result.current.goToPreviousYear(2)
86+
})
87+
expect(result.current.activeMonths[0].year).toBe(2019)
88+
expect(result.current.activeMonths[0].month).toBe(2)
89+
expect(result.current.activeMonths[1].year).toBe(2019)
90+
expect(result.current.activeMonths[1].month).toBe(3)
91+
7692
clear()
7793
})
7894

packages/hooks/src/useDatepicker/useDatepicker.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,17 @@ export function useDatepicker({
292292
setFocusedDate(null)
293293
}
294294

295-
function goToPreviousYear() {
296-
setActiveMonths(getNextActiveMonth(activeMonths, numberOfMonths, -(12 - numberOfMonths + 1)))
295+
function goToPreviousYear(numYears: number = 1) {
296+
setActiveMonths(
297+
getNextActiveMonth(activeMonths, numberOfMonths, -(numYears * 12 - numberOfMonths + 1)),
298+
)
297299
setFocusedDate(null)
298300
}
299301

300-
function goToNextYear() {
301-
setActiveMonths(getNextActiveMonth(activeMonths, numberOfMonths, 12 - numberOfMonths + 1))
302+
function goToNextYear(numYears: number = 1) {
303+
setActiveMonths(
304+
getNextActiveMonth(activeMonths, numberOfMonths, numYears * 12 - numberOfMonths + 1),
305+
)
302306
setFocusedDate(null)
303307
}
304308

0 commit comments

Comments
 (0)