Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Input for Radix UI

A one-line text input for React on Radix UI, for names, emails, searches and files, labeled with Field and styled for disabled and invalid states.

On this page9 sections

Input is a one-line text box for a name, an email address, a search term, a password or a file picked with the browser's own button. It renders a native <input>, so type, name, required and autofill work the way the browser defines them.

When the answer runs to more than one line, use a Textarea. When an icon, a unit or a button belongs inside the box, build it with Input Group, and give every field a label with Field.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Input anatomy: input, placeholder, value, caret

Usage

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

FieldLabel names the input and FieldDescription adds a hint under it. Nothing links the label to the input for you, so the label's htmlFor must match the input's id.

<Field className="max-w-xs">
  <FieldLabel htmlFor="api-key">API Key</FieldLabel>
  <Input id="api-key" placeholder="sk-..." type="password" />
  <FieldDescription>
    Your API key is encrypted and stored securely.
  </FieldDescription>
</Field>

Accessibility

Input renders a native <input>, so typing, selection, autofill and the phone keyboard that type picks all come from the browser, with no keys of its own. What it can't supply is a name. FieldLabel is a plain <label>, so give the Input an id and the label a matching htmlFor. Field names its FieldDescription in the input's aria-describedby, so the hint is read out when the box takes focus. Where no label shows on screen, set aria-label, because a placeholder disappears as soon as someone types.

Demos

Basic

A lone input with "Enter text" as its placeholder and nothing around it. Use it where something else on screen already says what goes in the box, such as a search field under a page heading, and give it an aria-label so a screen reader has a name too. A field people fill in on a form wants a visible label, as in Field.

Preview width

Field

Username, with its label above the box and a line under it about picking a unique name. The label's htmlFor matches the input's id, so clicking Username focuses the box. This is the shape to start from for any form field. Leave the description out when the label says enough.

Preview width

Field group

A name field, then an email field with a note about where updates go, then Reset and Submit side by side. FieldGroup stacks the fields with one even gap, and the button row is a Field with orientation set to horizontal. The group sits in a <form>, so Reset clears both fields and Submit fires the form's submit handler, which here only calls preventDefault.

Preview width

Disabled

The email box is disabled, so it can't be focused or edited, Tab skips it, and its value stays out of the form data. data-disabled set to true on Field fades the label to match. Use it for a value this screen shows but doesn't let people change.

Preview width

Invalid

The field is marked invalid with invalid on Field and aria-invalid on the input, which turns the box's border and ring to the destructive color and tells a screen reader the value is wrong. The note under it only says the field has errors. In a real form, say what to fix in a FieldError that shows while the value is wrong.

Preview width

File

A Picture field whose input has type set to file, so the box holds the browser's own file button and the name of the file picked. Clicking the Picture label opens the picker too. Use it for one upload in a settings form. When people should drag files in or see a list of what they added, use File Upload.

Preview width

Inline

A search box and a Search button on one row, laid side by side by orientation set to horizontal on Field. There is no label on screen, so the button and the placeholder tell people what the box is for. Use it in a toolbar or above a table. When the two should read as one joined control, use the Button group layout.

Preview width

Grid

First name and last name side by side, with the FieldGroup turned into a two-column grid. The columns stay at two on every screen width, so keep this layout to short values such as names, or add a breakpoint that stacks them on a phone.

Preview width

Required

The label ends in a red asterisk and the input is required, so a form around it won't submit while the box is empty. The asterisk is a span with aria-hidden, so a screen reader reads the label without it. required is what tells assistive technology the field must be filled in.

Preview width

Badge

Webhook URL, with a Beta Badge at the end of the label row and an input whose type is url. The badge sits inside the label, so a screen reader hears "Webhook URL Beta" as the field's name. Use it to flag a new or experimental setting when a sentence of description would be too much.

Preview width

Input group

The Website URL field shows https:// before the text and an info icon after it, inside one border. That is Input Group, with InputGroupInput as the part people type in and an InputGroupAddon on each side. Use it when a protocol, a unit or an icon belongs to the value, instead of positioning an icon over a plain Input.

