Input OTP
The InputOtp component allows users to enter one-time passcodes. It provides a series of input boxes that automatically handle focus and input.
Installation
$ cbui-cli install @crossbuildui/inputotp
cbui-cli
globally and authenticated your account. This will ensure you can access all the necessary features.Import
1import { InputOtp } from '@/crossbuildui/inputotp';
InputOtp Component
Basic Usage
Specify the length
of the OTP.
1<InputOtp length={6} />
Variants
InputOtp supports multiple visual variant
options for the input boxes: flat
, bordered
(default), faded
, and underlined
.
1<View style={{gap: 16}}>
2 <InputOtp length={4} variant="flat" />
3 <InputOtp length={4} variant="bordered" /> {/* Default */}
4 <InputOtp length={4} variant="faded" />
5 <InputOtp length={4} variant="underlined" />
6</View>
Sizes
Adjust input box size with sm
, md
(default), or lg
.
1<View style={{gap: 16, alignItems: 'center'}}>
2 <InputOtp length={4} size="sm" />
3 <InputOtp length={4} size="md" /> {/* Default */}
4 <InputOtp length={4} size="lg" />
5</View>
Radius
Control the border radius of input boxes with none
, sm
, md
(default), lg
, or full
.
1<View style={{gap: 16, alignItems: 'center'}}>
2 <InputOtp length={4} radius="none" />
3 <InputOtp length={4} radius="sm" />
4 <InputOtp length={4} radius="md" /> {/* Default */}
5 <InputOtp length={4} radius="lg" />
6 <InputOtp length={4} radius="full" />
7</View>
Colors
The color
prop influences the input box appearance on focus or when invalid. Supported colors include primary
, secondary
, success
, warning
, danger
, and default
.
1<View style={{gap: 16, alignItems: 'center'}}>
2 <InputOtp length={4} color="primary" placeholder="P" />
3 <InputOtp length={4} color="secondary" placeholder="S" />
4 <InputOtp length={4} color="success" placeholder="S" />
5 <InputOtp length={4} color="warning" placeholder="W" />
6 <InputOtp length={4} color="danger" isInvalid errorMessage="Invalid OTP" />
7 <InputOtp length={4} color="default" placeholder="D" />
8</View>
Description and Error Message
Provide helpful context with description
or display validation feedback using isInvalid
and errorMessage
.
1<View style={{gap: 16}}>
2 <InputOtp
3 length={6}
4 description="Enter the 6-digit code sent to your phone."
5 />
6 <InputOtp
7 length={4}
8 isInvalid
9 errorMessage="The OTP entered is incorrect. Please try again."
10 value="123" // Example value to show error state
11 />
12</View>
Controlled and Uncontrolled
Manage OTP state externally using value
and onValueChange
for a controlled component, or let it manage its own state (uncontrolled).
1// Controlled InputOtp
2function ControlledInputOtpExample() {
3 const [otp, setOtp] = React.useState('');
4
5 return (
6 <View style={{gap: 8}}>
7 <InputOtp
8 length={4}
9 value={otp}
10 onValueChange={setOtp}
11 placeholder="C"
12 />
13 <Text>Current OTP: {otp}</Text>
14 </View>
15 );
16}
17
18// Uncontrolled InputOtp (uses internal state)
19// <InputOtp length={4} placeholder="U" />
20
Disabled State
Disable the OTP input using the disabled
prop.
1<InputOtp length={4} disabled value="1234" />
Placeholder
Set a placeholder
character for empty input boxes and customize its color with placeholderTextColor
.
1<View style={{gap: 16}}>
2 <InputOtp length={4} placeholder="0" />
3 <InputOtp length={4} placeholder="*" placeholderTextColor="blue" />
4</View>
Custom TextInput Props
Pass additional props to the underlying TextInput
components using textInputProps
. This is useful for settings like keyboardType
, autoFocus
on the first input, etc.
1<InputOtp
2 length={6}
3 textInputProps={{
4 keyboardType: 'default', // Default is 'number-pad'
5 // You can pass other TextInput props like autoFocus on the first input, etc.
6 // Note: some props like 'value', 'onChangeText', 'maxLength' are controlled by InputOtp
7 }}
8 description="Uses 'default' keyboard type."
9/>
Props Overview
PROP | TYPE | DEFAULT | DESCRIPTION |
---|---|---|---|
length | number | - | Number of OTP input boxes. Required. |
variant | InputOtpVariant | 'bordered' | Visual style. |
color | InputOtpColor | 'default' | Thematic color on focus/invalid. |
size | InputOtpSize | 'md' | Size of input boxes. |
radius | InputOtpRadius | 'md' | Border radius of boxes. |
value | string | - | Controlled value of the OTP. |
onValueChange | (otp: string) => void | - | Callback when OTP value changes. |
description | ReactNode | - | Helper text below the OTP input. |
errorMessage | ReactNode | - | Error message when isInvalid . |
isInvalid | boolean | false | Marks input as invalid. |
styles | InputOtpSlotsStyles | - | Custom styles for slots (base, otpInputWrapper, etc.). |
style | StyleProp<ViewStyle> | - | Style for the outermost container (styles.base equivalent). |
disabled | boolean | false | Disable the input. |
placeholder | string | - | Placeholder character for each box. |
placeholderTextColor | ColorValue | - | Custom color for placeholder text. |
textInputProps | Omit<TextInputProps, ...> | - | Props for underlying TextInput components. |
Styling
Customize the InputOtp appearance using the style
prop for the main wrapper or the styles
prop for more granular control over internal slots:
style
: Applied to the outermost container (View
) of the entire component. Equivalent tostyles.base
.styles.base
: Styles for the outermost container.styles.otpInputWrapper
: Styles for theView
that wraps all the individual OTP input boxes.styles.otpInput
: Styles for each individual OTPTextInput
box.styles.description
: Styles for the descriptionText
component.styles.errorMessage
: Styles for the error messageText
component.
1<InputOtp
2 length={4}
3 value="STYL"
4 style={{ marginVertical: 20, padding: 10, backgroundColor: '#e6e6fa' }} // Styles the outermost 'base' container
5 styles={{
6 base: { borderWidth: 1, borderColor: 'purple' },
7 otpInputWrapper: { columnGap: 20 }, // Increased gap between boxes
8 otpInput: {
9 backgroundColor: 'lightcyan',
10 borderColor: 'teal',
11 borderWidth: 2,
12 color: 'darkgreen',
13 fontWeight: 'bold',
14 },
15 description: { color: 'navy', fontStyle: 'italic', marginTop: 10 },
16 errorMessage: { color: 'darkred', fontWeight: 'bold', marginTop: 10 }
17 }}
18 description="This OTP input has custom slot styling."
19/>
Accessibility
The InputOtp component includes features to aid accessibility:
- Each input box has
textContentType="oneTimeCode"
(iOS) andautoComplete="sms-otp"
(Android) /"one-time-code"
(web) to help with autofill from SMS. - Keyboard type defaults to
"number-pad"
for easier numeric input. - Consider providing an overall
accessibilityLabel
for the component if the context isn't clear from surrounding elements, especially if there's no visible description.