Skip to content
2k
On This Page

Input

A text input component for forms and user data entry with built-in styling and accessibility features.

Your API key is encrypted and stored securely.

import { Field, FieldDescription, FieldLabel } from "~/components/ui/field"
import { Input } from "~/components/ui/input"

export default function InputDemo() {
  return (
    <Field class="w-full max-w-xs">
      <FieldLabel for="input-demo-api-key">API Key</FieldLabel>
      <Input id="input-demo-api-key" placeholder="sk-..." type="password" />
      <FieldDescription>
        Your API key is encrypted and stored securely.
      </FieldDescription>
    </Field>
  )
}

Installation

pnpm dlx solidui-cli@latest add input

Copy and paste the following code into your project.

components/ui/input.tsx
import { type ComponentProps } from "@solidjs/web"

import { cn } from "~/lib/utils"

type InputProps = ComponentProps<"input">

const Input = (props: InputProps) => (
  <input
    data-slot="input"
    {...props}
    class={cn(
      "h-8 rounded-lg border border-input bg-transparent px-2.5 py-1 text-base transition-colors file:h-6 file:text-sm file:font-medium focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:bg-input/50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:disabled:bg-input/80 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 w-full min-w-0 outline-none file:inline-flex file:border-0 file:bg-transparent file:text-foreground placeholder:text-muted-foreground disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50",
      props.class
    )}
  />
)

export { Input, type InputProps }

Update the import paths to match your project setup.

Usage

import { Input } from "~/components/ui/input"
<Input />

Basic

import { Input } from "~/components/ui/input"

export default function InputBasic() {
  return <Input class="max-w-xs" placeholder="Enter text" />
}

Field

Use Field, FieldLabel, and FieldDescription to create an input with a label and description.

Choose a unique username for your account.

import { Field, FieldDescription, FieldLabel } from "~/components/ui/field"
import { Input } from "~/components/ui/input"

export default function InputField() {
  return (
    <Field class="w-full max-w-xs">
      <FieldLabel for="input-field-username">Username</FieldLabel>
      <Input
        id="input-field-username"
        placeholder="Enter your username"
        type="text"
      />
      <FieldDescription>
        Choose a unique username for your account.
      </FieldDescription>
    </Field>
  )
}

Field Group

Use FieldGroup to show multiple Field blocks and to build forms.

We'll send updates to this address.

import { Button } from "~/components/ui/button"
import {
  Field,
  FieldDescription,
  FieldGroup,
  FieldLabel
} from "~/components/ui/field"
import { Input } from "~/components/ui/input"

export default function InputFieldgroup() {
  return (
    <div class="w-full max-w-xs">
      <FieldGroup>
        <Field>
          <FieldLabel for="fieldgroup-name">Name</FieldLabel>
          <Input id="fieldgroup-name" placeholder="Jordan Lee" />
        </Field>

        <Field>
          <FieldLabel for="fieldgroup-email">Email</FieldLabel>
          <Input
            id="fieldgroup-email"
            placeholder="name@example.com"
            type="email"
          />
          <FieldDescription>
            We&apos;ll send updates to this address.
          </FieldDescription>
        </Field>

        <Field orientation="horizontal">
          <Button type="reset" variant="outline">
            Reset
          </Button>
          <Button type="submit">Submit</Button>
        </Field>
      </FieldGroup>
    </div>
  )
}

Disabled

Use the disabled prop to disable the input. To style the disabled state, add the data-disabled attribute to the Field component.

This field is currently disabled.

import { Field, FieldDescription, FieldLabel } from "~/components/ui/field"
import { Input } from "~/components/ui/input"

export default function InputDisabled() {
  return (
    <Field class="w-full max-w-xs" data-disabled>
      <FieldLabel for="input-demo-disabled">Email</FieldLabel>
      <Input
        disabled
        id="input-demo-disabled"
        placeholder="Email"
        type="email"
      />
      <FieldDescription>This field is currently disabled.</FieldDescription>
    </Field>
  )
}

Invalid

Use the aria-invalid prop to mark the input as invalid. To style the invalid state, add the data-invalid attribute to the Field component.

This field contains validation errors.

import { Field, FieldDescription, FieldLabel } from "~/components/ui/field"
import { Input } from "~/components/ui/input"

export default function InputInvalid() {
  return (
    <Field class="w-full max-w-xs" data-invalid>
      <FieldLabel for="input-invalid">Invalid Input</FieldLabel>
      <Input aria-invalid="true" id="input-invalid" placeholder="Error" />
      <FieldDescription>
        This field contains validation errors.
      </FieldDescription>
    </Field>
  )
}

File

Use the type="file" prop to create a file input.

Select a picture to upload.

import { Field, FieldDescription, FieldLabel } from "~/components/ui/field"
import { Input } from "~/components/ui/input"

export default function InputFile() {
  return (
    <Field class="w-full max-w-xs">
      <FieldLabel for="picture">Picture</FieldLabel>
      <Input id="picture" type="file" />
      <FieldDescription>Select a picture to upload.</FieldDescription>
    </Field>
  )
}

Inline

Use Field with orientation="horizontal" to create an inline input. Pair with Button to create a search input with a button.

import { Button } from "~/components/ui/button"
import { Field } from "~/components/ui/field"
import { Input } from "~/components/ui/input"

export default function InputInline() {
  return (
    <Field class="w-full max-w-xs" orientation="horizontal">
      <Input placeholder="Search..." type="search" />
      <Button>Search</Button>
    </Field>
  )
}

Grid

Use a grid layout to place multiple inputs side by side.

import { Field, FieldGroup, FieldLabel } from "~/components/ui/field"
import { Input } from "~/components/ui/input"

