Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Radio Group examples · Form

Radio Group in Form for React

A required notification setting in a form that shows an error when it is submitted with no option picked.

Framework
UI library

A radio group with no default can be submitted empty. This form asks how to notify you, starts with nothing selected, and on submit either confirms the choice or marks the group invalid with a message. It is built from FieldSet, FieldLegend, FieldDescription and FieldError in plain React state, with no form library, and Base UI renders the group.

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

What does the form require?

One of the three options. On submit the handler looks the value up in the options list. With no match it sets the field invalid, and FieldError shows You need to select a notification type.

How is the radio group connected to the state?

The group is controlled, and picking an option stores it and clears the error. It also has name set to type, so a real form submit sends the value. value is the state, and onValueChange receives the next string.

What are the three values?

all is All new messages, mentions is Direct messages and mentions, and none is Nothing. They come from a list at the top of the file.

How does the invalid state look?

The outer Field and each option's Field get invalid, so the legend and the radios turn the destructive color, and FieldError appears under the description.

What happens on a valid submit?

The handler prevents the page reload and shows You selected: with the option's label next to the button. Replace that line with your own save or request.

Can I pick an option from the keyboard?

Yes. Tab moves focus to the first item, since none is checked. The arrow keys move to another item and check it, Space checks the focused item, and Enter on the button submits.