Skip to content

Commit

Permalink
Support onPaste event handler (#415)
Browse files Browse the repository at this point in the history
* Adds onPaste handler

* Updates README.md

* Updates event type

* Update readme

* Update README.md

Co-authored-by: Prateek Surana <[email protected]>

* Update src/index.tsx

Co-authored-by: Prateek Surana <[email protected]>

---------

Co-authored-by: Prateek Surana <[email protected]>
  • Loading branch information
sdomino and prateek3255 authored Sep 26, 2023
1 parent bb9dbc2 commit 6084e4a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
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

0 comments on commit 6084e4a

Please sign in to comment.