Skip to content

Commit 34fa4ef

Browse files
committed
Change incrementTabKey -> incrementSelectedTab, only handle +/- 1 increments
1 parent 12d42c2 commit 34fa4ef

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

packages/components/TabList/src/TabList/useTabList.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,17 @@ export const useTabList = (props: TabListProps): TabListInfo => {
7474
[setTabKeys],
7575
);
7676

77-
const incrementTabKey = React.useCallback(
78-
(increment: number) => {
79-
if (increment === 0) {
80-
return;
81-
}
82-
77+
const incrementSelectedTab = React.useCallback(
78+
(goBackward: boolean) => {
8379
const currentIndex = tabKeys.indexOf(selectedTabKey);
8480

85-
// We want to only switch selection to non-disabled tabs. This skips over disabled ones.
86-
const direction = increment > 0 ? 1 : -1;
87-
const magnitude = increment * direction;
81+
const direction = goBackward ? -1 : 1;
82+
let increment = 1;
8883
let newTabKey: string;
89-
let retries = 0;
90-
while (retries < tabKeys.length) {
91-
let newIndex = (currentIndex + direction * (magnitude + retries)) % tabKeys.length;
84+
85+
// We want to only switch selection to non-disabled tabs. This loop allows us to skip over disabled ones.
86+
while (increment <= tabKeys.length) {
87+
let newIndex = (currentIndex + direction * increment) % tabKeys.length;
9288

9389
if (newIndex < 0) {
9490
newIndex = tabKeys.length + newIndex;
@@ -97,14 +93,14 @@ export const useTabList = (props: TabListProps): TabListInfo => {
9793
newTabKey = tabKeys[newIndex];
9894

9995
if (disabledStateMap[newTabKey]) {
100-
retries += 1;
96+
increment += 1;
10197
} else {
10298
break;
10399
}
104100
}
105101

106102
// Unable to find a non-disabled next tab, early return
107-
if (retries === tabKeys.length) {
103+
if (increment > tabKeys.length) {
108104
return;
109105
}
110106

@@ -172,13 +168,13 @@ export const useTabList = (props: TabListProps): TabListInfo => {
172168
const onRootKeyDown = React.useCallback(
173169
(e: IKeyboardEvent) => {
174170
if (e.nativeEvent.key === 'Tab' && e.nativeEvent.ctrlKey) {
175-
incrementTabKey(e.nativeEvent.shiftKey ? -1 : 1);
171+
incrementSelectedTab(e.nativeEvent.shiftKey);
176172
setInvoked(true); // on win32, set focus on the new tab without triggering narration twice
177173
}
178174

179175
props.onKeyDown?.(e);
180176
},
181-
[incrementTabKey, props],
177+
[incrementSelectedTab, props],
182178
);
183179

184180
return {

0 commit comments

Comments
 (0)