Skip to content

Commit a76ca04

Browse files
committed
feat: Add auto-focus to 2FA token field
1 parent ace7713 commit a76ca04

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

pages/index.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ Animated:
9696
<Box>
9797
<PasswordField name="password" />
9898
</Box>
99+
<Box>
100+
<TwoFactorTokenField name="2fa" />
101+
</Box>
99102
</Stack>
100103
</Form>
101104
)}

src/form/TwoFactorTokenField.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@ import { ErrorText } from './ErrorText'
66

77
export interface TwoFactorTokenFieldProps extends InputProps {
88
name: string
9+
autoFocus?: boolean
910
}
1011

1112
export const TwoFactorTokenField: React.FC<TwoFactorTokenFieldProps> = ({
1213
name,
14+
autoFocus = false,
1315
...props
1416
}) => {
1517
const theme = useTheme()
16-
const [field] = useField(name)
18+
const [{ onBlur, ...field }] = useField(name)
19+
const ref = React.useRef<HTMLInputElement>()
20+
21+
React.useEffect(() => {
22+
if (autoFocus && ref.current) {
23+
ref.current.focus()
24+
}
25+
}, [])
1726
return (
1827
<>
1928
<Input
@@ -23,12 +32,14 @@ export const TwoFactorTokenField: React.FC<TwoFactorTokenFieldProps> = ({
2332
placeholder="123456"
2433
mb={1}
2534
size="lg"
35+
maxW="9rem"
2636
inputMode="numeric" // Show numeric keyboard on mobile
2737
autoComplete="one-time-code"
2838
pattern="[0-9]{6}"
2939
textAlign="center"
30-
fontSize="1.8rem"
40+
fontSize="1.6rem"
3141
fontFamily={theme.fonts.mono}
42+
ref={ref as any}
3243
{...field}
3344
{...props}
3445
/>

0 commit comments

Comments
 (0)