Skip to content

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

Components

Free

Questionnaire for Radix UI

A questionnaire for React and Radix UI projects that asks one question at a time, with choices, a typed answer, skip and keyboard shortcuts.

On this page9 sections

A questionnaire asks one question at a time, with a few choices or a typed answer and buttons to go back, skip or move on, and posts every answer as one form at the end. It suits onboarding, a survey or an intake where each answer is quick. Every base builds it on the same primitive from @shadcn/react, so its props match everywhere.

When people should see and correct everything at once, a form of Field groups is faster, and when each step holds several fields, use Steps.

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

Questionnaire anatomy: progress, item, title, choice, previous, skip, next

Usage

import type { FormEvent } from "react";
import {
  Questionnaire, QuestionnaireActions, QuestionnaireChoice,
  QuestionnaireChoices, QuestionnaireDescription, QuestionnaireError,
  QuestionnaireItem, QuestionnaireNext, QuestionnairePrevious,
  QuestionnaireProgress, QuestionnaireSkip, QuestionnaireSubmit,
  QuestionnaireTitle,
} from "@/components/ui/questionnaire";

items lists the questions in order, each with its name, its required flag and its choices, and it has to agree with the rendered items. Every question needs an answer or a skip before Next moves on, and required takes the Skip button away.

const items = [
  {
    choices: [{ value: "email" }, { value: "chat" }, { value: "phone" }],
    name: "contact",
    required: true,
  },
  {
    choices: [{ value: "billing" }, { value: "fixes" }, { value: "features" }],
    name: "topics",
  },
] as const;

function handleSubmit(event: FormEvent<HTMLFormElement>) {
  event.preventDefault();
  const answers = new FormData(event.currentTarget);
  console.log(answers.get("contact"), answers.getAll("topics"));
}
<Questionnaire className="w-full max-w-sm" items={items} onSubmit={handleSubmit}>
  <QuestionnaireProgress />
  <QuestionnaireItem name="contact" required>
    <QuestionnaireTitle>How should we reach you?</QuestionnaireTitle>
    <QuestionnaireChoices>
      <QuestionnaireChoice value="email">Email</QuestionnaireChoice>
      <QuestionnaireChoice value="chat">Chat</QuestionnaireChoice>
      <QuestionnaireChoice value="phone">Phone</QuestionnaireChoice>
    </QuestionnaireChoices>
    <QuestionnaireError />
  </QuestionnaireItem>
  <QuestionnaireItem multiple name="topics">
    <QuestionnaireTitle>What should our updates cover?</QuestionnaireTitle>
    <QuestionnaireDescription>Pick any, or skip.</QuestionnaireDescription>
    <QuestionnaireChoices>
      <QuestionnaireChoice value="billing">Billing</QuestionnaireChoice>
      <QuestionnaireChoice value="fixes">Bug fixes</QuestionnaireChoice>
      <QuestionnaireChoice value="features">New features</QuestionnaireChoice>
    </QuestionnaireChoices>
    <QuestionnaireError />
  </QuestionnaireItem>
  <QuestionnaireActions>
    <QuestionnairePrevious />
    <QuestionnaireSkip />
    <QuestionnaireNext />
    <QuestionnaireSubmit />
  </QuestionnaireActions>
</Questionnaire>

Accessibility

Each question is a fieldset named by its QuestionnaireTitle, which renders a legend, and its description and any error are tied to it through aria-describedby. Questions that aren't current are hidden and inert, and Next or Previous moves focus to the new question. QuestionnaireProgress is a progressbar in a polite live region, and an error is an alert. Left to you: a label for each QuestionnaireInput, an aria-labelledby on the item when render turns the title into something other than a legend, and your own words for the English defaults.

Tab
Moves focus into the current question's answers, then on to the buttons. Questions that aren't current are inert, so focus never lands in them.
Space
When focus is on a choice, checks it, or toggles it in a question with multiple.
Arrow Down
Moves focus to the next answer, and from the last back to the first. In a single-answer question it also selects that choice. A text answer with text in it keeps the key for itself.
Arrow Up
Moves focus to the previous answer, and from the first to the last. In a single-answer question it also selects that choice. A text answer with text in it keeps the key for itself.
Arrow Left
Goes back to the previous question, unless focus is on a single-answer choice or in a text answer.
Arrow Right
Moves on to the next question once the current one is answered or skipped, unless focus is on a single-answer choice or in a text answer.
Enter
When focus is on a chosen answer or a filled text answer, moves on to the next question, or submits on the last one.
Ctrl+Enter
From anywhere in the questionnaire, moves on to the next question, or submits on the last one, once the current question has its answer. Cmd + Enter does the same on a Mac.
A-Z
With shortcuts set to letters, picks the choice marked with that letter, or toggles it in a question with multiple, while focus is outside a text answer.
1-9
With shortcuts set to numbers, picks the choice marked with that number, or toggles it in a question with multiple, while focus is outside a text answer.

