# Questionnaire

Collect structured answers one question at a time.

## Preview

questionnaire-preview-1.jsx

```jsx
import { Questionnaire } from "@/components/ui/questionnaire";

export default function Example() {
  return (
    <Preview>
      <Questionnaire
        questions={[deliveryQuestions[0]]}
        onSubmit={() => undefined}
      />
    </Preview>
  );
}

var deliveryQuestions = [
  {
    id: "delivery",
    title: "How should we deliver the replacement?",
    kind: "single",
    custom: {
      placeholder: "Type a delivery option\u2026",
      maxLength: 120,
    },
    required: true,
    options: [
      {
        value: "standard",
        label: "Standard delivery",
        description: "Arrives in 3\u20135 business days.",
      },
      {
        value: "express",
        label: "Express delivery",
        description: "Arrives tomorrow.",
      },
    ],
  },
  {
    id: "details",
    title: "What else should we know?",
    kind: "text",
    placeholder: "Add a short note",
    maxLength: 160,
  },
];

function Preview({ children }) {
  return <div data-preview-wide>{children}</div>;
}

```

The footer shows question progress. Your app supplies acknowledgement, cancellation, and expiry. Previously entered answers remain readable in the record.

## Design guidance

Place Questionnaire in ChatFooter when you want answers in place of the reply box. With appearance="composer", the question and choices sit in the secondary region. Navigation and custom answers sit in the primary region. Keep the editable form outside the message body.

### Accessibility

Each question has a real legend or Field label. Errors link to the invalid control and focus the first invalid question after validation. Back, Next, Skip, and Submit are native buttons; terminal responses are read-only and cancelled or expired requests are inert.

## Installation

```sh
npx shadcn@latest add https://ghost-ui.com/r/questionnaire.json
```

## Basic usage

```tsx
import { useState } from "react";
import {
  Questionnaire,
  type QuestionDefinition,
  type QuestionnaireResponse,
} from "@/components/ui/questionnaire";

const questions: QuestionDefinition[] = [
  {
    id: "work",
    title: "What is next?",
    kind: "single",
    required: true,
    options: [
      { value: "explore", label: "Explore" },
      { value: "refine", label: "Refine" },
    ],
  },
];

export function DesignQuestion() {
  const [response, setResponse] = useState<QuestionnaireResponse>();
  return (
    <Questionnaire
      questions={questions}
      response={response}
      onSubmit={setResponse}
    />
  );
}
```

## Structures

- Questionnaire
  - QuestionnaireProgress (optional)
  - QuestionnaireItem (current question)
  - QuestionnaireActions
- QuestionnaireRecord (separate acknowledged response)

Progress, the current item and actions share Questionnaire context. QuestionnaireItem accepts a render function for the current question, allowing a caller to supply its own input while controlling answers through value and onValueChange. Render QuestionnaireRecord separately for an acknowledged response.

## Examples

### Question and answer types

Choose a single option, several options, or a text answer from a stable question definition.

Use the QuestionnaireItem render function when a question needs a caller-owned input. Keep answer state in the caller and update it through value and onValueChange.

questionnaire-question-and-answer-types-2.jsx

```jsx
import { useState } from "react";
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import {
  Questionnaire,
  QuestionnaireActions,
  QuestionnaireItem,
} from "@/components/ui/questionnaire";

export default function Example() {
  const [answers, setAnswers] = useState({
    location: "",
  });
  const questions = [
    {
      id: "location",
      title: "Where should we deliver the replacement?",
      kind: "text",
      required: true,
    },
  ];
  return (
    <Preview>
      <Questionnaire
        questions={questions}
        value={answers}
        onValueChange={setAnswers}
        onSubmit={() => undefined}
      >
        <QuestionnaireItem id="location">
          {(question) => (
            <Field.Root>
              <Field.Label>{question.title}</Field.Label>
              <Field.Control
                render={
                  <Input
                    variant="ghost"
                    required
                    value={String(answers[question.id] ?? "")}
                    onChange={(event) =>
                      setAnswers((current) => ({
                        ...current,
                        [question.id]: event.target.value,
                      }))
                    }
                  />
                }
              />
            </Field.Root>
          )}
        </QuestionnaireItem>
        <QuestionnaireActions />
      </Questionnaire>
    </Preview>
  );
}

function Preview({ children }) {
  return <div data-preview-wide>{children}</div>;
}

```

#### Required single-choice

Move through required choices with the native Radio group and explicit arrow action.

questionnaire-required-single-choice-3.jsx

