Skip to content

Commit 5d6a4cc

Browse files
committed
chore: Shift focus to active ref by clicking / pressing on the recovery CTA
1 parent 36ebfc0 commit 5d6a4cc

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/select/__tests__/select.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ describe.each([false, true])('expandToViewport=%s', expandToViewport => {
210210
wrapper.openDropdown();
211211
onLoadItems.mockClear();
212212
wrapper.findErrorRecoveryButton({ expandToViewport })!.click();
213+
expect(wrapper.findFilteringInput()!.findNativeInput()!.getElement()).toHaveFocus();
213214
expect(onLoadItems).toHaveBeenCalledWith({ filteringText: '', firstPage: false, samePage: true });
214215
});
215216

src/select/internal.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ const InternalSelect = React.forwardRef(
122122
highlightOption,
123123
selectOption,
124124
announceSelected,
125+
focusActiveRef,
125126
} = useSelect({
126127
selectedOptions: selectedOption ? [selectedOption] : [],
127128
updateSelectedOption: option => fireNonCancelableEvent(onChange, { selectedOption: option }),
@@ -197,7 +198,10 @@ const InternalSelect = React.forwardRef(
197198
noMatch,
198199
filteringResultsText: filteredText,
199200
errorIconAriaLabel,
200-
onRecoveryClick: handleRecoveryClick,
201+
onRecoveryClick: () => {
202+
handleRecoveryClick();
203+
focusActiveRef();
204+
},
201205
hasRecoveryCallback: !!onLoadItems,
202206
});
203207

src/select/utils/use-select.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
import React, { RefObject } from 'react';
3+
import React, { RefObject, useCallback } from 'react';
44
import { useEffect, useRef } from 'react';
55

66
import { useUniqueId } from '@cloudscape-design/component-toolkit/internal';
@@ -337,14 +337,18 @@ export function useSelect({
337337
hasFilter,
338338
]);
339339

340+
const focusActiveRef = useCallback(() => {
341+
// dropdown-fit calculations ensure that the dropdown will fit inside the current
342+
// viewport, so prevent the browser from trying to scroll it into view (e.g. if
343+
// scroll-padding-top is set on a parent)
344+
activeRef.current?.focus({ preventScroll: true });
345+
}, [activeRef]);
346+
340347
useEffect(() => {
341348
if (isOpen && !embedded) {
342-
// dropdown-fit calculations ensure that the dropdown will fit inside the current
343-
// viewport, so prevent the browser from trying to scroll it into view (e.g. if
344-
// scroll-padding-top is set on a parent)
345-
activeRef.current?.focus({ preventScroll: true });
349+
focusActiveRef();
346350
}
347-
}, [isOpen, activeRef, embedded]);
351+
}, [isOpen, activeRef, embedded, focusActiveRef]);
348352

349353
useForwardFocus(externalRef, triggerRef as React.RefObject<HTMLElement>);
350354
const highlightedGroupSelected =
@@ -366,5 +370,6 @@ export function useSelect({
366370
selectOption,
367371
announceSelected,
368372
dialogId,
373+
focusActiveRef,
369374
};
370375
}

0 commit comments

Comments
 (0)