Skip to content

Commit 062f66f

Browse files
committed
fix(hooks): fix start of the month
affects: @datepicker-react/hooks, @datepicker-react/styled ISSUES CLOSED: #7
1 parent ea34eb0 commit 062f66f

6 files changed

Lines changed: 21 additions & 5 deletions

File tree

packages/hooks/lib/index.cjs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2359,7 +2359,7 @@ function getDays(e) {
23592359
u = startOfMonth(s),
23602360
d = getDay(u),
23612361
c = endOfMonth(s),
2362-
l = Array.from(Array(d >= a ? d - a : a).keys()).fill(0),
2362+
l = Array.from(Array(d >= a ? d - a : 6 - a + d + 1).keys()).fill(0),
23632363
f = eachDayOfInterval({start: u, end: c}).map(function(e) {
23642364
return {date: e, dayLabel: o(e)}
23652365
})

packages/hooks/lib/index.esm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2296,7 +2296,7 @@ function It(t) {
22962296
n = e.getMonth()
22972297
return e.setFullYear(e.getFullYear(), n + 1, 0), e.setHours(23, 59, 59, 999), e
22982298
})(u),
2299-
l = Array.from(Array(c >= a ? c - a : a).keys()).fill(0),
2299+
l = Array.from(Array(c >= a ? c - a : 6 - a + c + 1).keys()).fill(0),
23002300
f = Lt({start: s, end: d}).map(function(t) {
23012301
return {date: t, dayLabel: o(t)}
23022302
})

packages/hooks/src/useMonth/useMonth.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ describe('getDays', () => {
6464
expect(result.current[result.current.length - 1].dayLabel).toBe('31')
6565
})
6666

67+
test('should return days for september 2019', () => {
68+
const {result} = renderHook(() => getDays({year: 2019, month: 8}))
69+
expect(result.current.length).toBe(36)
70+
expect(typeof result.current[0]).toBe('number')
71+
expect(typeof result.current[5]).toBe('number')
72+
expect(typeof result.current[6]).toBe('object')
73+
// @ts-ignore
74+
expect(result.current[6].dayLabel).toBe('01')
75+
// @ts-ignore
76+
expect(result.current[result.current.length - 1].dayLabel).toBe('30')
77+
})
78+
6779
test('should return days for march 2019 start with sunday', () => {
6880
const {result} = renderHook(() => getDays({year: 2019, month: 2, firstDayOfWeek: 0}))
6981
expect(result.current.length).toBe(36)

packages/hooks/src/useMonth/useMonth.utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ export function getDays({
5050
const monthEnd = endOfMonth(date)
5151

5252
const prevMonthDays = Array.from(
53-
Array(monthStartDay >= firstDayOfWeek ? monthStartDay - firstDayOfWeek : firstDayOfWeek).keys(),
53+
Array(
54+
monthStartDay >= firstDayOfWeek
55+
? monthStartDay - firstDayOfWeek
56+
: 6 - firstDayOfWeek + monthStartDay + 1,
57+
).keys(),
5458
).fill(0)
5559
const days = eachDay({start: monthStart, end: monthEnd}).map(date => ({
5660
date,

packages/styled/lib/index.cjs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2304,7 +2304,7 @@ function It(e) {
23042304
r = t.getMonth()
23052305
return t.setFullYear(t.getFullYear(), r + 1, 0), t.setHours(23, 59, 59, 999), t
23062306
})(s),
2307-
u = Array.from(Array(c >= n ? c - n : n).keys()).fill(0),
2307+
u = Array.from(Array(c >= n ? c - n : 6 - n + c + 1).keys()).fill(0),
23082308
p = Lt({start: l, end: d}).map(function(e) {
23092309
return {date: e, dayLabel: i(e)}
23102310
})

packages/styled/lib/index.esm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2200,7 +2200,7 @@ function Oe(e) {
22002200
r = t.getMonth()
22012201
return t.setFullYear(t.getFullYear(), r + 1, 0), t.setHours(23, 59, 59, 999), t
22022202
})(s),
2203-
u = Array.from(Array(c >= a ? c - a : a).keys()).fill(0),
2203+
u = Array.from(Array(c >= a ? c - a : 6 - a + c + 1).keys()).fill(0),
22042204
p = Me({start: d, end: l}).map(function(e) {
22052205
return {date: e, dayLabel: i(e)}
22062206
})

0 commit comments

Comments
 (0)