# Code Input

A one-time code, one 48 px box per character, over one real input so paste and the phone's code autofill work. Built on ui-sdk's OTP Input. Used for phone verification in onboarding.

- Beta: ready to use; its API can still change in a minor release.
- Category: Form fields

```tsx
import { CodeInput } from "@leap-crm/design-system";
```

## Best practices

- Do: Use it for a one-time code with a fixed length, like the SMS code that verifies a lawyer's phone in onboarding.
- Do: Put it in a `Field` with a label. The field's error turns the boxes red and is announced, so the state isn't shown by colour alone.
- Do: Verify in `onComplete`, which runs once every box is filled, typed or pasted, so nobody has to press a button after pasting the code.
- Do: Write the error as what to do next, like "Please enter the code we sent you", and clear it in `onChange` when they start again.
- Don't: Use it for a matter number, a reference or any value without a fixed length. Use `Input`.
- Don't: Build the boxes from separate inputs. It sits over one real input so paste and the phone's code autofill work.

## Keyboard

| Keys | What they do |
| --- | --- |
| Typing | Fills the slots in order. |
| Backspace | Deletes the last character. |
| ArrowLeft | Goes back a slot; typing then replaces its character. |

## Related

- [Input](./input.md)
- [Field](./field.md)

## Examples

What the examples import:

```tsx
import { useState } from "react";
import { CodeInput, Field } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
function VerifyDemo() {
  const [error, setError] = useState<string | undefined>(undefined);
  return (
    <Field
      label="Verification code"
      hint="Try 123456. Anything else is wrong."
      error={error}
    >
      <CodeInput
        onChange={() => setError(undefined)}
        onComplete={code =>
          setError(
            code === "123456" ? undefined : "Please enter the code we sent you",
          )
        }
      />
    </Field>
  );
}
```

### In a field

Type, paste or let the phone fill it. onComplete runs once every box is filled; a wrong code shows the error under it and turns the boxes red.

```tsx
<VerifyDemo />
```

### States

Empty, part-filled, invalid and disabled. Click one to see the active box.

```tsx
<div className="flex flex-col gap-4">
  <CodeInput aria-label="Empty code" />
  <CodeInput aria-label="Part-filled code" defaultValue="427" />
  <CodeInput aria-label="Invalid code" defaultValue="427" isInvalid />
  <CodeInput aria-label="Disabled code" disabled />
</div>
```

### Length

Four boxes for a PIN.

```tsx
<CodeInput aria-label="PIN" length={4} />
```

## Props

Also takes ui-sdk's OTP Input options: value, defaultValue, pattern (digits by default), inputMode, autoComplete, name, disabled, id and autoFocus.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `length` | `number` | `6` | How many characters the code has, one box each. |
| `onChange` | `(value: string) => void` |  | Runs on every change with the code so far. |
| `onComplete` | `(value: string) => void` |  | Runs once every box is filled, typed or pasted, like to verify it. |
| `isInvalid` | `boolean` |  | Draws every box red, and the active one with the red ring. Inside a Field, its error does this. |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |
