# OTP Input

The OTP Input component is designed to collect secure, time-sensitive verification codes from users in a clear and accessible way. It emphasises clarity, accuracy, and ease of use across different devices, ensuring a smooth authentication process.

<figure><img src="https://3868804918-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FegsIWleSdyH9rMLJ8ShI%2Fuploads%2Fd9L5RsgzoDLWK8ozHbZ2%2Fimage.png?alt=media&#x26;token=c8fabcab-0918-4a1c-ba12-62a68e4cc5e3" alt=""><figcaption></figcaption></figure>

{% tabs %}
{% tab title="Component React" %}

```
// Sample code

const Component = () => {
const [otp, setOtp] = useState("");

//Example onChange logic
const handleOtpChange = (value) => {
setOtp(value);
if (value.length === args.length) {
const isValid = value.includes(1);
if (isValid) {
console.log("OTP is correct");
return null;
} else {
console.log("Invalid OTP");
return "Invalid OTP";
}
}
return null;
};
return <OTPInput length={6} type="numeric" onChange={handleOtpChange} placeholder={"123456"} label="Enter OTP" inline={false} className="" style={{ }} />;
};
```

{% endtab %}

{% tab title="Component Flutter" %}

```
// Sample code

DigitOTPInput(
          controller: OtpFieldControllerV2(),
          length: 6,
          autoFocus: true,
          width: MediaQuery.of(context).size.width,
          onChanged: (pin) {
            print("Changed: " + pin);
          },
          onCompleted: (pin) {
            print("Completed: " + pin);
          }
),
```

{% endtab %}

{% tab title="Component Design" %}

{% endtab %}
{% endtabs %}

## Anatomy

***

<figure><img src="https://3868804918-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FegsIWleSdyH9rMLJ8ShI%2Fuploads%2FLkmEt75ZW7CyLjNEux5n%2Fimage.png?alt=media&#x26;token=0d2f875c-5bc4-46fb-9936-1c5e0c747d41" alt=""><figcaption></figcaption></figure>

## Variants

***

<table data-header-hidden><thead><tr><th width="321"></th><th></th></tr></thead><tbody><tr><td><div><figure><img src="https://3868804918-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FegsIWleSdyH9rMLJ8ShI%2Fuploads%2F04TjaQhy2gT9jHy3NqDK%2Fimage.png?alt=media&#x26;token=342c9237-0ddd-4d7b-b293-a9c5534ed504" alt=""><figcaption></figcaption></figure></div></td><td><p><strong>4-characters</strong></p><p>Used when the OTP is limited to four digits, offering a compact layout ideal for simpler authentication flows.</p></td></tr><tr><td><div><figure><img src="https://3868804918-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FegsIWleSdyH9rMLJ8ShI%2Fuploads%2FOLyVJtcjmyJostDsDhLy%2Fimage.png?alt=media&#x26;token=a78028a2-1ed9-47ea-b922-f42b45c5a05f" alt=""><figcaption></figcaption></figure></div></td><td><p><strong>6-characters</strong></p><p>Common in more secure flows, this variant provides six input boxes for enhanced verification.</p></td></tr></tbody></table>

## Properties

<table data-header-hidden data-full-width="false"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td><strong>Label</strong><br>Positioned above the input fields (e.g., "Enter OTP"), it provides clear instructions to the user.<br></td><td><div><figure><img src="https://3868804918-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FegsIWleSdyH9rMLJ8ShI%2Fuploads%2FlVB63xMjn1WPNIDZU86f%2Fimage.png?alt=media&#x26;token=57ebddb1-6758-42b1-997e-378b0aa6cf14" alt=""><figcaption></figcaption></figure></div></td></tr><tr><td><strong>Error</strong><br>Displays an error message (e.g., “Invalid OTP”) with a red border and icon to indicate incorrect input.</td><td><div><figure><img src="https://3868804918-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FegsIWleSdyH9rMLJ8ShI%2Fuploads%2FQsAYb6sqEWtEkIm07tFy%2Fimage.png?alt=media&#x26;token=ad9406f8-afe3-4e81-ad9f-4adb97e0a95b" alt=""><figcaption></figcaption></figure></div></td></tr><tr><td><p><strong>Masking</strong></p><p>Optionally replaces typed characters with dots (●) for added security, particularly useful in shared or public environments.</p></td><td><div><figure><img src="https://3868804918-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FegsIWleSdyH9rMLJ8ShI%2Fuploads%2FmYezu1qmNjB0ncKU29jD%2Fimage.png?alt=media&#x26;token=c1926a62-7cc6-4270-91a6-2af5aba43ccc" alt=""><figcaption></figcaption></figure></div></td></tr><tr><td></td><td></td></tr></tbody></table>

