Questionnaire
Multiple-choice questions the assistant asks inside a chat: one answer (radios) or several (checkboxes), an optional "Other" text box, Submit and Skip, and a read-only view once answered. Same markup and props in every style: switch the style in the header to see it change.
Basic
One question with an "Other" text box that shows when you choose it. In HTML, add data-vin-questionnaire to the form.
<form class="vin-questionnaire" data-vin-questionnaire aria-label="Trip length" style="max-width: 440px">
<fieldset class="vin-questionnaire__question">
<legend class="vin-questionnaire__prompt">How long do you want to hike?</legend>
<div class="vin-choice">
<input type="radio" class="vin-radio" id="qb-len-0" name="qb-len" value="short">
<div class="vin-choice__body"><label class="vin-choice__label" for="qb-len-0">Under 2 hours</label></div>
</div>
<div class="vin-choice">
<input type="radio" class="vin-radio" id="qb-len-1" name="qb-len" value="half-day">
<div class="vin-choice__body"><label class="vin-choice__label" for="qb-len-1">Half a day</label></div>
</div>
<div class="vin-choice">
<input type="radio" class="vin-radio" id="qb-len-2" name="qb-len" value="full-day">
<div class="vin-choice__body"><label class="vin-choice__label" for="qb-len-2">A full day</label></div>
</div>
<div class="vin-choice">
<input type="radio" class="vin-radio" id="qb-len-other" name="qb-len" value="other">
<div class="vin-choice__body">
<label class="vin-choice__label" for="qb-len-other">Other</label>
<input type="text" class="vin-input vin-input--sm vin-questionnaire__other" name="qb-len-other" aria-label="Other answer">
</div>
</div>
</fieldset>
<div class="vin-questionnaire__actions">
<button type="button" class="vin-btn vin-btn--ghost vin-btn--sm" data-vin-skip>Skip</button>
<button type="submit" class="vin-btn vin-btn--sm">Submit</button>
</div>
</form>import { Questionnaire } from "@/components/vin/questionnaire";
export function QuestionnaireBasic() {
return (
<Questionnaire
label="Trip length"
style={{ maxWidth: 440 }}
questions={[
{
id: "length",
prompt: "How long do you want to hike?",
options: [
{ value: "short", label: "Under 2 hours" },
{ value: "half-day", label: "Half a day" },
{ value: "full-day", label: "A full day" },
],
other: true,
},
]}
onSubmit={(answers) => console.log(answers)}
/>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import Questionnaire, { type Question, type QuestionnaireAnswers } from "@/components/vin/questionnaire.vue";
const questions: Question[] = [
{
id: "length",
prompt: "How long do you want to hike?",
options: [
{ value: "short", label: "Under 2 hours" },
{ value: "half-day", label: "Half a day" },
{ value: "full-day", label: "A full day" },
],
other: true,
},
];
const onSubmit = (answers: QuestionnaireAnswers) => console.log(answers);
</script>
<template>
<Questionnaire label="Trip length" style="max-width: 440px" :questions="questions" @submit="onSubmit" />
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import Questionnaire from "$lib/components/vin/questionnaire.svelte";
</script>
<Questionnaire
label="Trip length"
style="max-width: 440px"
questions={[
{
id: "length",
prompt: "How long do you want to hike?",
options: [
{ value: "short", label: "Under 2 hours" },
{ value: "half-day", label: "Half a day" },
{ value: "full-day", label: "A full day" },
],
other: true,
},
]}
onSubmit={(answers) => console.log(answers)}
/>Several questions
A title, a single-choice question and a choose-any question with Other filled in.
<form class="vin-questionnaire" data-vin-questionnaire aria-labelledby="qm-title" style="max-width: 440px">
<div class="vin-questionnaire__header">
<p class="vin-questionnaire__title" id="qm-title">A few questions first</p>
<p class="vin-questionnaire__description">So I can plan the right trip for you.</p>
</div>
<fieldset class="vin-questionnaire__question">
<legend class="vin-questionnaire__prompt">Who is coming?</legend>
<div class="vin-choice">
<input type="radio" class="vin-radio" id="qm-who-0" name="qm-who" value="solo" checked>
<div class="vin-choice__body"><label class="vin-choice__label" for="qm-who-0">Just me</label></div>
</div>
<div class="vin-choice">
<input type="radio" class="vin-radio" id="qm-who-1" name="qm-who" value="friends">
<div class="vin-choice__body"><label class="vin-choice__label" for="qm-who-1">Friends</label></div>
</div>
<div class="vin-choice">
<input type="radio" class="vin-radio" id="qm-who-2" name="qm-who" value="kids">
<div class="vin-choice__body"><label class="vin-choice__label" for="qm-who-2">Family with kids</label></div>
</div>
</fieldset>
<fieldset class="vin-questionnaire__question">
<legend class="vin-questionnaire__prompt">What would you like to see?</legend>
<p class="vin-questionnaire__hint">Choose any.</p>
<div class="vin-choice">
<input type="checkbox" class="vin-checkbox" id="qm-see-0" name="qm-see" value="lakes" checked>
<div class="vin-choice__body"><label class="vin-choice__label" for="qm-see-0">Lakes</label></div>
</div>
<div class="vin-choice">
<input type="checkbox" class="vin-checkbox" id="qm-see-1" name="qm-see" value="views">
<div class="vin-choice__body"><label class="vin-choice__label" for="qm-see-1">Mountain views</label></div>
</div>
<div class="vin-choice">
<input type="checkbox" class="vin-checkbox" id="qm-see-2" name="qm-see" value="waterfalls">
<div class="vin-choice__body"><label class="vin-choice__label" for="qm-see-2">Waterfalls</label></div>
</div>
<div class="vin-choice">
<input type="checkbox" class="vin-checkbox" id="qm-see-other" name="qm-see" value="other" checked>
<div class="vin-choice__body">
<label class="vin-choice__label" for="qm-see-other">Other</label>
<input type="text" class="vin-input vin-input--sm vin-questionnaire__other" name="qm-see-other" value="Wildflowers" aria-label="Other answer">
</div>
</div>
</fieldset>
<div class="vin-questionnaire__actions">
<button type="button" class="vin-btn vin-btn--ghost vin-btn--sm" data-vin-skip>Skip</button>
<button type="submit" class="vin-btn vin-btn--sm">Submit</button>
</div>
</form>import { Questionnaire } from "@/components/vin/questionnaire";
export function QuestionnaireMultiple() {
return (
<Questionnaire
title="A few questions first"
description="So I can plan the right trip for you."
style={{ maxWidth: 440 }}
questions={[
{
id: "who",
prompt: "Who is coming?",
options: [
{ value: "solo", label: "Just me" },
{ value: "friends", label: "Friends" },
{ value: "kids", label: "Family with kids" },
],
},
{
id: "see",
prompt: "What would you like to see?",
hint: "Choose any.",
multiple: true,
options: [
{ value: "lakes", label: "Lakes" },
{ value: "views", label: "Mountain views" },
{ value: "waterfalls", label: "Waterfalls" },
],
other: true,
},
]}
defaultValues={{ who: ["solo"], see: ["lakes", "other"] }}
defaultOther={{ see: "Wildflowers" }}
onSubmit={(answers) => console.log(answers)}
/>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import Questionnaire, { type Question, type QuestionnaireAnswers } from "@/components/vin/questionnaire.vue";
const questions: Question[] = [
{
id: "who",
prompt: "Who is coming?",
options: [
{ value: "solo", label: "Just me" },
{ value: "friends", label: "Friends" },
{ value: "kids", label: "Family with kids" },
],
},
{
id: "see",
prompt: "What would you like to see?",
hint: "Choose any.",
multiple: true,
options: [
{ value: "lakes", label: "Lakes" },
{ value: "views", label: "Mountain views" },
{ value: "waterfalls", label: "Waterfalls" },
],
other: true,
},
];
const onSubmit = (answers: QuestionnaireAnswers) => console.log(answers);
</script>
<template>
<Questionnaire
title="A few questions first"
description="So I can plan the right trip for you."
style="max-width: 440px"
:questions="questions"
:default-values="{ who: ['solo'], see: ['lakes', 'other'] }"
:default-other="{ see: 'Wildflowers' }"
@submit="onSubmit"
/>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import Questionnaire from "$lib/components/vin/questionnaire.svelte";
</script>
<Questionnaire
title="A few questions first"
description="So I can plan the right trip for you."
style="max-width: 440px"
questions={[
{
id: "who",
prompt: "Who is coming?",
options: [
{ value: "solo", label: "Just me" },
{ value: "friends", label: "Friends" },
{ value: "kids", label: "Family with kids" },
],
},
{
id: "see",
prompt: "What would you like to see?",
hint: "Choose any.",
multiple: true,
options: [
{ value: "lakes", label: "Lakes" },
{ value: "views", label: "Mountain views" },
{ value: "waterfalls", label: "Waterfalls" },
],
other: true,
},
]}
values={{ who: ["solo"], see: ["lakes", "other"] }}
other={{ see: "Wildflowers" }}
onSubmit={(answers) => console.log(answers)}
/>Answered
Read-only after Submit: only the chosen answer stays, with a note that it was sent.
<form class="vin-questionnaire" data-state="submitted" aria-label="Trip length" style="max-width: 440px">
<fieldset class="vin-questionnaire__question" disabled>
<legend class="vin-questionnaire__prompt">How long do you want to hike?</legend>
<div class="vin-choice">
<input type="radio" class="vin-radio" id="qs-len-0" name="qs-len" value="short">
<div class="vin-choice__body"><label class="vin-choice__label" for="qs-len-0">Under 2 hours</label></div>
</div>
<div class="vin-choice">
<input type="radio" class="vin-radio" id="qs-len-1" name="qs-len" value="half-day" checked>
<div class="vin-choice__body"><label class="vin-choice__label" for="qs-len-1">Half a day</label></div>
</div>
<div class="vin-choice">
<input type="radio" class="vin-radio" id="qs-len-2" name="qs-len" value="full-day">
<div class="vin-choice__body"><label class="vin-choice__label" for="qs-len-2">A full day</label></div>
</div>
</fieldset>
<p class="vin-questionnaire__status" role="status"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="9"/><path d="m8.5 12 2.5 2.5 4.5-5"/></svg>Answer sent</p>
</form>import { Questionnaire } from "@/components/vin/questionnaire";
export function QuestionnaireSubmitted() {
return (
<Questionnaire
label="Trip length"
style={{ maxWidth: 440 }}
defaultState="submitted"
defaultValues={{ length: ["half-day"] }}
questions={[
{
id: "length",
prompt: "How long do you want to hike?",
options: [
{ value: "short", label: "Under 2 hours" },
{ value: "half-day", label: "Half a day" },
{ value: "full-day", label: "A full day" },
],
},
]}
/>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import Questionnaire, { type Question } from "@/components/vin/questionnaire.vue";
const questions: Question[] = [
{
id: "length",
prompt: "How long do you want to hike?",
options: [
{ value: "short", label: "Under 2 hours" },
{ value: "half-day", label: "Half a day" },
{ value: "full-day", label: "A full day" },
],
},
];
</script>
<template>
<Questionnaire
label="Trip length"
style="max-width: 440px"
default-state="submitted"
:default-values="{ length: ['half-day'] }"
:questions="questions"
/>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import Questionnaire from "$lib/components/vin/questionnaire.svelte";
</script>
<Questionnaire
label="Trip length"
style="max-width: 440px"
state="submitted"
values={{ length: ["half-day"] }}
questions={[
{
id: "length",
prompt: "How long do you want to hike?",
options: [
{ value: "short", label: "Under 2 hours" },
{ value: "half-day", label: "Half a day" },
{ value: "full-day", label: "A full day" },
],
},
]}
/>Installation
React, Vue and Svelte: add the component to your project with the CLI. It reads your framework from vin.json and also copies any component this one needs. Then import it:
npx @vinish/ui add questionnaireimport { Questionnaire } from "@/components/vin/questionnaire";import Questionnaire from "@/components/vin/questionnaire.vue";import Questionnaire from "$lib/components/vin/questionnaire.svelte";The import paths assume the default folders: @/components/vin (React and Vue, with the usual @ → src alias) and $lib/components/vin (SvelteKit). Use your own if you chose another folder in init. How the Vue and Svelte versions pass values and content is in Frameworks.
Plain HTML: the classes come with the style's stylesheet. This component also needs vinui.js for its keyboard and pointer behaviour.
API
From spec/questionnaire.json, which every framework version is tested against.
| Prop | Values | Default | In HTML |
|---|---|---|---|
questions{ id, prompt, hint?, multiple?, options: (string | { value, label })[], other?: boolean | string }. multiple uses checkboxes; other adds an "Other" option with a text box. | Question[] | — | fieldset.vin-questionnaire__question > legend.vin-questionnaire__prompt + .vin-choice (Radio or Checkbox) ... |
title / descriptionA heading above the questions; it names the form. | ReactNode | — | .vin-questionnaire__header > .vin-questionnaire__title + .vin-questionnaire__description |
labelName for the form when there's no title. | string | — | aria-label on the form |
defaultValues / defaultOtherChosen values ("other" for the Other option) and the typed Other text, by question id. | Record<string, string[]> / Record<string, string> | — | — |
onSubmitAnswers by question id: a string, or a list for multiple. Other gives the typed text. | (answers) => void | — | vinui.js fires vin-submit with detail.answers, keyed by each question's input name (add data-vin-questionnaire to the form) |
onSkip | () => void | — | button[data-vin-skip]; vinui.js fires vin-skip |
state / defaultStateAfter Submit only the chosen answers stay, read-only; after Skip the questions fold away. | opensubmittedskipped | open | data-state on the form, fieldsets disabled, p.vin-questionnaire__status[role=status] |
submitLabel / skipLabelskipLabel={null} hides Skip. | string | Submit / Skip | — |
submittedText / skippedText | ReactNode | Answers sent / Skipped | — |
Parts
| React, Vue, Svelte | HTML classes |
|---|---|
<Questionnaire> | .vin-questionnaire.vin-questionnaire__header.vin-questionnaire__title.vin-questionnaire__description.vin-questionnaire__question.vin-questionnaire__prompt.vin-questionnaire__hint.vin-questionnaire__other.vin-questionnaire__actions.vin-questionnaire__status |
Accessibility
- Each question is a fieldset with its prompt as the legend, so screen readers read the question with every option.
- Options are real radios and checkboxes: arrow keys move between radios, Space toggles checkboxes, and they work in forms without JavaScript.
- The "Other" text box has its own label and only appears once Other is chosen.
- After Submit or Skip, focus moves to the status line (role="status"), which says what happened; the answered questions stay readable but disabled.
Customize
Saved in this browser. Style and accent are in the header.