-
-
Notifications
You must be signed in to change notification settings - Fork 735
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: focus and autofocus improvements (#2265)
- Loading branch information
Showing
10 changed files
with
127 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from "react"; | ||
|
||
import { DayPicker } from "react-day-picker"; | ||
|
||
/** Test for the next focus day to not cause an infinite recursion. */ | ||
export function AutoFocus() { | ||
return ( | ||
<div> | ||
<DayPicker autoFocus mode="single" /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { DayFlag } from "../UI.js"; | ||
import type { CalendarDay } from "../classes/index.js"; | ||
import type { Modifiers } from "../types/index.js"; | ||
import { UseCalendar } from "../useCalendar.js"; | ||
|
||
export function calculateFocusTarget( | ||
calendar: UseCalendar, | ||
getModifiers: (day: CalendarDay) => Modifiers, | ||
isSelected: (date: Date) => boolean, | ||
lastFocused: CalendarDay | undefined | ||
) { | ||
let focusTarget: CalendarDay | undefined; | ||
|
||
let index = 0; | ||
let found = false; | ||
|
||
while (index < calendar.days.length && !found) { | ||
const day = calendar.days[index]; | ||
const m = getModifiers(day); | ||
|
||
if (!m[DayFlag.disabled] && !m[DayFlag.hidden] && !m[DayFlag.outside]) { | ||
if (m[DayFlag.focused]) { | ||
focusTarget = day; | ||
found = true; | ||
} else if (lastFocused?.isEqualTo(day)) { | ||
focusTarget = day; | ||
found = true; | ||
} else if (isSelected(day.date)) { | ||
focusTarget = day; | ||
found = true; | ||
} else if (m[DayFlag.today]) { | ||
focusTarget = day; | ||
found = true; | ||
} | ||
} | ||
|
||
index++; | ||
} | ||
|
||
if (!focusTarget) { | ||
// return the first day that is focusable | ||
focusTarget = calendar.days.find((day) => { | ||
const m = getModifiers(day); | ||
return !m[DayFlag.disabled] && !m[DayFlag.hidden] && !m[DayFlag.outside]; | ||
}); | ||
} | ||
return focusTarget; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters