Skip to main content

Input

Edit single-line text with native input events and Field integration.

Props
Slots

Anatomy#

Input [component; slot=root; <input>]

Usage#

Use Input for native single-line text editing. Compose it with Field for a label and validation message, or InputGroup for adjacent addons. Use Textarea for multi-line text and NumberInput for a constrained numeric stepper.

import { Field, Input } from 'moraine'

export function Example() {
  return (
    <Field label="Your name">
      <Input placeholder="Your name" />
    </Field>
  )
}

Native attributes#

Input forwards native <input> attributes and defaults autoComplete to 'off':

  • value and onValueChange: control the current text. A controlled input restores its displayed text to value after every edit, so update value from onValueChange.
  • defaultValue: sets the initial uncontrolled text and is restored on native form reset. Passing value={undefined} leaves the input uncontrolled.
  • readOnly and disabled: readOnly keeps the field focusable without allowing edits; disabled removes it from interaction.
<Input value={query()} onValueChange={setQuery} placeholder="Search projects..." />

Inside a Field, Input inherits size, required, disabled, and readOnly, takes its name from the field when omitted, and links the label, description, and error through ARIA attributes.

Set variant ('outline', 'subtle', 'ghost', or 'none') and size ('sm', 'md', or 'lg') to change presentation.

Value modifiers#

modifiers transforms the text before it reaches onValueChange and form state, and sets the callback’s value type.

Modifier Effect
trim Strips surrounding whitespace, also from the displayed text on change
lazy Updates the value on change (blur or Enter) instead of on every keystroke
number Parses the text to a number; empty or non-numeric text gives null
empty 'null' turns empty text into null; 'preserve' keeps the string
<Input modifiers={{ number: true, empty: 'null' }} onValueChange={(value) => setAge(value)} />

Here value is number | null. Use NumberInput when the user needs stepping, bounds, or formatting.

Composed controls#

Use InputGroup.Leading and InputGroup.Trailing for static context such as an icon or unit. Password visibility, clearing, copying, and loading stay in your application: compose buttons and icons into these parts and provide their labels and handlers.

<InputGroup>
  <InputGroup.Leading>
    <Icon name="i-lucide:key-round" />
  </InputGroup.Leading>
  <Input type={showPassword() ? 'text' : 'password'} placeholder="Enter secret token" />
  <InputGroup.Trailing compact>
    <Button
      variant="ghost"
      size="icon-sm"
      leading={showPassword() ? 'i-lucide:eye-off' : 'i-lucide:eye'}
      onClick={() => setShowPassword((prev) => !prev)}
      aria-label={showPassword() ? 'Hide token' : 'Show token'}
    />
  </InputGroup.Trailing>
</InputGroup>

Examples#

Controlled#

Control the input value with value and onValueChange.

import { Field, Input } from 'moraine'
import { createMemo, createSignal, For, Show } from 'solid-js'

const PROJECTS = ['Atlas design system', 'Billing portal', 'Customer dashboard', 'Release tracker']

export function ControlledInput() {
  const [query, setQuery] = createSignal('')
  const matches = createMemo(() =>
    PROJECTS.filter((project) => project.toLowerCase().includes(query().trim().toLowerCase())),
  )

  return (
    <div class="max-w-md w-full space-y-3">
      <Field label="Find a project">
        <Input value={query()} onValueChange={setQuery} placeholder="Search projects..." />
      </Field>
      <Show when={matches().length} fallback={<p class="text-sm">No matching projects.</p>}>
        <ul class="text-sm divide-border divide-y">
          <For each={matches()}>{(project) => <li class="py-2">{project}</li>}</For>
        </ul>
      </Show>
    </div>
  )
}

InputGroup addons#

Compose InputGroup addons around Input. Your application owns visibility toggles, clear buttons, and unit suffixes, including the state and accessible labels of their buttons.

import { Button, Icon, Input, InputGroup } from 'moraine'
import { createSignal, Show } from 'solid-js'

