# Questionnaire

A composable multi-step form for fixed, multiple, freeform, and optional answers.

Questionnaire coordinates ordered questions, answer state, validation, progress, and navigation while leaving persistence, transport, cancellation, and its surrounding card or dialog to the application.

```tsx
import { Questionnaire, Text } from "@zentara-co/zenos-ui";
import { useState } from "react";

function QuestionnaireExample() {
  const [result, setResult] = useState("Complete the questionnaire to save your plan.");
  const items = [
    { name: "direction", required: true },
    { name: "context", multiple: true },
    { name: "timing" },
  ] as const;

  return (
    <Questionnaire
      items={items}
      className="w-full max-w-xl rounded-xl border border-border-default bg-background-neutral-card p-5"
      onSubmit={(event, answers) => {
        event.preventDefault();
        const context = Array.isArray(answers.context) ? answers.context.join(", ") : "none";
        setResult(`Saved ${String(answers.direction)} with ${context} context.`);
      }}
    >
      <Questionnaire.Progress />
      <Questionnaire.Item name="direction">
        <Questionnaire.Title>What should the agent do next?</Questionnaire.Title>
        <Questionnaire.Description>
          Choose a direction or describe another task.
        </Questionnaire.Description>
        <Questionnaire.Choices>
          <Questionnaire.Choice value="implement">
            <span className="block font-medium">Implement a feature</span>
            <span className="block text-text-subtle">Build the requested product behavior.</span>
          </Questionnaire.Choice>
          <Questionnaire.Choice value="debug">
            <span className="block font-medium">Debug an issue</span>
            <span className="block text-text-subtle">Find the cause and verify the fix.</span>
          </Questionnaire.Choice>
          <Questionnaire.Input aria-label="Another task" placeholder="Describe another task…" />
        </Questionnaire.Choices>
        <Questionnaire.Error />
      </Questionnaire.Item>
      <Questionnaire.Item name="context">
        <Questionnaire.Title>What context should it inspect?</Questionnaire.Title>
        <Questionnaire.Description>Select every source that applies.</Questionnaire.Description>
        <Questionnaire.Choices>
          <Questionnaire.Choice value="source">Relevant source files</Questionnaire.Choice>
          <Questionnaire.Choice value="tests">Existing tests</Questionnaire.Choice>
          <Questionnaire.Choice value="docs">Architecture documentation</Questionnaire.Choice>
        </Questionnaire.Choices>
        <Questionnaire.Error />
      </Questionnaire.Item>
      <Questionnaire.Item name="timing">
        <Questionnaire.Title>When should work begin?</Questionnaire.Title>
        <Questionnaire.Description>
          This optional question may be left empty.
        </Questionnaire.Description>
        <Questionnaire.Choices>
          <Questionnaire.Choice value="now">Start now</Questionnaire.Choice>
          <Questionnaire.Choice value="next-cycle">Next development cycle</Questionnaire.Choice>
        </Questionnaire.Choices>
        <Questionnaire.Error />
      </Questionnaire.Item>
      <Questionnaire.Actions>
        <Questionnaire.Previous />
        <Questionnaire.Skip />
        <Questionnaire.Next />
        <Questionnaire.Submit>Save plan</Questionnaire.Submit>
      </Questionnaire.Actions>
      <Text aria-live="polite" color="subtle" size="s">
        {result}
      </Text>
    </Questionnaire>
  );
}
```

## Import

```tsx
import { Questionnaire } from "@zentara-co/zenos-ui";
```

## Composition

Define the ordered item metadata once on the root, then compose the visible content with compound parts. The root uses each item definition to determine required validation, multiple selection, disabled navigation, and progress.

