# Input

A one-line text field: xs 28, sm 32, md 36 (forms) and lg 40. Built on ui-sdk's Input and InputGroup, so a press on an affix focuses the field, and it wires itself to the Field around it.

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

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

## Best practices

- Do: Put it in a `Field` for its label, hint and error. Alone, like a search in a table toolbar, name it with `aria-label`.
- Do: Use `prefix` and `suffix` for units, like `$` before a fee or `%` after a discount, so the person types only the number.
- Do: Use `validation` for a check that takes a moment, like whether an email is already on a contact, and put what to do in the `Field`'s `error`. The cross alone doesn't say what's wrong.
- Do: Set `copyable` on a value people paste elsewhere, like a trust account reference, and `clearable` on a search or filter.
- Don't: Use it for several lines of text, like a file note. Use `Textarea`.
- Don't: Use it for a date or a one-time code. Use `DateInput` or `DatePicker` for a date and `CodeInput` for a code.

## Related

- [Field](./field.md)
- [Textarea](./textarea.md)
- [Combobox](./combobox.md)
- [Code Input](./code-input.md)

## Examples

What the examples import:

```tsx
import { Fragment, useEffect, useState } from "react";
import { Field, Input, type InputValidation } from "@leap-crm/design-system";
```

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

```tsx
const taken = new Set(["sam@citizen.com.au", "info@citizen.com.au"]);

function CheckedEmail() {
  const [email, setEmail] = useState("jane@citizen.com.au");
  const [validation, setValidation] = useState<InputValidation>();

  useEffect(() => {
    if (!email.includes("@")) return undefined;
    const typing = setTimeout(() => setValidation("checking"), 400);
    const answer = setTimeout(
      () => setValidation(taken.has(email) ? "invalid" : "valid"),
      1400,
    );
    return () => {
      clearTimeout(typing);
      clearTimeout(answer);
    };
  }, [email]);

  const change = (value: string) => {
    setEmail(value);
    setValidation(undefined);
  };

  return (
    <div className="w-80">
      <Field
        label="Email"
        error={
          validation === "invalid"
            ? "Another contact already uses this email"
            : undefined
        }
      >
        <Input value={email} onValueChange={change} validation={validation} />
      </Field>
    </div>
  );
}

const sizes = ["xs", "sm", "md", "lg"] as const;
```

### Sizes

md is the form size. On a touch screen the text grows to 16 px so iOS doesn't zoom in.

```tsx
<div className="grid w-80 grid-cols-[auto_1fr] items-center gap-x-4 gap-y-3">
  {sizes.map(size => (
    <Fragment key={size}>
      <span className="text-small text-neutral-text-muted">{size}</span>
      <Input
        size={size}
        aria-label={`Full name, ${size}`}
        defaultValue="Jane Citizen"
      />
    </Fragment>
  ))}
</div>
```

### Checked as you type

validation shows a check on the value at the end of the field. Checking spins (for at least one pass), then the spinner turns into a tick that draws itself in, or a cross while the Field's error fades in beneath without moving anything. Its space opens and closes on the resize curve, so a suffix slides instead of jumping. Try sam@citizen.com.au, then any other email.

```tsx
<CheckedEmail />
```

### Affix

A muted prefix or suffix inside the edge, 8 px from the value, like a currency or a percent. Values are right-aligned only in tables.

```tsx
<div className="flex w-56 flex-col gap-3">
  <Input
    aria-label="Discount"
    defaultValue="12.5"
    suffix="%"
    inputMode="decimal"
  />
  <Input
    aria-label="Fee"
    defaultValue="4,400.00"
    prefix="$"
    inputMode="decimal"
  />
</div>
```

### Clear and copy

clearable shows a × while the field holds text; it empties the field and keeps focus there. copyable copies the value; its icon turns into a tick that draws itself in and says Copied, or a cross if the clipboard refuses, then turns back.

```tsx
<div className="flex w-64 flex-col gap-3">
  <Input
    aria-label="Search contacts"
    defaultValue="Citizen"
    clearable
  />
  <Input
    aria-label="Matter number"
    defaultValue="CRM/2041"
    readOnly
    copyable
  />
</div>
```

### States

Hover darkens the edge; Tab in to see focus, blue or red when invalid. Read-only takes the subtle surface, disabled dims to 50%.

```tsx
<div className="grid w-full max-w-2xl grid-cols-2 gap-x-6 gap-y-6">
  <Field label="Default">
    <Input defaultValue="Jane Citizen" />
  </Field>
  <Field label="Error" error="Please enter a full name">
    <Input defaultValue="J" />
  </Field>
  <Field label="Read-only">
    <Input defaultValue="Jane Citizen" readOnly />
  </Field>
  <Field label="Disabled" disabled>
    <Input defaultValue="Jane Citizen" />
  </Field>
</div>
```

## Props

Also takes every native input prop (type, placeholder, required, readOnly, disabled, ref, onChange…) and ui-sdk's value, defaultValue and onValueChange.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `size` | `TextSize` | `"md"` | xs 28, sm 32, md 36 (forms, the default), lg 40. On touch screens the text is 16 px, so iOS doesn't zoom. |
| `prefix` | `ReactNode` |  | Muted text before the value, like a currency sign. |
| `suffix` | `ReactNode` |  | Muted text after the value, like a percent sign. |
| `clearable` | `boolean` | `false` | Shows a × that empties the field while it holds text. |
| `copyable` | `boolean` | `false` | Shows a button that copies the value. |
| `validation` | `"checking" \| "valid" \| "invalid"` |  | A check run on the value, like whether an email is taken: checking spins at the end of the field, then turns into a tick (valid) or a cross (invalid) that draws itself in. Put the message in the Field's error. |
| `className` | `string` |  | Classes for the box around the text, which draws the edge and the states. |