```jsx
import { Questionnaire } from "@/components/ui/questionnaire";

export default function Example() {
  return (
    <Preview>
      <Questionnaire questions={deliveryQuestions} onSubmit={() => undefined} />
    </Preview>
  );
}

var deliveryQuestions = [
  {
    id: "delivery",
    title: "How should we deliver the replacement?",
    kind: "single",
    custom: {
      placeholder: "Type a delivery option\u2026",
      maxLength: 120,
    },
    required: true,
    options: [
      {
        value: "standard",
        label: "Standard delivery",
        description: "Arrives in 3\u20135 business days.",
      },
      {
        value: "express",
        label: "Express delivery",
        description: "Arrives tomorrow.",
      },
    ],
  },
  {
    id: "details",
    title: "What else should we know?",
    kind: "text",
    placeholder: "Add a short note",
    maxLength: 160,
  },
];

function Preview({ children }) {
  return <div data-preview-wide>{children}</div>;
}

```

#### Multiple choices

Toggle one or more options with the shared Checkbox owner, then continue explicitly.

questionnaire-multiple-choices-4.jsx

```jsx
import { Questionnaire } from "@/components/ui/questionnaire";

export default function Example() {
  return (
    <Preview>
      <Questionnaire
        questions={preferencesQuestions}
        onSubmit={() => undefined}
      />
    </Preview>
  );
}

var preferencesQuestions = [
  {
    id: "updates",
    title: "Which updates should we send?",
    kind: "multiple",
    custom: {
      placeholder: "Type another update\u2026",
      maxLength: 120,
    },
    required: true,
    minSelections: 1,
    options: [
      {
        value: "email",
        label: "Email",
      },
      {
        value: "sms",
        label: "SMS",
      },
      {
        value: "push",
        label: "Push notification",
      },
    ],
  },
];

function Preview({ children }) {
  return <div data-preview-wide>{children}</div>;
}

```

#### Freeform

Enter a required text answer with the shared Field and Input owners.

questionnaire-freeform-5.jsx

```jsx
import { Questionnaire } from "@/components/ui/questionnaire";

export default function Example() {
  return (
    <Preview>
      <Questionnaire questions={freeformQuestions} onSubmit={() => undefined} />
    </Preview>
  );
}

var freeformQuestions = [
  {
    id: "note",
    title: "Tell us what changed.",
    kind: "text",
    required: true,
    maxLength: 240,
  },
];

function Preview({ children }) {
  return <div data-preview-wide>{children}</div>;
}

```

### Validation and navigation

Required choice and text questions use explicit Back, Next, and Submit actions.

#### Validation

Submit without a required choice to show the linked error and focus the first invalid control.

questionnaire-validation-6.jsx

```jsx
import { Questionnaire } from "@/components/ui/questionnaire";

export default function Example() {
  return (
    <Preview>
      <Questionnaire
        questions={preferencesQuestions}
        onSubmit={() => undefined}
      />
    </Preview>
  );
}

var preferencesQuestions = [
  {
    id: "updates",
    title: "Which updates should we send?",
    kind: "multiple",
    custom: {
      placeholder: "Type another update\u2026",
      maxLength: 120,
    },
    required: true,
    minSelections: 1,
    options: [
      {
        value: "email",
        label: "Email",
      },
      {
        value: "sms",
        label: "SMS",
      },
      {
        value: "push",
        label: "Push notification",
      },
    ],
  },
];

function Preview({ children }) {
  return <div data-preview-wide>{children}</div>;
}

```

#### Optional skip

Skip an optional question while preserving the skipped id in the response.

questionnaire-optional-skip-7.jsx

```jsx
import { Questionnaire } from "@/components/ui/questionnaire";

export default function Example() {
  const questions = [
    {
      id: "required",
      title: "What is your order number?",
      kind: "text",
      required: true,
      placeholder: "Order number",
    },
    {
      id: "optional",
      title: "Add a delivery note?",
      kind: "text",
      placeholder: "Optional note",
    },
  ];
  return (
    <Preview>
      <Questionnaire questions={questions} onSubmit={() => undefined} />
    </Preview>
  );
}

function Preview({ children }) {
  return <div data-preview-wide>{children}</div>;
}

```

### Submit, retry, and acknowledge

Submission is a caller-owned operation; acknowledgement is a separate status update.

#### Failed send and acknowledgement

A rejected send retains the draft. A later success waits for the parent to confirm receipt.

questionnaire-failed-send-and-acknowledgement-8.jsx

