---
title: "Questionnaire"
description: "Questionnaire with accessible presentation and local state."
---

# Questionnaire

Questionnaire with accessible presentation and local state.

## Installation

Install the shared package once in your Exact app. This version is a beta.

```sh
bun add --exact @rightui/ui@0.4.0-beta.4
```

Or choose another package manager:

```sh
npm install --save-exact @rightui/ui@0.4.0-beta.4
pnpm add --save-exact @rightui/ui@0.4.0-beta.4
yarn add --exact @rightui/ui@0.4.0-beta.4
```

Exact compilation uses Bun 1.4.2. Register Geist and Newsreader, declare the font marker and add the root provider using [Installation](/docs/installation).

### Manual source setup

Install an editable copy of the complete package:

```sh
mkdir -p vendor/rightui vendor/rightui-ui
npm pack @rightui/ui@0.4.0-beta.4 --pack-destination vendor/rightui
tar -xzf ./vendor/rightui/rightui-ui-0.4.0-beta.4.tgz -C vendor/rightui-ui --strip-components=1
bun add file:./vendor/rightui-ui
```

Keep package.json, the Contract files, ui.tamber, assets and licenses together.

### Import

```contract
use QuestionnaireQuestion, QuestionnaireAnswer, Questionnaire from "@rightui/ui"
```

## Usage

Import QuestionnaireQuestion, QuestionnaireAnswer from @rightui/ui. Store its value in state and update it through the provided action callback.

## Example

```contract
use FontWeights from "@rightui/ui"
use uiFontFamily, QuestionnaireQuestion, QuestionnaireAnswer, Questionnaire, Card, Button, Dialog from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  derive narrow = width < 600
  action completed(v: QuestionnaireAnswer[])
    result = v
  state result: QuestionnaireAnswer[] = []
  state questionsOpen = false
  action openQuestions
    questionsOpen = true
  action closeQuestions
    questionsOpen = false
  view
    column gap="ui.space-16" min-width=0 max-width="100%"
      Questionnaire(id="content-questionnaire", questions=[QuestionnaireQuestion(id="format", title="Which format do you prefer?", description="Choose an answer.", kind="single", options=["Photo", "Video", "Both"], optional=true), QuestionnaireQuestion(id="contenus", title="Which content do you need?", description="Select all that apply.", kind="multi", options=["Chambre", "Restaurant", "Outdoors"], optional=true), QuestionnaireQuestion(id="precisions", title="Anything else to add?", description="Describe what you need.", kind="freeform", options=[], optional=true)], complete=completed)
      text "Card questionnaire" font-size="ui.text-sm" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      Card(id="variation-questionnaire-card", title="Project preferences", description="Complete the short questionnaire.")
        Questionnaire(id="variation-questionnaire-card-form", questions=[QuestionnaireQuestion(id="format", title="Which format do you prefer?", description="Choose an answer.", kind="single", options=["Photo", "Video", "Both"], optional=true), QuestionnaireQuestion(id="contenus", title="Which content do you need?", description="Select all that apply.", kind="multi", options=["Chambre", "Restaurant", "Outdoors"], optional=true), QuestionnaireQuestion(id="precisions", title="Anything else to add?", description="Describe what you need.", kind="freeform", options=[], optional=true)], complete=completed)
      text "Dialog questionnaire" font-size="ui.text-sm" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      Button(id="variation-questionnaire-open", label="Open questionnaire", variant="outline", disabled=false, press=openQuestions)
      Dialog(id="variation-questionnaire-dialog", open=questionsOpen, reduced=reduced, title="Project preferences", description="Complete your preferences.", confirmLabel="Done", focusIds=[], dismiss=closeQuestions, confirm=closeQuestions)
        Questionnaire(id="variation-questionnaire-dialog-form", questions=[QuestionnaireQuestion(id="format", title="Which format do you prefer?", description="Choose an answer.", kind="single", options=["Photo", "Video", "Both"], optional=true), QuestionnaireQuestion(id="contenus", title="Which content do you need?", description="Select all that apply.", kind="multi", options=["Chambre", "Restaurant", "Outdoors"], optional=true), QuestionnaireQuestion(id="precisions", title="Anything else to add?", description="Describe what you need.", kind="freeform", options=[], optional=true)], complete=completed)

```

## API Reference

| Property | Type |
| --- | --- |
| Questionnaire.id | string |
| Questionnaire.questions | QuestionnaireQuestion[] |
| Questionnaire.complete | action |

## Source

File: packages/ui/questionnaire.contract