Demos

Multiple

multiple on QuestionnaireItem turns the choices into checkboxes, here the sources an agent should inspect: source files, tests and docs. Every checked value submits under the item's name, so read them with getAll, since get returns only the first. Use it when several answers are honest and you can act on any combination of them.

Preview width

Freeform

A QuestionnaireInput after the three choices lets people type an approach the options missed. Typing clears the chosen option and the text submits under the item's name in its place, while picking an option again leaves the text on screen but out of the submission. The input has no visible label, so it carries an aria-label.

Preview width

Skip

The middle question, about constraints, is optional, so QuestionnaireSkip shows on it and stays hidden on the two required ones around it. Skipping clears any choice there, sends nothing for it, and fires onStatusChange with skipped, which the toast after Save uses to tell a skip from an answer. Offer it on a question that helps but shouldn't block anyone.

Preview width

Shortcuts

shortcuts gives each choice a key at the end of its row, letters from A or numbers from 1, and the select above switches between the two or turns them off. A key picks its choice while focus is anywhere in the questionnaire outside a text answer. Use letters for a tool people drive from the keyboard, and leave them off on a survey that's mostly tapped.

Preview width

Validation

Both questions are required, and on submit a Zod schema checks them together: a public audience with only a summary fails. The handler stores the message, marks the detail question invalid, sends people back to it through the controlled item, and shows the message in QuestionnaireError. Use this for rules across answers, since a missing answer is caught before onSubmit ever runs.

Preview width

Controlled

item and onItemChange keep the current question's name in state, and the line above the form turns it into a checkpoint label. Take control when something outside the questionnaire has to know where people are, such as a list of steps beside it, or has to send them to a question. Setting item from your own code also moves focus to that question.

Preview width

Resume

The flow reopens on its second question, with a saved migration type, two checked verification steps and a note filled in through defaultChecked and defaultValue. A plain button with type set to reset puts every answer back to those defaults and returns to defaultItem, and onReset shows a toast. Use it when people come back to a draft they left halfway.

Preview width

Conditional

The environment question is disabled, on its QuestionnaireItem and in items, until Cloud is picked, so a local run goes straight from runtime to approval. A disabled question leaves the progress count and the submission, and the runtime choice is held in state so the list updates as people click. Use it for a follow-up that only applies to one answer.

Preview width

onStatusChange on each item records whether it's answered, and Next and Submit get disabled while the current one isn't. Next also takes a class keyed on its data-status, which reads unanswered, answered or skipped. Use this when a blocked button should look blocked. Without it, Next stays enabled and shows the error when pressed.

Preview width

Progress

QuestionnaireProgress takes a render function that receives the element's props and a state with current and total, and here it draws one bar segment per question with the count under it. Spread the props onto your element so the progress bar role and its live announcement stay. Use it when the default line of text is too quiet for a longer flow.

Preview width

Animated

Each QuestionnaireItem gets a class keyed on data-active that fades it in and lifts it 8 pixels over 300 milliseconds, and motion-reduce turns the animation off. Only the entrance animates, because a question that loses data-active is hidden at once. Keep it short, since people sit through it on every Next.

Preview width

Card

The questions sit in a Card: render turns each title into a CardTitle and each description into a CardDescription, progress sits in CardAction and the buttons in CardFooter. A CardTitle isn't a legend, so each item gets an aria-labelledby pointing at its title's id. Use the card when the questionnaire is the main thing on the page.

Preview width

Dialog

A button opens a Dialog with two questions. A successful submit closes it, and Cancel closes it without sending anything. A visually hidden DialogTitle names the dialog, and each question's title has an id that its QuestionnaireItem points at with aria-labelledby, as in the Card demo. Use a dialog for a quick side task, such as clarifying a request, while the page behind stays where it is.

