Skip to content

Commit 6040c7b

Browse files
author
Miha Sedej
committed
feat(hooks): add goToNextYear and goToPreviousYear methods to the hooks.
affects: @datepicker-react/hooks, @datepicker-react/styled
1 parent 1f6dc35 commit 6040c7b

File tree

8 files changed

+367
-273
lines changed

8 files changed

+367
-273
lines changed

packages/hooks/lib/index.cjs.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ function getNextActiveMonth(e, t, a) {
719719
var r = e[a > 0 ? e.length - 1 : 0].date
720720
return Array.from(Array(t).keys()).reduce(function(e) {
721721
return (
722-
(r = add_months(r, a)),
722+
(r = 0 === e.length ? add_months(r, a) : add_months(r, a >= 0 ? 1 : -1)),
723723
a > 0 ? e.concat([getDateMonthAndYear(r)]) : [getDateMonthAndYear(r)].concat(e)
724724
)
725725
}, [])
@@ -803,12 +803,12 @@ function useDatepicker(e) {
803803
}),
804804
h = y[0],
805805
T = y[1],
806-
p = react.useState(null),
807-
M = p[0],
808-
k = p[1],
809-
v = react.useState(t),
810-
S = v[0],
811-
I = v[1],
806+
M = react.useState(null),
807+
p = M[0],
808+
v = M[1],
809+
k = react.useState(t),
810+
S = k[0],
811+
I = k[1],
812812
Y = react.useCallback(
813813
function(e) {
814814
I(e), (!S || (S && !is_same_month(e, S))) && T(getInitialMonths(D, e))
@@ -851,15 +851,15 @@ function useDatepicker(e) {
851851
function(e) {
852852
return isDateHovered({
853853
date: e,
854-
hoveredDate: M,
854+
hoveredDate: p,
855855
startDate: t,
856856
endDate: a,
857857
minBookingDays: c,
858858
exactMinBookingDays: u,
859859
isDateBlocked: m,
860860
})
861861
},
862-
[M, t, a, c, u, m],
862+
[p, t, a, c, u, m],
863863
)
864864
function E(e) {
865865
if (
@@ -892,7 +892,7 @@ function useDatepicker(e) {
892892
numberOfMonths: D,
893893
isDateFocused: b,
894894
focusedDate: S,
895-
hoveredDate: M,
895+
hoveredDate: p,
896896
onResetDates: function() {
897897
s({startDate: null, endDate: null, focusedInput: START_DATE})
898898
},
@@ -909,9 +909,9 @@ function useDatepicker(e) {
909909
l = t && !a && !u && s && i,
910910
g = !(c > 1 && t) || is_within_range(e, t, add_days(t, c - 2)),
911911
m = t && is_same_day(e, t) && g
912-
r && (_ || l || m) ? k(e) : null !== M && k(null)
912+
r && (_ || l || m) ? v(e) : null !== p && v(null)
913913
}
914-
} else k(null)
914+
} else v(null)
915915
},
916916
onDateSelect: function(e) {
917917
;(r === END_DATE || r === START_DATE) &&
@@ -955,6 +955,12 @@ function useDatepicker(e) {
955955
goToNextMonths: function() {
956956
T(getNextActiveMonth(h, D, 1)), I(null)
957957
},
958+
goToPreviousYear: function() {
959+
T(getNextActiveMonth(h, D, -11)), I(null)
960+
},
961+
goToNextYear: function() {
962+
T(getNextActiveMonth(h, D, 11)), I(null)
963+
},
958964
}
959965
)
960966
}

0 commit comments

Comments
 (0)