-
Notifications
You must be signed in to change notification settings - Fork 15
/
useSingleCalendarState.ts
248 lines (206 loc) · 6.47 KB
/
useSingleCalendarState.ts
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import { useMemo, useState } from 'react'
import { isWeekend } from '../../libs/date'
import { useFirstDayOfWeek } from '../../libs/i18n'
import { clamp } from '../../libs/utils'
import { MAX_DATE, MIN_DATE } from './constants'
import {
CalendarCellState,
CalendarNavigationAction,
CalendarView,
CalendarViewKind,
SingleCalendarProps,
SingleCalendarState,
} from './types'
import {
clampDate,
closestViewDate,
createRange,
durationInViews,
getViewDate,
getViews,
isEqualDate,
isInRange,
isSameCell,
isSameView,
move,
nearest,
startOfCell,
startOfView,
subViews,
} from './utils'
export type UseSingleCalendarStateProps = SingleCalendarProps
export type UseSingleCalendarStateResult = SingleCalendarState
export function useSingleCalendarState(
props: UseSingleCalendarStateProps,
): UseSingleCalendarStateResult {
const {
value: propValue,
defaultFocusedDate = propValue,
onChange,
autoFocus = false,
readOnly = false,
disabled = false,
min: propMin = MIN_DATE,
max: propMax = MAX_DATE,
viewsCount = 1,
minCalendarView = 'day',
defaultCalendarView = minCalendarView,
maxCalendarView = 'year',
} = props
const min = useMemo(() => new Date(propMin), [propMin])
const max = useMemo(() => new Date(propMax), [propMax])
const value = useMemo(() => (propValue ? new Date(propValue) : undefined), [propValue])
const firstDayOfWeek = useFirstDayOfWeek()
const [activeView, setActiveView] = useState(() => normalizeView(defaultCalendarView))
const [isCalendarFocused, focusCalendar] = useState(autoFocus)
const [focusedDate, setFocusedDate] = useState(() =>
normalizeFocusedDate(defaultFocusedDate ? new Date(defaultFocusedDate) : new Date()),
)
const [baseDate, setBaseDate] = useState(() => normalizeBaseDate(activeView, focusedDate))
function normalizeFocusedDate(date: Date) {
return startOfCell(minCalendarView, clampDate(date, min, max))
}
function normalizeBaseDate(view: CalendarView, date: Date) {
const maxDate = subViews(view, max, viewsCount - 1)
const candidate = startOfView(view, clampDate(date, min, min > maxDate ? min : maxDate))
return getViewDate(view, candidate, date)
}
function normalizeView(view: CalendarView) {
const key = clamp(
CalendarViewKind[view],
CalendarViewKind[minCalendarView],
CalendarViewKind[maxCalendarView],
)
return CalendarViewKind[key] as CalendarView
}
function moveView(view: CalendarView, offset: number = 0) {
const key = CalendarViewKind[view] + offset
const nextView = CalendarViewKind[key] as CalendarView | undefined
return normalizeView(nextView ?? view)
}
function navigateTo(date: Date) {
const nextBaseDate = getViewDate(activeView, date, focusedDate)
if (isEqualDate(baseDate, nextBaseDate)) {
return
}
setBaseDate(nextBaseDate)
const nextFocusedDate = normalizeFocusedDate(
nearest(activeView, focusedDate, nextBaseDate, viewsCount),
)
if (isEqualDate(focusedDate, nextFocusedDate)) {
return
}
setFocusedDate(nextFocusedDate)
}
function focusTo(view: CalendarView, date: Date) {
const nextFocusedDate = clampDate(startOfCell(minCalendarView, date), min, max)
if (isEqualDate(focusedDate, nextFocusedDate)) {
return
}
setFocusedDate(nextFocusedDate)
const nextBaseDate = getViewDate(
view,
closestViewDate(view, baseDate, viewsCount, nextFocusedDate),
nextFocusedDate,
)
if (isEqualDate(baseDate, nextBaseDate)) {
return
}
setBaseDate(nextBaseDate)
}
function setViewAndFocusedDate(view: CalendarView, date: Date) {
if (
CalendarViewKind[view] < CalendarViewKind[minCalendarView] ||
CalendarViewKind[view] > CalendarViewKind[maxCalendarView]
) {
return
}
if (view !== activeView) {
setActiveView(view)
}
const nextFocusedDate = normalizeFocusedDate(date)
if (!isEqualDate(focusedDate, nextFocusedDate)) {
setFocusedDate(nextFocusedDate)
}
const offset = durationInViews(
activeView,
baseDate,
normalizeBaseDate(activeView, nextFocusedDate),
)
const nextBaseDate = subViews(view, normalizeBaseDate(view, nextFocusedDate), offset)
if (!isEqualDate(baseDate, nextBaseDate)) {
setBaseDate(nextBaseDate)
}
}
function setView(view: CalendarView) {
setViewAndFocusedDate(view, focusedDate)
}
function focusDate(date: Date) {
focusTo(activeView, date)
}
function selectDate(date: Date) {
if (readOnly || disabled || !isInRange(activeView, date, createRange(min, max))) {
return
}
const nextView = moveView(activeView, -1)
if (nextView !== activeView) {
setViewAndFocusedDate(nextView, date)
} else {
focusTo(nextView, date)
if (!readOnly && nextView === minCalendarView) {
onChange?.({ value: date })
}
}
}
function getCellState(cellValue: Date, viewDate: Date) {
const today = new Date()
const sameView = isSameView(activeView, cellValue, viewDate)
const isDisabled = disabled || !isInRange(activeView, cellValue, createRange(min, max))
const isFocused = sameView && isSameCell(activeView, cellValue, focusedDate)
const isSelected = value ? isSameCell(activeView, cellValue, value) : false
const isToday = isSameCell(activeView, cellValue, today)
const cellState: CalendarCellState = {
isDisabled,
isFocused,
isSameView: sameView,
isSelected,
isToday,
isWeekend: activeView === 'day' && isWeekend(cellValue),
}
return cellState
}
function moveDate(date: Date, action: CalendarNavigationAction) {
return move(activeView, date, action, { firstDayOfWeek })
}
function canNavigateTo(date: Date) {
const minViewDate = startOfView(activeView, min)
const maxViewDate = new Date(
Math.max(
minViewDate.getTime(),
startOfView(activeView, subViews(activeView, max, viewsCount - 1)).getTime(),
),
)
return isInRange(activeView, date, createRange(minViewDate, maxViewDate))
}
const views = useMemo(() => {
return getViews(activeView, baseDate, viewsCount, { firstDayOfWeek })
}, [baseDate, viewsCount, activeView, firstDayOfWeek])
return {
mode: 'single',
value,
isCalendarFocused,
baseDate,
focusedDate,
activeView,
views,
focusCalendar,
focusDate,
selectDate,
getCellState,
moveDate,
setView,
canNavigateTo,
navigateTo,
moveView,
}
}