```jsx
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Questionnaire } from "@/components/ui/questionnaire";

export default function Example() {
  const [attempts, setAttempts] = useState(0);
  const [status, setStatus] = useState("editing");
  const [lastResponse, setLastResponse] = useState();
  const submit = (next) => {
    setLastResponse(next);
    setAttempts((count) => count + 1);
    if (attempts === 0)
      return Promise.reject(
        new Error("The response could not be sent. Try again."),
      );
    return Promise.resolve();
  };
  return (
    <Preview>
      <Questionnaire
        questions={deliveryQuestions}
        status={status}
        response={lastResponse}
        onSubmit={submit}
      />
      {lastResponse && status === "editing" && attempts > 1 ? (
        <Button type="button" onClick={() => setStatus("submitted")}>
          Confirm received response
        </Button>
      ) : null}
    </Preview>
  );
}

var deliveryQuestions = [
  {
    id: "delivery",
    title: "How should we deliver the replacement?",
    kind: "single",
    custom: {
      placeholder: "Type a delivery option\u2026",
      maxLength: 120,
    },
    required: true,
    options: [
      {
        value: "standard",
        label: "Standard delivery",
        description: "Arrives in 3\u20135 business days.",
      },
      {
        value: "express",
        label: "Express delivery",
        description: "Arrives tomorrow.",
      },
    ],
  },
  {
    id: "details",
    title: "What else should we know?",
    kind: "text",
    placeholder: "Add a short note",
    maxLength: 160,
  },
];

function Preview({ children }) {
  return <div data-preview-wide>{children}</div>;
}

```

#### Submitted record

Show the accepted response as a read-only record.

questionnaire-submitted-record-9.jsx

```jsx
import { Questionnaire } from "@/components/ui/questionnaire";

export default function Example() {
  const response = {
    answers: {
      delivery: "express",
      details: "Please leave it with reception.",
    },
    skipped: [],
  };
  return (
    <Preview>
      <Questionnaire
        questions={deliveryQuestions}
        status="submitted"
        response={response}
        onSubmit={() => undefined}
      />
    </Preview>
  );
}

var deliveryQuestions = [
  {
    id: "delivery",
    title: "How should we deliver the replacement?",
    kind: "single",
    custom: {
      placeholder: "Type a delivery option\u2026",
      maxLength: 120,
    },
    required: true,
    options: [
      {
        value: "standard",
        label: "Standard delivery",
        description: "Arrives in 3\u20135 business days.",
      },
      {
        value: "express",
        label: "Express delivery",
        description: "Arrives tomorrow.",
      },
    ],
  },
  {
    id: "details",
    title: "What else should we know?",
    kind: "text",
    placeholder: "Add a short note",
    maxLength: 160,
  },
];

function Preview({ children }) {
  return <div data-preview-wide>{children}</div>;
}

```

### Cancelled, expired, or unavailable

Keep retained answers readable and terminal requests inert. If a submitted response is unavailable, say so without inferring an empty answer.

questionnaire-cancelled-expired-or-unavailable-10.jsx

```jsx
import { Questionnaire } from "@/components/ui/questionnaire";

export default function Example() {
  const response = {
    answers: {
      delivery: "express",
      details: "Please leave it with reception.",
    },
    skipped: [],
  };
  return (
    <Preview>
      <Questionnaire
        questions={deliveryQuestions}
        status="cancelled"
        response={response}
        onSubmit={() => undefined}
      />
    </Preview>
  );
}

var deliveryQuestions = [
  {
    id: "delivery",
    title: "How should we deliver the replacement?",
    kind: "single",
    custom: {
      placeholder: "Type a delivery option\u2026",
      maxLength: 120,
    },
    required: true,
    options: [
      {
        value: "standard",
        label: "Standard delivery",
        description: "Arrives in 3\u20135 business days.",
      },
      {
        value: "express",
        label: "Express delivery",
        description: "Arrives tomorrow.",
      },
    ],
  },
  {
    id: "details",
    title: "What else should we know?",
    kind: "text",
    placeholder: "Add a short note",
    maxLength: 160,
  },
];

function Preview({ children }) {
  return <div data-preview-wide>{children}</div>;
}

```

questionnaire-cancelled-expired-or-unavailable-11.jsx

```jsx
import { Questionnaire } from "@/components/ui/questionnaire";

export default function Example() {
  return (
    <Preview>
      <Questionnaire
        questions={deliveryQuestions}
        status="expired"
        response={{
          answers: {},
          skipped: ["delivery", "details"],
        }}
        onSubmit={() => undefined}
      />
    </Preview>
  );
}

var deliveryQuestions = [
  {
    id: "delivery",
    title: "How should we deliver the replacement?",
    kind: "single",
    custom: {
      placeholder: "Type a delivery option\u2026",
      maxLength: 120,
    },
    required: true,
    options: [
      {
        value: "standard",
        label: "Standard delivery",
        description: "Arrives in 3\u20135 business days.",
      },
      {
        value: "express",
        label: "Express delivery",
        description: "Arrives tomorrow.",
      },
    ],
  },
  {
    id: "details",
    title: "What else should we know?",
    kind: "text",
    placeholder: "Add a short note",
    maxLength: 160,
  },
];

function Preview({ children }) {
  return <div data-preview-wide>{children}</div>;
}

```

