Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ export default function App() {
<td>console.log</td>
<td>Returns OTP code typed in inputs.</td>
</tr>
<tr>
<td>onPaste</td>
<td>function</td>
<td>false</td>
<td>none</td>
<td>Provide a custom onPaste event handler scoped to the OTP inputs container. Executes when content is pasted into any OTP field.
</br></br>
Example:
<pre>
const handlePaste: React.ClipboardEventHandler<HTMLDivElement> = (event) => {
const data = event.clipboardData.getData('text');
console.log(data)
};</pre>

</td>
</tr>
<tr>
<td>value</td>
<td>string / number</td>
Expand Down
4 changes: 4 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ interface OTPInputProps {
numInputs?: number;
/** 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;
/** Function to render the input */
renderInput: (inputProps: InputProps, index: number) => React.ReactNode;
/** Whether the first input should be auto focused */
Expand All @@ -54,6 +56,7 @@ const OTPInput = ({
value = '',
numInputs = 4,
onChange,
onPaste,
renderInput,
shouldAutoFocus = false,
inputType = 'text',
Expand Down Expand Up @@ -217,6 +220,7 @@ const OTPInput = ({
<div
style={Object.assign({ display: 'flex', alignItems: 'center' }, isStyleObject(containerStyle) && containerStyle)}
className={typeof containerStyle === 'string' ? containerStyle : undefined}
onPaste={onPaste}
>
{Array.from({ length: numInputs }, (_, index) => index).map((index) => (
<React.Fragment key={index}>
Expand Down