summaryrefslogtreecommitdiff
path: root/shared/components/src/utils/getUpdatedFocusedIndex.ts
diff options
context:
space:
mode:
authorrxliuli <rxliuli@gmail.com>2025-11-04 05:03:50 +0800
committerrxliuli <rxliuli@gmail.com>2025-11-04 05:03:50 +0800
commitbce557cc2dc767628bed6aac87301a1be7c5431b (patch)
treeb51a051228d01fe3306cd7626d4a96768aadb944 /shared/components/src/utils/getUpdatedFocusedIndex.ts
init commit
Diffstat (limited to 'shared/components/src/utils/getUpdatedFocusedIndex.ts')
-rw-r--r--shared/components/src/utils/getUpdatedFocusedIndex.ts25
1 files changed, 25 insertions, 0 deletions
diff --git a/shared/components/src/utils/getUpdatedFocusedIndex.ts b/shared/components/src/utils/getUpdatedFocusedIndex.ts
new file mode 100644
index 0000000..ca2c765
--- /dev/null
+++ b/shared/components/src/utils/getUpdatedFocusedIndex.ts
@@ -0,0 +1,25 @@
+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;
+ }
+ }
+}