Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add onBlur and onFocus callback support #436

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ A fully customizable, one-time password input component for the web built with R
![see here](https://media.giphy.com/media/lN98dFU6h3oP0wWS5x/giphy.gif)

[Live Demo](https://devfolioco.github.io/react-otp-input)
<!--

<!--
[CodeSandbox](https://codesandbox.io/s/react-otp-input-demo-v2-1iy52) -->

## Installation
Expand All @@ -24,6 +25,7 @@ npm install --save react-otp-input
```

### Still using v2?

No problem! You can find the documentation for v2 [here](https://github.com/devfolioco/react-otp-input/tree/v2.4.0)

#### Basic usage:
Expand Down Expand Up @@ -73,6 +75,13 @@ export default function App() {
The function will get two arguments: <code>inputProps</code> and <code>index</code>. <code>inputProps</code> is an object that contains all the props <b>that should be passed to the input being rendered</b> (Overriding these props is not recommended because it might lead to some unexpected behaviour). <code>index</code> is the index of the input being rendered.
</td>
</tr>
<tr>
<td>onFocus</td>
<td>function</td>
<td>false</td>
<td>none</td>
<td>Called when OTP has received focus. The <code>index</code> of the input gaining focus is passed as the first argument</td>
</tr>
<tr>
<td>onChange</td>
<td>function</td>
Expand All @@ -96,6 +105,13 @@ const handlePaste: React.ClipboardEventHandler<HTMLDivElement> = (event) => {

</td>
</tr>
<tr>
<td>onBlur</td>
<td>function</td>
<td>false</td>
<td>none</td>
<td>Called when OTP has lost focus.</td>
</tr>
<tr>
<td>value</td>
<td>string / number</td>
Expand Down Expand Up @@ -155,7 +171,9 @@ const handlePaste: React.ClipboardEventHandler<HTMLDivElement> = (event) => {
</table>

### ⚠️ Warning

Do not override the following props on the input component that you return from the `renderInput` prop. Doing so might lead to unexpected behaviour.

- `ref`
- `value`
- `onChange`
Expand Down
2 changes: 2 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ function App() {
<OTPInput
inputStyle="inputStyle"
numInputs={numInputs}
onFocus={(index) => console.log(`OTP: Input #${index + 1} has gained focus!`)}
onChange={handleOTPChange}
onBlur={(index) => console.log(`OTP: Input #${index + 1} has lost focus!`)}
renderSeparator={<span>{separator}</span>}
value={otp}
placeholder={placeholder}
Expand Down
17 changes: 15 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ interface OTPInputProps {
value?: string;
/** Number of OTP inputs to be rendered */
numInputs?: number;
/** Callback to be called when the OTP has received focued */
onFocus?: (index: number) => void;
/** Callback to be called when the OTP value changes */
onChange: (otp: string) => void;
/** Callback to be called when pasting content into the component */
onPaste?: (event: React.ClipboardEvent<HTMLDivElement>) => void;
/** Callback to be called when the OTP has lost focus */
onBlur?: (index: number) => void;
/** Function to render the input */
renderInput: (inputProps: InputProps, index: number) => React.ReactNode;
/** Whether the first input should be auto focused */
Expand All @@ -56,8 +60,10 @@ const isStyleObject = (obj: unknown) => typeof obj === 'object' && obj !== null;
const OTPInput = ({
value = '',
numInputs = 4,
onFocus,
onChange,
onPaste,
onBlur,
renderInput,
shouldAutoFocus = false,
inputType = 'text',
Expand Down Expand Up @@ -145,10 +151,17 @@ const OTPInput = ({
const handleFocus = (event: React.FocusEvent<HTMLInputElement>) => (index: number) => {
setActiveInput(index);
event.target.select();

if (!inputRefs.current.includes(event.relatedTarget as HTMLInputElement)) {
onFocus?.(index);
}
};

const handleBlur = () => {
const handleBlur = (event: React.FocusEvent<HTMLInputElement>) => (index: number) => {
setActiveInput(activeInput - 1);
if (!inputRefs.current.includes(event.relatedTarget as HTMLInputElement)) {
onBlur?.(index);
}
};

const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -247,7 +260,7 @@ const OTPInput = ({
ref: (element) => (inputRefs.current[index] = element),
onChange: handleChange,
onFocus: (event) => handleFocus(event)(index),
onBlur: handleBlur,
onBlur: (event) => handleBlur(event)(index),
onKeyDown: handleKeyDown,
onPaste: handlePaste,
autoComplete: 'off',
Expand Down