Skip to content

Commit 3edc07c

Browse files
feat(clerk-js): Add autoFocus prop to input-otp (#6863)
1 parent f737d26 commit 3edc07c

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

.changeset/clean-islands-judge.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Add auto focus behavior to otp input to enable pasting codes immediately

packages/clerk-js/src/ui/elements/CodeControl.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export const OTPCodeControl = () => {
180180
sx={{ position: 'relative' }}
181181
>
182182
<OTPInput
183+
autoFocus // eslint-disable-line jsx-a11y/no-autofocus
183184
aria-label='Enter verification code'
184185
aria-required
185186
maxLength={length}

packages/clerk-js/src/ui/elements/__tests__/CodeControl.test.tsx

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { ClerkRuntimeError } from '@clerk/shared/error';
2-
import { fireEvent, render, waitFor } from '@testing-library/react';
2+
import { render, waitFor } from '@testing-library/react';
33
import type { UserEvent } from '@testing-library/user-event';
44
import userEvent from '@testing-library/user-event';
5-
import React from 'react';
65
import { describe, expect, it, vi } from 'vitest';
76

87
import { bindCreateFixtures } from '@/test/create-fixtures';
@@ -188,25 +187,24 @@ describe('CodeControl', () => {
188187
const Component = createOTPComponent(onCodeEntryFinished);
189188

190189
const { getAllByTestId, getByLabelText } = render(<Component />, { wrapper });
190+
const user = userEvent.setup();
191191

192192
const input = getByLabelText(label);
193-
const inputDivs = getAllByTestId(testId);
194193

195-
// Simulate autofill with mixed characters
196-
fireEvent.change(input, { target: { value: '1a2b3c' } });
194+
await user.click(input);
195+
await user.paste('1a2b3c');
197196

198197
await waitFor(() => {
199-
expect(inputDivs[0]).toBeEmptyDOMElement();
200-
expect(inputDivs[1]).toBeEmptyDOMElement();
201-
expect(inputDivs[2]).toBeEmptyDOMElement();
202-
expect(inputDivs[3]).toBeEmptyDOMElement();
203-
expect(inputDivs[4]).toBeEmptyDOMElement();
204-
expect(inputDivs[5]).toBeEmptyDOMElement();
198+
expect(input).toHaveValue('');
205199
});
206200

207-
// Simulate autofill with expected characters
208-
fireEvent.change(input, { target: { value: '123456' } });
201+
await user.paste('123456');
202+
203+
await waitFor(() => {
204+
expect(input).toHaveValue('123456');
205+
});
209206

207+
const inputDivs = getAllByTestId(testId);
210208
await waitFor(() => {
211209
expect(inputDivs[0]).toHaveTextContent('1');
212210
expect(inputDivs[1]).toHaveTextContent('2');

0 commit comments

Comments
 (0)