1 | import { useState } from "react";
|
2 | import { calculateFocusTarget } from "./helpers/calculateFocusTarget.js";
|
3 | import { getNextFocus } from "./helpers/getNextFocus.js";
|
4 |
|
5 | export function useFocus(props, calendar, getModifiers, isSelected, dateLib) {
|
6 | const { autoFocus } = props;
|
7 | const [lastFocused, setLastFocused] = useState();
|
8 | const focusTarget = calculateFocusTarget(calendar.days, getModifiers, isSelected || (() => false), lastFocused);
|
9 | const [focusedDay, setFocused] = useState(autoFocus ? focusTarget : undefined);
|
10 | const blur = () => {
|
11 | setLastFocused(focusedDay);
|
12 | setFocused(undefined);
|
13 | };
|
14 | const moveFocus = (moveBy, moveDir) => {
|
15 | if (!focusedDay)
|
16 | return;
|
17 | const nextFocus = getNextFocus(moveBy, moveDir, focusedDay, calendar.navStart, calendar.navEnd, props, dateLib);
|
18 | if (!nextFocus)
|
19 | return;
|
20 | calendar.goToDay(nextFocus);
|
21 | setFocused(nextFocus);
|
22 | };
|
23 | const isFocusTarget = (day) => {
|
24 | return Boolean(focusTarget?.isEqualTo(day));
|
25 | };
|
26 | const useFocus = {
|
27 | isFocusTarget,
|
28 | setFocused,
|
29 | focused: focusedDay,
|
30 | blur,
|
31 | moveFocus
|
32 | };
|
33 | return useFocus;
|
34 | }
|
35 |
|
\ | No newline at end of file |