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.
- Size
- 0.9 kB
Example
Your page: example.com/@ada
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>
);
}
Anatomy
<Input.Root />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.
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.
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>
);
}