Skip to content

Commit 1a96b42

Browse files
committed
update forward ref
1 parent 1625a77 commit 1a96b42

File tree

9 files changed

+31
-13
lines changed

9 files changed

+31
-13
lines changed

dist/cjs/index.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@ interface IOtpInput {
1919
'data-testid'?: string;
2020
'data-cy'?: string;
2121
}
22-
declare const OtpInput: (props: IOtpInput) => import("react/jsx-runtime").JSX.Element;
22+
export type OtpInputHandle = {
23+
focusInput: (index: number) => void;
24+
};
25+
declare const OtpInput: React.ForwardRefExoticComponent<IOtpInput & React.RefAttributes<OtpInputHandle>>;
2326
export { OtpInput };

dist/cjs/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/cjs/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/esm/index.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@ interface IOtpInput {
1919
'data-testid'?: string;
2020
'data-cy'?: string;
2121
}
22-
declare const OtpInput: (props: IOtpInput) => import("react/jsx-runtime").JSX.Element;
22+
export type OtpInputHandle = {
23+
focusInput: (index: number) => void;
24+
};
25+
declare const OtpInput: React.ForwardRefExoticComponent<IOtpInput & React.RefAttributes<OtpInputHandle>>;
2326
export { OtpInput };

dist/esm/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/esm/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as react_jsx_runtime from 'react/jsx-runtime';
21
import React from 'react';
32

43
interface IOtpInput {
@@ -21,6 +20,9 @@ interface IOtpInput {
2120
'data-testid'?: string;
2221
'data-cy'?: string;
2322
}
24-
declare const OtpInput: (props: IOtpInput) => react_jsx_runtime.JSX.Element;
23+
type OtpInputHandle = {
24+
focusInput: (index: number) => void;
25+
};
26+
declare const OtpInput: React.ForwardRefExoticComponent<IOtpInput & React.RefAttributes<OtpInputHandle>>;
2527

26-
export { OtpInput };
28+
export { OtpInput, OtpInputHandle };

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "reactjs-otp-input",
3-
"version": "2.0.4",
3+
"version": "2.0.6",
44
"description": "A fully customizable, one-time password input component for the web built with React",
55
"scripts": {
66
"build": "rollup -c",

src/index.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useMemo, useRef, useState } from 'react';
1+
import React, { forwardRef, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
22

33
const BACKSPACE = 8;
44
const LEFT_ARROW = 37;
@@ -140,7 +140,11 @@ const SingleOtpInput = (props: ISingleOtpInput) => {
140140
);
141141
};
142142

143-
const OtpInput = (props: IOtpInput) => {
143+
export type OtpInputHandle = {
144+
focusInput: (index: number) => void;
145+
};
146+
147+
const OtpInput = forwardRef<OtpInputHandle, IOtpInput>((props, ref) => {
144148
const {
145149
numInputs = 4,
146150
onChange = (otp: string) => console.log(otp),
@@ -183,6 +187,12 @@ const OtpInput = (props: IOtpInput) => {
183187
return '';
184188
}, [placeholder]);
185189

190+
useImperativeHandle(ref, () => {
191+
return {
192+
focusInput,
193+
};
194+
});
195+
186196
// Helper to return OTP from input
187197
const handleOtpChange = (otp: string[]) => {
188198
const otpValue = otp.join('');
@@ -347,6 +357,6 @@ const OtpInput = (props: IOtpInput) => {
347357
})}
348358
</div>
349359
);
350-
};
360+
});
351361

352362
export { OtpInput };

0 commit comments

Comments
 (0)