Skip to content
Ginga UI
Ginga UI
日本語

Getting started

  • Overview
  • Quick start
  • Theme generation
  • Custom rendering
  • All components

Forms

  • Button
  • Checkbox
  • Input
  • Radio
  • Questionnaire
  • SegmentedControl
  • Select
  • ComboBox
  • NumberField
  • SearchField
  • Slider
  • Switch
  • FormControl

Display

  • Accordion
  • Alert
  • AlertDialog
  • Badge
  • CodeBlock
  • Card
  • Dialog
  • Drawer
  • Image
  • List
  • Popover
  • Menu
  • Tooltip
  • Table
  • Tab
  • Toast
  • Trace

Typography

  • Heading
  • Paragraph

Navigation

  • Anchor

Layout

  • Box

Resources

  • Changelog
  • GitHub (opens in a new tab)

Logo: Twemoji (opens in a new tab) (CC-BY 4.0)

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.

What should we build next?
Who will use it most?
Question 1 of 2
questionnaire.tsx
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.

Which platforms should it support?
questionnaire.tsx
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.

Do you have a deadline?
Is there a budget limit?
Question 1 of 2
questionnaire.tsx
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.

Choose a tone for replies
questionnaire.tsx
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

NameTypeDefaultDescription
itemsreadonly { 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

NameTypeDefaultDescription
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

NameTypeDefaultDescription
namestring-Required. Name of the question. It becomes the field name in the submitted form data.
requiredbooleanfalseRequires an answer. The skip button is hidden.
multiplebooleanfalseAllows 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

NameTypeDefaultDescription
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

NameTypeDefaultDescription
childrenReactNode-QuestionnaireChoice elements.
classNamestring-Class name.
render(props, renderProps) => ReactElement-Replaces the root div element.

QuestionnaireChoice

NameTypeDefaultDescription
valuestring-Required. Value submitted when chosen.
childrenReactNode-Label of the choice.
descriptionReactNode-Supplementary text shown below the label.
disabledbooleanfalseDisables the choice.
classNamestring-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

NameTypeDefaultDescription
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.

NameTypeDefaultDescription
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.

NameTypeDefaultDescription
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.