Skip to content

Commit b1d9b0c

Browse files
committed
fix(initialVisibleMonth): fix InitialVisibleMonth
affects: @datepicker-react/hooks, @datepicker-react/styled ISSUES CLOSED: #48
1 parent 3916927 commit b1d9b0c

18 files changed

Lines changed: 1720 additions & 1644 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ numberOfMonths?: number // Default: 2
114114
minBookingDays?: number // Default: 1
115115
exactMinBookingDays?: boolean // Default: false
116116
firstDayOfWeek?: FirstDayOfWeek // Default: 1
117-
initialVisibleMonth?(numberOfMonths: number): MonthType[]
117+
initialVisibleMonth?: Date
118118
isDateBlocked?(date: Date): boolean
119119
dayLabelFormat?(date: Date): string
120120
weekdayLabelFormat?(date: Date): string
@@ -183,7 +183,7 @@ numberOfMonths?: number // Default: 2
183183
minBookingDays?: number // Default: 1
184184
exactMinBookingDays?: boolean // Default: false
185185
firstDayOfWeek?: FirstDayOfWeek // Default: 0
186-
initialVisibleMonth?(numberOfMonths: number): MonthType[]
186+
initialVisibleMonth?: Date
187187
isDateBlocked?(date: Date): boolean
188188
dayLabelFormat?(date: Date): string
189189
weekdayLabelFormat?(date: Date): string
@@ -252,7 +252,7 @@ showResetDate?: boolean
252252
showClose?: boolean
253253
rtl?: boolean
254254
placement?: 'top' | 'bottom'
255-
initialVisibleMonth?(numberOfMonths: number): MonthType[]
255+
initialVisibleMonth?: Date
256256
unavailableDates?: Date[] // Default: []
257257
isDateBlocked?(date: Date): boolean
258258
onClose?(): void

packages/hooks/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ If `exactMinBookingDays` is `true`, then the range of dates is always the same a
7474

7575
First day of the week.
7676

77+
#### `initialVisibleMonth? Date`
78+
79+
Initial visible month
80+
7781
#### `isDateBlocked: (date: Date) => boolean`
7882

7983
If `isDateBlocked` returns `true`, then the date is blocked.

packages/hooks/lib/index.cjs.js

