Files
apps.apple.com/shared/components/src/utils/getUpdatedFocusedIndex.ts
2025-11-04 05:03:50 +08:00

26 lines
717 B
TypeScript

export function getUpdatedFocusedIndex(
incrementAmount: number,
currentFocusedIndex: number | null,
numberOfItems: number,
): number {
const potentialFocusedIndex = incrementAmount + currentFocusedIndex;
if (incrementAmount > 0) {
if (currentFocusedIndex === null) {
return 0;
} else {
return potentialFocusedIndex >= numberOfItems
? 0
: potentialFocusedIndex;
}
} else {
if (currentFocusedIndex === null) {
return numberOfItems - 1;
} else {
return potentialFocusedIndex < 0
? numberOfItems - 1
: potentialFocusedIndex;
}
}
}