```contract
use FontWeights from "./typography-context.contract"
use uiFontFamily from "./typography-context.contract"
use Button from "./button.contract"
use Control, Field from "./styles.contract"
shape QuestionnaireQuestion
  id: string
  title: string
  description: string
  kind: string
  options: string[]
  optional: bool

shape QuestionnaireAnswer
  id: string
  values: string[]

component Questionnaire
  props
    id: string
    questions: QuestionnaireQuestion[]
    complete: action
  inject
    uiWeights: FontWeights
    uiFont: string
  state step = 0
  state answers: QuestionnaireAnswer[] = []
  state finished = false
  derive question = match questions.at(step) { case some(q) => q, case none => QuestionnaireQuestion(id="", title="", description="", kind="freeform", options=[], optional=true) }
  derive values = match first(answers.filter(a => a.id == question.id)) { case some(a) => a.values, case none => [] }
  derive freeform = match first(values) { case some(v) => v, case none => "" }
  derive ready = question.optional or values.filter(v => v.trim() != "").length > 0
  action choose(v: string)
    let selection = question.kind == "multi" ? (values.includes(v) ? values.filter(x => x != v) : values.concat([v])) : [v]
    answers = answers.filter(a => a.id != question.id).concat([QuestionnaireAnswer(id=question.id, values=selection)])
  action edit(v: string)
    answers = answers.filter(a => a.id != question.id).concat([QuestionnaireAnswer(id=question.id, values=(v.trim() == "" ? [] : [v]))])
  action next
    if ready and questions.length > 0
      if step == questions.length - 1
        complete(answers)
        finished = true
      else
        step = step + 1
  action skip
    if question.optional
      let nextAnswers = answers.filter(a => a.id != question.id).concat([QuestionnaireAnswer(id=question.id, values=[])])
      if step == questions.length - 1
        complete(nextAnswers)
        finished = true
      else
        step = step + 1
      answers = nextAnswers
  action back
    if finished
      finished = false
    else
      step = max(0, step - 1)
  action restart
    step = 0
    answers = []
    finished = false
  action choiceKey(i: number, k: string)
    if question.kind == "single" and question.options.length > 0 and (k == "ArrowDown" or k == "ArrowRight" or k == "ArrowUp" or k == "ArrowLeft" or k == "Home" or k == "End")
      preventDefault()
      let index = k == "Home" ? 0 : k == "End" ? question.options.length - 1 : (i + (k == "ArrowDown" or k == "ArrowRight" ? 1 : question.options.length - 1)) % question.options.length
      match question.options.at(index)
        case some(v)
          choose(v)
          focus(id + "-choice-" + index.toString())
        case none
          preventDefault()
  view
    column id=id testId=id gap="ui.space-16" min-width="0px"
      if questions.length == 0
        text "Aucune question." font-size="ui.text-sm" color="ui.muted-foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"
      else if finished
        text "Your answers" font-size="ui.text-lg" font-weight=uiWeights.semibold color="ui.foreground" role="heading" aria-level=3 font-family=uiFontFamily(uiFont) data-rightui-font="body"
        column testId=(id + "-result") gap="ui.space-8" role="status"
          for q in questions key=q.id
            column gap="ui.space-4"
              text q.title font-size="ui.text-sm" font-weight=uiWeights.medium color="ui.foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"
              text (match first(answers.filter(a => a.id == q.id)) { case some(a) => a.values.length == 0 ? "Skipped" : a.values.join(", "), case none => "Skipped" }) testId=(id + "-answer-" + q.id) font-size="ui.text-sm" color="ui.muted-foreground" overflow-wrap="break-word" font-family=uiFontFamily(uiFont) data-rightui-font="body"
        row gap="ui.space-8" flex-wrap="wrap"
          Button(id=id + "-back", label="Modifier", variant="outline", disabled=false, press=back)
          Button(id=id + "-restart", label="Recommencer", variant="primary", disabled=false, press=restart)
      else
        text ("Question " + (step + 1).toString() + " sur " + questions.length.toString()) testId=(id + "-progress") font-size="ui.text-xs" font-weight=uiWeights.medium color="ui.muted-foreground" aria-live="polite" font-family=uiFontFamily(uiFont) data-rightui-font="body"
        text question.title font-size="ui.text-lg" font-weight=uiWeights.semibold color="ui.foreground" role="heading" aria-level=3 font-family=uiFontFamily(uiFont) data-rightui-font="body"
        text question.description font-size="ui.text-sm" color="ui.muted-foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"
        if question.kind == "freeform"
          textarea id=(id + "-input") testId=(id + "-input") class=Field font-weight=uiWeights.regular value=freeform input=edit aria-label=question.title placeholder="Your answer…" min-height="6.25rem" font-family=uiFontFamily(uiFont) data-rightui-font="body"
        else
          column role=(question.kind == "single" ? "radiogroup" : "group") aria-label=question.title gap="ui.space-8"
            for option, i in question.options key=option
              button id=(id + "-choice-" + i.toString()) testId=(id + "-choice-" + i.toString()) class=Control font-weight=uiWeights.medium tabindex=(question.kind == "multi" or values.includes(option) or (values.length == 0 and i == 0) ? 0 : -1) role=(question.kind == "single" ? "radio" : "checkbox") aria-checked=values.includes(option) press=choose(option) key=choiceKey(i) padding="ui.space-12" min-height="ui.space-44" border-width=(values.includes(option) ? "ui.border-width" : "ui.border-width") border-style=(values.includes(option) ? "solid" : "solid") border-color=(values.includes(option) ? "ui.foreground" : "ui.border") background-color=(values.includes(option) ? "ui.muted" : "ui.surface") color="ui.foreground" text-align="left"
                text option font-family=uiFontFamily(uiFont) data-rightui-font="body"
        row gap="ui.space-8" flex-wrap="wrap" align-items="center"
          Button(id=id + "-back", label="Retour", variant="outline", disabled=(step == 0), press=back)
          if question.optional
            Button(id=id + "-skip", label="Passer", variant="ghost", disabled=false, press=skip)
          Button(id=id + "-next", label=(step == questions.length - 1 ? "Terminer" : "Continuer"), variant="primary", disabled=(not ready), press=next)

```