## Property Configuration Table

Each design component offers a range of configurable options. These options are intentionally platform-agnostic, allowing implementations to adapt and tailor them to align with the specific requirements of the chosen framework.

{% tabs %}
{% tab title="React" %}

<table><thead><tr><th width="257">Property</th><th>Value</th><th>Default</th></tr></thead><tbody><tr><td>length</td><td>text</td><td>-</td></tr><tr><td>type</td><td>text</td><td>no</td></tr><tr><td>onChange</td><td>yes/no</td><td>no</td></tr><tr><td>placeholder</td><td>yes/no</td><td>-</td></tr><tr><td>className</td><td>number</td><td>-</td></tr><tr><td>style</td><td>yes/no</td><td>-</td></tr><tr><td>label</td><td>yes/no</td><td>no</td></tr><tr><td>inline</td><td>yes/no</td><td>no</td></tr><tr><td>masking</td><td>yes/no</td><td>no</td></tr></tbody></table>
{% endtab %}

{% tab title="Flutter" %}

<table><thead><tr><th>Property</th><th width="209">Value</th><th>Default</th></tr></thead><tbody><tr><td>controller</td><td>OtpFieldControllerV2</td><td>-</td></tr><tr><td>length</td><td>int</td><td>-</td></tr><tr><td>width</td><td>double</td><td>-</td></tr><tr><td>fieldWidth</td><td>double</td><td>-</td></tr><tr><td>spaceBetween</td><td>double</td><td>-</td></tr><tr><td>contentPadding</td><td>EdgeInsets</td><td>-</td></tr><tr><td>keyboardType</td><td>TextInputType</td><td>-</td></tr><tr><td>hasError</td><td>bool</td><td>false</td></tr><tr><td>obscureText</td><td>bool</td><td>false</td></tr><tr><td>isDense</td><td>bool</td><td>false</td></tr><tr><td>onChanged</td><td>ValueChanged&#x3C;String></td><td>-</td></tr><tr><td>onCompleted</td><td>ValueChanged&#x3C;String></td><td>-</td></tr><tr><td>autoFocus</td><td>bool</td><td>true</td></tr><tr><td>errorMessage</td><td>String</td><td>-</td></tr></tbody></table>
{% endtab %}
{% endtabs %}

## Interaction State

***

<table data-header-hidden data-full-width="false"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td><strong>Single Focus</strong><br>In Single Focus mode, only one input box is actively focused at a time. As the user types, the focus automatically moves to the next box, improving usability and reducing the need for manual navigation between fields.<br></td><td><div><figure><img src="https://3868804918-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FegsIWleSdyH9rMLJ8ShI%2Fuploads%2FyiNpPpKM7egKFdyLott8%2Fimage.png?alt=media&#x26;token=308a07f6-30e8-416f-b7ed-187712c161c3" alt=""><figcaption></figcaption></figure></div></td></tr></tbody></table>

## Behaviours

|                                                                                                                                                                                                                                                                                            |                                                                                                                                                        |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| <div><figure><img src="https://3868804918-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FegsIWleSdyH9rMLJ8ShI%2Fuploads%2F6seOhwFDEoFnl3TdAj7M%2Fimage.png?alt=media&#x26;token=3960cb3f-0500-4561-98a1-ac589056ad01" alt=""><figcaption></figcaption></figure></div> | <p><strong>Auto Tabbing</strong></p><p>The cursor automatically jumps to the next field as each digit is entered.</p>                                  |
| <div><figure><img src="https://3868804918-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FegsIWleSdyH9rMLJ8ShI%2Fuploads%2FD8Qi78A83MQHLSZMXSMQ%2Fimage.png?alt=media&#x26;token=02a99be0-b7e6-45ab-89ce-16a4398ba60e" alt=""><figcaption></figcaption></figure></div> | <p><strong>Backspace Navigation</strong></p><p>Pressing backspace in an empty field moves the focus to the previous box, allowing easy correction.</p> |
| <div><figure><img src="https://3868804918-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FegsIWleSdyH9rMLJ8ShI%2Fuploads%2FgDtYh253qQ0uMPhMvgNt%2Fimage.png?alt=media&#x26;token=ec05bb0a-a9de-49b4-8b31-eab21d15b7ad" alt=""><figcaption></figcaption></figure></div> | <p><strong>Paste Handling</strong></p><p>Supports pasting a full OTP string, automatically distributing characters across input fields.</p>            |