```tsx
const items = [
  { name: "scope", required: true },
  { name: "checks", multiple: true },
  { name: "notes" },
] as const;

<Questionnaire items={items} onSubmit={handleSubmit}>
  <Questionnaire.Progress />
  <Questionnaire.Item name="scope">
    <Questionnaire.Title>What may change?</Questionnaire.Title>
    <Questionnaire.Description>Choose one scope.</Questionnaire.Description>
    <Questionnaire.Choices>
      <Questionnaire.Choice value="component">The component</Questionnaire.Choice>
      <Questionnaire.Choice value="feature">The complete feature</Questionnaire.Choice>
      <Questionnaire.Input aria-label="Another scope" placeholder="Describe another scope…" />
    </Questionnaire.Choices>
    <Questionnaire.Error />
  </Questionnaire.Item>
  <Questionnaire.Actions>
    <Questionnaire.Previous />
    <Questionnaire.Skip />
    <Questionnaire.Next />
    <Questionnaire.Submit>Save plan</Questionnaire.Submit>
  </Questionnaire.Actions>
</Questionnaire>;
```

## Answer types

An item without `multiple` renders its choices as native radio controls. Set `multiple: true` to render native checkboxes and store a string array. `Questionnaire.Input` supplies a freeform string answer and clears a previously selected fixed choice for that item.

## Required and optional items

Set `required: true` on an item definition to block forward navigation until it has an answer. `Questionnaire.Error` renders the validation message and associates it with the active fieldset. Optional items expose `Questionnaire.Skip` before the final item; skipping removes any existing answer for that item.

## Controlled state

Use `value`, `defaultValue`, and `onValueChange` for the active item. Use `answers`, `defaultAnswers`, and `onAnswersChange` when the host needs to restore a draft, validate externally, or persist changes as they happen.

```tsx
<Questionnaire
  items={items}
  value={activeItem}
  onValueChange={setActiveItem}
  answers={answers}
  onAnswersChange={setAnswers}
>
  {/* questionnaire parts */}
</Questionnaire>
```

## Custom progress

`Questionnaire.Progress` accepts a render function when the product needs different wording or a visual progress treatment.

```tsx
<Questionnaire.Progress>
  {({ current, itemCount }) => `Checkpoint ${current} of ${itemCount}`}
</Questionnaire.Progress>
```

## Anatomy

```text
Questionnaire.Root
Questionnaire.Progress
Questionnaire.Item
Questionnaire.Title
Questionnaire.Description
Questionnaire.Choices
Questionnaire.Choice
Questionnaire.Input
Questionnaire.Error
Questionnaire.Actions
Questionnaire.Previous
Questionnaire.Skip
Questionnaire.Next
Questionnaire.Submit
```

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| items | readonly QuestionnaireItemDefinition[] | — | Ordered question metadata used for navigation, validation, and input type. |
| value / defaultValue | string | — | Controlled or initial active item name. |
| onValueChange | (value: string) => void | — | Runs when navigation changes the active item. |
| answers / defaultAnswers | QuestionnaireAnswers | — | Controlled or initial string and string-array answers. |
| onAnswersChange | (answers: QuestionnaireAnswers) => void | — | Runs with the complete answer collection after an answer changes. |
| onSubmit | (event, answers) => void | — | Runs after the final active item passes validation. |
| Item.name | string | — | Matches an item definition and supplies the native form field name. |
| Choice.value | string | — | Value stored by the native radio or checkbox control. |
| Input.onValueChange | (value: string) => void | — | Runs when the freeform answer changes. |
| native props | React.FormHTMLAttributes<HTMLFormElement> | — | Forwards native attributes, events, aria-*, data-*, className, style, and ref. |

Unless narrowed above, each DOM-rendering part also accepts its underlying native or Base UI props, including className, style, aria-*, and data-*.

## Accessibility

Each item is a native `fieldset`, and its title is a `legend`. Fixed answers preserve native radio and checkbox behavior. Descriptions and active errors are associated with the fieldset, progress is exposed as a named progress bar, and navigation uses native buttons. Successful navigation focuses the new question; failed required validation focuses the first available answer. Always give `Questionnaire.Input` a visible label, `aria-label`, or `aria-labelledby`; a placeholder is not an accessible name.

Source: https://zenos-ui.pages.dev/components/questionnaire/