Skip to content

Commit 6cda5e3

Browse files
author
ugogo
committed
support pasted string when numbers
1 parent 3c7e93e commit 6cda5e3

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/index.tsx

+18
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,24 @@ const ReactInputVerificationCode = ({
111111
setActiveIndex(-1);
112112
};
113113

114+
React.useEffect(() => {
115+
const codeInput = codeInputRef.current;
116+
if (!codeInput) return;
117+
118+
const onPaste = (e: ClipboardEvent) => {
119+
e.preventDefault();
120+
121+
const pastedString = e.clipboardData?.getData('text');
122+
if (!pastedString) return;
123+
124+
const isNumber = !Number.isNaN(+pastedString);
125+
if (isNumber) setValue(pastedString.split(''));
126+
};
127+
128+
codeInput.addEventListener('paste', onPaste);
129+
return () => codeInput.removeEventListener('paste', onPaste);
130+
}, []);
131+
114132
React.useEffect(() => {
115133
onChange(value.join(''));
116134
}, [value]);

0 commit comments

Comments
 (0)