generated from a-type/react-typescript-library-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalendarDay.tsx
225 lines (205 loc) · 6.33 KB
/
CalendarDay.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
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
import React, { FC, forwardRef, HTMLAttributes, MouseEvent, useCallback, useEffect, useRef } from 'react';
import { useCalendarContext } from './CalendarContext';
import { VALUE_DATA_ATTRIBUTE } from './constants';
import { serializeDateValue } from './dataAttributeUtils';
import {
getIsFirstRow,
getIsFirstWeek,
getIsLastRow,
getIsLastWeek,
getIsWeekend,
isBefore,
isBetweenDays,
isSameDay,
} from './dateUtils';
import useCombinedRef from './useCombinedRef';
export interface CalendarDayValue {
date: Date;
isDifferentMonth?: boolean;
}
export interface CalendarDayProps
extends Omit<HTMLAttributes<HTMLButtonElement>, 'value' | 'onClick'> {
/** the day this calendar day box represents */
value: CalendarDayValue;
/** called when the user clicks the day box - even if the date is not allowed to be selected */
onClick?: (
ev: MouseEvent<HTMLButtonElement>,
value: Date,
isDisabled: boolean
) => any;
disabled?: boolean;
}
/** the basic props needed for each day element */
const useDayProps = (value: Date, isDifferentMonth: boolean) => {
const {
value: selectedValue,
highlightedDate,
rangeValue,
isFocusWithin,
weekStartsOn,
} = useCalendarContext();
const attributes = {
[VALUE_DATA_ATTRIBUTE]: serializeDateValue(value),
'aria-label': value.toDateString(),
'data-date-number': value.getDate(),
'data-day-number': value.getDay(),
} as Record<string, any>;
const firstDay = new Date(value.getFullYear(), value.getMonth(), 1);
const lastDay = new Date(value.getFullYear(), value.getMonth() + 1, 0);
const isRangeReversed =
rangeValue && isBefore(rangeValue.end, rangeValue.start);
if (isDifferentMonth) {
attributes['data-different-month'] = true;
} else {
// these attributes aren't assigned to days
// rendered in a grid for a different month
if (isSameDay(value, firstDay)) {
attributes['data-day-first'] = true;
}
if (isSameDay(value, lastDay)) {
attributes['data-day-last'] = true;
}
if (getIsFirstRow(value, weekStartsOn)) {
attributes['data-first-row'] = true;
}
if (getIsLastRow(value, weekStartsOn)) {
attributes['data-last-row'] = true;
}
if (getIsFirstWeek(value)) {
attributes['data-top-edge'] = true;
}
if (getIsLastWeek(value)) {
attributes['data-bottom-edge'] = true;
}
if (value.getDay() === 0) {
attributes['data-first-column'] = true;
}
if (value.getDay() === 6) {
attributes['data-last-column'] = true;
}
if (isSameDay(value, new Date())) {
attributes['data-today'] = true;
}
}
if (getIsWeekend(value)) {
attributes['data-weekend'] = true;
}
if (selectedValue && isSameDay(value, selectedValue)) {
attributes['data-selected'] = true;
}
if (isSameDay(value, highlightedDate)) {
if (isFocusWithin) {
attributes['data-highlighted'] = true;
} else {
attributes['data-highlighted-inactive'] = true;
}
}
if (!!rangeValue && isSameDay(value, rangeValue.start)) {
if (isRangeReversed) {
attributes['data-range-end'] = true;
} else {
attributes['data-range-start'] = true;
}
}
if (!!rangeValue && isSameDay(value, rangeValue.end)) {
if (isRangeReversed) {
attributes['data-range-start'] = true;
} else {
attributes['data-range-end'] = true;
}
}
if (!!rangeValue && isBetweenDays(value, rangeValue.start, rangeValue.end)) {
attributes['data-in-range'] = true;
}
return attributes;
};
/**
* Renders a single day in a calendar. The proper rendering of a day depends on the presence
* of a wrapping Calendar component. This component renders an unstyled <button> with a
* variety of attributes you can use to style the day. It automatically reports interactions
* to the parent Calendar context; there's no need to add your own event handlers to achieve
* functionality.
* @public
*/
export const CalendarDay = forwardRef<HTMLButtonElement, CalendarDayProps>(
(
{
onClick,
onMouseEnter,
onMouseLeave,
value: { date, isDifferentMonth = false },
children,
disabled: userDisabled = false,
...restProps
},
ref
) => {
const {
setDay,
setDayHovered,
getDateEnabled,
isFocusWithin,
highlightedDate,
} = useCalendarContext();
const disabled = !getDateEnabled(date);
const cannotBeSelected = disabled || userDisabled;
const highlighted = isSameDay(date, highlightedDate);
const handleClick = useCallback(
(ev: MouseEvent<HTMLButtonElement>) => {
if (!cannotBeSelected) {
setDay(date);
}
onClick?.(ev, date, cannotBeSelected);
},
[date, onClick, setDay]
);
const handleHover = useCallback(
(ev: MouseEvent<HTMLButtonElement>) => {
const successfulHover = setDayHovered(date);
if (!successfulHover) {
(ev.target as HTMLElement).setAttribute('data-invalid-hover', '');
}
onMouseEnter?.(ev);
},
[date, onMouseEnter, setDayHovered]
);
const handleUnhover = useCallback(
(ev: MouseEvent<HTMLButtonElement>) => {
(ev.target as HTMLElement).removeAttribute('data-invalid-hover');
onMouseLeave?.(ev);
},
[onMouseLeave]
);
const dayProps = useDayProps(date, isDifferentMonth);
const privateRef = useRef<Element | null>(null);
const combinedRef = useCombinedRef<Element | null>(ref, privateRef);
// auto-focus when highlighted and calendar is focused - this
// moves focus from day to day as the user navigates
const shouldAutoFocus = highlighted && isFocusWithin;
useEffect(() => {
if (shouldAutoFocus) {
privateRef.current && (privateRef.current as HTMLElement).focus();
}
}, [shouldAutoFocus]);
const extraProps = cannotBeSelected ? { ['data-disabled']: true } : {};
return (
<button
ref={combinedRef as any}
onClick={handleClick}
onMouseEnter={handleHover}
onMouseLeave={handleUnhover}
tabIndex={highlighted ? 0 : -1}
{...extraProps}
{...dayProps}
{...restProps}
>
{children || date.getDate()}
</button>
);
}
);
CalendarDay.displayName = 'CalendarDay';
/** @private docs only! */
export const DocsCalendarDay = (props: CalendarDayProps) => (
<CalendarDay {...props} />
);