Skip to content

fix: Focus behaviour on inputs inside a FocusScope #8498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@react-aria/autocomplete/src/useAutocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export function useAutocomplete(props: AriaAutocompleteOptions, state: Autocompl
// Ensure input is focused if the user clicks on the collection directly.
if (!e.isTrusted && shouldUseVirtualFocus && inputRef.current && getActiveElement(getOwnerDocument(inputRef.current)) !== inputRef.current) {
inputRef.current.focus();
inputRef.current.select?.();
}

let target = e.target as Element | null;
Expand Down
3 changes: 3 additions & 0 deletions packages/@react-aria/focus/src/FocusScope.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ function useFocusContainment(scopeRef: RefObject<Element[] | null>, contain?: bo
// restore focus to the previously focused node or the first tabbable element in the active scope.
if (focusedNode.current) {
focusedNode.current.focus();
(focusedNode.current as HTMLInputElement).select?.();
} else if (activeScope && activeScope.current) {
focusFirstInScope(activeScope.current);
}
Expand Down Expand Up @@ -400,6 +401,7 @@ function useFocusContainment(scopeRef: RefObject<Element[] | null>, contain?: bo
if (target && target.isConnected) {
focusedNode.current = target;
focusedNode.current?.focus();
(focusedNode.current as HTMLInputElement).select?.();
} else if (activeScope.current) {
focusFirstInScope(activeScope.current);
}
Expand Down Expand Up @@ -487,6 +489,7 @@ function focusElement(element: FocusableElement | null, scroll = false) {
} else if (element != null) {
try {
element.focus();
(element as HTMLInputElement).select?.();
} catch {
// ignore
}
Expand Down
2 changes: 2 additions & 0 deletions packages/@react-aria/interactions/src/focusSafely.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ export function focusSafely(element: FocusableElement): void {
// If focus did not move and the element is still in the document, focus it.
if (getActiveElement(ownerDocument) === lastFocusedElement && element.isConnected) {
focusWithoutScrolling(element);
(element as HTMLInputElement).select?.();
}
});
} else {
focusWithoutScrolling(element);
(element as HTMLInputElement).select?.();
}
}
4 changes: 2 additions & 2 deletions packages/@react-spectrum/combobox/test/ComboBox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4398,7 +4398,7 @@ describe('ComboBox', function () {
if (Method === 'escape key') {
expect(button).toHaveAttribute('aria-labelledby', `${tree.getByText('Test').id} ${tree.getByText('Two').id}`);
} else {
expect(button).toHaveAttribute('aria-labelledby', `${tree.getByText('Test').id} ${tree.getByText('Twor').id}`);
expect(button).toHaveAttribute('aria-labelledby', `${tree.getByText('Test').id} ${tree.getByText('r').id}`);
}
tree.unmount();

Expand All @@ -4412,7 +4412,7 @@ describe('ComboBox', function () {

await performInteractions(tree);
expect(() => tree.getByTestId('tray')).toThrow();
expect(button).toHaveAttribute('aria-labelledby', `${tree.getByText('Test').id} ${tree.getByText('Twor').id}`);
expect(button).toHaveAttribute('aria-labelledby', `${tree.getByText('Test').id} ${tree.getByText('r').id}`);
});

it('menutrigger=focus doesn\'t reopen the tray on close', async function () {
Expand Down
9 changes: 6 additions & 3 deletions packages/@react-spectrum/s2/stories/Dialog.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ const ExampleRender = (args: ExampleRenderProps): ReactElement => (
<Heading slot="title">Dialog title</Heading>
<Header>Header</Header>
<Content>
{[...Array(args.paragraphs)].map((_, i) =>
<p key={i} style={{marginTop: i === 0 ? 0 : undefined, marginBottom: i === args.paragraphs - 1 ? 0 : undefined}}>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in</p>
)}
<>
{[...Array(args.paragraphs)].map((_, i) =>
<p key={i} style={{marginTop: i === 0 ? 0 : undefined, marginBottom: i === args.paragraphs - 1 ? 0 : undefined}}>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in</p>
)}
<input type="text" defaultValue="Hello" />
</>
</Content>
<Footer><Checkbox>Don't show this again</Checkbox></Footer>
<ButtonGroup>
Expand Down