Preview width

FAQ

When should I use a Questionnaire instead of a regular form?

Use a Questionnaire when each question is quick and people should take them one at a time: onboarding, a survey, a quiz, or an intake where a later question depends on an earlier one. Only the current question is on screen, and every answer still posts as one form at the end.

A form built from Field groups shows everything at once, which suits settings and anything people scan or correct. When each step holds several fields, such as an address, use Steps.

How do I get the answers from a Questionnaire?

Handle onSubmit on Questionnaire, which renders a form, call preventDefault to stay on the page, and read FormData by each item's name. A question with multiple needs getAll, since get returns only the first checked value.

The handler runs only once every enabled question is answered or skipped. Until then, Submit jumps back to the first one that isn't. A skipped or disabled question sends nothing, and a typed answer goes under its item's name in place of a choice.

How do required and optional questions work in a Questionnaire?

Every question needs an answer before Next moves on, required or not. The difference is the way out: an optional question shows QuestionnaireSkip and its error reads "Choose an answer or skip this question.", while required hides Skip and the error says "Choose an answer to continue."

Set required on the QuestionnaireItem and on its entry in items, because Skip reads it from items when you pass them, and the console warns when the two disagree. Pass your own text as the children of QuestionnaireError to replace the default message.

How do I show a Questionnaire question only for some answers?

Hold the deciding answer in state through checked and onChange on its choices, and set disabled on the follow-up's QuestionnaireItem and on its entry in items until that answer is picked. A disabled question drops out of navigation, the progress count and the submission, as the cloud environment question does in the Conditional demo.

Build the items array in a useMemo that depends on that state, so the list changes when the answer does.

How do I control which question a Questionnaire shows in React?

Pass the current question's name as item and update it in onItemChange, as the Controlled demo does. Setting item from your own code moves to that question and puts focus on it. A name that isn't an enabled question sends people to the first one, through onItemChange.

To choose only where it opens, pass defaultItem instead. The Resume demo opens a saved draft on its second question that way.

Why don't my Questionnaire keyboard shortcuts work?

The keys only work while focus is inside the questionnaire and outside a text answer, because the form itself listens for them. After someone clicks a choice or presses Next, focus is inside, but on a fresh page it isn't until they click or tab in.

When you pass items, a choice gets a key only if it's listed in its item's choices, in that order. A choice left out has none, and the console says so. letters covers 26 choices and numbers stops at 9.

Why is my Questionnaire blank until the page hydrates?

Without items, the questionnaire learns its questions as they mount in the browser, so the server's HTML has no progress text, no buttons and every question hidden unless defaultItem names one. Pass items, the list of questions with their name, required and disabled, and the server renders the first question with its progress and buttons.

The same list sets the question order and the shortcut keys, so keep it in step with the rendered items.

How do I validate a typed email in a Questionnaire?

Set type to email on QuestionnaireInput and noValidate to false on Questionnaire. By default the form skips the browser's own checks, so a typed answer only has to be non-empty. With them on, Next and Submit stop at a filled field that fails its type and show the browser's message.

The input's type also takes url, tel, number, date and a few other values. For rules the browser can't check, validate in onSubmit and set invalid on the item, as the Validation demo does.

API Reference

Questionnaire

<form> data-slot="questionnaire"

PropType
item?string

Controlled active item name.

defaultItem?string

Initially active item name. Defaults to the first enabled item.

onItemChange?(item: string) → void

Called when navigation requests a different item.

Parameters

item – string

Returns
void
items?readonly QuestionnaireItemDefinition[]

Optional item data for server rendering and item order.

Type
readonly QuestionnaireItemDefinition[]
noValidate?boolean

Disables native constraint UI while preserving questionnaire validation.

Default
true
onReset?FormEventHandler<HTMLFormElement>

Native reset handler. Prevent default to stop questionnaire reset.

Type
FormEventHandler<HTMLFormElement>
onSubmit?FormEventHandler<HTMLFormElement>

Native form submission handler after every item validates.

Type
FormEventHandler<HTMLFormElement>
shortcuts?"letters" | "numbers"

Assigns scoped answer shortcuts in definition or rendered DOM order.

Type
"letters" | "numbers"
className?string

Merged with the part's classes.

AttributeType
data-currentstring

One-based active item position.

data-first?string

Present on the first item.

data-last?string

Present on the last item.

