Skip to main content

API

const { 
register,
setValue,
setDisabled,
clear,
value,
inputs,
error,
setError
} = useOtpInput<T>(options); // T can be HTMLTextAreaElement or HTMLInputElement;

Options

NameValuesTypeDefaultDescription
type"numeric", "alphanumeric", "password", "password-numeric"string"alphanumeric"Defines the type of the otp input
onInputValueChange(value) => {}(val: string) => void...Change listener to listen to the latest value
blankAllowedtrue , falsebooleanfalseSpacebar will be allowed if set to true
focusOnLoadtrue , falsebooleanfalseFocuses the first input on load
defaultInlineStyles...CSSProperties{}any css style object
cycletrue, falsebooleanfalseWill cycle back to first input if key is pressed on the last input
placeholder...string''Placeholder value for every input
numberOfInputs...numberundefinedNeed to provide this option in order to use the dynaic inputs return value
autoCompleteAttribute"on" , "off"string"off"Adds the autocomplete attribute

Types

interface InputOptions {
required?: boolean;
maxLength?: number;
}

type RegisterReturn<T> = {
autoComplete: string;
"aria-label": string;
name: string;
onKeyDown: (e: React.KeyboardEvent<T>) => void;
onInput: (e: React.FormEvent<T>) => void;
onKeyUp: (e: React.KeyboardEvent<T>) => void;
onFocus: (e: React.FocusEvent<T>) => void;
ref: (fieldRef: T) => void;
style: React.CSSProperties | undefined;
placeholder: string;
onPaste: (e: ClipboardEvent<T>) => void;
}

Returned values

NameTypeDescription
register(name: string, options?: InputOptions) => RegisterReturn<T>Used to register an input, can be provided maxLength and required as props
setValue(val: string | number) => voidUsed to set the value for all inputs
setDisabled(disabled: boolean) => voidDisables all the inputs
clear() => voidClears all inputs
valuestring | numberProvides the value of the otp input
inputsRegisterReturn<T>[]Returns the inputs based on the value provided in numberOfInputs. Can be used to set the inputs in a loop instead of registering it one by one
errorstringGet an error string
setErrorReact.Dispatch<React.SetStateAction<string>>Set an error string