export function InputWithIcons() {
  const [showPassword, setShowPassword] = createSignal(false)
  const [query, setQuery] = createSignal('SolidJS reactive components')

  return (
    <div class="flex flex-col gap-4 max-w-sm w-full">
      <InputGroup>
        <InputGroup.Leading>
          <Icon name="i-lucide:lock" />
        </InputGroup.Leading>
        <Input
          type={showPassword() ? 'text' : 'password'}
          defaultValue="my_super_secret_token_123"
          placeholder="Enter password"
        />
        <InputGroup.Trailing compact>
          <Button
            variant="ghost"
            size="icon-sm"
            onClick={() => setShowPassword((prev) => !prev)}
            aria-label={showPassword() ? 'Hide password' : 'Show password'}
          >
            <Icon name={showPassword() ? 'i-lucide:eye-off' : 'i-lucide:eye'} />
          </Button>
        </InputGroup.Trailing>
      </InputGroup>

      <InputGroup>
        <InputGroup.Leading>
          <Icon name="i-lucide:search" />
        </InputGroup.Leading>
        <Input value={query()} onValueChange={setQuery} placeholder="Search documents..." />
        <InputGroup.Trailing compact>
          <Show when={query()}>
            <Button
              variant="ghost"
              size="icon-sm"
              onClick={() => setQuery('')}
              aria-label="Clear search"
            >
              <Icon name="i-lucide:x" />
            </Button>
          </Show>
        </InputGroup.Trailing>
      </InputGroup>

      <InputGroup>
        <InputGroup.Leading compact>
          <div class="text-xs text-muted-foreground font-mono flex gap-1 items-center">
            <Icon name="i-lucide:globe" class="size-3.5" />
            https://
          </div>
        </InputGroup.Leading>
        <Input placeholder="project-slug" />
        <InputGroup.Trailing compact>
          <span class="text-xs text-muted-foreground font-mono">.moraine.dev</span>
        </InputGroup.Trailing>
      </InputGroup>

      <InputGroup>
        <InputGroup.Leading>
          <span class="text-xs text-muted-foreground font-semibold">$</span>
        </InputGroup.Leading>
        <Input defaultValue="49.00" placeholder="0.00" />
        <InputGroup.Trailing>
          <span class="text-xs text-muted-foreground">USD / mo</span>
        </InputGroup.Trailing>
      </InputGroup>
    </div>
  )
}

Form#

Bind Input to form.Field so schema validation owns the error. Omit name on the input; the bound field supplies it.

import { Button, createForm, Input, InputGroup, Icon } from 'moraine'
import { createSignal } from 'solid-js'
import * as v from 'valibot'

export function FormIntegration() {
  const [submittedEmail, setSubmittedEmail] = createSignal('')
  const form = createForm({
    schema: v.object({
      email: v.pipe(v.string(), v.email('Please enter a valid work email address.')),
    }),
    initialInput: { email: '' },
    validate: 'input',
  })

  return (
    <form.Form onSubmit={(output) => setSubmittedEmail(output.email)}>
      <div class="max-w-xl space-y-4">
        <form.Field
          name="email"
          label="Work Email"
          description="We will send your verification link here."
          required
        >
          <InputGroup>
            <InputGroup.Leading>
              <Icon name="i-lucide:mail" />
            </InputGroup.Leading>
            <Input type="email" placeholder="[email protected]" />
          </InputGroup>
        </form.Field>
        <div class="flex gap-3 items-center">
          <Button type="submit" variant="soft" tone="neutral" size="sm">
            Validate
          </Button>
          <p class="text-xs text-muted-foreground">Submitted: {submittedEmail() || 'none'}</p>
        </div>
      </div>
    </form.Form>
  )
}

API Reference#

Attributes#

Attributes
data-disabled
Slot: input
Description: Present when the component, slot, or item is disabled.
data-invalid
Slot: input
Description: Present when the field or form has a validation error.
data-readonly
Slot: input
Description: Present when the field is in read-only mode.
data-required
Slot: input
Description: Present when the field input is required.

Props#

Props for the Input component.

Prop