# Input

A text input whose value you can control: a native <input> with value, defaultValue and onValueChange. Controlled, it shows your value and nothing else: an edit you decline is undone, a form reset asks you, and nothing is written while an input method composes. Uncontrolled, the value stays in the input, and a reset puts defaultValue back. In a form it submits its text under its name, and the browser validates it.

- Entry point: `menhir/input`
- Size: 0.9 kB, minified and gzipped, Solid left out

> To add Input to a project, install the packages below with the project's package manager, then copy the example that matches how the project is styled, Tailwind CSS or plain CSS, and adapt its markup and class names to the project.

## Install

```sh
pnpm add menhir solid-js @solidjs/web
```

## Anatomy

```tsx
<Input.Root />
```

## Example

### Tailwind CSS

`InputExample.tsx`:

```tsx
import { createSignal } from "solid-js";
import { Input } from "menhir/input";

export default function InputExample() {
  const [username, setUsername] = createSignal("ada");
  return (
    <div class="flex flex-col items-start gap-1">
      <label for="username" class="text-sm font-medium">
        Username
      </label>
      <Input.Root
        id="username"
        name="username"
        value={username()}
        // Lower case, and no spaces: a space typed is undone.
        onValueChange={(value) =>
          setUsername(value.toLowerCase().replace(/\s/g, ""))
        }
        class="rounded-md border border-stone-300 bg-transparent px-3 py-1.5 dark:border-stone-700"
      />
      <p class="text-sm text-stone-500 dark:text-stone-400">
        Your page: example.com/@{username()}
      </p>
    </div>
  );
}
```

### CSS

`InputExample.tsx`:

```tsx
import { createSignal } from "solid-js";
import { Input } from "menhir/input";

import "./input.css";

export default function InputExample() {
  const [username, setUsername] = createSignal("ada");
  return (
    <div class="input-example">
      <label for="username">Username</label>
      <Input.Root
        id="username"
        name="username"
        value={username()}
        // Lower case, and no spaces: a space typed is undone.
        onValueChange={(value) =>
          setUsername(value.toLowerCase().replace(/\s/g, ""))
        }
        class="input"
      />
      <p class="hint">Your page: example.com/@{username()}</p>
    </div>
  );
}
```

`input.css`:

```css
/* The least CSS that lays the example out: the label above the input, and
   the hint below it. Everything else is the browser's. Colours are
   Tailwind's stone palette; light-dark() picks the second one in dark mode
   (color-scheme: dark). */
.input-example {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.25rem;
  font-family: system-ui, sans-serif;
}

.hint {
  margin: 0;
  font-size: 0.875rem;
  color: light-dark(
    oklch(55.3% 0.013 58.071) /* stone-500 */,
    oklch(70.9% 0.01 56.259) /* stone-400 */
  );
}
```

## API reference

### Root

The input: an <input> that takes every attribute of one, such as name, type, required, placeholder or autocomplete. It is for text: text, email, password, search, tel, url and the date and time types. It has no data-* attributes of its own: style it with :disabled, :read-only, :placeholder-shown or :user-invalid, or the data-* attributes of a Field around it.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string` | — | The text, when controlled. After each edit the input shows it again, so an edit you decline is undone. |
| `defaultValue` | `string` | `""` | The text to start with, when uncontrolled, and the text a form reset puts back. |
| `onValueChange` | `(value: string, details: { reason: "input-change" \| "reset"; event: Event }) => void` | — | Called with the new text on each edit, and with defaultValue on a form reset. onInput and onChange are the browser's events, and run as well. |
| `render` | `(props) => JSX.Element` | — | Renders the input as another element, with the same value props: render={(props) => <textarea {...props} />}. Read once. |

## Keyboard

| Keys | Action |
| --- | --- |
| `Tab` | Moves focus to the input and away from it. Every other key edits the text as the browser does; Input adds none. |

## In a Field

An Input joins a Field as Field.Control's render, like any control: the Field gives it its id, name, label, description and error, and the value props go on the Input. A Field reads nothing from the Input, so an Input outside a Field costs nothing more.

`InputInField.tsx`:

```tsx
import { createSignal } from "solid-js";
import { Field } from "menhir/field";
import { Input } from "menhir/input";

export default function InputInField() {
  const [email, setEmail] = createSignal("");
  return (
    <Field.Root name="email">
      <Field.Label>Email</Field.Label>
      <Field.Control
        type="email"
        required
        render={(props) => (
          <Input.Root {...props} value={email()} onValueChange={setEmail} />
        )}
      />
      <Field.Description>We send the receipt here.</Field.Description>
      <Field.Error />
    </Field.Root>
  );
}
```

## One-time code

A code sent by text message or email is one input with autocomplete="one-time-code", which phones offer to fill from the message, inputmode="numeric" for a number pad, and maxlength and pattern for its length. A pasted code lands in it whole. A box per digit is left out: the boxes would have to split a paste and an autofill between them and move focus from box to box.

`OneTimeCode.tsx`:

```tsx
import { Input } from "menhir/input";

export default function OneTimeCode() {
  return (
    <label>
      Code
      <Input.Root
        name="code"
        autocomplete="one-time-code"
        inputmode="numeric"
        maxlength={6}
        pattern="\d{6}"
        required
      />
    </label>
  );
}
```
