Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unavailable dates feature #45

Merged
merged 2 commits into from Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22,330 changes: 22,330 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 16 additions & 6 deletions packages/hooks/lib/index.cjs.js
Expand Up @@ -2789,13 +2789,14 @@ function useDay(e) {
d = e.onDateFocus,
c = e.onDateHover,
l = e.dayRef,
f = react.useCallback(
f = e.unavailableDates,
g = react.useCallback(
function() {
return u(t)
},
[t, u],
),
g = react.useCallback(
h = react.useCallback(
function() {
return c(t)
},
Expand All @@ -2807,13 +2808,22 @@ function useDay(e) {
},
[l, t, a],
)
var h = s(t) && !o(t)
var m =
(s(t) && !o(t)) ||
(function(e, t) {
return (
void 0 === e && (e = []),
e.some(function(e) {
return isSameDay(t, e)
})
)
})(f, t)
return {
tabIndex: null === r || a(t) ? 0 : -1,
isSelected: n(t),
isSelectedStartOrEnd: i(t),
isWithinHoverRange: o(t),
disabledDate: h,
disabledDate: m,
onKeyDown: function(e) {
'ArrowRight' === e.key
? d(addDays(t, 1))
Expand All @@ -2823,8 +2833,8 @@ function useDay(e) {
? d(addDays(t, -7))
: 'ArrowDown' === e.key && d(addDays(t, 7))
},
onClick: h ? function() {} : f,
onMouseEnter: g,
onClick: m ? function() {} : g,
onMouseEnter: h,
}
}
;(exports.END_DATE = END_DATE),
Expand Down
22 changes: 16 additions & 6 deletions packages/hooks/lib/index.esm.js
Expand Up @@ -2712,13 +2712,14 @@ function le(t) {
l = t.onDateFocus,
f = t.onDateHover,
h = t.dayRef,
g = n(
g = t.unavailableDates,
w = n(
function() {
return d(e)
},
[e, d],
),
w = n(
m = n(
function() {
return f(e)
},
Expand All @@ -2730,13 +2731,22 @@ function le(t) {
},
[h, e, o],
)
var m = c(e) && !s(e)
var y =
(c(e) && !s(e)) ||
(function(t, e) {
return (
void 0 === t && (t = []),
t.some(function(t) {
return _t(e, t)
})
)
})(g, e)
return {
tabIndex: null === a || o(e) ? 0 : -1,
isSelected: i(e),
isSelectedStartOrEnd: u(e),
isWithinHoverRange: s(e),
disabledDate: m,
disabledDate: y,
onKeyDown: function(t) {
'ArrowRight' === t.key
? l(Nt(e, 1))
Expand All @@ -2746,8 +2756,8 @@ function le(t) {
? l(Nt(e, -7))
: 'ArrowDown' === t.key && l(Nt(e, 7))
},
onClick: m ? function() {} : g,
onMouseEnter: w,
onClick: y ? function() {} : w,
onMouseEnter: m,
}
}
export {
Expand Down
2 changes: 2 additions & 0 deletions packages/hooks/lib/useDay/useDay.d.ts
Expand Up @@ -11,6 +11,7 @@ interface UseDayProps {
onDateSelect(date: Date): void
onDateHover(date: Date): void
dayRef: React.RefObject<HTMLButtonElement>
unavailableDates?: Date[]
}
declare function useDay({
date,
Expand All @@ -24,6 +25,7 @@ declare function useDay({
onDateFocus,
onDateHover,
dayRef,
unavailableDates,
}: UseDayProps): {
tabIndex: number
isSelected: boolean
Expand Down
4 changes: 2 additions & 2 deletions packages/hooks/package.json
@@ -1,5 +1,5 @@
{
"name": "@datepicker-react/hooks",
"name": "@staydomio/datepicker-react-hooks",
"version": "2.2.0",
"description": "React hooks for datepicker.",
"keywords": [
Expand All @@ -22,7 +22,7 @@
],
"repository": {
"type": "git",
"url": "git+https://github.com/tresko/react-datepicker.git"
"url": "git+https://github.com/staydomio/react-datepicker.git"
},
"scripts": {
"build": "../../node_modules/.bin/rollup -c rollup.config.js",
Expand Down
36 changes: 36 additions & 0 deletions packages/hooks/src/useDay/useDay.test.ts
Expand Up @@ -24,6 +24,7 @@ test('should execute onClick callback', () => {
isDateBlocked: jest.fn(),
onDateFocus: jest.fn(),
onDateHover: jest.fn(),
unavailableDates: [],
}),
)

Expand All @@ -50,6 +51,35 @@ test('should not execute onClick callback, because day is disabled', () => {
isDateBlocked: () => true,
onDateFocus: jest.fn(),
onDateHover: jest.fn(),
unavailableDates: [],
}),
)

act(() => {
result.current.onClick()
})

expect(result.current.disabledDate).toBe(true)
expect(onDateSelect).not.toBeCalled()
})

test('should not allow date to be selected when supplied as unavailable', () => {
const onDateSelect = jest.fn()
const {result} = renderHook(() =>
useDay({
date,
// @ts-ignore
dayRef,
onDateSelect,
focusedDate: null,
isDateSelected: jest.fn(),
isDateFocused: jest.fn(),
isFirstOrLastSelectedDate: jest.fn(),
isDateHovered: jest.fn(),
isDateBlocked: () => true,
onDateFocus: jest.fn(),
onDateHover: jest.fn(),
unavailableDates: [new Date()],
}),
)

Expand All @@ -76,6 +106,7 @@ test('should be active', () => {
isDateBlocked: jest.fn(),
onDateFocus: jest.fn(),
onDateHover: jest.fn(),
unavailableDates: [],
}),
)

Expand All @@ -97,6 +128,7 @@ test('should be active first or last day', () => {
isDateBlocked: jest.fn(),
onDateFocus: jest.fn(),
onDateHover: jest.fn(),
unavailableDates: [],
}),
)

Expand All @@ -118,6 +150,7 @@ test('should be within range', () => {
isDateBlocked: jest.fn(),
onDateFocus: jest.fn(),
onDateHover: jest.fn(),
unavailableDates: [],
}),
)

Expand All @@ -139,6 +172,7 @@ test('tabIndex should be 0', () => {
isDateBlocked: jest.fn(),
onDateFocus: jest.fn(),
onDateHover: jest.fn(),
unavailableDates: [],
}),
)

Expand All @@ -160,6 +194,7 @@ test('should be unfocused', () => {
isDateBlocked: jest.fn(),
onDateFocus: jest.fn(),
onDateHover: jest.fn(),
unavailableDates: [],
}),
)

Expand All @@ -181,6 +216,7 @@ test('should be focused', () => {
isDateBlocked: jest.fn(),
onDateFocus: jest.fn(),
onDateHover: jest.fn(),
unavailableDates: [],
}),
)

Expand Down
11 changes: 10 additions & 1 deletion packages/hooks/src/useDay/useDay.ts
@@ -1,5 +1,6 @@
import React, {useCallback, useEffect} from 'react'
import addDays from 'date-fns/addDays'
import {isSameDay} from 'date-fns'

interface UseDayProps {
date: Date
Expand All @@ -13,6 +14,7 @@ interface UseDayProps {
onDateSelect(date: Date): void
onDateHover(date: Date): void
dayRef: React.RefObject<HTMLButtonElement>
unavailableDates?: Date[]
}

function useDay({
Expand All @@ -27,16 +29,23 @@ function useDay({
onDateFocus,
onDateHover,
dayRef,
unavailableDates,
}: UseDayProps) {
const onClick = useCallback(() => onDateSelect(date), [date, onDateSelect])
const onMouseEnter = useCallback(() => onDateHover(date), [date, onDateHover])

const isInUnavailableDates = (unavailableDates: Date[] = [], date: Date) => {
return unavailableDates.some(_date => isSameDay(date, _date))
}

useEffect(() => {
if (dayRef && dayRef.current && isDateFocused(date)) {
dayRef.current.focus()
}
}, [dayRef, date, isDateFocused])

const disabled = isDateBlocked(date) && !isDateHovered(date)
const disabled =
(isDateBlocked(date) && !isDateHovered(date)) || isInUnavailableDates(unavailableDates, date)

return {
tabIndex: focusedDate === null || isDateFocused(date) ? 0 : -1,
Expand Down
25 changes: 0 additions & 25 deletions packages/styled/lib/components/Box/Box.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/styled/lib/components/Box/Box.test.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/styled/lib/components/Box/index.d.ts

This file was deleted.

7 changes: 0 additions & 7 deletions packages/styled/lib/components/Close/Close.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/styled/lib/components/Close/Close.test.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/styled/lib/components/Close/index.d.ts

This file was deleted.

55 changes: 0 additions & 55 deletions packages/styled/lib/components/DateRangeInput/DateRangeInput.d.ts

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions packages/styled/lib/components/DateRangeInput/index.d.ts

This file was deleted.