export default function InputGrid() {
  return (
    <FieldGroup class="grid w-full max-w-sm grid-cols-2">
      <Field>
        <FieldLabel for="first-name">First Name</FieldLabel>
        <Input id="first-name" placeholder="Jordan" />
      </Field>
      <Field>
        <FieldLabel for="last-name">Last Name</FieldLabel>
        <Input id="last-name" placeholder="Lee" />
      </Field>
    </FieldGroup>
  )
}

Required

Use the required attribute to indicate required inputs.

This field must be filled out.

import { Field, FieldDescription, FieldLabel } from "~/components/ui/field"
import { Input } from "~/components/ui/input"

export default function InputRequired() {
  return (
    <Field class="w-full max-w-xs">
      <FieldLabel for="input-required">
        Required Field <span class="text-destructive">*</span>
      </FieldLabel>
      <Input
        id="input-required"
        placeholder="This field is required"
        required
      />
      <FieldDescription>This field must be filled out.</FieldDescription>
    </Field>
  )
}

Badge

Use Badge in the label to highlight a recommended field.

import { Field, FieldLabel } from "~/components/ui/field"
import { Input } from "~/components/ui/input"

export default function InputBadge() {
  return (
    <Field class="w-full max-w-xs">
      <FieldLabel for="input-badge">
        Webhook URL
        <span class="h-5 gap-1 rounded-4xl border border-transparent px-2 py-0.5 text-xs font-medium transition-all has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&>svg]:size-3! bg-secondary text-secondary-foreground [a]:hover:bg-secondary/80 ml-auto">Beta</span>
      </FieldLabel>
      <Input
        id="input-badge"
        placeholder="https://api.example.com/webhook"
        type="url"
      />
    </Field>
  )
}

Input Group

To add icons, text, or buttons inside an input, use the InputGroup component. See the Input Group component for more examples.

https://
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.

import { Field, FieldLabel } from "~/components/ui/field"
import {
  InputGroup,
  InputGroupAddon,
  InputGroupInput,
  InputGroupText
} from "~/components/ui/input-group"

export default function InputInputGroup() {
  return (
    <Field class="w-full max-w-xs">
      <FieldLabel for="input-group-url">Website URL</FieldLabel>
      <InputGroup>
        <InputGroupInput id="input-group-url" placeholder="example.com" />
        <InputGroupAddon>
          <InputGroupText>https://</InputGroupText>
        </InputGroupAddon>
        <InputGroupAddon align="inline-end">
          <svg
            width="24"
            height="24"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
            aria-hidden="true"
            class="size-4"
          >
            <circle cx="12" cy="12" r="10" />
            <path d="M12 16v-4" />
            <path d="M12 8h.01" />
          </svg>
        </InputGroupAddon>
      </InputGroup>
    </Field>
  )
}

Button Group

To add buttons to an input, use the ButtonGroup component. See the Button Group component for more examples.

import { Button } from "~/components/ui/button"
import { ButtonGroup } from "~/components/ui/button-group"
import { Field, FieldLabel } from "~/components/ui/field"
import { Input } from "~/components/ui/input"

export default function InputButtonGroup() {
  return (
    <Field class="w-full max-w-xs">
      <FieldLabel for="input-button-group">Search</FieldLabel>
      <ButtonGroup>
        <Input id="input-button-group" placeholder="Type to search..." />
        <Button variant="outline">Search</Button>
      </ButtonGroup>
    </Field>
  )
}

Form

A full form example with multiple inputs, a select, and a button.

We'll never share your email with anyone.

import { For } from "solid-js"

import { Button } from "~/components/ui/button"
import {
  Field,
  FieldDescription,
  FieldGroup,
  FieldLabel
} from "~/components/ui/field"
import { Input } from "~/components/ui/input"
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue
} from "~/components/ui/select"

type Country = {
  label: string
  value: string
}

const countries: Country[] = [
  { label: "United States", value: "us" },
  { label: "United Kingdom", value: "uk" },
  { label: "Canada", value: "ca" }
]

export default function InputForm() {
  return (
    <form class="w-full max-w-sm">
      <FieldGroup>
        <Field>
          <FieldLabel for="form-name">Name</FieldLabel>
          <Input
            id="form-name"
            placeholder="Evil Rabbit"
            required
            type="text"
          />
        </Field>

        <Field>
          <FieldLabel for="form-email">Email</FieldLabel>
          <Input id="form-email" placeholder="john@example.com" type="email" />
          <FieldDescription>
            We&apos;ll never share your email with anyone.
          </FieldDescription>
        </Field>

        <div class="grid grid-cols-2 gap-4">
          <Field>
            <FieldLabel for="form-phone">Phone</FieldLabel>
            <Input id="form-phone" placeholder="+1 (555) 123-4567" type="tel" />
          </Field>

          <Field>
            <FieldLabel for="form-country">Country</FieldLabel>
            <Select<Country>
              itemToLabel={(option) => String(option["label"])}
              itemToValue={(option) => String(option["value"])}
              defaultValue={countries[0]!}
            >
              <SelectTrigger
                aria-label="Select an option"
                id="form-country"
                class="w-full"
              >
                <SelectValue />
              </SelectTrigger>
              <SelectContent>
                <For each={countries}>
                  {(option) => (
                    <SelectItem value={option} label={option["label"]}>
                      {option.label}
                    </SelectItem>
                  )}
                </For>
              </SelectContent>
            </Select>
          </Field>
        </div>

        <Field>
          <FieldLabel for="form-address">Address</FieldLabel>
          <Input id="form-address" placeholder="123 Main St" type="text" />
        </Field>

        <Field orientation="horizontal">
          <Button type="button" variant="outline">
            Cancel
          </Button>
          <Button type="submit">Submit</Button>
        </Field>
      </FieldGroup>
    </form>
  )
}

API Reference

All native input attributes are forwarded to the root element.