Skip to content

Commit

Permalink
feat: add isStartDate and isEndDate
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermepatriarca authored and tresko committed Jun 22, 2020
1 parent 87687cf commit 94f4118
Show file tree
Hide file tree
Showing 18 changed files with 260 additions and 130 deletions.
8 changes: 8 additions & 0 deletions packages/hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ Returns `true` if a date is focused, otherwise `false`.

Returns `true` if a date is the first or the last date in the selected range, otherwise `false`.

#### `isStartDate: (date: Date) => boolean`

Returns `true` if a date is the first or the last date in the selected range, otherwise `false`.

#### `isEndDate: (date: Date) => boolean`

Returns `true` if a date is the first or the last date in the selected range, otherwise `false`.

#### `onResetDates: () => void`

Reset start and end date.
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
import {
isDateSelected,
isFirstOrLastSelectedDate,
isStartDate,
isEndDate,
isDateBlocked,
getCurrentYearMonthAndDate,
getDateMonthAndYear,
Expand Down Expand Up @@ -43,6 +45,8 @@ export {
CalendarDay,
isDateSelected,
isFirstOrLastSelectedDate,
isStartDate,
isEndDate,
isDateBlocked,
getCurrentYearMonthAndDate,
getDateMonthAndYear,
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/lib/useDatepicker/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
import {
isDateSelected,
isFirstOrLastSelectedDate,
isStartDate,
isEndDate,
isDateBlocked,
getCurrentYearMonthAndDate,
getDateMonthAndYear,
Expand All @@ -25,6 +27,8 @@ export {
useDatepicker,
isDateSelected,
isFirstOrLastSelectedDate,
isStartDate,
isEndDate,
isDateBlocked,
getCurrentYearMonthAndDate,
getDateMonthAndYear,
Expand Down
2 changes: 2 additions & 0 deletions packages/hooks/lib/useDatepicker/useDatepicker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export declare function useDatepicker({
isDateSelected: (date: Date) => boolean
isDateHovered: (date: Date) => boolean
isFirstOrLastSelectedDate: (date: Date) => boolean
isStartDate: (date: Date) => boolean
isEndDate: (date: Date) => boolean
isDateBlocked: (date: Date) => boolean
numberOfMonths: number
isDateFocused: (date: Date) => boolean
Expand Down
8 changes: 8 additions & 0 deletions packages/hooks/lib/useDatepicker/useDatepicker.utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ export declare function isFirstOrLastSelectedDate(
startDate: Date | null,
endDate: Date | null,
): boolean
export declare function isStartDate(
date: Date,
startDate: Date | null,
): boolean
export declare function isEndDate(
date: Date,
endDate: Date | null,
): boolean
interface IsDateBlockedProps {
date: Date
startDate: Date | null
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/lib/useDay/useDay.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ interface UseDayProps {
isDateHovered(date: Date): boolean
isDateBlocked(date: Date): boolean
isFirstOrLastSelectedDate(date: Date): boolean
isStartDate(date: Date): boolean
isEndDate(date: Date): boolean
onDateFocus(date: Date): void
onDateSelect(date: Date): void
onDateHover(date: Date): void
Expand All @@ -18,6 +20,8 @@ declare function useDay({
isDateSelected,
isDateFocused,
isFirstOrLastSelectedDate,
isStartDate,
isEndDate,
isDateHovered,
isDateBlocked,
onDateSelect,
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
import {
isDateSelected,
isFirstOrLastSelectedDate,
isEndDate,
isStartDate,
isDateBlocked,
getCurrentYearMonthAndDate,
getDateMonthAndYear,
Expand Down Expand Up @@ -44,6 +46,8 @@ export {
CalendarDay,
isDateSelected,
isFirstOrLastSelectedDate,
isStartDate,
isEndDate,
isDateBlocked,
getCurrentYearMonthAndDate,
getDateMonthAndYear,
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useDatepicker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
import {
isDateSelected,
isFirstOrLastSelectedDate,
isStartDate,
isEndDate,
isDateBlocked,
getCurrentYearMonthAndDate,
getDateMonthAndYear,
Expand All @@ -26,6 +28,8 @@ export {
useDatepicker,
isDateSelected,
isFirstOrLastSelectedDate,
isStartDate,
isEndDate,
isDateBlocked,
getCurrentYearMonthAndDate,
getDateMonthAndYear,
Expand Down
46 changes: 46 additions & 0 deletions packages/hooks/src/useDatepicker/useDatepicker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,52 @@ describe('useDatepicker', () => {
clear()
})


test('should check if date is start date', () => {
const onDatesChange = jest.fn()
advanceTo(new Date(2019, 2, 27, 0, 0, 0))
const {result} = renderHook(() =>
useDatepicker({
startDate: new Date(2019, 3, 1, 0, 0, 0),
endDate: new Date(2019, 3, 3, 0, 0, 0),
minBookingDate: new Date(2019, 3, 1, 0, 0, 0),
maxBookingDate: new Date(2019, 3, 10, 0, 0, 0),
focusedInput: START_DATE,
onDatesChange: onDatesChange,
}),
)

expect(result.current.isStartDate(new Date(2019, 3, 1, 0, 0, 0))).toBe(true)
expect(result.current.isStartDate(new Date(2019, 3, 2, 0, 0, 0))).toBe(false)
expect(result.current.isStartDate(new Date(2019, 3, 3, 0, 0, 0))).toBe(false)
expect(result.current.isStartDate(new Date(2019, 3, 10, 0, 0, 0))).toBe(false)
expect(result.current.isStartDate(new Date(2019, 2, 27, 0, 0, 0))).toBe(false)
clear()
})


test('should check if date is end date', () => {
const onDatesChange = jest.fn()
advanceTo(new Date(2019, 3, 27, 0, 0, 0))
const {result} = renderHook(() =>
useDatepicker({
startDate: new Date(2019, 3, 1, 0, 0, 0),
endDate: new Date(2019, 3, 3, 0, 0, 0),
minBookingDate: new Date(2019, 3, 1, 0, 0, 0),
maxBookingDate: new Date(2019, 3, 10, 0, 0, 0),
focusedInput: START_DATE,
onDatesChange: onDatesChange,
}),
)

expect(result.current.isEndDate(new Date(2019, 3, 1, 0, 0, 0))).toBe(false)
expect(result.current.isEndDate(new Date(2019, 3, 2, 0, 0, 0))).toBe(false)
expect(result.current.isEndDate(new Date(2019, 3, 3, 0, 0, 0))).toBe(true)
expect(result.current.isEndDate(new Date(2019, 3, 10, 0, 0, 0))).toBe(false)
expect(result.current.isEndDate(new Date(2019, 3, 27, 0, 0, 0))).toBe(false)
clear()
})

test.each([
{
startDate: null,
Expand Down
8 changes: 8 additions & 0 deletions packages/hooks/src/useDatepicker/useDatepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
isDateSelected as isDateSelectedFn,
isDateBlocked as isDateBlockedFn,
isFirstOrLastSelectedDate as isFirstOrLastSelectedDateFn,
isEndDate as isEndDateFn,
isStartDate as isStartDateFn,
canSelectRange,
isDateHovered as isDateHoveredFn,
isInUnavailableDates,
Expand Down Expand Up @@ -95,6 +97,10 @@ export function useDatepicker({
const isFirstOrLastSelectedDate = (date: Date) =>
isFirstOrLastSelectedDateFn(date, startDate, endDate)

const isStartDate = (date: Date) => isStartDateFn(date, startDate)

const isEndDate = (date: Date) => isEndDateFn(date, endDate)

const isDateBlocked = (date: Date) =>
isDateBlockedFn({
date,
Expand Down Expand Up @@ -321,6 +327,8 @@ export function useDatepicker({
isDateSelected,
isDateHovered,
isFirstOrLastSelectedDate,
isStartDate,
isEndDate,
isDateBlocked,
numberOfMonths,
isDateFocused,
Expand Down
16 changes: 16 additions & 0 deletions packages/hooks/src/useDatepicker/useDatepicker.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ export function isFirstOrLastSelectedDate(
return !!((startDate && isSameDay(date, startDate)) || (endDate && isSameDay(date, endDate)))
}

export function isStartDate(
date: Date,
startDate: Date | null,
) {
return !!(startDate && isSameDay(date, startDate))
}

export function isEndDate(
date: Date,
endDate: Date | null,
) {
return !!(endDate && isSameDay(date, endDate))
}



interface IsDateBlockedProps {
date: Date
startDate: Date | null
Expand Down
2 changes: 2 additions & 0 deletions packages/hooks/src/useDay/useDay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ test('should not execute onClick callback, because day is disabled', () => {
isDateSelected: jest.fn(),
isDateFocused: jest.fn(),
isFirstOrLastSelectedDate: jest.fn(),
isStartDate: jest.fn(),
isEndDate: jest.fn(),
isDateHovered: jest.fn(),
isDateBlocked: () => true,
onDateFocus: jest.fn(),
Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/useDay/useDay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ interface UseDayProps {
isDateHovered(date: Date): boolean
isDateBlocked(date: Date): boolean
isFirstOrLastSelectedDate(date: Date): boolean
isStartDate(date: Date): boolean
isEndDate(date: Date): boolean
onDateFocus(date: Date): void
onDateSelect(date: Date): void
onDateHover(date: Date): void
Expand All @@ -23,6 +25,8 @@ function useDay({
isDateSelected,
isDateFocused,
isFirstOrLastSelectedDate,
isStartDate,
isEndDate,
isDateHovered,
isDateBlocked,
onDateSelect,
Expand Down
4 changes: 4 additions & 0 deletions packages/styled/lib/context/datepickerContext.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface DatepickerContext {
isDateHovered(date: Date): boolean
isDateBlocked(date: Date): boolean
isFirstOrLastSelectedDate(date: Date): boolean
isStartDate(date: Date): boolean
isEndDate(date: Date): boolean
onDayRender?(date: Date): React.ReactNode
}
export declare const datepickerContextDefaultValue: {
Expand All @@ -20,6 +22,8 @@ export declare const datepickerContextDefaultValue: {
isDateHovered: () => boolean
isDateBlocked: () => boolean
isFirstOrLastSelectedDate: () => boolean
isStartDate: () => boolean
isEndDate: () => boolean
onDateFocus: () => void
onDateHover: () => void
onDateSelect: () => void
Expand Down
6 changes: 5 additions & 1 deletion packages/styled/src/components/Datepicker/Datepicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const StyledDatepicker = styled('div')<StyledDatepicker>`
css`
direction: rtl;
`}
animation-name: ${opacity0To100};
animation-duration: 0.15s;
animation-timing-function: ease-in;
Expand Down Expand Up @@ -177,6 +177,8 @@ function Datepicker(
activeMonths,
isDateSelected,
isFirstOrLastSelectedDate,
isStartDate,
isEndDate,
isDateHovered,
firstDayOfWeek,
onDateSelect,
Expand Down Expand Up @@ -281,6 +283,8 @@ function Datepicker(
isDateSelected,
isDateHovered,
isFirstOrLastSelectedDate,
isStartDate,
isEndDate,
onDateFocus,
focusedDate,
onDateSelect,
Expand Down
Loading

0 comments on commit 94f4118

Please sign in to comment.