data-shortcuts?"letters" | "numbers"

"letters" or "numbers", when enabled.

Type
"letters" | "numbers"
data-totalstring

Number of enabled items.

QuestionnaireActions

<div> data-slot="questionnaire-actions"

PropType
className?string

Merged with the part's classes.

QuestionnaireChoice

<label> data-slot="questionnaire-choice"

PropType
valuestring

Native answer value.

checked?boolean

Controlled checked state.

defaultChecked?boolean

Initial checked state.

Default
false
disabled?boolean

Disables the native answer control.

Default
false
onChange?React.ChangeEventHandler<HTMLInputElement>

Native input change handler.

Type
React.ChangeEventHandler<HTMLInputElement>
render?ReactElement | ((props: object, state: object) => ReactElement)

Custom render target with choice state. Its function's state has checked, disabled, invalid, shortcut and type.

Type
ReactElement | ((props: object, state: object) => ReactElement)
Default
<label>
className?string

Merged with the part's classes.

AttributeType
data-checked?string

Present when selected.

data-disabled?string

Present when disabled.

data-invalid?string

Present when its item is invalid.

data-shortcutstring

Assigned letter or number.

data-type"radio" | "checkbox"

"radio" or "checkbox".

Type
"radio" | "checkbox"
data-unchecked?string

Present when not selected.

QuestionnaireChoiceDescription

<span> data-slot="questionnaire-choice-description"

PropType
className?string

Merged with the part's classes.

QuestionnaireChoices

<div> data-slot="questionnaire-choices"

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Custom render target. Its function's state has shortcuts.

Type
ReactElement | ((props: object, state: object) => ReactElement)
Default
<div>
className?string

Merged with the part's classes.

AttributeType
data-shortcuts?"letters" | "numbers"

"letters" or "numbers", when enabled.

Type
"letters" | "numbers"

QuestionnaireDescription

<p> data-slot="questionnaire-description"

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Custom render target.

Type
ReactElement | ((props: object, state: object) => ReactElement)
Default
<p>
className?string

Merged with the part's classes.

QuestionnaireError

<p> data-slot="questionnaire-error"

PropType
children?React.ReactNode

Custom validation message. Defaults to the contextual message.

render?ReactElement | ((props: object, state: object) => ReactElement)

Custom render target with invalid state. Its function's state has invalid.

Type
ReactElement | ((props: object, state: object) => ReactElement)
Default
<p>
className?string

Merged with the part's classes.

AttributeType
data-invalid?string

Present while the error is active.

QuestionnaireInput

<div> data-slot="questionnaire-input"

PropType
defaultValue?string | number | readonly string[]

Initial native input value.

Type
string | number | readonly string[]
disabled?boolean

Disables the input.

Default
false
onChange?ChangeEventHandler<HTMLInputElement>

Native input change handler.

Type
ChangeEventHandler<HTMLInputElement>
type?"date" | "datetime-local" | "email" | "month" | "number" | "password" | "search" | "tel" | "text" | "time" | "url" | "week"

A text-entry type such as "email", "number", or "date".

Type
"date" | "datetime-local" | "email" | "month" | "number" | "password" | "search" | "tel" | "text" | "time" | "url" | "week"
Default
"text"
value?string | number | readonly string[]

Controlled native input value.

Type
string | number | readonly string[]
render?ReactElement | ((props: object, state: object) => ReactElement)

Custom render target with input state. Its function's state has disabled, filled and invalid.

Type
ReactElement | ((props: object, state: object) => ReactElement)
Default
<input>
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-empty?string

Present when the input has no answer text.

data-filled?string

Present when the input has non-empty text.

data-invalid?string

Present when its item is invalid.

QuestionnaireItem

<fieldset> data-slot="questionnaire-item"

PropType
namestring

Unique item identifier and native form field name.

disabled?boolean

Omits the item from progress and navigation.

Default
false
invalid?boolean

Marks the item invalid from an external validator.

Default
false
multiple?boolean

Renders fixed choices as checkboxes.

Default
false
onStatusChange?(status: QuestionnaireItemStatus) → void

Observes "unanswered", "answered", and "skipped" changes.

Type
(status: QuestionnaireItemStatus) → void
Parameters

status – QuestionnaireItemStatus

Returns
void
required?boolean

Requires an answer and prevents Skip.

Default
false
className?string

Merged with the part's classes.

