react-native-otp-entry

repository·main·Indexed 19 days ago

https://github.com/anday013/react-native-otp-entry

A highly customizable OTP (One-Time Password) input component for React Native, supporting iOS, Android, and Web. Version 1.8.6 features autofill support, paste functionality, and full TypeScript coverage. It provides the OtpInput component with configurable props for digit count, input types (alpha, numeric, alphanumeric), and secure text entry, as well as a theme object for deep visual customization and a ref-based API for programmatic control of focus and values.

Tokens
4.2K
Snippets
13
Records
19
Agent score
63%

What's inside react-native-otp-entry

  1. Customize OtpInput with the theme prop

    main

    Use the theme prop to pass a Theme object containing ViewStyle and TextStyle objects for deep customization of the component's parts:

    • containerStyle: Root View.
    • pinCodeContainerStyle: Container wrapping each individual digit.
    • pinCodeTextStyle: Text within each individual digit.
    • placeholderTextStyle: Placeholder text style.
    • focusStickStyle: The focus stick (cursor) style.
    • focusedPinCodeContainerStyle: Container style when focused.
    • filledPinCodeContainerStyle: Container style when it has a value.
    • disabledPinCodeContainerStyle: Container style when disabled.

    Tip: If you have trouble applying gap or spacing between containers, set containerStyle.width to 'auto' or undefined, as it defaults to '100%'.

    <OtpInput
      numberOfDigits={6}
      theme={{
        containerStyle: styles.container,
        pinCodeContainerStyle: styles.pinCodeContainer,
        pinCodeTextStyle: styles.pinCodeText,
        focusStickStyle: styles.focusStick,
        focusedPinCodeContainerStyle: styles.activePinCodeContainer,
        placeholderTextStyle: styles.placeholderText,
        filledPinCodeContainerStyle: styles.filledPinCodeContainer,
        disabledPinCodeContainerStyle: styles.disabledPinCodeContainer,
      }}
    />
  2. Basic usage of OtpInput

    main

    To use the component, import OtpInput from react-native-otp-entry and specify the numberOfDigits. You can use onTextChange to track the input value.

    import { OtpInput } from "react-native-otp-entry";
    
    <OtpInput 
      numberOfDigits={6} 
      onTextChange={(text) => console.log(text)} 
    />
  3. Configure OtpInput props

    main

    The OtpInput component accepts several props to control its behavior and appearance:

    • numberOfDigits (number): The number of digits to be displayed.
    • autoFocus (boolean): Sets autofocus. Defaults to true.
    • focusColor (ColorValue): Color of the input field border and stick when focused.
    • placeholder (string): Placeholder value.
    • onTextChange ((text: string) => void): Callback when text changes.
    • onFilled ((text: string) => void): Callback when the input is fully filled.
    • blurOnFilled (boolean): Blurs the input when fully filled. Defaults to false.
    • hideStick (boolean): Hides the cursor of the focused input. Defaults to false.
    • focusStickBlinkingDuration (number): Blinking duration for the focus stick in ms.
    • disabled (boolean): Disables the input. Defaults to false.
    • type ('alpha' | 'numeric' | 'alphanumeric'): Input type restriction.
    • secureTextEntry (boolean): Obscures text (e.g., for PINs). Defaults to false.
    • onFocus ((): void): Callback when focused.
    • onBlur ((): void): Callback when blurred.
    • textInputProps (TextInputProps): Extra props for the underlying hidden TextInput.
    • textProps (TextProps): Props for the Text component rendering each digit.
  4. Customize styles using the theme object

    main

    The theme prop allows you to override the default styles for various parts of the component. This is the preferred way to apply custom styling to the container and individual cells.

    Available theme keys:

    • containerStyle: Style for the outer wrapper.
    • inputsContainerStyle: Style for the container holding the input cells.
    • pinCodeContainerStyle: Base style for each individual input cell.
    • pinCodeTextStyle: Base style for the text inside the cells.
    • focusStickStyle: Style for the blinking vertical cursor.
    • focusedPinCodeContainerStyle: Style applied to a cell when it is focused.
    • filledPinCodeContainerStyle: Style applied to a cell when it contains a character.
    • disabledPinCodeContainerStyle: Style applied to cells when disabled is true.
    • placeholderTextStyle: Style for the placeholder text.
    <OtpInput
      theme={{
        pinCodeContainerStyle: { borderRadius: 8 },
        pinCodeTextStyle: { fontSize: 20, fontWeight: 'bold' },
        focusedPinCodeContainerStyle: { borderColor: 'blue' },
        filledPinCodeContainerStyle: { backgroundColor: '#f0f0f0' }
      }}
    />
  5. Customize the OTPInput appearance with Theme

    main

    The Theme interface allows for deep customization of the component's visual styles. Use the theme prop to pass a Theme object containing:

    • containerStyle (ViewStyle): Styles for the root View. (Note: inputsContainerStyle is deprecated; use containerStyle instead).
    • pinCodeContainerStyle (ViewStyle): Styles for the container wrapping each individual digit.
    • pinCodeTextStyle (TextStyle): Styles for the text within each digit.
    • focusStickStyle (ViewStyle): Styles for the focus stick (the indicator of the focused field).
    • filledPinCodeContainerStyle (ViewStyle): Styles for the container of a filled digit.
    • focusedPinCodeContainerStyle (ViewStyle): Styles for the container of the currently focused digit.
    • disabledPinCodeContainerStyle (ViewStyle): Styles for the container of a disabled digit.
    • placeholderTextStyle (TextStyle): Styles for the placeholder text.
    const customTheme: Theme = {
      containerStyle: { marginTop: 20 },
      pinCodeContainerStyle: { borderWidth: 1, borderRadius: 4 },
      pinCodeTextStyle: { fontSize: 18, color: 'black' },
      focusedPinCodeContainerStyle: { borderColor: 'blue' },
      filledPinCodeContainerStyle: { backgroundColor: '#eee' },
    };
    
    <OTPInput
      numberOfDigits={6}
      theme={customTheme}
    />
  6. Customize the component appearance with Theme

    main

    The Theme interface allows you to customize the styles of various parts of the OTP input, including the container, individual pin code containers, text styles, and the focus stick. Note that inputsContainerStyle is deprecated in favor of containerStyle.

    interface Theme {
      containerStyle?: ViewStyle;
      /**
       * @deprecated Use `containerStyle` instead
       */
      inputsContainerStyle?: ViewStyle;
      pinCodeContainerStyle?: ViewStyle;
      filledPinCodeContainerStyle?: ViewStyle;
      pinCodeTextStyle?: TextStyle;
      focusStickStyle?: ViewStyle;
      focusedPinCodeContainerStyle?: ViewStyle;
      disabledPinCodeContainerStyle?: ViewStyle;
      placeholderTextStyle?: TextStyle;
    }
  7. Import OtpInput and its types

    main

    The react-native-otp-entry package exports the main OtpInput component and its associated type definitions. You can import everything needed for implementation from the root package entry point.

    import OtpInput from 'react-native-otp-entry';
    import { OtpInputProps } from 'react-native-otp-entry';
  8. Use the OtpInput component

    main

    The OtpInput component provides a specialized UI for entering One-Time Passwords (OTP). It renders a series of input cells and manages a hidden TextInput to handle keyboard interactions, focus, and text entry.

    Key features include:

    • Automatic focus management and blinking cursor (stick).
    • Support for numeric or text input types.
    • Secure text entry (masking characters with dots).
    • Custom styling via a theme object.
    • Accessibility support with textContentType="oneTimeCode" and autoComplete settings.
    import { OtpInput } from 'react-native-otp-entry';
    
    // Basic usage
    <OtpInput 
      numberOfDigits={6} 
      onTextChange={(text) => console.log(text)} 
    />
  9. Configure OtpInputProps

    main

    The OtpInputProps interface defines the configuration for the OtpInput component. Use these props to control the number of digits, input type, focus behavior, and event callbacks.

    interface OtpInputProps {
      numberOfDigits?: number;
      autoFocus?: boolean;
      focusColor?: ColorValue;
      onTextChange?: (text: string) => void;
      onFilled?: (text: string) => void;
      onFocus?: () => void;
      onBlur?: () => void;
      blurOnFilled?: boolean;
      hideStick?: boolean;
      focusStickBlinkingDuration?: number;
      secureTextEntry?: boolean;
      theme?: Theme;
      disabled?: boolean;
      textInputProps?: TextInputProps;
      textProps?: TextProps;
      type?: "alpha" | "numeric" | "alphanumeric";
      placeholder?: string;
    }
  10. Control OtpInput using a ref

    main

    You can interact with the OtpInput programmatically using a ref. The component exposes the following methods via useImperativeHandle:

    • clear(): Clears the current input value.
    • focus(): Sets focus to the input.
    • setValue(value: string): Programmatically sets the input value.
    • blur(): Removes focus from the input.
    import React, { useRef } from 'react';
    import { OtpInput, OtpInputRef } from 'react-native-otp-entry';
    
    const MyComponent = () => {
      const otpRef = useRef<OtpInputRef>(null);
    
      const handleReset = () => {
        otpRef.current?.clear();
      };
    
      return (
        <>
          <OtpInput ref={otpRef} />
          <Button title="Reset" onPress={handleReset} />
        </>
      );
    };