Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/forty-taxis-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@radix-ui/react-one-time-password-field': patch
---

fix: focus 1st input when autoFocus is true
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ describe('given a default OneTimePasswordField', () => {
expect(hiddenInput.value).toBe('1');
});

it('should focus on first input when autoFocus is true', () => {
rendered.rerender(
<OneTimePasswordField.Root autoFocus>
<OneTimePasswordField.Input />
<OneTimePasswordField.Input />
<OneTimePasswordField.Input />
<OneTimePasswordField.Input />
<OneTimePasswordField.Input />
<OneTimePasswordField.Input />
<OneTimePasswordField.HiddenInput name="code" />
</OneTimePasswordField.Root>
);

const firstInput = rendered.container.querySelector(
'input:not([type="hidden"])'
) as HTMLInputElement;
expect(document.activeElement).toBe(firstInput);
});

// TODO: userEvent paste not behaving as expected. Debug and unskip.
// Replicated in storybook for now.
it.todo('pastes the code into the input', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,12 @@ const OneTimePasswordField = React.forwardRef<HTMLDivElement, OneTimePasswordFie
formElement?.requestSubmit();
}, [locateForm]);

React.useEffect(() => {
if (autoFocus && firstInput) {
firstInput.focus();
}
}, [autoFocus, firstInput]);

React.useEffect(() => {
const form = locateForm();
if (form) {
Expand Down