- Label
- Icon label
- Field
- Form
- Frontmatter form
- Questionnaire
- Input
- Input group
- Input OTP
- Password input
- Password strength
- Code input
- Number field
- Tag input
- Textarea
- Checkbox
- Radio group
- Language toggle group
- Emoji appearance toggle group
- Emoji picker
- Avatar picker
- AI provider picker
- Select
- Native select
- Combobox
- Language combobox
- Slider
- Calendar
- Switch
- Alert
- Avatar
- Badge
- Card
- AI provider card
- Model info card
- Model list
- Empty
- Item
- Tree item
- File tree
- Icon media
- File type icon
- Font preview
- Image preview
- Kbd
- Table
- Data table
- Data table column header
- Chart
- Code Block
- Code card
- Markdown view
- Attachment
- Bubble
- Marker
- Message
- Chat message
- Chat suggestion item
- Message scroller
- Trace
- Progress
- Spinner
- Sonner
- Toast
- Unsaved changes toolbar
- Unsaved indicator
- Status indicator
- Permission card
- Separator
- Skeleton
- Animated grid pattern
- Meteors
A form that asks one question at a time, with choices, a free-text answer, validation and steps.
Installation#
pnpm dlx shadcn@latest add @zui/questionnaireThe CLI writes components/ui/questionnaire.tsx, installs Button,
Input, Text,
Icon, Focus visible and Utils, and adds lucide-react-native.
Usage#
import {
Questionnaire,
QuestionnaireActions,
QuestionnaireChoice,
QuestionnaireChoiceDescription,
QuestionnaireChoices,
QuestionnaireDescription,
QuestionnaireError,
QuestionnaireInput,
QuestionnaireItem,
QuestionnaireNext,
QuestionnairePrevious,
QuestionnaireProgress,
QuestionnaireSkip,
QuestionnaireSubmit,
QuestionnaireTitle,
type QuestionnaireValues,
} from '@/components/ui/questionnaire';Define the questions once, pass them to Questionnaire for the order of the steps, and map them into the parts:
const items = [
{
name: 'direction',
required: true,
prompt: 'What should we prototype next?',
description: 'Choose a direction or write your own.',
choices: [
{ value: 'delegation', label: 'Delegation', description: 'Show how work moves to a specialist.' },
{ value: 'both', label: 'Both together' },
],
},
{
name: 'detail',
required: false,
prompt: 'How much detail should it include?',
description: 'Skip this if you are not sure yet.',
choices: [
{ value: 'focused', label: 'Focused' },
{ value: 'complete', label: 'Complete flow' },
],
},
] as const;<Questionnaire items={items} onSubmit={(answers: QuestionnaireValues) => save(answers)}>
<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}>
<Text className="font-medium">{choice.label}</Text>
{'description' in choice ? (
<QuestionnaireChoiceDescription>{choice.description}</QuestionnaireChoiceDescription>
) : null}
</QuestionnaireChoice>
))}
</QuestionnaireChoices>
<QuestionnaireError />
</QuestionnaireItem>
))}
<QuestionnaireActions>
<QuestionnairePrevious />
<QuestionnaireSkip />
<QuestionnaireNext />
<QuestionnaireSubmit />
</QuestionnaireActions>
</Questionnaire>onSubmit gets each answered question's answer by its name: the choice's value or the text typed in a
QuestionnaireInput, and a list for a multiple question. A skipped question is left out.
How it steps#
- Only the question shown is drawn. The others keep their answers while hidden.
- Next goes on once the question can be left: answered, or
invalidis not set. A required question left unanswered shows itsQuestionnaireError, which a screen reader announces. - Skip shows for a question that is not required. It clears the answer, marks the question skipped and goes on, or finishes on the last question.
- Submit shows on the last question. It goes back to the first question that cannot be left yet, or hands the
answers to
onSubmit. - Each step moves the screen reader to the new question's title on a device, and the keyboard focus to the question on the web.
- Return in a
QuestionnaireInputthat holds the answer goes on or submits. Shift with Return breaks the line of amultilineone. - On the web, Ctrl or Cmd with Enter goes on or submits, and so does Enter on a picked choice. Space picks a
choice. The up and down arrows move between the answers, picking a radio, and the left and right arrows move
between radios, or step back and forward once the question is answered from anywhere else. With
shortcuts, each choice shows the letter or number that picks it. Each part names the keys it answers inaria-keyshortcuts. - In development, the Questionnaire warns where
itemsand the rendered questions disagree.
API reference#
Questionnaire#
Takes every prop of React Native's View, plus:
| Prop | Type | Default |
|---|---|---|
items | QuestionnaireItemDefinition[] | - |
item | string | - |
defaultItem | string | - |
onItemChange | (item: string) => void | - |
shortcuts | 'letters' | 'numbers' | - |
onSubmit | (values: QuestionnaireValues) => void | - |
Without items, the steps are the rendered questions in the order they mount.
QuestionnaireProgress#
Takes every prop of Text. Reads "Question 2 of 3" unless given its own children, announced as a progress bar and read out as it changes.
QuestionnaireItem#
Takes every prop of React Native's View, plus name (string), multiple, required, invalid and
disabled (each boolean, false by default), and onStatusChange, called with 'unanswered', 'answered'
or 'skipped'. It is a radio group, or a group for multiple, named by its title.
QuestionnaireTitle, QuestionnaireDescription, QuestionnaireChoiceDescription, QuestionnaireError#
Each takes every prop of Text. The error reads "Choose an answer to continue." for a required question and "Choose an answer or skip this question." for another, unless given its own text.
QuestionnaireChoices#
Takes every prop of React Native's View.
QuestionnaireChoice#
Takes every prop of React Native's Pressable, plus value (string), checked, defaultChecked, disabled
and onChange, called with whether the choice is now picked. It is a radio, or a checkbox in a multiple
question, named by its text. An onPress, or an onKeyDown on the web, runs before the choice's own handling,
which it skips by calling preventDefault().
QuestionnaireInput#
Takes every prop of Input. It answers the question while it holds text.
Name it with aria-label.
QuestionnaireActions#
Takes every prop of React Native's View.
QuestionnairePrevious, QuestionnaireSkip, QuestionnaireNext, QuestionnaireSubmit#
Each takes every prop of Button. Previous and Skip are outline, and each
is labelled with its own word unless given children.
Differences from upstream#
- Upstream's behaviour comes from
@shadcn/react, which draws a form, fieldsets and inputs. A device has none of those, so the native item keeps the parts and props and holds the answers in the Questionnaire itself. - Upstream's
onSubmitgets the form's submit event and reads the answers from its form data. A device has no form to submit, soonSubmitgets the answers. - Upstream's
QuestionnaireChoicetakes the input's change event inonChange. The native one takes whether the choice is now picked. - Upstream's shortcuts and arrow keys work wherever there is a keyboard. On a device, where a keyboard is rare and React Native reports no key presses, there are no shortcuts, and the shortcut letters are not shown.
- Upstream's parts take a
renderprop. The native ones take children.
On this page
InstallationUsageHow it stepsAPI referenceQuestionnaireQuestionnaireProgressQuestionnaireItemQuestionnaireTitle, QuestionnaireDescription, QuestionnaireChoiceDescription, QuestionnaireErrorQuestionnaireChoicesQuestionnaireChoiceQuestionnaireInputQuestionnaireActionsQuestionnairePrevious, QuestionnaireSkip, QuestionnaireNext, QuestionnaireSubmitDifferences from upstream