ZUI

Search documentation

Find a page or a heading.

Questionnaire

A form that asks one question at a time, with choices, a free-text answer, validation and steps.

Installation

bash

The CLI writes components/ui/questionnaire.tsx, installs Button, Input, Text, Icon, Focus visible and Utils, and adds lucide-react-native.

Usage

tsx

Define the questions once, pass them to Questionnaire for the order of the steps, and map them into the parts:

tsx
tsx

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 invalid is not set. A required question left unanswered shows its QuestionnaireError, 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 QuestionnaireInput that holds the answer goes on or submits. Shift with Return breaks the line of a multiline one.
  • 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 in aria-keyshortcuts.
  • In development, the Questionnaire warns where items and the rendered questions disagree.

API reference

Questionnaire

Takes every prop of React Native's View, plus:

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 onSubmit gets the form's submit event and reads the answers from its form data. A device has no form to submit, so onSubmit gets the answers.
  • Upstream's QuestionnaireChoice takes the input's change event in onChange. 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 render prop. The native ones take children.