/ shared / components / src / utils / getUpdatedFocusedIndex.ts
getUpdatedFocusedIndex.ts
 1  export function getUpdatedFocusedIndex(
 2      incrementAmount: number,
 3      currentFocusedIndex: number | null,
 4      numberOfItems: number,
 5  ): number {
 6      const potentialFocusedIndex = incrementAmount + currentFocusedIndex;
 7  
 8      if (incrementAmount > 0) {
 9          if (currentFocusedIndex === null) {
10              return 0;
11          } else {
12              return potentialFocusedIndex >= numberOfItems
13                  ? 0
14                  : potentialFocusedIndex;
15          }
16      } else {
17          if (currentFocusedIndex === null) {
18              return numberOfItems - 1;
19          } else {
20              return potentialFocusedIndex < 0
21                  ? numberOfItems - 1
22                  : potentialFocusedIndex;
23          }
24      }
25  }