questionnaire-cancelled-expired-or-unavailable-12.jsx

```jsx
import { QuestionnaireRecord } from "@/components/ui/questionnaire";

export default function Example() {
  return (
    <Preview>
      <QuestionnaireRecord questions={deliveryQuestions} status="unavailable" />
    </Preview>
  );
}

var deliveryQuestions = [
  {
    id: "delivery",
    title: "How should we deliver the replacement?",
    kind: "single",
    custom: {
      placeholder: "Type a delivery option\u2026",
      maxLength: 120,
    },
    required: true,
    options: [
      {
        value: "standard",
        label: "Standard delivery",
        description: "Arrives in 3\u20135 business days.",
      },
      {
        value: "express",
        label: "Express delivery",
        description: "Arrives tomorrow.",
      },
    ],
  },
  {
    id: "details",
    title: "What else should we know?",
    kind: "text",
    placeholder: "Add a short note",
    maxLength: 160,
  },
];

function Preview({ children }) {
  return <div data-preview-wide>{children}</div>;
}

```

### Conversation handoff

See the [required questionnaire dock](/ai-elements/chat#required-questionnaire-dock) for answering questions before continuing a conversation.

## API reference

Questionnaire properties

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| questions (required) | readonly QuestionDefinition[] | — | The stable-id questions to answer. |
| appearance | standalone \| composer | standalone | Uses the standalone card or places question content and primary actions in Composer's layered surface. |
| value | QuestionnaireAnswers \| undefined | — | Controls the draft answers. |
| defaultValue | QuestionnaireAnswers \| undefined | — | Seeds an uncontrolled draft. |
| onValueChange | (value: QuestionnaireAnswers) => void | — | Receives reconciled draft answers. |
| onSubmit (required) | (response: QuestionnaireResponse) => void \| Promise<void> | — | Receives a validated response. |
| onCancel | () => void | — | Adds a Close icon inside the questionnaire navigation. The caller owns cancellation and dismissal; it remains available during a pending submission. |
| status | editing \| submitting \| submitted \| cancelled \| expired | editing | Reports caller-owned lifecycle state. |
| response | QuestionnaireResponse \| undefined | — | Provides the accepted response for read-only display. |
| error | string \| undefined | — | Returns a controlled submission to editing with retained answers. |
| label | string | Questionnaire | Names the form. |
| disabled | boolean | false | Disables question controls while retaining the draft. |

QuestionnaireItem properties

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| id (required) | string | — | Identifies the question to render when it is current. |
| children | ReactNode \| (question: QuestionDefinition) => ReactNode | — | Renders caller-owned input, optionally from the current question definition. |

Question definitions and answers

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| QuestionDefinition.id / title (required) | string / string | — | Stable question ID and visible prompt. |
| kind (required) | single \| multiple \| text | — | Answer shape; text questions use the text input contract. |
| required / description / options | boolean / string / readonly QuestionOption[] | — | Validation requirement, optional supporting text, or choices with value, label, optional description and disabled state. |
| placeholder / maxLength | string / number | — | Optional text-answer hint and character limit. |
| minSelections / maxSelections | number / number | — | Optional bounds for multiple-choice answers. |
| custom | boolean \| QuestionCustomDefinition | — | Adds a custom answer row to single- or multiple-choice questions; an object may set placeholder and maxLength. |
| QuestionnaireResponse.answers / skipped | QuestionnaireAnswers / readonly string[] | — | Answer records keyed by question ID and IDs the caller skipped. |

QuestionDefinition.custom adds a final editable row to a single- or multiple-choice question. Set it to true, or provide its placeholder and maximum length as an object. The submitted value is { type: "custom", id: "custom:<question-id>", value: "..." }; multiple-choice answers contain this record beside known option values.

QuestionnaireRecord properties

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| questions (required) | readonly QuestionDefinition[] | — | The stable-id questions to display. |
| response | QuestionnaireResponse \| undefined | — | Retains submitted or cancelled answers when available. |
| status (required) | submitted \| cancelled \| expired \| unavailable | — | Selects the read-only record state. |

QuestionnaireRecord displays submitted, cancelled, expired, or unavailable answers as a read-only record and forwards native div props and its ref.
