Field

A form field: a label, a control, a description, and an error.

Payment method

All transactions are secure and encrypted.

Enter your 16-digit card number.

Billing address

The billing address for this payment method.

Also uses Button, Checkbox, Input, Native Select and Textarea.

field-demo.tsx
import { Button } from './ui/button.tsx'
import { Checkbox } from './ui/checkbox.tsx'
import {
  Field,
  FieldDescription,
  FieldGroup,
  FieldLabel,
  FieldLegend,
  FieldSeparator,
  FieldSet,
} from './ui/field.tsx'
import { Input } from './ui/input.tsx'
import { NativeSelect, NativeSelectOption } from './ui/native-select.tsx'
import { Textarea } from './ui/textarea.tsx'

export default () => (
  <form style="width: 100%; max-width: 28rem">
    <FieldGroup>
      <FieldSet>
        <FieldLegend>Payment method</FieldLegend>
        <FieldDescription>All transactions are secure and encrypted.</FieldDescription>
        <FieldGroup>
          <Field>
            <FieldLabel for="card-name">Name on card</FieldLabel>
            <Input id="card-name" name="name" placeholder="Evil Rabbit" required />
          </Field>
          <Field>
            <FieldLabel for="card-number">Card number</FieldLabel>
            <Input
              id="card-number"
              name="number"
              inputmode="numeric"
              placeholder="1234 5678 9012 3456"
              required
            />
            <FieldDescription>Enter your 16-digit card number.</FieldDescription>
          </Field>
          <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 1rem">
            <Field>
              <FieldLabel for="card-month">Month</FieldLabel>
              <NativeSelect id="card-month" name="month" style="width: 100%">
                {Array.from({ length: 12 }, (_, i) => (
                  <NativeSelectOption>{String(i + 1).padStart(2, '0')}</NativeSelectOption>
                ))}
              </NativeSelect>
            </Field>
            <Field>
              <FieldLabel for="card-cvv">CVV</FieldLabel>
              <Input id="card-cvv" name="cvv" inputmode="numeric" placeholder="123" required />
            </Field>
          </div>
        </FieldGroup>
      </FieldSet>
      <FieldSeparator />
      <FieldSet>
        <FieldLegend>Billing address</FieldLegend>
        <FieldDescription>The billing address for this payment method.</FieldDescription>
        <Field orientation="horizontal">
          <Checkbox id="same-address" name="same" checked />
          <FieldLabel for="same-address" style="font-weight: 400">
            Same as shipping address
          </FieldLabel>
        </Field>
      </FieldSet>
      <Field>
        <FieldLabel for="card-comments">Comments</FieldLabel>
        <Textarea id="card-comments" name="comments" placeholder="Add any comments" />
      </Field>
      <Field orientation="horizontal">
        <Button type="submit">Submit</Button>
        <Button type="reset" variant="outline">
          Cancel
        </Button>
      </Field>
    </FieldGroup>
  </form>
)

Installation

dui add field

Or copy ui/field.tsx and ui/field.css into your project, with lib/jsx/jsx-runtime.ts, lib/jsx/jsx-dev-runtime.ts, lib/css.d.ts, and lib/theme.css.

Examples

Input

Choose a unique username for your account.

Also uses Input.

field-input.tsx
import { Field, FieldDescription, FieldLabel } from './ui/field.tsx'
import { Input } from './ui/input.tsx'

export default () => (
  <Field style="max-width: 24rem">
    <FieldLabel for="username">Username</FieldLabel>
    <Input id="username" name="username" placeholder="evilrabbit" />
    <FieldDescription>Choose a unique username for your account.</FieldDescription>
  </Field>
)

Checked as you type, with Datastar

We'll send your receipt here.

Enter an email address, like m@example.com.

Also uses Input.

field-validation.tsx
import { Field, FieldDescription, FieldError, FieldLabel } from './ui/field.tsx'
import { Input } from './ui/input.tsx'

// Datastar checks the value as it's typed, marks the field invalid, and fills in the error.
export default () => (
  <Field
    style="max-width: 24rem"
    data-signals="{email: 'not-an-email'}"
    data-computed:email-error="$email.includes('@') ? '' : 'Enter an email address, like m@example.com.'"
    data-attr:data-invalid="String(!!$emailError)"
    data-invalid="true"
  >
    <FieldLabel for="field-email">Email</FieldLabel>
    <Input
      id="field-email"
      type="email"
      data-bind:email
      data-attr:aria-invalid="String(!!$emailError)"
      aria-invalid="true"
    />
    <FieldDescription>We'll send your receipt here.</FieldDescription>
    <FieldError data-text="$emailError">Enter an email address, like m@example.com.</FieldError>
  </Field>
)

Errors from the server

  • Use at least 12 characters.
  • Add a number or a symbol.

Also uses Input.

field-errors.tsx
import { Field, FieldError, FieldLabel } from './ui/field.tsx'
import { Input } from './ui/input.tsx'

// Errors from the server, rendered with the page.
export default () => (
  <Field data-invalid="true" style="max-width: 24rem">
    <FieldLabel for="new-password">Password</FieldLabel>
    <Input id="new-password" type="password" value="hunter2" aria-invalid="true" />
    <FieldError errors={['Use at least 12 characters.', 'Add a number or a symbol.']} />
  </Field>
)

Choice cards

Also uses Radio Group.

field-choice-card.tsx
import { Field, FieldContent, FieldDescription, FieldLabel, FieldTitle } from './ui/field.tsx'
import { RadioGroup, RadioGroupItem } from './ui/radio-group.tsx'

// A label around a whole field makes a card to pick, highlighted while it's checked.
export default () => (
  <RadioGroup style="width: 100%; max-width: 24rem">
    <FieldLabel>
      <Field orientation="horizontal">
        <FieldContent>
          <FieldTitle>Kubernetes</FieldTitle>
          <FieldDescription>Run GPU workloads on a K8s cluster.</FieldDescription>
        </FieldContent>
        <RadioGroupItem name="compute" value="kubernetes" checked />
      </Field>
    </FieldLabel>
    <FieldLabel>
      <Field orientation="horizontal">
        <FieldContent>
          <FieldTitle>Virtual machine</FieldTitle>
          <FieldDescription>Access a cluster to run GPU workloads.</FieldDescription>
        </FieldContent>
        <RadioGroupItem name="compute" value="vm" />
      </Field>
    </FieldLabel>
  </RadioGroup>
)

Responsive

Shown on your profile.

A sentence or two about you.

Also uses Input and Textarea.

field-responsive.tsx
import { Field, FieldContent, FieldDescription, FieldGroup, FieldLabel } from './ui/field.tsx'
import { Input } from './ui/input.tsx'
import { Textarea } from './ui/textarea.tsx'

// Side by side when the group is wide enough, stacked when it isn't.
export default () => (
  <FieldGroup style="max-width: 36rem">
    <Field orientation="responsive">
      <FieldContent>
        <FieldLabel for="resp-name">Name</FieldLabel>
        <FieldDescription>Shown on your profile.</FieldDescription>
      </FieldContent>
      <Input id="resp-name" placeholder="Evil Rabbit" style="max-width: 16rem" />
    </Field>
    <Field orientation="responsive">
      <FieldContent>
        <FieldLabel for="resp-bio">Bio</FieldLabel>
        <FieldDescription>A sentence or two about you.</FieldDescription>
      </FieldContent>
      <Textarea id="resp-bio" style="max-width: 16rem" />
    </Field>
  </FieldGroup>
)

Compared to shadcn/ui