Atoms

Questionnaire

A multi-step questionnaire with single-choice, multiple-choice, freeform, and skippable questions.

shadcn/ui
Docs

Installation#

npx shadcn@latest add @formance/questionnaire

Install the following dependencies:

npm install @shadcn/react lucide-react

Copy and paste the following code into your project.

Update the import paths to match your project setup.

Usage#

import {  Questionnaire,  QuestionnaireActions,  QuestionnaireChoice,  QuestionnaireChoices,  QuestionnaireDescription,  QuestionnaireError,  QuestionnaireInput,  QuestionnaireItem,  QuestionnaireNext,  QuestionnairePrevious,  QuestionnaireProgress,  QuestionnaireSkip,  QuestionnaireSubmit,  QuestionnaireTitle,} from '@/components/ui/questionnaire';
const items = [  {    name: 'difference',    required: true,    prompt: 'How does yours differ?',    description: 'Choose a difference or write your own.',    choices: [      {        value: 'two-banks',        label: 'Two banks, not one',        description: 'Client funds are split across two omnibus accounts.',      },      {        value: 'multi-currency',        label: 'EUR and USD balances',        description: 'Each client holds a balance in both currencies.',      },      { value: 'interest', label: 'Daily interest accrual' },    ],    input: { label: 'Another difference', placeholder: 'Type another answer…' },  },  {    name: 'flows',    required: false,    prompt: 'Which flows should the schema cover?',    description: 'Skip this if you are not sure yet.',    choices: [      { value: 'deposits', label: 'Deposits' },      { value: 'withdrawals', label: 'Withdrawals' },    ],  },] as const;

Define the items one time. Give them to Questionnaire for server-rendered progress, actions, and shortcuts. Then map them into the parts.

