Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Input examples · Standard

Optional Field Input for React

A field whose label marks it as optional.

Framework
UI library

When most of a form is required, mark the exceptions. This example adds a muted (optional) note inside the Middle Name label, smaller and lighter than the label itself, so people can skip the field with confidence. Use it in sign-up, profile and checkout forms where only a few fields can be left empty.

Preview

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.

FAQ

How is the optional note styled?

It's a span inside FieldLabel with text-xs, font-normal and text-muted-foreground, so it reads as secondary to the label. The label's own gap spaces it from Middle Name.

Do screen readers hear (optional)?

Yes. The note is part of the label's text, so the field is announced as Middle Name (optional).

Is the input itself marked optional?

No attribute is needed: a field without required is optional. The note only tells people so.

Should I mark optional or required fields?

Mark whichever is rarer. When most fields are required, label the optional ones, as here; when most are optional, mark the required ones with an asterisk, as in the Required Field example.

What kind of input is it?

A type text input with the placeholder Enter your middle name, at the field's full width up to max-w-sm.

How is Middle Name tied to the field?

Clicking the label focuses the field, and screen readers announce it with the field. FieldLabel sets htmlFor to optional-middle-name, the input's id.