Skip to content
Ballpoint

Questionnaire

Questions one at a time: choose from a few answers or write your own, step back and forward, skip what is optional, and send them all at once as an ordinary form.

From shadcn/ui
x
What should we draw next?

Choose a direction or write your own.

Installation

pnpm dlx shadcn@latest add @ballpoint/questionnaire

Usage

tsx
import { Questionnaire, QuestionnaireActions, QuestionnaireChoice, QuestionnaireChoices, QuestionnaireItem, QuestionnaireNext, QuestionnaireSubmit, QuestionnaireTitle } from "@/components/ui/questionnaire"

<Questionnaire onSubmit={(event) => { event.preventDefault(); const answers = new FormData(event.currentTarget) }}>
  <QuestionnaireItem name="direction" required>
    <QuestionnaireTitle>What should we draw next?</QuestionnaireTitle>
    <QuestionnaireChoices>
      <QuestionnaireChoice value="margins">Margin notes</QuestionnaireChoice>
      <QuestionnaireChoice value="maps">Maps</QuestionnaireChoice>
    </QuestionnaireChoices>
  </QuestionnaireItem>
  <QuestionnaireActions>
    <QuestionnaireNext />
    <QuestionnaireSubmit />
  </QuestionnaireActions>
</Questionnaire>

Examples

Several answers, with shortcuts

A question that takes more than one; `shortcuts` numbers the answers and lets the keys 1–9 choose them.

API reference

Questionnaire items
{ name, required?, multiple? }[]

The questions in order. Given, the count and the first question render on the server; without it they are learnt from the items inside.

Questionnaire activeItem / defaultActiveItem / onActiveItemChange
string

The question showing, by name, controlled or not.

Questionnaire defaultAnswers
Record<string, string | string[]>

Answers to start from, to resume a saved form.

Questionnaire shortcuts
default false
boolean

Number the answers and let the keys 1–9 choose them.

Questionnaire onSubmit
(event) => void

Called once every required question is answered. Read the answers with new FormData(event.currentTarget).

QuestionnaireItem name / required / multiple
string / boolean / boolean

The answer's name in the form data; whether it can be left; radios or checkboxes.

QuestionnaireInput
Input props

A write-in answer. It needs a label: aria-label will do. Typing sets a chosen answer aside, for a question that takes one.

QuestionnaireSkip / Next / Previous / Submit
Button props

Skip is only shown for optional questions; Submit only on the last.