Skip to content

Commit 6084e4a

Browse files
sdominoprateek3255
andauthored
Support onPaste event handler (#415)
* 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]>
1 parent bb9dbc2 commit 6084e4a

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Diff for: README.md

+16
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ export default function App() {
8080
<td>console.log</td>
8181
<td>Returns OTP code typed in inputs.</td>
8282
</tr>
83+
<tr>
84+
<td>onPaste</td>
85+
<td>function</td>
86+
<td>false</td>
87+
<td>none</td>
88+
<td>Provide a custom onPaste event handler scoped to the OTP inputs container. Executes when content is pasted into any OTP field.
89+
</br></br>
90+
Example:
91+
<pre>
92+
const handlePaste: React.ClipboardEventHandler<HTMLDivElement> = (event) => {
93+
const data = event.clipboardData.getData('text');
94+
console.log(data)
95+
};</pre>
96+
97+
</td>
98+
</tr>
8399
<tr>
84100
<td>value</td>
85101
<td>string / number</td>

Diff for: src/index.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ interface OTPInputProps {
3232
numInputs?: number;
3333
/** Callback to be called when the OTP value changes */
3434
onChange: (otp: string) => void;
35+
/** Callback to be called when pasting content into the component */
36+
onPaste?: (event: React.ClipboardEvent<HTMLDivElement>) => void;
3537
/** Function to render the input */
3638
renderInput: (inputProps: InputProps, index: number) => React.ReactNode;
3739
/** Whether the first input should be auto focused */
@@ -54,6 +56,7 @@ const OTPInput = ({
5456
value = '',
5557
numInputs = 4,
5658
onChange,
59+
onPaste,
5760
renderInput,
5861
shouldAutoFocus = false,
5962
inputType = 'text',
@@ -217,6 +220,7 @@ const OTPInput = ({
217220
<div
218221
style={Object.assign({ display: 'flex', alignItems: 'center' }, isStyleObject(containerStyle) && containerStyle)}
219222
className={typeof containerStyle === 'string' ? containerStyle : undefined}
223+
onPaste={onPaste}
220224
>
221225
{Array.from({ length: numInputs }, (_, index) => index).map((index) => (
222226
<React.Fragment key={index}>

0 commit comments

Comments
 (0)