Preview width

Button group

A Search label over a text box and a Search button joined edge to edge, with the corners between them squared off, so they read as one control. Button Group does the joining. Use it when the button acts on this field alone, such as a search or a coupon code. A prefix or an icon that belongs inside the box goes in an input group instead.

Preview width

Form

A whole contact form: name, email, a phone number beside a country Select, an address, then Cancel and Submit. One FieldGroup inside a <form> spaces every field, and a plain two-column grid holds the phone row. Name is required, and phone and email set type to tel and email, so phones show a matching keyboard.

Preview width

RTL

The API key field from the top of the page, in Arabic, with dir set to rtl on Field and on the input. The label, the box and the note line up from the right edge, and typing starts there too. Use it when one field is in Arabic or Hebrew on a left-to-right page. When the whole page reads right to left, set dir on the document instead.

Preview width

FAQ

How do I add a label to an Input in React?

Put a FieldLabel above it inside Field, give the Input an id, and set the label's htmlFor to the same value, as the Field demo does. FieldLabel is a plain <label>, and until the two match, clicking it does nothing and a screen reader hears an unnamed text box.

When the design has no visible label, such as a search box next to a magnifying glass, set aria-label on the Input. A placeholder is no substitute, because it disappears once someone starts typing.

How do I control an Input's value in React?

Pass value and update your state in onChange, reading the new text from event.target.value. Without value, the Input keeps its own state, and defaultValue sets the text it starts with.

Control it when something reacts while people type, such as a live search or a character count. A value you only need on submit can stay uncontrolled and be read from the form.

How do I show a validation error under an Input?

Set invalid on Field and put the message in a FieldError inside it. Field sets aria-invalid on the input, and FieldError is announced as an alert when it appears, showing your text or each message of a form library's errors array once. It hides itself while invalid is false.

Say what to fix, such as "Enter an email address with an @", rather than "Invalid input". The Invalid demo shows the styling.

How do I get an Input's value when a form is submitted?

Give the Input a name. It renders a real <input>, so the browser adds its value to the form's data under that name, and your submit handler reads it with FormData. Before it submits, the browser also checks required, pattern and types such as email, and points people to the first field that fails.

A disabled Input is left out of the data, so make a value people may not edit read-only instead when it still has to be sent. With React Hook Form, spread what register returns for the field's name onto the Input, which passes the ref and the change and blur handlers through to the <input>.

Should a locked Input be disabled or read-only?

Make it read-only when people should still see, select and copy the value, such as an account ID or an API key. A read-only Input stays in the tab order and its value is submitted. A disabled one can't be focused, Tab skips it, and its value is left out of the form data.

Set disabled or readOnly on the Input. Field has no prop for either, so add data-disabled set to true to it, as the Disabled demo does, to fade the label.

How do I change the height or width of an Input?

Add a height class such as h-10 to the Input's className, and it replaces the height from the component's styles. The Radix UI Input has no size prop. To change every input in the app, edit the height in components/ui/input instead.

The width comes from the container. Input is w-full, so it fills whatever holds it. Most demos cap it with max-w-xs on the Field, and the Basic demo, which has no Field, puts the class on the Input itself.

How do I put an icon or a button inside an Input?

Use Input Group: swap the Input for an InputGroupInput inside an InputGroup, then put the icon, the text or an InputGroupButton in an InputGroupAddon. An addon sits at the start of the box until you set its align: inline-end moves it to the end, and block-start or block-end gives it a full-width row above or below. The group draws one border and one focus ring around all of it, as the Input group demo shows.

For a button beside the box rather than inside it, join the two with Button Group, as in the Button group demo.

How do I make a file picker with Input?

Set type to file, as the File demo does, and add accept to narrow the picker, such as image/* for pictures or .pdf for documents. Add multiple to let people choose more than one file. Read the chosen files from the input's files list when it changes, or from FormData on submit under the input's name.

accept only filters the picker, so check the type and size again before you upload. When people should drag files in, or see and remove what they added, use File Upload.

API Reference

Input

<input> data-slot="input"

PropType
className?string

Merged with the part's classes.