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

add: new boolean type prop to consider or not consider spaces as valid characters in OTP input #397

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ function App() {
inputType={inputType}
renderInput={(props) => <input {...props} />}
shouldAutoFocus
shouldRemoveWhiteSpaceOnPaste
justrawat marked this conversation as resolved.
Show resolved Hide resolved
/>
</div>
<div className="btn-row">
Expand Down
9 changes: 8 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ interface OTPInputProps {
inputStyle?: React.CSSProperties | string;
/** The type that will be passed to the input being rendered */
inputType?: AllowedInputTypes;
/** Boolean to consider spaces as valid OTP values */
justrawat marked this conversation as resolved.
Show resolved Hide resolved
shouldRemoveWhiteSpaceOnPaste?: boolean;
}

const isStyleObject = (obj: unknown) => typeof obj === 'object' && obj !== null;
Expand All @@ -59,6 +61,7 @@ const OTPInput = ({
placeholder,
containerStyle,
inputStyle,
shouldRemoveWhiteSpaceOnPaste = false,
}: OTPInputProps) => {
const [activeInput, setActiveInput] = React.useState(0);
const inputRefs = React.useRef<Array<HTMLInputElement | null>>([]);
Expand Down Expand Up @@ -184,11 +187,15 @@ const OTPInput = ({
let nextActiveInput = activeInput;

// Get pastedData in an array of max size (num of inputs - current position)
const pastedData = event.clipboardData
let pastedData = event.clipboardData
.getData('text/plain')
.slice(0, numInputs - activeInput)
.split('');
justrawat marked this conversation as resolved.
Show resolved Hide resolved

if(shouldRemoveWhiteSpaceOnPaste) {
pastedData = pastedData.filter((character) => character !== ' ');
}

// Prevent pasting if the clipboard data contains non-numeric values for number inputs
if (isInputNum && pastedData.some((value) => isNaN(Number(value)))) {
return;
Expand Down