Questionnaire
A multi-step form that asks one question at a time. Supports single choice, multiple choice, free text, skipping, and keyboard shortcuts.
View in Storybook (opens in a new tab)
Installation
import { Questionnaire } from "@ginga-ui/core";Basic usage
Pass the list of questions to items and render a QuestionnaireItem for each. Next and Submit stay disabled until required questions are answered. Read the answers from FormData in onSubmit.
import { useState } from "react";
import { Paragraph, Questionnaire, QuestionnaireActions, QuestionnaireChoice, QuestionnaireChoices, QuestionnaireInput, QuestionnaireItem, QuestionnaireNext, QuestionnairePrevious, QuestionnaireProgress, QuestionnaireSubmit, QuestionnaireTitle } from "@ginga-ui/core";
export function BasicExample() {
const questions = [
{
name: "feature",
title: "What should we build next?",
choices: [
{
value: "timeline",
label: "Tool call timeline",
description: "Show what ran and what came back.",
},
{
value: "approval",
label: "Approval checkpoints",
description: "Ask before irreversible actions.",
},
],
},
{
name: "audience",
title: "Who will use it most?",
choices: [
{
value: "developer",
label: "Developers",
description: "Build features with the components.",
},
{
value: "designer",
label: "Designers",
description: "Tune themes and visuals.",
},
],
},
];
const [result, setResult] = useState("");
return (
<Questionnaire
items={questions}
onSubmit={(event) => {
event.preventDefault();
const answers = new FormData(event.currentTarget);
setResult(
questions
.map(({ name }) => {
const answer = answers.get(name);
return `${name}: ${typeof answer === "string" ? answer : ""}`;
})
.join(", ")
);
}}
>
{questions.map((question) => (
<QuestionnaireItem key={question.name} name={question.name} required>
<QuestionnaireTitle>{question.title}</QuestionnaireTitle>
<QuestionnaireChoices>
{question.choices.map((choice) => (
<QuestionnaireChoice
key={choice.value}
value={choice.value}
description={choice.description}
>
{choice.label}
</QuestionnaireChoice>
))}
<QuestionnaireInput
aria-label="Other answer"
placeholder="Something else..."
/>
</QuestionnaireChoices>
</QuestionnaireItem>
))}
<QuestionnaireActions>
<QuestionnairePrevious size="sm">Previous</QuestionnairePrevious>
<QuestionnaireProgress>
{({ current, total }) => `Question ${current} of ${total}`}
</QuestionnaireProgress>
<QuestionnaireNext size="sm">Next</QuestionnaireNext>
<QuestionnaireSubmit size="sm">Submit</QuestionnaireSubmit>
</QuestionnaireActions>
{result && <Paragraph>{result}</Paragraph>}
</Questionnaire>
);
}Multiple choice
Specify multiple on an item to accept several answers. Use getAll to read them.
import { Questionnaire, QuestionnaireActions, QuestionnaireChoice, QuestionnaireChoices, QuestionnaireItem, QuestionnaireSubmit, QuestionnaireTitle } from "@ginga-ui/core";
export function MultipleExample() {
const platforms = [
{ value: "web", label: "Web" },
{ value: "ios", label: "iOS" },
{ value: "android", label: "Android" },
];
return (
<Questionnaire
items={[{ name: "platforms" }]}
onSubmit={(event) => {
event.preventDefault();
console.log(new FormData(event.currentTarget).getAll("platforms"));
}}
>
<QuestionnaireItem name="platforms" required multiple>
<QuestionnaireTitle>
Which platforms should it support?
</QuestionnaireTitle>
<QuestionnaireChoices>
{platforms.map((platform) => (
<QuestionnaireChoice key={platform.value} value={platform.value}>
{platform.label}
</QuestionnaireChoice>
))}
</QuestionnaireChoices>
</QuestionnaireItem>
<QuestionnaireActions>
<QuestionnaireSubmit size="sm">Submit</QuestionnaireSubmit>
</QuestionnaireActions>
</Questionnaire>
);
}Skipping
QuestionnaireSkip appears on items without required, letting users leave them unanswered.
import { Questionnaire, QuestionnaireActions, QuestionnaireChoice, QuestionnaireChoices, QuestionnaireInput, QuestionnaireItem, QuestionnaireNext, QuestionnairePrevious, QuestionnaireProgress, QuestionnaireSkip, QuestionnaireSubmit, QuestionnaireTitle } from "@ginga-ui/core";
export function SkipExample() {
return (
<Questionnaire
items={[{ name: "deadline" }, { name: "budget" }]}
onSubmit={(event) => event.preventDefault()}
>
<QuestionnaireItem name="deadline">
<QuestionnaireTitle>Do you have a deadline?</QuestionnaireTitle>
<QuestionnaireChoices>
<QuestionnaireChoice value="week">This week</QuestionnaireChoice>
<QuestionnaireChoice value="month">This month</QuestionnaireChoice>
</QuestionnaireChoices>
</QuestionnaireItem>
<QuestionnaireItem name="budget">
<QuestionnaireTitle>Is there a budget limit?</QuestionnaireTitle>
<QuestionnaireChoices>
<QuestionnaireInput
aria-label="Budget limit"
placeholder="e.g. $1,000"
/>
</QuestionnaireChoices>
</QuestionnaireItem>
<QuestionnaireActions>
<QuestionnairePrevious size="sm">Previous</QuestionnairePrevious>
<QuestionnaireProgress>
{({ current, total }) => `Question ${current} of ${total}`}
</QuestionnaireProgress>
<QuestionnaireSkip size="sm">Skip</QuestionnaireSkip>
<QuestionnaireNext size="sm">Next</QuestionnaireNext>
<QuestionnaireSubmit size="sm">Submit</QuestionnaireSubmit>
</QuestionnaireActions>
</Questionnaire>
);
}Keyboard shortcuts
Specify shortcuts to select choices with letter or number keys.
import { Questionnaire, QuestionnaireActions, QuestionnaireChoice, QuestionnaireChoices, QuestionnaireItem, QuestionnaireSubmit, QuestionnaireTitle } from "@ginga-ui/core";
export function ShortcutsExample() {
return (
<Questionnaire
items={[{ name: "tone" }]}
shortcuts="letters"
onSubmit={(event) => event.preventDefault()}
>
<QuestionnaireItem name="tone" required>
<QuestionnaireTitle>Choose a tone for replies</QuestionnaireTitle>
<QuestionnaireChoices>
<QuestionnaireChoice value="formal">Formal</QuestionnaireChoice>
<QuestionnaireChoice value="casual">Casual</QuestionnaireChoice>
<QuestionnaireChoice value="concise">Concise</QuestionnaireChoice>
</QuestionnaireChoices>
</QuestionnaireItem>
<QuestionnaireActions>
<QuestionnaireSubmit size="sm">Submit</QuestionnaireSubmit>
</QuestionnaireActions>
</Questionnaire>
);
}Props
Questionnaire
| Name | Type | Default | Description |
|---|---|---|---|
items | readonly { name: string; required?: boolean }[] | - | Required. Questions in display order. name must match QuestionnaireItem. |
shortcuts | "letters" | "numbers" | - | Lets the user pick choices with A, B, C… or 1, 2, 3…. Disabled when omitted. |
onSubmit | (event: SubmitEvent) => void | - | Called on submit. It is not called while a required question is unanswered. |
render | (props) => ReactElement | - | Replaces the root element. Return an element that receives the given props, such as next/link. |
Also accepts the attributes of the HTML form (opens in a new tab) element.
QuestionnaireProgress
| Name | Type | Default | Description |
|---|---|---|---|
children | (progress: { current: number; total: number }) => ReactNode | - | Required. Function that returns the progress text. |
render | (props) => ReactElement | - | Replaces the root element. Return an element that receives the given props, such as next/link. |
Also accepts the attributes of the HTML output (opens in a new tab) element.
QuestionnaireItem
| Name | Type | Default | Description |
|---|---|---|---|
name | string | - | Required. Name of the question. It becomes the field name in the submitted form data. |
required | boolean | false | Requires an answer. The skip button is hidden. |
multiple | boolean | false | Allows choosing multiple choices. |
render | (props) => ReactElement | - | Replaces the root element. Return an element that receives the given props, such as next/link. |
Also accepts the attributes of the HTML fieldset (opens in a new tab) element.
QuestionnaireTitle
| Name | Type | Default | Description |
|---|---|---|---|
render | (props) => ReactElement | - | Replaces the root element. Return an element that receives the given props, such as next/link. |
Also accepts the attributes of the HTML legend (opens in a new tab) element.
QuestionnaireChoices
| Name | Type | Default | Description |
|---|---|---|---|
children | ReactNode | - | QuestionnaireChoice elements. |
className | string | - | Class name. |
render | (props, renderProps) => ReactElement | - | Replaces the root div element. |
QuestionnaireChoice
| Name | Type | Default | Description |
|---|---|---|---|
value | string | - | Required. Value submitted when chosen. |
children | ReactNode | - | Label of the choice. |
description | ReactNode | - | Supplementary text shown below the label. |
disabled | boolean | false | Disables the choice. |
className | string | - | Class name. |
render | (props, renderProps) => ReactElement | - | Replaces the root label element. |
QuestionnaireInput
name, value, and defaultValue are managed by the questionnaire and cannot be passed.
Accepts the props of Input.
QuestionnaireActions
| Name | Type | Default | Description |
|---|---|---|---|
render | (props) => ReactElement | - | Replaces the root element. Return an element that receives the given props, such as next/link. |
Also accepts the attributes of the HTML div (opens in a new tab) element.
QuestionnairePrevious
Hidden on the first question. type cannot be passed.
| Name | Type | Default | Description |
|---|---|---|---|
variant | "filled" | "light" | "outline" | "ghost" | "reverse" | "ghost" | Visual style of the button. |
Also accepts the props of Button.
QuestionnaireSkip
Hidden on required questions. On the last question, it submits the form. type cannot be passed.
| Name | Type | Default | Description |
|---|---|---|---|
variant | "filled" | "light" | "outline" | "ghost" | "reverse" | "ghost" | Visual style of the button. |
Also accepts the props of Button.
QuestionnaireNext
Hidden on the last question and disabled while a required question is unanswered. type cannot be passed.
Accepts the props of Button.
QuestionnaireSubmit
Shown only on the last question and disabled while a required question is unanswered. type cannot be passed.
Accepts the props of Button.