-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmonth-days.tsx
145 lines (125 loc) · 3.46 KB
/
month-days.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import * as React from 'react'
import { Grid, useMultiStyleConfig } from '@chakra-ui/react'
import {
eachDayOfInterval,
endOfMonth,
format,
isAfter,
isBefore,
isSameDay,
isWeekend,
startOfMonth,
} from 'date-fns'
import type { CalendarMonthStyles } from './types'
import { CalendarContext } from './context'
import { Day } from './day'
import { MonthContext } from './month'
export function CalendarDays({ children }: CalendarDayProps) {
const styles = useMultiStyleConfig('CalendarMonth', {}) as CalendarMonthStyles
const { dates } = React.useContext(CalendarContext)
const { month } = React.useContext(MonthContext)
return (
<Grid sx={styles.days}>
{dates[Number(month)].days.map((day, index) => (
<DayContext.Provider value={{ day }}>
<CalendarDay key={day ? format(day, 'd-M') : `not-a-day-${index}`}>
{children}
</CalendarDay>
</DayContext.Provider>
))}
</Grid>
)
}
export type CalendarDayContext = {
day: CalendarContext['dates'][number]['days'][number]
}
export const DayContext = React.createContext<CalendarDayContext>({ day: 0 })
export interface CalendarDayProps {
children?: React.ReactNode | ((props: CalendarDayContext) => React.ReactNode)
}
export function CalendarDay({ children }: CalendarDayProps) {
const { onSelectDates } = React.useContext(CalendarContext)
const { day } = React.useContext(DayContext)
const { variant, isDisabled } = useCalendarDay()
if (!day) {
return <span />
}
return (
<Day
variant={variant}
day={day}
disabled={isDisabled}
onSelectDate={onSelectDates}
>
{typeof children === 'function' ? children({ day }) : children}
</Day>
) as JSX.Element
}
export const useCalendarDay = () => {
const {
dates,
onSelectDates,
startSelectedDate,
endSelectedDate,
disableDates,
disableFutureDates,
disablePastDates,
disableWeekends,
highlightToday,
} = React.useContext(CalendarContext)
const { month } = React.useContext(MonthContext)
const { day } = React.useContext(DayContext)
if (!day) return {}
let variant: 'selected' | 'range' | 'outside' | 'today' | undefined
const isSelected =
(startSelectedDate && isSameDay(day, startSelectedDate)) ||
(endSelectedDate && isSameDay(day, endSelectedDate))
if (isSelected) {
variant = 'selected'
}
if (
(isBefore(day, startOfMonth(dates[Number(month)].startDateOfMonth)) ||
isAfter(day, endOfMonth(dates[Number(month)].startDateOfMonth))) &&
!isSelected
) {
variant = 'outside'
}
if (highlightToday && isSameDay(new Date(), day)) {
variant = 'today'
}
const interval =
startSelectedDate &&
endSelectedDate &&
eachDayOfInterval({
start: startSelectedDate,
end: endSelectedDate,
})
const isInRange = interval
? interval.some(date => isSameDay(day, date))
: false
if (isInRange && !isSelected) {
variant = 'range'
}
const isDisabled =
(disablePastDates &&
isBefore(
day,
disablePastDates instanceof Date ? disablePastDates : new Date()
)) ||
(disableFutureDates &&
isAfter(
day,
disableFutureDates instanceof Date ? disableFutureDates : new Date()
)) ||
(disableWeekends && isWeekend(day)) ||
(disableDates && disableDates.some(date => isSameDay(day, date)))
return {
day,
variant,
isSelected,
interval,
isInRange,
isDisabled,
onSelectDates,
}
}