Skip to content

Commit 4e00d96

Browse files
author
Asen Mitrev
committed
Allow imperative focus on input via ref
1 parent 8fa61d6 commit 4e00d96

File tree

2 files changed

+231
-197
lines changed

2 files changed

+231
-197
lines changed

example/src/App.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import OTPInput from '../../src';
2+
import OTPInput, { OTPRef } from '../../src';
33

44
function App() {
55
const [{ otp, numInputs, separator, minLength, maxLength, placeholder, inputType }, setConfig] = React.useState({
@@ -12,6 +12,12 @@ function App() {
1212
inputType: 'text' as const,
1313
});
1414

15+
const otpRef = React.useRef<OTPRef>(null);
16+
17+
const focusOtp = () => {
18+
otpRef.current?.focus();
19+
};
20+
1521
const handleOTPChange = (otp: string) => {
1622
setConfig((prevConfig) => ({ ...prevConfig, otp }));
1723
};
@@ -108,6 +114,7 @@ function App() {
108114
<p>Enter verification code</p>
109115
<div className="margin-top--small">
110116
<OTPInput
117+
ref={otpRef}
111118
inputStyle="inputStyle"
112119
numInputs={numInputs}
113120
onChange={handleOTPChange}
@@ -120,6 +127,9 @@ function App() {
120127
/>
121128
</div>
122129
<div className="btn-row">
130+
<button className="btn margin-top--large" type="button" onClick={focusOtp}>
131+
Focus
132+
</button>
123133
<button className="btn margin-top--large" type="button" disabled={otp.trim() === ''} onClick={clearOtp}>
124134
Clear
125135
</button>

0 commit comments

Comments
 (0)