Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Required and Optional Fields Form Block for React Aria

An add-a-vendor form that shows the three required fields first, folds four optional ones behind a counted disclosure, labels them optional and names what is missing on submit.

Framework
UI library

Preview

Preview width

Installation

Part of Pro
Pro unlocks the code and one-line install of every Pro button and text effect, plus the MCP for your AI editor.
Have Pro already? Sign in to open it.Opens here as soon as you're Pro.

Overview

Ask for what you need first with this required and optional fields form for React with React Aria. A compact header whose sentence says all fields are required unless marked optional sits over a hairline frame with a plus mark on each corner. The first band holds the three required fields (contact name, work email and company) with a running "1 of 3 filled" count. Under it, one button reading "Add optional details (4)" opens a second band with phone, tax ID, payment terms and notes, each label ending in "(optional)", and the button counts how many of the four are filled. Add vendor checks the required fields first, names each one that is missing in words, and puts focus on the first; a phone number typed in the folded band that is too short opens it. The Shine Button is the only colored control. It is built from Input, Native Select, Textarea, Shine Button and Guides, with a CSS fold and entrance only. For vendor and customer records, sign-ups and any form where most fields can wait.

FAQ

How do I change which fields are required?

REQUIRED and OPTIONAL list the field names in each band and problemsOf holds the rule for each one, so moving a field between bands is a change to those two lists, the band it renders in, and its optional prop, which prints the "(optional)" label. Required rules run first and always, optional rules only when something was typed.

How does the optional band open and close?

It is a disclosure button with aria-expanded and aria-controls over a region whose height animates from zero. While it is closed the region is hidden and inert, so Tab skips its fields, and the values stay in state, so closing it loses nothing. Reduced motion turns the animation off.

What happens when an optional field has a problem?

Only the phone number is checked, and only when it is not empty. If it is wrong while the band is folded, Add vendor opens the band and puts focus on that field, so an error is never hidden behind a closed section. An untouched optional field never blocks the form.

How does it work with a keyboard and a screen reader?

Every field has a visible label tied by for, and its hint or problem sits in a line that is always reserved and tied by aria-describedby, so nothing shifts when an error appears. Fields in error carry aria-invalid. The foot status line is the one polite region and says how many required fields are filled, or how many to fix.

What stays local, and how do I connect it for real?

Add vendor says the vendor was added in this demo and sends nothing. To make it real, post the values from the submit handler once problemsOf returns nothing, and show your server's field errors in the same slots by setting them from the response.

How do I connect it to react-hook-form or TanStack Form?

With react-hook-form, register the seven fields and pass the rules to register or a resolver; read each field's error from formState.errors into the same slot component, and open the optional band when one of its names is in errors.