Skip to content

Input

Takes a single line of text, such as a supplier name, an order number or an email address.

The legal name, as it appears on the purchase order.

Installation

pnpm dlx shadcn@latest add @silkware/input

To copy the component by hand, install its dependencies:

pnpm add react-aria-components cn

Then copy the source into components/ui/input.tsx:

components/ui/input.tsx
"use client"
import * as React from "react"import { cn } from "cn"import {  composeRenderProps,  Input as InputPrimitive,} from "react-aria-components"
function Input({  className,  type,  ...props}: React.ComponentProps<typeof InputPrimitive>) {  return (    <InputPrimitive      type={type}      data-slot="input"      className={composeRenderProps(className, (className) =>        cn(          "h-9 w-full min-w-0 rounded-md border border-input bg-transparent px-2.5 py-1 text-base shadow-xs transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40",          className        )      )}      {...props}    />  )}
export { Input }

Usage

import { Field, FieldDescription, FieldLabel } from "@/components/ui/field"import { Input } from "@/components/ui/input"
<Field>  <FieldLabel htmlFor="supplier">Supplier name</FieldLabel>  <Input id="supplier" placeholder="Cavenzo Metalli" />  <FieldDescription>    The legal name, as it appears on the purchase order.  </FieldDescription></Field>

Input is the bare React Aria Input, not a full text field. It does not render a label, a description or an error. Compose it with Field, and connect the parts yourself: the id on the input matches the htmlFor on the label, and aria-describedby points to the description or the error.

Examples

Types

Set type to get the right keyboard on touch screens and the right browser checks: email, number, tel, password, search, date. Add autoComplete so the browser can fill the value in.

Disabled

The disabled attribute removes the input from the tab order and dims it. Put data-disabled on the Field so the label dims too. Prefer read-only when the person should still be able to read or copy the value.

Assigned when the order is created. It cannot be changed.

Read-only

readOnly keeps the input focusable and its text selectable, but the value cannot be changed. Use it for values the person can see and copy but not edit.

Set by the supplier contract. You can select and copy it.

Required

required asks the browser to check the value when the form is submitted. Mark the label too, and hide the asterisk from screen readers because the input already announces that it is required.

Required. Purchasing calls this person.

Invalid

Set aria-invalid on the input and data-invalid on the Field, so the border, the label and the error turn to the destructive colour. Show the reason in a FieldError and connect it with aria-describedby.

Eleven digits after the country code.

File

type="file" styles the native file button to match the input.

PDF, up to 10 MB.

Inline

A horizontal Field puts the input and a button on one row. Without a visible label, give the input an aria-label.

Grid

Put fields in a grid to place short inputs side by side.

In a form with validation

Validate on submit, show one specific message per field and keep the values the person typed. This form uses noValidate and its own checks, then sets aria-invalid, data-invalid and aria-describedby from state.

We send purchase orders to this address.

Accessibility

KeyAction
TabMoves focus to the input.
ShiftTabMoves focus to the previous control.
EnterSubmits the form that holds the input when the form has a submit button or this is its only text field, as a native input does.

The browser provides, because Input renders a native <input> through React Aria:

  • Text editing, selection, autofill and the right virtual keyboard for the type.
  • Focus in the tab order, with a visible ring through focus-visible.
  • Native required, minLength, maxLength, pattern and type validation when the form is submitted.

You provide:

  • A visible FieldLabel whose htmlFor matches the input id, or an aria-label when the design has no visible label. A placeholder is not a label.
  • aria-describedby that points to the description and, when the value is wrong, to the error.
  • aria-invalid together with an error message in words. Colour alone does not say what is wrong.
  • autoComplete on personal data fields.

Guidelines

When to use

  • For one line of free text: a name, a code, a reference, an email address.
  • For a short typed number, with type="number" or inputMode.

When not to use

Content

  • Label with a noun in sentence case: "Supplier name", not "Please enter the supplier name".
  • Use the placeholder only for an example of the format, such as "Cavenzo Metalli". It disappears when the person types, so never put the instruction there.
  • Say what is wrong and how to fix it in the error: "Enter 11 digits after IT."
  • Mark the exception: label the few optional fields, or mark the required ones, but not both.

Labels

Every input has a label that stays visible while the person types.

Do

Keep a visible label above the input.

Don’t

Don’t use the placeholder as the label. It vanishes as soon as the person starts to type.

API reference

Input

Renders an <input> through the React Aria Input. It accepts every React Aria Input prop and the standard attributes of an HTML input, which the table lists in part.

PropTypeDefaultDescription
type?string"text"The input type, such as email, number, password, search, tel or file.
placeholder?stringNoneA short example of the expected format. It is not a label.
disabled?booleanfalseRemoves the input from the tab order and dims it.
readOnly?booleanfalseKeeps the input focusable but stops the value from changing.
required?booleanfalseAsks the browser to check that the input has a value on submit.
aria-invalid?booleanNoneMarks the value as wrong and applies the destructive styles.
className?string | ((values) => string)NoneClasses merged after the base classes. It can also be a function of the render state.