Skip to content

Commit 6f758d8

Browse files
authored
fix: Focus behaviour on inputs inside a FocusScope (adobe#8903)
* Revert "Revert "fix: Focus behaviour on inputs inside a FocusScope (adobe#8498)" (…" This reverts commit 84ff482. * Update to only happen on Tab * fix lint * undo a few other extra and unnecessary selects
1 parent 2d3b48d commit 6f758d8

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

packages/@react-aria/focus/src/FocusScope.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,9 @@ function useFocusContainment(scopeRef: RefObject<Element[] | null>, contain?: bo
373373
e.preventDefault();
374374
if (nextElement) {
375375
focusElement(nextElement, true);
376+
if (nextElement instanceof getOwnerWindow(nextElement).HTMLInputElement) {
377+
nextElement.select();
378+
}
376379
}
377380
};
378381

packages/@react-aria/focus/test/FocusScope.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,27 @@ describe('FocusScope', function () {
354354

355355
expect(document.activeElement).toBe(input2);
356356
});
357+
358+
it('should select all text in input when tabbing', async function () {
359+
let {getByTestId} = render(
360+
<FocusScope contain>
361+
<input data-testid="input1" defaultValue="Test1" />
362+
<input data-testid="input2" defaultValue="Test2" />
363+
<input data-testid="input3" defaultValue="Test3" />
364+
</FocusScope>
365+
);
366+
367+
let input1 = getByTestId('input1');
368+
let input2 = getByTestId('input2');
369+
370+
act(() => {input1.focus();});
371+
expect(document.activeElement).toBe(input1);
372+
373+
await user.tab();
374+
expect(document.activeElement).toBe(input2);
375+
await user.keyboard('{Delete}');
376+
expect(input2.value).toBe('');
377+
});
357378
});
358379

359380
describe('focus restoration', function () {

packages/@react-spectrum/s2/stories/Dialog.stories.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ const ExampleRender = (args: ExampleRenderProps): ReactElement => (
4848
<Heading slot="title">Dialog title</Heading>
4949
<Header>Header</Header>
5050
<Content>
51-
{[...Array(args.paragraphs)].map((_, i) =>
52-
<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>
53-
)}
51+
<>
52+
{[...Array(args.paragraphs)].map((_, i) =>
53+
<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>
54+
)}
55+
<input type="text" defaultValue="Hello" />
56+
</>
5457
</Content>
5558
<Footer><Checkbox>Don't show this again</Checkbox></Footer>
5659
<ButtonGroup>

0 commit comments

Comments
 (0)