AttributeType
data-active?string

Present when active.

data-disabled?string

Present when disabled.

data-invalid?string

Present after failed validation.

data-multiple?string

Present when multiple.

data-required?string

Present when required.

data-status"unanswered" | "answered" | "skipped"

"unanswered", "answered" or "skipped".

Type
"unanswered" | "answered" | "skipped"

QuestionnaireNext

<button> data-slot="questionnaire-next"

PropType
size?"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"

The size.

Type
"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"
Default
"md"
variant?"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"

The visual style.

Type
"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"
Default
"default"
render?ReactElement | ((props: object, state: object) => ReactElement)

Custom render target with navigation state. Its function's state has disabled, shortcut, status and visible.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-hidden?string

Present when the action does not apply.

data-shortcut?"Enter"

"Enter", on an enabled, visible Next or Submit.

data-size?string

Mirrors size.

data-status"unanswered" | "answered" | "skipped"

Active item: "unanswered", "answered", or "skipped".

Type
"unanswered" | "answered" | "skipped"
data-variant?string

Mirrors variant.

data-visible?string

Present when the action applies to the active item.

QuestionnairePrevious

<button> data-slot="questionnaire-previous"

PropType
size?"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"

The size.

Type
"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"
Default
"md"
variant?"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"

Button's default: default.

Type
"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"
Default
"outline"
render?ReactElement | ((props: object, state: object) => ReactElement)

Custom render target with navigation state. Its function's state has disabled, shortcut, status and visible.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-hidden?string

Present when the action does not apply.

data-shortcut?"Enter"

"Enter", on an enabled, visible Next or Submit.

data-size?string

Mirrors size.

data-status"unanswered" | "answered" | "skipped"

Active item: "unanswered", "answered", or "skipped".

Type
"unanswered" | "answered" | "skipped"
data-variant?string

Mirrors variant.

data-visible?string

Present when the action applies to the active item.

QuestionnaireProgress

<div> data-slot="questionnaire-progress"

PropType
aria-label?string

Accessible progressbar name.

Default
Questionnaire progress
children?React.ReactNode

Custom progress content.

Default
Question {current} of {total}
render?ReactElement | ((props: object, state: object) => ReactElement)

Custom render target with progress state. Its function's state has current, first, last and total.

Type
ReactElement | ((props: object, state: object) => ReactElement)
Default
<div>
className?string

Merged with the part's classes.

AttributeType
data-currentstring

One-based active item position.

data-first?string

Present on the first item.

data-last?string

Present on the last item.

data-totalstring

Number of enabled items.

QuestionnaireSkip

<button> data-slot="questionnaire-skip"

PropType
size?"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"

The size.

Type
"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"
Default
"md"
variant?"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"

Button's default: default.

Type
"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"
Default
"outline"
render?ReactElement | ((props: object, state: object) => ReactElement)

Custom render target with navigation state. Its function's state has disabled, shortcut, status and visible.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-hidden?string

Present when the action does not apply.

data-shortcut?"Enter"

"Enter", on an enabled, visible Next or Submit.

data-size?string

Mirrors size.

data-status"unanswered" | "answered" | "skipped"

Active item: "unanswered", "answered", or "skipped".

Type
"unanswered" | "answered" | "skipped"
data-variant?string

Mirrors variant.

data-visible?string

Present when the action applies to the active item.

QuestionnaireSubmit

<button> data-slot="questionnaire-submit"

PropType
size?"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"

The size.

Type
"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"
Default
"md"
variant?"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"

The visual style.

Type
"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"
Default
"default"
render?ReactElement | ((props: object, state: object) => ReactElement)

Custom render target with navigation state. Its function's state has disabled, shortcut, status and visible.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-hidden?string

Present when the action does not apply.

data-shortcut?"Enter"

"Enter", on an enabled, visible Next or Submit.

data-size?string

Mirrors size.

data-status"unanswered" | "answered" | "skipped"

Active item: "unanswered", "answered", or "skipped".

Type
"unanswered" | "answered" | "skipped"
data-variant?string

Mirrors variant.

data-visible?string

Present when the action applies to the active item.

QuestionnaireTitle

<legend> data-slot="questionnaire-title"

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Custom render target. Pair its id with the item’s aria-labelledby.

Type
ReactElement | ((props: object, state: object) => ReactElement)
Default
<legend>
className?string

Merged with the part's classes.