***

## Usage Guide

***

| <div><figure><img src="https://3868804918-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FegsIWleSdyH9rMLJ8ShI%2Fuploads%2F2Z7qhfTH8du1Rztb6bGx%2Fimage.png?alt=media&#x26;token=be6d462f-00a0-4c75-8769-1d2dbfc2b1d6" alt=""><figcaption></figcaption></figure></div> | <p><strong>Use Clear Focus State for Better User Guidance</strong></p><p>Ensure that the currently active OTP input field is visually distinct so users can immediately recognise where they need to type next. A proper focus state improves accessibility and enhances the user experience by providing clear feedback on input interactions.  Users should not struggle to identify which input box is currently active. Lack of clear focus indication can result in hesitation or mistakes, causing frustration.</p> |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <div><figure><img src="https://3868804918-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FegsIWleSdyH9rMLJ8ShI%2Fuploads%2FtqI0Id1ULy2ZjCJhRbre%2Fimage.png?alt=media&#x26;token=8dd12ef3-612a-4620-bf4e-deb33aae1cd9" alt=""><figcaption></figcaption></figure></div> |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |

## Changelog

***

| Date         | Number  | Notes                                                                                           |
| ------------ | ------- | ----------------------------------------------------------------------------------------------- |
| Dec 15, 2024 | v-0.0.2 | <p>This component is added to the website.<br>This component is now individually versioned.</p> |

## Design Checklist

***

<table data-header-hidden><thead><tr><th width="129" data-type="checkbox"></th><th></th></tr></thead><tbody><tr><td>true</td><td><strong>All interactive states</strong> - Includes all interactive states that are applicable (hover, down, focus, keyboard focus, disabled).</td></tr><tr><td>true</td><td><strong>Accessible use of colours</strong> - Colour is not used as the only visual means of conveying information (WCAG 2.1 1.4.1)</td></tr><tr><td>true</td><td><strong>Accessible contrast for text</strong> - Text has a contrast ratio of at least 4.5:1 for small text and at least 3:1 for large text (WCAG 2.0 1.4.3).</td></tr><tr><td>true</td><td><strong>Accessible contrast for UI components</strong> - Visual information required to identify components and states (except inactive components) has a contrast ratio of at least 3:1 (WCAG 2.1 1.4.11).</td></tr><tr><td>true</td><td><strong>Keyboard interactions</strong> - Includes all interactive states that are applicable (hover, down, focus, keyboard focus, disabled).</td></tr><tr><td>false</td><td><strong>Screen reader accessible</strong> - All content, including headings, labels, and descriptions, is meaningful, concise, contextual and accessible by screen readers.</td></tr><tr><td>true</td><td><strong>Responsive for all breakpoints</strong> - Responsiveness for 3 breakpoints - Mobile, Tablet and Desktop</td></tr><tr><td>true</td><td><strong>Usage guidelines</strong> - Includes a list of dos and don'ts that highlight best practices and common mistakes.</td></tr><tr><td>false</td><td><strong>Content guidelines</strong> - Content standards and usage guidelines for writing and formatting in-product content for the component.</td></tr><tr><td>true</td><td><strong>Defined variants and properties</strong> - Includes relevant variants and properties (style, size, orientation, optional iconography, decorative elements, selection states, error states, etc.)</td></tr><tr><td>true</td><td><strong>Defined behaviours</strong> - Guidelines for keyboard navigation and focus, layout management (including wrapping, truncation, and overflow), animations, and user interactions.</td></tr><tr><td>true</td><td><strong>Design Kit</strong> - Access to the design file for the component in Figma, multiple options, states, colour themes, and platform scales.</td></tr></tbody></table>