Lines changed: 78 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,164 +2608,165 @@ function useDatepicker(e) {
26082608
a = e.minBookingDate,
26092609
i = e.maxBookingDate,
26102610
o = e.onDatesChange,
2611-
s = e.exactMinBookingDays,
2612-
u = void 0 !== s && s,
2613-
d = e.minBookingDays,
2614-
c = void 0 === d ? 1 : d,
2615-
l = e.numberOfMonths,
2616-
f = void 0 === l ? 2 : l,
2617-
g = e.firstDayOfWeek,
2618-
h = void 0 === g ? 1 : g,
2619-
m = e.isDateBlocked,
2620-
w =
2621-
void 0 === m
2611+
s = e.initialVisibleMonth,
2612+
u = e.exactMinBookingDays,
2613+
d = void 0 !== u && u,
2614+
c = e.minBookingDays,
2615+
l = void 0 === c ? 1 : c,
2616+
f = e.numberOfMonths,
2617+
g = void 0 === f ? 2 : f,
2618+
h = e.firstDayOfWeek,
2619+
m = void 0 === h ? 1 : h,
2620+
w = e.isDateBlocked,
2621+
y =
2622+
void 0 === w
26222623
? function() {
26232624
return !1
26242625
}
2625-
: m,
2626-
y = e.unavailableDates,
2627-
D = void 0 === y ? [] : y,
2628-
p = react.useState(function() {
2629-
return getInitialMonths(f, t)
2626+
: w,
2627+
D = e.unavailableDates,
2628+
p = void 0 === D ? [] : D,
2629+
T = react.useState(function() {
2630+
return getInitialMonths(g, t || s || null)
26302631
}),
2631-
T = p[0],
2632-
b = p[1],
2633-
v = react.useState(null),
2634-
k = v[0],
2635-
M = v[1],
2636-
C = react.useState(t),
2637-
x = C[0],
2638-
E = C[1]
2632+
b = T[0],
2633+
v = T[1],
2634+
k = react.useState(null),
2635+
M = k[0],
2636+
C = k[1],
2637+
x = react.useState(t),
2638+
E = x[0],
2639+
S = x[1]
26392640
react.useEffect(function() {
26402641
return (
2641-
'undefined' != typeof window && window.addEventListener('keydown', N),
2642+
'undefined' != typeof window && window.addEventListener('keydown', I),
26422643
function() {
2643-
window.removeEventListener('keydown', N)
2644+
window.removeEventListener('keydown', I)
26442645
}
26452646
)
26462647
})
2647-
var S = function(e) {
2648-
return isInUnavailableDates(D, e) || w(e)
2649-
},
2650-
P = function(e) {
2651-
E(e), (!x || (x && !isSameDay(e, x))) && b(getInitialMonths(f, e))
2648+
var P = function(e) {
2649+
return isInUnavailableDates(p, e) || y(e)
26522650
},
26532651
O = function(e) {
2652+
S(e), (!E || (E && !isSameDay(e, E))) && v(getInitialMonths(g, e))
2653+
},
2654+
N = function(e) {
26542655
return isDateBlocked({
26552656
date: e,
26562657
minBookingDate: a,
26572658
maxBookingDate: i,
26582659
startDate: t,
26592660
endDate: r,
2660-
minBookingDays: c,
2661-
isDateBlockedFn: S,
2661+
minBookingDays: l,
2662+
isDateBlockedFn: P,
26622663
})
26632664
}
2664-
function N(e) {
2665+
function I(e) {
26652666
if (
26662667
('ArrowRight' === e.key ||
26672668
'ArrowLeft' === e.key ||
26682669
'ArrowDown' === e.key ||
26692670
'ArrowUp' === e.key) &&
2670-
!x
2671+
!E
26712672
) {
2672-
var t = T[0]
2673-
P(t.date), b(getInitialMonths(f, t.date))
2673+
var t = b[0]
2674+
O(t.date), v(getInitialMonths(g, t.date))
26742675
}
26752676
}
26762677
return {
2677-
firstDayOfWeek: h,
2678-
activeMonths: T,
2678+
firstDayOfWeek: m,
2679+
activeMonths: b,
26792680
isDateSelected: function(e) {
26802681
return isDateSelected(e, t, r)
26812682
},
26822683
isDateHovered: function(e) {
26832684
return isDateHovered({
26842685
date: e,
2685-
hoveredDate: k,
2686+
hoveredDate: M,
26862687
startDate: t,
26872688
endDate: r,
2688-
minBookingDays: c,
2689-
exactMinBookingDays: u,
2690-
isDateBlocked: S,
2689+
minBookingDays: l,
2690+
exactMinBookingDays: d,
2691+
isDateBlocked: P,
26912692
})
26922693
},
26932694
isFirstOrLastSelectedDate: function(e) {
26942695
return isFirstOrLastSelectedDate(e, t, r)
26952696
},
2696-
isDateBlocked: O,
2697-
numberOfMonths: f,
2697+
isDateBlocked: N,
2698+
numberOfMonths: g,
26982699
isDateFocused: function(e) {
2699-
return !!x && isSameDay(e, x)
2700+
return !!E && isSameDay(e, E)
27002701
},
2701-
focusedDate: x,
2702-
hoveredDate: k,
2702+
focusedDate: E,
2703+
hoveredDate: M,
27032704
onResetDates: function() {
27042705
o({startDate: null, endDate: null, focusedInput: START_DATE})
27052706
},
27062707
onDateHover: function(e) {
27072708
if (e) {
27082709
if (e) {
2709-
var n = !O(e) || (t && isSameDay(e, t)),
2710+
var n = !N(e) || (t && isSameDay(e, t)),
27102711
o = !a || !isBefore(e, addDays(a, -1)),
27112712
s = !i || !isAfter(e, i),
2712-
d = addDays(e, c - 1),
2713-
l = !a || !isBefore(d, a),
2714-
f = !i || !isAfter(d, i),
2715-
g = u && c > 1 && o && s && l && f,
2716-
h = t && !r && !u && o && s,
2717-
m = !(c > 1 && t) || isWithinInterval(e, {start: t, end: addDays(t, c - 2)}),
2713+
u = addDays(e, l - 1),
2714+
c = !a || !isBefore(u, a),
2715+
f = !i || !isAfter(u, i),
2716+
g = d && l > 1 && o && s && c && f,
2717+
h = t && !r && !d && o && s,
2718+
m = !(l > 1 && t) || isWithinInterval(e, {start: t, end: addDays(t, l - 2)}),
27182719
w = t && isSameDay(e, t) && m
2719-
n && (g || h || w) ? M(e) : null !== k && M(null)
2720+
n && (g || h || w) ? C(e) : null !== M && C(null)
27202721
}
2721-
} else M(null)
2722+
} else C(null)
27222723
},
27232724
onDateSelect: function(e) {
27242725
;(n === END_DATE || n === START_DATE) &&
2725-
c > 0 &&
2726-
u &&
2726+
l > 0 &&
2727+
d &&
27272728
canSelectRange({
2728-
minBookingDays: c,
2729-
exactMinBookingDays: u,
2729+
minBookingDays: l,
2730+
exactMinBookingDays: d,
27302731
minBookingDate: a,
27312732
maxBookingDate: i,
2732-
isDateBlocked: S,
2733+
isDateBlocked: P,
27332734
startDate: e,
27342735
endDate: null,
27352736
})
2736-
? o({startDate: e, endDate: addDays(e, c - 1), focusedInput: null})
2737+
? o({startDate: e, endDate: addDays(e, l - 1), focusedInput: null})
27372738
: ((n === END_DATE && t && isBefore(e, t)) || (n === START_DATE && r && isAfter(e, r))) &&
2738-
!u &&
2739-
canSelectRange({minBookingDays: c, isDateBlocked: S, startDate: e, endDate: null})
2739+
!d &&
2740+
canSelectRange({minBookingDays: l, isDateBlocked: P, startDate: e, endDate: null})
27402741
? o({endDate: null, startDate: e, focusedInput: END_DATE})
27412742
: n === START_DATE &&
2742-
!u &&
2743-
canSelectRange({minBookingDays: c, isDateBlocked: S, endDate: r, startDate: e})
2743+
!d &&
2744+
canSelectRange({minBookingDays: l, isDateBlocked: P, endDate: r, startDate: e})
27442745
? o({endDate: r, startDate: e, focusedInput: END_DATE})
27452746
: n === START_DATE &&
2746-
!u &&
2747-
canSelectRange({minBookingDays: c, isDateBlocked: S, endDate: null, startDate: e})
2747+
!d &&
2748+
canSelectRange({minBookingDays: l, isDateBlocked: P, endDate: null, startDate: e})
27482749
? o({endDate: null, startDate: e, focusedInput: END_DATE})
27492750
: n === END_DATE &&
27502751
t &&
27512752
!isBefore(e, t) &&
2752-
!u &&
2753-
canSelectRange({minBookingDays: c, isDateBlocked: S, startDate: t, endDate: e}) &&
2753+
!d &&
2754+
canSelectRange({minBookingDays: l, isDateBlocked: P, startDate: t, endDate: e}) &&
27542755
o({startDate: t, endDate: e, focusedInput: null}),
2755-
n === END_DATE || (x && (!x || isSameDay(e, x))) || b(getInitialMonths(f, e))
2756+
n === END_DATE || (E && (!E || isSameDay(e, E))) || v(getInitialMonths(g, e))
27562757
},
2757-
onDateFocus: P,
2758+
onDateFocus: O,
27582759
goToPreviousMonths: function() {
2759-
b(getNextActiveMonth(T, f, -1)), E(null)
2760+
v(getNextActiveMonth(b, g, -1)), S(null)
27602761
},
27612762
goToNextMonths: function() {
2762-
b(getNextActiveMonth(T, f, 1)), E(null)
2763+
v(getNextActiveMonth(b, g, 1)), S(null)
27632764
},
27642765
goToPreviousYear: function(e) {
2765-
void 0 === e && (e = 1), b(getNextActiveMonth(T, f, -(12 * e - f + 1))), E(null)
2766+
void 0 === e && (e = 1), v(getNextActiveMonth(b, g, -(12 * e - g + 1))), S(null)
27662767
},
27672768
goToNextYear: function(e) {
2768-
void 0 === e && (e = 1), b(getNextActiveMonth(T, f, 12 * e - f + 1)), E(null)
2769+
void 0 === e && (e = 1), v(getNextActiveMonth(b, g, 12 * e - g + 1)), S(null)
27692770
},
27702771
}
27712772
}

0 commit comments

Comments
 (0)