<Questionnaire items={items} onSubmit={handleSubmit}>  <QuestionnaireProgress />  {items.map((question) => (    <QuestionnaireItem      key={question.name}      name={question.name}      required={question.required}    >      <QuestionnaireTitle>{question.prompt}</QuestionnaireTitle>      <QuestionnaireDescription>        {question.description}      </QuestionnaireDescription>      <QuestionnaireChoices>        {question.choices.map((choice) => (          <QuestionnaireChoice key={choice.value} value={choice.value}>            <span className="font-medium">{choice.label}</span>            {'description' in choice ? (              <span className="text-muted-foreground">                {choice.description}              </span>            ) : null}          </QuestionnaireChoice>        ))}        {'input' in question ? (          <QuestionnaireInput            aria-label={question.input.label}            placeholder={question.input.placeholder}          />        ) : null}      </QuestionnaireChoices>      <QuestionnaireError />    </QuestionnaireItem>  ))}  <QuestionnaireActions>    <QuestionnairePrevious />    <QuestionnaireSkip />    <QuestionnaireNext />    <QuestionnaireSubmit />  </QuestionnaireActions></Questionnaire>
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {  event.preventDefault();  const answers = new FormData(event.currentTarget);  // answers.get('difference'), or answers.getAll('flows') for a multiple item.}

Composition#

Questionnaire├── QuestionnaireProgress├── QuestionnaireItem│   ├── QuestionnaireTitle│   ├── QuestionnaireDescription│   ├── QuestionnaireChoices│   │   ├── QuestionnaireChoice│   │   └── QuestionnaireInput│   └── QuestionnaireError└── QuestionnaireActions    ├── QuestionnairePrevious    ├── QuestionnaireSkip    ├── QuestionnaireNext    └── QuestionnaireSubmit

Questionnaire controls the order of the items, the active item, the answers, the validation, the progress, and the navigation. The page, card, dialog, or drawer around it controls the close and cancel behavior, the storage of the answers, the transport, and the branches.

Server rendering#

Give items to Questionnaire to server-render the active item, the progress, the actions, and the answer shortcuts. The headless Questionnaire page gives the full behavior.

Examples#

Multiple selection#

Use multiple for an item that accepts more than one fixed answer.

Freeform answer#

Add a QuestionnaireInput to the fixed choices when the user can give a different answer.

This is the only example on this page that puts a choice and an input in the same item. In next dev, it shows a fallback message instead of the form. See Known issue.

Explicit skip#

Add QuestionnaireSkip when the user can leave an optional item without an answer.

Shortcuts#

Use shortcuts to give each answer a letter key or a number key.

Custom validation#

Use controlled navigation with a schema library such as Zod. When an answer is not valid, go back to its item and show the error.

Controlled#

Control the active item from the state of the host, for example to go back to a step that is not valid.

Resume#

Restore a saved active item and saved default answers. A reset then goes back to that saved state.

Conditional items#

Disable the items that do not apply after the earlier answers of the user.

Read the status of the item to disable the navigation and to change the style of the actions.

Custom progress#

Use the render state of QuestionnaireProgress to make a custom progress indicator.

Animated items#

Animate the active item. The progress and the navigation do not move.

Card#

Put the questionnaire in the slots of a Card. The title and the description of the question keep their semantic elements.

Dialog#

Put the questionnaire in a Dialog. The host controls the cancel and close behavior.

Disabled choice#

Set disabled on a QuestionnaireChoice and on its entry in items to show a choice that the person cannot select.

No description#

QuestionnaireDescription is optional. Leave it out when the title is sufficient.

Accessibility#

QuestionnaireItem renders a fieldset, and QuestionnaireTitle renders its legend. The descriptions and the active errors are linked to the current item. Items and answer controls that are not valid have aria-invalid.

Fixed choices keep the native radio and checkbox behavior. The progress is a named progressbar, and the navigation uses real buttons. Items and actions that are not active are hidden and inert. After a successful navigation, the focus goes to the new active item. After a failed validation, the focus goes to an answer control.

Always give QuestionnaireInput an accessible name: a visible label, aria-label, or aria-labelledby. A placeholder is not a label.

Formance differences#

The props and the parts are the same as in shadcn/ui. The navigation parts (QuestionnairePrevious, QuestionnaireSkip, QuestionnaireNext, and QuestionnaireSubmit) accept the size and variant props of the Button. The Formance Button has no default size or variant, so the default size is md, and the default variant of Next and Submit is primary.

Known issue#

In @shadcn/react 0.3.1, an item that has a QuestionnaireChoice and a QuestionnaireInput causes an infinite render loop ("Maximum update depth exceeded"). The raw primitive does this without the Formance wrapper. The loop occurs in next dev (Strict Mode is on by default). It did not occur in a plain Vite app with Strict Mode on. Production builds are not verified.

Two defects in the primitive cause the loop:

  1. mergeProps (use-render/index.ts) wraps each ref in a new callback on each render, also when there is only one ref. React detaches and attaches each ref on each commit. The primitive keeps its elements in state through these refs, so each commit sets state two times.
  2. useQuestionnaireItem sorts the answer controls into a new array on each render (use-questionnaire-item.ts). When React applies the queued registration updates again, the array has a new identity, so the item registers itself with the root again. The root changes state, the item renders again, and the loop does not stop.

Both fixes are necessary: with only one of them, the loop continues. The bug is reported upstream in shadcn-ui/ui#12299, which links it to React 19.3 (Next.js 16.3 bundles a React 19.3 canary). Until a fixed version ships, keep the input in its own item. Only the Freeform answer example uses this combination. In next dev, it shows a fallback message instead of the form.

Unstyled#

The behavior of Questionnaire comes from the @shadcn/react package. To use it with your own markup and styles, see Questionnaire in @shadcn/react.

Compound Components

Compose the pieces you need:

<Questionnaire />Root form. Holds the ordered items, the active item, the answers, the validation, and the navigation.
<QuestionnaireItem />One question, rendered as a fieldset. Set multiple, required, or disabled.
<QuestionnaireTitle />The question, rendered as the legend of the item.
<QuestionnaireDescription />Help text linked to the item.
<QuestionnaireChoices />Container for the answers of the item.
<QuestionnaireChoice />One fixed answer, with a native radio or checkbox.
<QuestionnaireChoiceDescription />Secondary text inside a choice.
<QuestionnaireInput />Freeform answer next to the fixed choices. Needs an accessible name.
<QuestionnaireError />The validation error of the active item.
<QuestionnaireProgress />Progress bar. Accepts a render prop for a custom indicator.
<QuestionnaireActions />Layout for the navigation buttons.
<QuestionnairePrevious />Goes to the previous item. Accepts Button size and variant.
<QuestionnaireSkip />Leaves an optional item without an answer.
<QuestionnaireNext />Validates the item and goes to the next one.
<QuestionnaireSubmit />Submits the form on the last item.