Skip to content

Commit 5039530

Browse files
Rares Crisantresko
authored andcommitted
feat(unavailable-dates): add unavaiable dates feature
chore: add unavailableDates to useDate tests fix: fix comments chore: build chore: clean up
1 parent 09f4a14 commit 5039530

File tree

93 files changed

+22452
-11408
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+22452
-11408
lines changed

package-lock.json

Lines changed: 22330 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/hooks/lib/index.cjs.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,13 +2789,14 @@ function useDay(e) {
27892789
d = e.onDateFocus,
27902790
c = e.onDateHover,
27912791
l = e.dayRef,
2792-
f = react.useCallback(
2792+
f = e.unavailableDates,
2793+
g = react.useCallback(
27932794
function() {
27942795
return u(t)
27952796
},
27962797
[t, u],
27972798
),
2798-
g = react.useCallback(
2799+
h = react.useCallback(
27992800
function() {
28002801
return c(t)
28012802
},
@@ -2807,13 +2808,22 @@ function useDay(e) {
28072808
},
28082809
[l, t, a],
28092810
)
2810-
var h = s(t) && !o(t)
2811+
var m =
2812+
(s(t) && !o(t)) ||
2813+
(function(e, t) {
2814+
return (
2815+
void 0 === e && (e = []),
2816+
e.some(function(e) {
2817+
return isSameDay(t, e)
2818+
})
2819+
)
2820+
})(f, t)
28112821
return {
28122822
tabIndex: null === r || a(t) ? 0 : -1,
28132823
isSelected: n(t),
28142824
isSelectedStartOrEnd: i(t),
28152825
isWithinHoverRange: o(t),
2816-
disabledDate: h,
2826+
disabledDate: m,
28172827
onKeyDown: function(e) {
28182828
'ArrowRight' === e.key
28192829
? d(addDays(t, 1))
@@ -2823,8 +2833,8 @@ function useDay(e) {
28232833
? d(addDays(t, -7))
28242834
: 'ArrowDown' === e.key && d(addDays(t, 7))
28252835
},
2826-
onClick: h ? function() {} : f,
2827-
onMouseEnter: g,
2836+
onClick: m ? function() {} : g,
2837+
onMouseEnter: h,
28282838
}
28292839
}
28302840
;(exports.END_DATE = END_DATE),

packages/hooks/lib/index.esm.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,13 +2712,14 @@ function le(t) {
27122712
l = t.onDateFocus,
27132713
f = t.onDateHover,
27142714
h = t.dayRef,
2715-
g = n(
2715+
g = t.unavailableDates,
2716+
w = n(
27162717
function() {
27172718
return d(e)
27182719
},
27192720
[e, d],
27202721
),
2721-
w = n(
2722+
m = n(
27222723
function() {
27232724
return f(e)
27242725
},
@@ -2730,13 +2731,22 @@ function le(t) {
27302731
},
27312732
[h, e, o],
27322733
)
2733-
var m = c(e) && !s(e)
2734+
var y =
2735+
(c(e) && !s(e)) ||
2736+
(function(t, e) {
2737+
return (
2738+
void 0 === t && (t = []),
2739+
t.some(function(t) {
2740+
return _t(e, t)
2741+
})
2742+
)
2743+
})(g, e)
27342744
return {
27352745
tabIndex: null === a || o(e) ? 0 : -1,
27362746
isSelected: i(e),
27372747
isSelectedStartOrEnd: u(e),
27382748
isWithinHoverRange: s(e),
2739-
disabledDate: m,
2749+
disabledDate: y,
27402750
onKeyDown: function(t) {
27412751
'ArrowRight' === t.key
27422752
? l(Nt(e, 1))
@@ -2746,8 +2756,8 @@ function le(t) {
27462756
? l(Nt(e, -7))
27472757
: 'ArrowDown' === t.key && l(Nt(e, 7))
27482758
},
2749-
onClick: m ? function() {} : g,
2750-
onMouseEnter: w,
2759+
onClick: y ? function() {} : w,
2760+
onMouseEnter: m,
27512761
}
27522762
}
27532763
export {

packages/hooks/lib/useDay/useDay.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ interface UseDayProps {
1111
onDateSelect(date: Date): void
1212
onDateHover(date: Date): void
1313
dayRef: React.RefObject<HTMLButtonElement>
14+
unavailableDates?: Date[]
1415
}
1516
declare function useDay({
1617
date,
@@ -24,6 +25,7 @@ declare function useDay({
2425
onDateFocus,
2526
onDateHover,
2627
dayRef,
28+
unavailableDates,
2729
}: UseDayProps): {
2830
tabIndex: number
2931
isSelected: boolean

packages/hooks/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@datepicker-react/hooks",
2+
"name": "@staydomio/datepicker-react-hooks",
33
"version": "2.2.0",
44
"description": "React hooks for datepicker.",
55
"keywords": [
@@ -22,7 +22,7 @@
2222
],
2323
"repository": {
2424
"type": "git",
25-
"url": "git+https://github.com/tresko/react-datepicker.git"
25+
"url": "git+https://github.com/staydomio/react-datepicker.git"
2626
},
2727
"scripts": {
2828
"build": "../../node_modules/.bin/rollup -c rollup.config.js",

packages/hooks/src/useDay/useDay.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ test('should execute onClick callback', () => {
2424
isDateBlocked: jest.fn(),
2525
onDateFocus: jest.fn(),
2626
onDateHover: jest.fn(),
27+
unavailableDates: [],
2728
}),
2829
)
2930

@@ -50,6 +51,35 @@ test('should not execute onClick callback, because day is disabled', () => {
5051
isDateBlocked: () => true,
5152
onDateFocus: jest.fn(),
5253
onDateHover: jest.fn(),
54+
unavailableDates: [],
55+
}),
56+
)
57+
58+
act(() => {
59+
result.current.onClick()
60+
})
61+
62+
expect(result.current.disabledDate).toBe(true)
63+
expect(onDateSelect).not.toBeCalled()
64+
})
65+
66+
test('should not allow date to be selected when supplied as unavailable', () => {
67+
const onDateSelect = jest.fn()
68+
const {result} = renderHook(() =>
69+
useDay({
70+
date,
71+
// @ts-ignore
72+
dayRef,
73+
onDateSelect,
74+
focusedDate: null,
75+
isDateSelected: jest.fn(),
76+
isDateFocused: jest.fn(),
77+
isFirstOrLastSelectedDate: jest.fn(),
78+
isDateHovered: jest.fn(),
79+
isDateBlocked: () => true,
80+
onDateFocus: jest.fn(),
81+
onDateHover: jest.fn(),
82+
unavailableDates: [new Date()],
5383
}),
5484
)
5585

@@ -76,6 +106,7 @@ test('should be active', () => {
76106
isDateBlocked: jest.fn(),
77107
onDateFocus: jest.fn(),
78108
onDateHover: jest.fn(),
109+
unavailableDates: [],
79110
}),
80111
)
81112

@@ -97,6 +128,7 @@ test('should be active first or last day', () => {
97128
isDateBlocked: jest.fn(),
98129
onDateFocus: jest.fn(),
99130
onDateHover: jest.fn(),
131+
unavailableDates: [],
100132
}),
101133
)
102134

@@ -118,6 +150,7 @@ test('should be within range', () => {
118150
isDateBlocked: jest.fn(),
119151
onDateFocus: jest.fn(),
120152
onDateHover: jest.fn(),
153+
unavailableDates: [],
121154
}),
122155
)
123156

@@ -139,6 +172,7 @@ test('tabIndex should be 0', () => {
139172
isDateBlocked: jest.fn(),
140173
onDateFocus: jest.fn(),
141174
onDateHover: jest.fn(),
175+
unavailableDates: [],
142176
}),
143177
)
144178

@@ -160,6 +194,7 @@ test('should be unfocused', () => {
160194
isDateBlocked: jest.fn(),
161195
onDateFocus: jest.fn(),
162196
onDateHover: jest.fn(),
197+
unavailableDates: [],
163198
}),
164199
)
165200

@@ -181,6 +216,7 @@ test('should be focused', () => {
181216
isDateBlocked: jest.fn(),
182217
onDateFocus: jest.fn(),
183218
onDateHover: jest.fn(),
219+
unavailableDates: [],
184220
}),
185221
)
186222

packages/hooks/src/useDay/useDay.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, {useCallback, useEffect} from 'react'
22
import addDays from 'date-fns/addDays'
3+
import {isSameDay} from 'date-fns'
34

45
interface UseDayProps {
56
date: Date
@@ -13,6 +14,7 @@ interface UseDayProps {
1314
onDateSelect(date: Date): void
1415
onDateHover(date: Date): void
1516
dayRef: React.RefObject<HTMLButtonElement>
17+
unavailableDates?: Date[]
1618
}
1719

1820
function useDay({
@@ -27,16 +29,23 @@ function useDay({
2729
onDateFocus,
2830
onDateHover,
2931
dayRef,
32+
unavailableDates,
3033
}: UseDayProps) {
3134
const onClick = useCallback(() => onDateSelect(date), [date, onDateSelect])
3235
const onMouseEnter = useCallback(() => onDateHover(date), [date, onDateHover])
36+
37+
const isInUnavailableDates = (unavailableDates: Date[] = [], date: Date) => {
38+
return unavailableDates.some(_date => isSameDay(date, _date))
39+
}
40+
3341
useEffect(() => {
3442
if (dayRef && dayRef.current && isDateFocused(date)) {
3543
dayRef.current.focus()
3644
}
3745
}, [dayRef, date, isDateFocused])
3846

39-
const disabled = isDateBlocked(date) && !isDateHovered(date)
47+
const disabled =
48+
(isDateBlocked(date) && !isDateHovered(date)) || isInUnavailableDates(unavailableDates, date)
4049

4150
return {
4251
tabIndex: focusedDate === null || isDateFocused(date) ? 0 : -1,

packages/styled/lib/components/Box/Box.d.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/styled/lib/components/Box/Box.test.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/styled/lib/components/Box/index.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)