@mostra/kit 0.0.0-stage → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -2
- package/css/kit.css +59 -0
- package/css/shadcn.css +638 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/layout.d.ts +60 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +112 -0
- package/dist/layout.js.map +1 -0
- package/dist/questionnaire.d.ts +100 -0
- package/dist/questionnaire.d.ts.map +1 -0
- package/dist/questionnaire.js +632 -0
- package/dist/questionnaire.js.map +1 -0
- package/package.json +120 -3
- package/registry/LICENSE.md +21 -0
- package/registry/base/accordion.json +19 -0
- package/registry/base/alert-dialog.json +21 -0
- package/registry/base/alert.json +18 -0
- package/registry/base/aspect-ratio.json +17 -0
- package/registry/base/attachment.json +22 -0
- package/registry/base/avatar.json +18 -0
- package/registry/base/badge.json +19 -0
- package/registry/base/breadcrumb.json +19 -0
- package/registry/base/bubble.json +19 -0
- package/registry/base/button-group.json +22 -0
- package/registry/base/button.json +19 -0
- package/registry/base/calendar.json +22 -0
- package/registry/base/card.json +17 -0
- package/registry/base/carousel.json +22 -0
- package/registry/base/chart.json +21 -0
- package/registry/base/checkbox.json +19 -0
- package/registry/base/code-block.json +23 -0
- package/registry/base/collapsible.json +17 -0
- package/registry/base/combobox.json +23 -0
- package/registry/base/command.json +23 -0
- package/registry/base/components/mostra/layout.tsx +221 -0
- package/registry/base/components/mostra/questionnaire.tsx +1005 -0
- package/registry/base/components/ui/accordion.tsx +71 -0
- package/registry/base/components/ui/alert-dialog.tsx +187 -0
- package/registry/base/components/ui/alert.tsx +75 -0
- package/registry/base/components/ui/aspect-ratio.tsx +22 -0
- package/registry/base/components/ui/attachment.tsx +207 -0
- package/registry/base/components/ui/avatar.tsx +108 -0
- package/registry/base/components/ui/badge.tsx +54 -0
- package/registry/base/components/ui/breadcrumb.tsx +124 -0
- package/registry/base/components/ui/bubble.tsx +127 -0
- package/registry/base/components/ui/button-group.tsx +87 -0
- package/registry/base/components/ui/button.tsx +57 -0
- package/registry/base/components/ui/calendar.tsx +221 -0
- package/registry/base/components/ui/card.tsx +102 -0
- package/registry/base/components/ui/carousel.tsx +242 -0
- package/registry/base/components/ui/chart.tsx +372 -0
- package/registry/base/components/ui/checkbox.tsx +28 -0
- package/registry/base/components/ui/code-block.tsx +249 -0
- package/registry/base/components/ui/collapsible.tsx +21 -0
- package/registry/base/components/ui/combobox.tsx +297 -0
- package/registry/base/components/ui/command.tsx +196 -0
- package/registry/base/components/ui/context-menu.tsx +270 -0
- package/registry/base/components/ui/dialog.tsx +160 -0
- package/registry/base/components/ui/direction.tsx +6 -0
- package/registry/base/components/ui/drawer.tsx +227 -0
- package/registry/base/components/ui/dropdown-menu.tsx +267 -0
- package/registry/base/components/ui/empty.tsx +103 -0
- package/registry/base/components/ui/field.tsx +238 -0
- package/registry/base/components/ui/hover-card.tsx +50 -0
- package/registry/base/components/ui/input-group.tsx +158 -0
- package/registry/base/components/ui/input-otp.tsx +86 -0
- package/registry/base/components/ui/input.tsx +19 -0
- package/registry/base/components/ui/item.tsx +201 -0
- package/registry/base/components/ui/kbd.tsx +26 -0
- package/registry/base/components/ui/label.tsx +19 -0
- package/registry/base/components/ui/marker.tsx +70 -0
- package/registry/base/components/ui/menubar.tsx +280 -0
- package/registry/base/components/ui/message-scroller.tsx +131 -0
- package/registry/base/components/ui/message.tsx +91 -0
- package/registry/base/components/ui/native-select.tsx +60 -0
- package/registry/base/components/ui/navigation-menu.tsx +167 -0
- package/registry/base/components/ui/pagination.tsx +130 -0
- package/registry/base/components/ui/popover.tsx +89 -0
- package/registry/base/components/ui/progress.tsx +82 -0
- package/registry/base/components/ui/questionnaire.tsx +324 -0
- package/registry/base/components/ui/radio-group.tsx +37 -0
- package/registry/base/components/ui/resizable.tsx +49 -0
- package/registry/base/components/ui/scroll-area.tsx +53 -0
- package/registry/base/components/ui/select.tsx +200 -0
- package/registry/base/components/ui/separator.tsx +24 -0
- package/registry/base/components/ui/sheet.tsx +138 -0
- package/registry/base/components/ui/sidebar.tsx +723 -0
- package/registry/base/components/ui/skeleton.tsx +13 -0
- package/registry/base/components/ui/slider.tsx +51 -0
- package/registry/base/components/ui/sonner.tsx +65 -0
- package/registry/base/components/ui/spinner.tsx +10 -0
- package/registry/base/components/ui/switch.tsx +31 -0
- package/registry/base/components/ui/table.tsx +116 -0
- package/registry/base/components/ui/tabs.tsx +85 -0
- package/registry/base/components/ui/textarea.tsx +17 -0
- package/registry/base/components/ui/toast.tsx +234 -0
- package/registry/base/components/ui/toggle-group.tsx +89 -0
- package/registry/base/components/ui/toggle.tsx +44 -0
- package/registry/base/components/ui/tooltip.tsx +65 -0
- package/registry/base/components/ui/trace.tsx +162 -0
- package/registry/base/context-menu.json +19 -0
- package/registry/base/dialog.json +22 -0
- package/registry/base/direction.json +17 -0
- package/registry/base/drawer.json +18 -0
- package/registry/base/dropdown-menu.json +19 -0
- package/registry/base/empty.json +18 -0
- package/registry/base/field.json +22 -0
- package/registry/base/hooks/use-highlighted-code.ts +75 -0
- package/registry/base/hooks/use-mobile.ts +19 -0
- package/registry/base/hover-card.json +18 -0
- package/registry/base/input-group.json +23 -0
- package/registry/base/input-otp.json +19 -0
- package/registry/base/input.json +18 -0
- package/registry/base/item.json +22 -0
- package/registry/base/kbd.json +17 -0
- package/registry/base/label.json +17 -0
- package/registry/base/lib/highlighter.tsx +124 -0
- package/registry/base/lib/utils.ts +1 -0
- package/registry/base/marker.json +19 -0
- package/registry/base/menubar.json +22 -0
- package/registry/base/message-scroller.json +22 -0
- package/registry/base/message.json +17 -0
- package/registry/base/mostra-layout.json +20 -0
- package/registry/base/mostra-questionnaire.json +22 -0
- package/registry/base/native-select.json +18 -0
- package/registry/base/navigation-menu.json +20 -0
- package/registry/base/pagination.json +21 -0
- package/registry/base/popover.json +18 -0
- package/registry/base/progress.json +18 -0
- package/registry/base/questionnaire.json +22 -0
- package/registry/base/radio-group.json +18 -0
- package/registry/base/registry.json +287 -0
- package/registry/base/resizable.json +18 -0
- package/registry/base/scroll-area.json +18 -0
- package/registry/base/select.json +19 -0
- package/registry/base/separator.json +18 -0
- package/registry/base/sheet.json +22 -0
- package/registry/base/sidebar.json +29 -0
- package/registry/base/skeleton.json +17 -0
- package/registry/base/slider.json +18 -0
- package/registry/base/sonner.json +18 -0
- package/registry/base/spinner.json +18 -0
- package/registry/base/switch.json +18 -0
- package/registry/base/table.json +17 -0
- package/registry/base/tabs.json +19 -0
- package/registry/base/textarea.json +17 -0
- package/registry/base/toast.json +22 -0
- package/registry/base/toggle-group.json +22 -0
- package/registry/base/toggle.json +19 -0
- package/registry/base/tooltip.json +18 -0
- package/registry/base/trace.json +21 -0
- package/registry/base/use-highlighted-code.json +21 -0
- package/registry/base/use-mobile.json +14 -0
- package/registry/base/utils.json +17 -0
- package/registry/radix/accordion.json +19 -0
- package/registry/radix/alert-dialog.json +21 -0
- package/registry/radix/alert.json +18 -0
- package/registry/radix/aspect-ratio.json +17 -0
- package/registry/radix/attachment.json +22 -0
- package/registry/radix/avatar.json +18 -0
- package/registry/radix/badge.json +19 -0
- package/registry/radix/breadcrumb.json +19 -0
- package/registry/radix/bubble.json +19 -0
- package/registry/radix/button-group.json +22 -0
- package/registry/radix/button.json +19 -0
- package/registry/radix/calendar.json +22 -0
- package/registry/radix/card.json +17 -0
- package/registry/radix/carousel.json +22 -0
- package/registry/radix/chart.json +18 -0
- package/registry/radix/checkbox.json +19 -0
- package/registry/radix/code-block.json +23 -0
- package/registry/radix/collapsible.json +17 -0
- package/registry/radix/combobox.json +23 -0
- package/registry/radix/command.json +23 -0
- package/registry/radix/components/mostra/layout.tsx +221 -0
- package/registry/radix/components/mostra/questionnaire.tsx +1005 -0
- package/registry/radix/components/ui/accordion.tsx +80 -0
- package/registry/radix/components/ui/alert-dialog.tsx +199 -0
- package/registry/radix/components/ui/alert.tsx +75 -0
- package/registry/radix/components/ui/aspect-ratio.tsx +11 -0
- package/registry/radix/components/ui/attachment.tsx +204 -0
- package/registry/radix/components/ui/avatar.tsx +111 -0
- package/registry/radix/components/ui/badge.tsx +51 -0
- package/registry/radix/components/ui/breadcrumb.tsx +121 -0
- package/registry/radix/components/ui/bubble.tsx +124 -0
- package/registry/radix/components/ui/button-group.tsx +83 -0
- package/registry/radix/components/ui/button.tsx +66 -0
- package/registry/radix/components/ui/calendar.tsx +222 -0
- package/registry/radix/components/ui/card.tsx +102 -0
- package/registry/radix/components/ui/carousel.tsx +242 -0
- package/registry/radix/components/ui/chart.tsx +372 -0
- package/registry/radix/components/ui/checkbox.tsx +32 -0
- package/registry/radix/components/ui/code-block.tsx +249 -0
- package/registry/radix/components/ui/collapsible.tsx +33 -0
- package/registry/radix/components/ui/combobox.tsx +299 -0
- package/registry/radix/components/ui/command.tsx +195 -0
- package/registry/radix/components/ui/context-menu.tsx +262 -0
- package/registry/radix/components/ui/dialog.tsx +168 -0
- package/registry/radix/components/ui/direction.tsx +22 -0
- package/registry/radix/components/ui/drawer.tsx +133 -0
- package/registry/radix/components/ui/dropdown-menu.tsx +268 -0
- package/registry/radix/components/ui/empty.tsx +103 -0
- package/registry/radix/components/ui/field.tsx +238 -0
- package/registry/radix/components/ui/hover-card.tsx +43 -0
- package/registry/radix/components/ui/input-group.tsx +156 -0
- package/registry/radix/components/ui/input-otp.tsx +86 -0
- package/registry/radix/components/ui/input.tsx +18 -0
- package/registry/radix/components/ui/item.tsx +196 -0
- package/registry/radix/components/ui/kbd.tsx +26 -0
- package/registry/radix/components/ui/label.tsx +23 -0
- package/registry/radix/components/ui/marker.tsx +68 -0
- package/registry/radix/components/ui/menubar.tsx +279 -0
- package/registry/radix/components/ui/message-scroller.tsx +131 -0
- package/registry/radix/components/ui/message.tsx +91 -0
- package/registry/radix/components/ui/native-select.tsx +60 -0
- package/registry/radix/components/ui/navigation-menu.tsx +163 -0
- package/registry/radix/components/ui/pagination.tsx +129 -0
- package/registry/radix/components/ui/popover.tsx +88 -0
- package/registry/radix/components/ui/progress.tsx +30 -0
- package/registry/radix/components/ui/questionnaire.tsx +324 -0
- package/registry/radix/components/ui/radio-group.tsx +43 -0
- package/registry/radix/components/ui/resizable.tsx +49 -0
- package/registry/radix/components/ui/scroll-area.tsx +54 -0
- package/registry/radix/components/ui/select.tsx +191 -0
- package/registry/radix/components/ui/separator.tsx +27 -0
- package/registry/radix/components/ui/sheet.tsx +147 -0
- package/registry/radix/components/ui/sidebar.tsx +702 -0
- package/registry/radix/components/ui/skeleton.tsx +13 -0
- package/registry/radix/components/ui/slider.tsx +62 -0
- package/registry/radix/components/ui/sonner.tsx +65 -0
- package/registry/radix/components/ui/spinner.tsx +10 -0
- package/registry/radix/components/ui/switch.tsx +32 -0
- package/registry/radix/components/ui/table.tsx +116 -0
- package/registry/radix/components/ui/tabs.tsx +129 -0
- package/registry/radix/components/ui/textarea.tsx +17 -0
- package/registry/radix/components/ui/toggle-group.tsx +89 -0
- package/registry/radix/components/ui/toggle.tsx +46 -0
- package/registry/radix/components/ui/tooltip.tsx +56 -0
- package/registry/radix/components/ui/trace.tsx +162 -0
- package/registry/radix/context-menu.json +19 -0
- package/registry/radix/dialog.json +22 -0
- package/registry/radix/direction.json +17 -0
- package/registry/radix/drawer.json +18 -0
- package/registry/radix/dropdown-menu.json +19 -0
- package/registry/radix/empty.json +18 -0
- package/registry/radix/field.json +22 -0
- package/registry/radix/hooks/use-highlighted-code.ts +75 -0
- package/registry/radix/hooks/use-mobile.ts +19 -0
- package/registry/radix/hover-card.json +18 -0
- package/registry/radix/input-group.json +23 -0
- package/registry/radix/input-otp.json +19 -0
- package/registry/radix/input.json +17 -0
- package/registry/radix/item.json +22 -0
- package/registry/radix/kbd.json +17 -0
- package/registry/radix/label.json +18 -0
- package/registry/radix/lib/highlighter.tsx +124 -0
- package/registry/radix/lib/utils.ts +1 -0
- package/registry/radix/marker.json +19 -0
- package/registry/radix/menubar.json +19 -0
- package/registry/radix/message-scroller.json +22 -0
- package/registry/radix/message.json +17 -0
- package/registry/radix/mostra-layout.json +20 -0
- package/registry/radix/mostra-questionnaire.json +22 -0
- package/registry/radix/native-select.json +18 -0
- package/registry/radix/navigation-menu.json +20 -0
- package/registry/radix/pagination.json +21 -0
- package/registry/radix/popover.json +18 -0
- package/registry/radix/progress.json +18 -0
- package/registry/radix/questionnaire.json +22 -0
- package/registry/radix/radio-group.json +18 -0
- package/registry/radix/registry.json +283 -0
- package/registry/radix/resizable.json +18 -0
- package/registry/radix/scroll-area.json +18 -0
- package/registry/radix/select.json +19 -0
- package/registry/radix/separator.json +18 -0
- package/registry/radix/sheet.json +22 -0
- package/registry/radix/sidebar.json +29 -0
- package/registry/radix/skeleton.json +17 -0
- package/registry/radix/slider.json +18 -0
- package/registry/radix/sonner.json +18 -0
- package/registry/radix/spinner.json +18 -0
- package/registry/radix/switch.json +18 -0
- package/registry/radix/table.json +17 -0
- package/registry/radix/tabs.json +19 -0
- package/registry/radix/textarea.json +17 -0
- package/registry/radix/toggle-group.json +22 -0
- package/registry/radix/toggle.json +19 -0
- package/registry/radix/tooltip.json +18 -0
- package/registry/radix/trace.json +21 -0
- package/registry/radix/use-highlighted-code.json +21 -0
- package/registry/radix/use-mobile.json +14 -0
- package/registry/radix/utils.json +17 -0
- package/src/index.ts +15 -0
- package/src/layout.tsx +221 -0
- package/src/questionnaire.tsx +1005 -0
|
@@ -0,0 +1,1005 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The questionnaire (ROUND-3 §1.4, KT-05): questions people answer, each answer a field
|
|
5
|
+
* (`<questionnaire>.<question>`, D52) saved as they go, labelled in results with the options the
|
|
6
|
+
* person saw and whether they took the one recommended. Choices (one or several, with an answer of
|
|
7
|
+
* their own), scales, rankings and text; a comment on any question; required questions, which the
|
|
8
|
+
* trusted Submit waits for; sections; progress; on one page or a step at a time.
|
|
9
|
+
*
|
|
10
|
+
* Ordinary React on native inputs, so it works in either base and anywhere React runs; every part
|
|
11
|
+
* reads the theme's tokens and takes `className`, and `mostra add mostra-questionnaire` copies it
|
|
12
|
+
* into the artifact for editing, where the copy then wins (D68, KT-10).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { useField } from "@mostra/react";
|
|
16
|
+
import { cn } from "cn";
|
|
17
|
+
import { ArrowDown, ArrowUp, Check, X } from "lucide-react";
|
|
18
|
+
import * as React from "react";
|
|
19
|
+
|
|
20
|
+
/* -------------------------------------------------------------------------------------------- */
|
|
21
|
+
/* The questionnaire */
|
|
22
|
+
|
|
23
|
+
interface QuestionnaireContextValue {
|
|
24
|
+
readonly id: string;
|
|
25
|
+
/** Each question's number on the page, by its ID, in reading order. */
|
|
26
|
+
readonly numbers: ReadonlyMap<string, number>;
|
|
27
|
+
readonly report: (question: string, answered: boolean) => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const QuestionnaireContext = React.createContext<QuestionnaireContextValue | null>(null);
|
|
31
|
+
|
|
32
|
+
function useQuestionnaire(part: string): QuestionnaireContextValue {
|
|
33
|
+
const context = React.useContext(QuestionnaireContext);
|
|
34
|
+
if (context === null) throw new Error(`<${part}> belongs inside a <Questionnaire>.`);
|
|
35
|
+
return context;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The `id`s of the questions under `children`, in reading order, through sections and fragments. */
|
|
39
|
+
function questionIds(children: React.ReactNode): string[] {
|
|
40
|
+
const ids: string[] = [];
|
|
41
|
+
React.Children.forEach(children, (child) => {
|
|
42
|
+
if (!React.isValidElement(child)) return;
|
|
43
|
+
const props = child.props as { id?: unknown; children?: React.ReactNode };
|
|
44
|
+
if (child.type === Question && typeof props.id === "string") ids.push(props.id);
|
|
45
|
+
else if (child.type === Section || child.type === React.Fragment) {
|
|
46
|
+
ids.push(...questionIds(props.children));
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
return ids;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface QuestionnaireProps extends Omit<React.ComponentProps<"div">, "id" | "title"> {
|
|
53
|
+
/** The questionnaire's ID: every answer is the field `<id>.<question>`. */
|
|
54
|
+
readonly id: string;
|
|
55
|
+
readonly title?: React.ReactNode;
|
|
56
|
+
readonly description?: React.ReactNode;
|
|
57
|
+
/** `page` (the default): every question at once. `steps`: a section, or a question, at a time. */
|
|
58
|
+
readonly mode?: "page" | "steps";
|
|
59
|
+
/** Show how many questions are answered (the default). */
|
|
60
|
+
readonly progress?: boolean;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function Questionnaire({
|
|
64
|
+
id,
|
|
65
|
+
title,
|
|
66
|
+
description,
|
|
67
|
+
mode = "page",
|
|
68
|
+
progress = true,
|
|
69
|
+
className,
|
|
70
|
+
children,
|
|
71
|
+
...props
|
|
72
|
+
}: QuestionnaireProps): React.JSX.Element {
|
|
73
|
+
const ids = questionIds(children);
|
|
74
|
+
const numbers = new Map(ids.map((question, index) => [question, index + 1]));
|
|
75
|
+
const [answered, setAnswered] = React.useState<ReadonlySet<string>>(new Set());
|
|
76
|
+
const report = React.useCallback((question: string, isAnswered: boolean) => {
|
|
77
|
+
setAnswered((current) => {
|
|
78
|
+
if (current.has(question) === isAnswered) return current;
|
|
79
|
+
const next = new Set(current);
|
|
80
|
+
if (isAnswered) next.add(question);
|
|
81
|
+
else next.delete(question);
|
|
82
|
+
return next;
|
|
83
|
+
});
|
|
84
|
+
}, []);
|
|
85
|
+
const context: QuestionnaireContextValue = { id, numbers, report };
|
|
86
|
+
const numbered = numberSections(children);
|
|
87
|
+
const steps = React.Children.toArray(numbered).filter(React.isValidElement);
|
|
88
|
+
const [step, setStep] = React.useState(0);
|
|
89
|
+
const current = Math.min(step, Math.max(steps.length - 1, 0));
|
|
90
|
+
const count = ids.filter((question) => answered.has(question)).length;
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<QuestionnaireContext.Provider value={context}>
|
|
94
|
+
<div
|
|
95
|
+
data-slot="questionnaire"
|
|
96
|
+
data-mode={mode}
|
|
97
|
+
className={cn("flex flex-col gap-(--stack-gap)", className)}
|
|
98
|
+
{...props}
|
|
99
|
+
>
|
|
100
|
+
{(title !== undefined || description !== undefined || progress) && (
|
|
101
|
+
<header data-slot="questionnaire-header" className="flex flex-col gap-3">
|
|
102
|
+
{title !== undefined && <h1>{title}</h1>}
|
|
103
|
+
{description !== undefined && (
|
|
104
|
+
<p className="max-w-prose text-pretty text-muted-foreground">{description}</p>
|
|
105
|
+
)}
|
|
106
|
+
{progress && ids.length > 0 && <AnswerProgress answered={count} total={ids.length} />}
|
|
107
|
+
</header>
|
|
108
|
+
)}
|
|
109
|
+
{mode === "page"
|
|
110
|
+
? numbered
|
|
111
|
+
: steps.map((child, index) => (
|
|
112
|
+
// Every step stays mounted, so a required question on a later step still waits for
|
|
113
|
+
// its answer and a submit sends what each step holds (D110).
|
|
114
|
+
<div
|
|
115
|
+
key={child.key ?? index}
|
|
116
|
+
data-slot="questionnaire-step"
|
|
117
|
+
hidden={index !== current}
|
|
118
|
+
>
|
|
119
|
+
{child}
|
|
120
|
+
</div>
|
|
121
|
+
))}
|
|
122
|
+
{mode === "steps" && steps.length > 1 && (
|
|
123
|
+
<nav
|
|
124
|
+
data-slot="questionnaire-steps"
|
|
125
|
+
aria-label="Steps"
|
|
126
|
+
className="flex items-center justify-between gap-4 border-t border-border pt-5"
|
|
127
|
+
>
|
|
128
|
+
<span className="text-sm text-muted-foreground tabular-nums">
|
|
129
|
+
Step {current + 1} of {steps.length}
|
|
130
|
+
</span>
|
|
131
|
+
<span className="flex gap-2">
|
|
132
|
+
<button
|
|
133
|
+
type="button"
|
|
134
|
+
disabled={current === 0}
|
|
135
|
+
onClick={() => setStep(current - 1)}
|
|
136
|
+
className={stepButton("secondary")}
|
|
137
|
+
>
|
|
138
|
+
Previous
|
|
139
|
+
</button>
|
|
140
|
+
<button
|
|
141
|
+
type="button"
|
|
142
|
+
disabled={current === steps.length - 1}
|
|
143
|
+
onClick={() => setStep(current + 1)}
|
|
144
|
+
className={stepButton("primary")}
|
|
145
|
+
>
|
|
146
|
+
Next
|
|
147
|
+
</button>
|
|
148
|
+
</span>
|
|
149
|
+
</nav>
|
|
150
|
+
)}
|
|
151
|
+
</div>
|
|
152
|
+
</QuestionnaireContext.Provider>
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function stepButton(kind: "primary" | "secondary"): string {
|
|
157
|
+
return cn(
|
|
158
|
+
"press inline-flex h-(--control-h-sm) items-center rounded-button px-4 text-sm font-medium outline-none focus-visible:ring-3 focus-visible:ring-ring/40 disabled:pointer-events-none disabled:opacity-40",
|
|
159
|
+
kind === "primary"
|
|
160
|
+
? "bg-primary text-primary-foreground shadow-button hover:bg-[color-mix(in_oklab,var(--primary),var(--foreground)_14%)]"
|
|
161
|
+
: "bg-secondary text-secondary-foreground hover:bg-[color-mix(in_oklab,var(--secondary),var(--foreground)_6%)]",
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/* -------------------------------------------------------------------------------------------- */
|
|
166
|
+
/* Sections */
|
|
167
|
+
|
|
168
|
+
interface SectionProps extends Omit<React.ComponentProps<"section">, "title"> {
|
|
169
|
+
readonly title: React.ReactNode;
|
|
170
|
+
readonly description?: React.ReactNode;
|
|
171
|
+
/** Its number, which the questionnaire gives each of its sections in order. */
|
|
172
|
+
readonly index?: number;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function Section({
|
|
176
|
+
title,
|
|
177
|
+
description,
|
|
178
|
+
index,
|
|
179
|
+
className,
|
|
180
|
+
children,
|
|
181
|
+
...props
|
|
182
|
+
}: SectionProps): React.JSX.Element {
|
|
183
|
+
const headingId = React.useId();
|
|
184
|
+
return (
|
|
185
|
+
<section
|
|
186
|
+
data-slot="questionnaire-section"
|
|
187
|
+
aria-labelledby={headingId}
|
|
188
|
+
className={cn(
|
|
189
|
+
"flex flex-col gap-8 rounded-(--section-radius) bg-(--section-bg) p-(--section-pad) shadow-(--section-shadow) [border-top:var(--section-rule)]",
|
|
190
|
+
className,
|
|
191
|
+
)}
|
|
192
|
+
{...props}
|
|
193
|
+
>
|
|
194
|
+
<header className="flex flex-col gap-1.5">
|
|
195
|
+
<h2 id={headingId} className="flex items-center gap-[0.6em]">
|
|
196
|
+
{index !== undefined && (
|
|
197
|
+
<span
|
|
198
|
+
data-slot="section-index"
|
|
199
|
+
aria-hidden
|
|
200
|
+
className="grid size-(--section-index-size) shrink-0 place-items-center rounded-full bg-(--section-index-bg) text-[0.8em] text-(--section-index-color) tabular-nums"
|
|
201
|
+
>
|
|
202
|
+
{index}
|
|
203
|
+
</span>
|
|
204
|
+
)}
|
|
205
|
+
<span>{title}</span>
|
|
206
|
+
</h2>
|
|
207
|
+
{description !== undefined && (
|
|
208
|
+
<p className="max-w-prose text-pretty text-muted-foreground">{description}</p>
|
|
209
|
+
)}
|
|
210
|
+
</header>
|
|
211
|
+
{children}
|
|
212
|
+
</section>
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** The questionnaire's sections, numbered in order: those it holds directly or in fragments. */
|
|
217
|
+
function numberSections(children: React.ReactNode, next = { value: 1 }): React.ReactNode {
|
|
218
|
+
return React.Children.map(children, (child) => {
|
|
219
|
+
if (!React.isValidElement(child)) return child;
|
|
220
|
+
if (child.type === Section) {
|
|
221
|
+
const props = child.props as SectionProps;
|
|
222
|
+
return props.index !== undefined
|
|
223
|
+
? child
|
|
224
|
+
: React.cloneElement(child as React.ReactElement<SectionProps>, { index: next.value++ });
|
|
225
|
+
}
|
|
226
|
+
if (child.type === React.Fragment) {
|
|
227
|
+
const props = child.props as { children?: React.ReactNode };
|
|
228
|
+
return (
|
|
229
|
+
<React.Fragment key={child.key}>{numberSections(props.children, next)}</React.Fragment>
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
return child;
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/* -------------------------------------------------------------------------------------------- */
|
|
237
|
+
/* Questions */
|
|
238
|
+
|
|
239
|
+
interface QuestionContextValue {
|
|
240
|
+
/** The answer's field: `<questionnaire>.<question>`. */
|
|
241
|
+
readonly field: string;
|
|
242
|
+
readonly label: string | undefined;
|
|
243
|
+
readonly required: boolean;
|
|
244
|
+
readonly multiple: boolean;
|
|
245
|
+
readonly titleId: string;
|
|
246
|
+
readonly descriptionId: string | undefined;
|
|
247
|
+
readonly report: (answered: boolean) => void;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const QuestionContext = React.createContext<QuestionContextValue | null>(null);
|
|
251
|
+
|
|
252
|
+
function useQuestion(part: string): QuestionContextValue {
|
|
253
|
+
const context = React.useContext(QuestionContext);
|
|
254
|
+
if (context === null) throw new Error(`<${part}> belongs inside a <Question>.`);
|
|
255
|
+
return context;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** The text of a title, for results' labels, when it is text. */
|
|
259
|
+
function textOf(node: React.ReactNode): string | undefined {
|
|
260
|
+
if (typeof node === "string") return node;
|
|
261
|
+
if (typeof node === "number") return String(node);
|
|
262
|
+
return undefined;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
interface QuestionProps extends Omit<React.ComponentProps<"fieldset">, "id" | "title"> {
|
|
266
|
+
/** The question's ID inside its questionnaire: the answer is `<questionnaire>.<id>`. */
|
|
267
|
+
readonly id: string;
|
|
268
|
+
readonly title: React.ReactNode;
|
|
269
|
+
/** What results call it, when the title isn't plain text. */
|
|
270
|
+
readonly label?: string;
|
|
271
|
+
readonly description?: React.ReactNode;
|
|
272
|
+
/** The trusted Submit waits until it is answered. */
|
|
273
|
+
readonly required?: boolean;
|
|
274
|
+
/** Its choices take several answers. */
|
|
275
|
+
readonly multiple?: boolean;
|
|
276
|
+
/** A comment beside the answer: the field `<questionnaire>.<id>.comment`. */
|
|
277
|
+
readonly comments?: boolean;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function Question({
|
|
281
|
+
id,
|
|
282
|
+
title,
|
|
283
|
+
label,
|
|
284
|
+
description,
|
|
285
|
+
required = false,
|
|
286
|
+
multiple = false,
|
|
287
|
+
comments = false,
|
|
288
|
+
className,
|
|
289
|
+
children,
|
|
290
|
+
...props
|
|
291
|
+
}: QuestionProps): React.JSX.Element {
|
|
292
|
+
const questionnaire = useQuestionnaire("Question");
|
|
293
|
+
const field = `${questionnaire.id}.${id}`;
|
|
294
|
+
const titleId = React.useId();
|
|
295
|
+
const descriptionId = React.useId();
|
|
296
|
+
const reportTo = questionnaire.report;
|
|
297
|
+
const report = React.useCallback((answered: boolean) => reportTo(id, answered), [reportTo, id]);
|
|
298
|
+
const context = React.useMemo<QuestionContextValue>(
|
|
299
|
+
() => ({
|
|
300
|
+
field,
|
|
301
|
+
label: label ?? textOf(title),
|
|
302
|
+
required,
|
|
303
|
+
multiple,
|
|
304
|
+
titleId,
|
|
305
|
+
descriptionId: description === undefined ? undefined : descriptionId,
|
|
306
|
+
report,
|
|
307
|
+
}),
|
|
308
|
+
[field, label, title, required, multiple, titleId, description, descriptionId, report],
|
|
309
|
+
);
|
|
310
|
+
const choices = React.Children.toArray(children).some(
|
|
311
|
+
(child) => React.isValidElement(child) && (child.type === Choice || child.type === Other),
|
|
312
|
+
);
|
|
313
|
+
const number = questionnaire.numbers.get(id);
|
|
314
|
+
return (
|
|
315
|
+
<QuestionContext.Provider value={context}>
|
|
316
|
+
<fieldset
|
|
317
|
+
data-slot="question"
|
|
318
|
+
aria-describedby={description === undefined ? undefined : descriptionId}
|
|
319
|
+
className={cn("m-0 flex min-w-0 flex-col gap-4 border-0 p-0", className)}
|
|
320
|
+
{...props}
|
|
321
|
+
>
|
|
322
|
+
<legend id={titleId} className="contents">
|
|
323
|
+
<span className="flex items-baseline gap-3 font-heading text-[1.0625rem] leading-snug font-(--heading-weight) tracking-(--heading-tracking) text-balance">
|
|
324
|
+
{number !== undefined && (
|
|
325
|
+
<span
|
|
326
|
+
data-slot="question-number"
|
|
327
|
+
aria-hidden
|
|
328
|
+
className="font-sans text-sm font-semibold text-(--question-number-color) tabular-nums"
|
|
329
|
+
>
|
|
330
|
+
{number}
|
|
331
|
+
</span>
|
|
332
|
+
)}
|
|
333
|
+
<span>
|
|
334
|
+
{title}
|
|
335
|
+
{required ? (
|
|
336
|
+
<span className="ml-2 align-middle font-sans text-xs font-medium text-muted-foreground">
|
|
337
|
+
Required
|
|
338
|
+
</span>
|
|
339
|
+
) : null}
|
|
340
|
+
</span>
|
|
341
|
+
</span>
|
|
342
|
+
</legend>
|
|
343
|
+
{description !== undefined && (
|
|
344
|
+
<p id={descriptionId} className="-mt-2 text-sm text-pretty text-muted-foreground">
|
|
345
|
+
{description}
|
|
346
|
+
</p>
|
|
347
|
+
)}
|
|
348
|
+
{choices ? <ChoiceAnswer>{children}</ChoiceAnswer> : children}
|
|
349
|
+
{comments && <Comment />}
|
|
350
|
+
</fieldset>
|
|
351
|
+
</QuestionContext.Provider>
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** Reports to the questionnaire whether the question is answered. */
|
|
356
|
+
function useAnswered(report: (answered: boolean) => void, answered: boolean): void {
|
|
357
|
+
React.useEffect(() => report(answered), [report, answered]);
|
|
358
|
+
React.useEffect(() => () => report(false), [report]);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/* -------------------------------------------------------------------------------------------- */
|
|
362
|
+
/* Choices */
|
|
363
|
+
|
|
364
|
+
interface ChoiceProps {
|
|
365
|
+
/** The value the answer holds when this is chosen. */
|
|
366
|
+
readonly value: string;
|
|
367
|
+
readonly children: React.ReactNode;
|
|
368
|
+
/** What results call it, when its children aren't plain text. */
|
|
369
|
+
readonly label?: string;
|
|
370
|
+
readonly description?: React.ReactNode;
|
|
371
|
+
/** The option the page recommends, marked; a string names the mark ("Our pick"). */
|
|
372
|
+
readonly recommended?: boolean | string;
|
|
373
|
+
/** A figure beside it: a price, a count. */
|
|
374
|
+
readonly meta?: React.ReactNode;
|
|
375
|
+
/** A picture above it: a sketch, a screenshot. */
|
|
376
|
+
readonly media?: React.ReactNode;
|
|
377
|
+
readonly className?: string;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** A choice of a question: rendered by its question, which owns the answer. */
|
|
381
|
+
function Choice(_props: ChoiceProps): React.ReactNode {
|
|
382
|
+
return null;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
interface OtherProps {
|
|
386
|
+
/** Its label: "Something else" unless given. */
|
|
387
|
+
readonly children?: React.ReactNode;
|
|
388
|
+
readonly placeholder?: string;
|
|
389
|
+
readonly className?: string;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** The value an answer of the person's own holds; the text is `<question>.other`. */
|
|
393
|
+
const OTHER = "other";
|
|
394
|
+
|
|
395
|
+
/** An answer of the person's own: chosen, then written. */
|
|
396
|
+
function Other(_props: OtherProps): React.ReactNode {
|
|
397
|
+
return null;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function ChoiceAnswer({ children }: { readonly children: React.ReactNode }) {
|
|
401
|
+
const question = useQuestion("Choice");
|
|
402
|
+
const items = React.Children.toArray(children).filter(React.isValidElement) as React.ReactElement<
|
|
403
|
+
ChoiceProps | OtherProps
|
|
404
|
+
>[];
|
|
405
|
+
const choices = items.filter((item) => item.type === Choice) as React.ReactElement<ChoiceProps>[];
|
|
406
|
+
const other = items.find((item) => item.type === Other) as
|
|
407
|
+
| React.ReactElement<OtherProps>
|
|
408
|
+
| undefined;
|
|
409
|
+
const otherLabel = textOf(other?.props.children) ?? "Something else";
|
|
410
|
+
const options = [
|
|
411
|
+
...choices.map((choice) => ({
|
|
412
|
+
value: choice.props.value,
|
|
413
|
+
...((choice.props.label ?? textOf(choice.props.children)) !== undefined
|
|
414
|
+
? { label: (choice.props.label ?? textOf(choice.props.children)) as string }
|
|
415
|
+
: {}),
|
|
416
|
+
...(choice.props.recommended ? { recommended: true } : {}),
|
|
417
|
+
})),
|
|
418
|
+
...(other ? [{ value: OTHER, label: otherLabel }] : []),
|
|
419
|
+
];
|
|
420
|
+
const fieldOptions = {
|
|
421
|
+
...(question.label !== undefined ? { label: question.label } : {}),
|
|
422
|
+
required: question.required,
|
|
423
|
+
options,
|
|
424
|
+
};
|
|
425
|
+
return question.multiple ? (
|
|
426
|
+
<ManyChoices choices={choices} other={other} otherLabel={otherLabel} options={fieldOptions} />
|
|
427
|
+
) : (
|
|
428
|
+
<OneChoice choices={choices} other={other} otherLabel={otherLabel} options={fieldOptions} />
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
interface ChoiceListProps {
|
|
433
|
+
readonly choices: readonly React.ReactElement<ChoiceProps>[];
|
|
434
|
+
readonly other: React.ReactElement<OtherProps> | undefined;
|
|
435
|
+
readonly otherLabel: string;
|
|
436
|
+
readonly options: {
|
|
437
|
+
readonly label?: string;
|
|
438
|
+
readonly required: boolean;
|
|
439
|
+
readonly options: readonly { value: string; label?: string; recommended?: boolean }[];
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function OneChoice({ choices, other, otherLabel, options }: ChoiceListProps) {
|
|
444
|
+
const question = useQuestion("Choice");
|
|
445
|
+
const [value, setValue] = useField<string>(question.field, "", options);
|
|
446
|
+
useAnswered(question.report, value !== "");
|
|
447
|
+
const media = choices.some((choice) => choice.props.media !== undefined);
|
|
448
|
+
return (
|
|
449
|
+
<div className="flex flex-col gap-3">
|
|
450
|
+
<div
|
|
451
|
+
role="radiogroup"
|
|
452
|
+
aria-labelledby={question.titleId}
|
|
453
|
+
aria-required={question.required || undefined}
|
|
454
|
+
className={cn("grid gap-2.5", media && "sm:grid-cols-2")}
|
|
455
|
+
>
|
|
456
|
+
{choices.map((choice) => (
|
|
457
|
+
<ChoiceCard
|
|
458
|
+
key={choice.props.value}
|
|
459
|
+
type="radio"
|
|
460
|
+
name={question.field}
|
|
461
|
+
checked={value === choice.props.value}
|
|
462
|
+
onChange={() => setValue(choice.props.value)}
|
|
463
|
+
{...choice.props}
|
|
464
|
+
/>
|
|
465
|
+
))}
|
|
466
|
+
{other && (
|
|
467
|
+
<ChoiceCard
|
|
468
|
+
type="radio"
|
|
469
|
+
name={question.field}
|
|
470
|
+
value={OTHER}
|
|
471
|
+
checked={value === OTHER}
|
|
472
|
+
onChange={() => setValue(OTHER)}
|
|
473
|
+
className={other.props.className}
|
|
474
|
+
>
|
|
475
|
+
{otherLabel}
|
|
476
|
+
</ChoiceCard>
|
|
477
|
+
)}
|
|
478
|
+
</div>
|
|
479
|
+
{other && value === OTHER && (
|
|
480
|
+
<OtherText placeholder={other.props.placeholder} label={otherLabel} />
|
|
481
|
+
)}
|
|
482
|
+
{!question.required && value !== "" && (
|
|
483
|
+
<button
|
|
484
|
+
type="button"
|
|
485
|
+
onClick={() => setValue("")}
|
|
486
|
+
aria-label={question.label ? `Clear my answer to “${question.label}”` : "Clear my answer"}
|
|
487
|
+
className="press inline-flex w-fit items-center gap-1.5 rounded-button px-2 py-1 text-sm text-muted-foreground outline-none hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/40"
|
|
488
|
+
>
|
|
489
|
+
<X aria-hidden className="size-3.5" />
|
|
490
|
+
Clear my answer
|
|
491
|
+
</button>
|
|
492
|
+
)}
|
|
493
|
+
</div>
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function ManyChoices({ choices, other, otherLabel, options }: ChoiceListProps) {
|
|
498
|
+
const question = useQuestion("Choice");
|
|
499
|
+
const [value, setValue] = useField<string[]>(question.field, [], options);
|
|
500
|
+
useAnswered(question.report, value.length > 0);
|
|
501
|
+
const toggle = (choice: string, on: boolean) =>
|
|
502
|
+
setValue((current) =>
|
|
503
|
+
on ? [...current.filter((v) => v !== choice), choice] : current.filter((v) => v !== choice),
|
|
504
|
+
);
|
|
505
|
+
const media = choices.some((choice) => choice.props.media !== undefined);
|
|
506
|
+
return (
|
|
507
|
+
<div className="flex flex-col gap-3">
|
|
508
|
+
<div className={cn("grid gap-2.5", media && "sm:grid-cols-2")}>
|
|
509
|
+
{choices.map((choice) => (
|
|
510
|
+
<ChoiceCard
|
|
511
|
+
key={choice.props.value}
|
|
512
|
+
type="checkbox"
|
|
513
|
+
name={question.field}
|
|
514
|
+
checked={value.includes(choice.props.value)}
|
|
515
|
+
onChange={(event) => toggle(choice.props.value, event.currentTarget.checked)}
|
|
516
|
+
{...choice.props}
|
|
517
|
+
/>
|
|
518
|
+
))}
|
|
519
|
+
{other && (
|
|
520
|
+
<ChoiceCard
|
|
521
|
+
type="checkbox"
|
|
522
|
+
name={question.field}
|
|
523
|
+
value={OTHER}
|
|
524
|
+
checked={value.includes(OTHER)}
|
|
525
|
+
onChange={(event) => toggle(OTHER, event.currentTarget.checked)}
|
|
526
|
+
className={other.props.className}
|
|
527
|
+
>
|
|
528
|
+
{otherLabel}
|
|
529
|
+
</ChoiceCard>
|
|
530
|
+
)}
|
|
531
|
+
</div>
|
|
532
|
+
{other && value.includes(OTHER) && (
|
|
533
|
+
<OtherText placeholder={other.props.placeholder} label={otherLabel} />
|
|
534
|
+
)}
|
|
535
|
+
</div>
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function OtherText({
|
|
540
|
+
placeholder,
|
|
541
|
+
label,
|
|
542
|
+
}: {
|
|
543
|
+
readonly placeholder?: string | undefined;
|
|
544
|
+
readonly label: string;
|
|
545
|
+
}) {
|
|
546
|
+
const question = useQuestion("Other");
|
|
547
|
+
const [text, setText] = useField<string>(`${question.field}.other`, "", {
|
|
548
|
+
label: question.label !== undefined ? `${question.label}: ${label}` : label,
|
|
549
|
+
});
|
|
550
|
+
return (
|
|
551
|
+
<input
|
|
552
|
+
type="text"
|
|
553
|
+
data-slot="question-other"
|
|
554
|
+
aria-label={label}
|
|
555
|
+
value={text}
|
|
556
|
+
placeholder={placeholder ?? "Say what you'd choose"}
|
|
557
|
+
onChange={(event) => setText(event.currentTarget.value)}
|
|
558
|
+
className={fieldClasses}
|
|
559
|
+
/>
|
|
560
|
+
);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
interface ChoiceCardProps extends Omit<ChoiceProps, "children" | "value" | "label" | "className"> {
|
|
564
|
+
readonly type: "radio" | "checkbox";
|
|
565
|
+
readonly name: string;
|
|
566
|
+
readonly value: string;
|
|
567
|
+
readonly checked: boolean;
|
|
568
|
+
readonly onChange: React.ChangeEventHandler<HTMLInputElement>;
|
|
569
|
+
readonly children: React.ReactNode;
|
|
570
|
+
readonly label?: string;
|
|
571
|
+
readonly className?: string | undefined;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
function ChoiceCard({
|
|
575
|
+
type,
|
|
576
|
+
name,
|
|
577
|
+
value,
|
|
578
|
+
checked,
|
|
579
|
+
onChange,
|
|
580
|
+
children,
|
|
581
|
+
description,
|
|
582
|
+
recommended,
|
|
583
|
+
meta,
|
|
584
|
+
media,
|
|
585
|
+
className,
|
|
586
|
+
}: ChoiceCardProps) {
|
|
587
|
+
const descriptionId = React.useId();
|
|
588
|
+
const body = (
|
|
589
|
+
<>
|
|
590
|
+
<AnswerMark shape={type === "radio" ? "circle" : "square"} />
|
|
591
|
+
<span className="flex min-w-0 flex-1 flex-col gap-1">
|
|
592
|
+
<span className="flex flex-wrap items-center gap-x-2.5 gap-y-1">
|
|
593
|
+
<span className="font-medium">{children}</span>
|
|
594
|
+
{recommended ? (
|
|
595
|
+
<span
|
|
596
|
+
data-slot="recommended"
|
|
597
|
+
className="inline-flex items-center gap-1.5 text-xs font-semibold text-highlight group-data-[checked=true]/choice:text-(--choice-selected-fg)"
|
|
598
|
+
>
|
|
599
|
+
<span
|
|
600
|
+
aria-hidden
|
|
601
|
+
className="size-1.5 rounded-full bg-highlight group-data-[checked=true]/choice:bg-answer"
|
|
602
|
+
/>
|
|
603
|
+
{typeof recommended === "string" ? recommended : "Recommended"}
|
|
604
|
+
</span>
|
|
605
|
+
) : null}
|
|
606
|
+
</span>
|
|
607
|
+
{description !== undefined && (
|
|
608
|
+
<span
|
|
609
|
+
id={descriptionId}
|
|
610
|
+
className="text-sm text-muted-foreground group-data-[checked=true]/choice:text-(--choice-selected-muted)"
|
|
611
|
+
>
|
|
612
|
+
{description}
|
|
613
|
+
</span>
|
|
614
|
+
)}
|
|
615
|
+
{media !== undefined && meta !== undefined && (
|
|
616
|
+
<span className="pt-1 text-sm font-medium tabular-nums">{meta}</span>
|
|
617
|
+
)}
|
|
618
|
+
</span>
|
|
619
|
+
{media === undefined && meta !== undefined && (
|
|
620
|
+
<span className="shrink-0 text-sm text-muted-foreground tabular-nums group-data-[checked=true]/choice:text-(--choice-selected-muted)">
|
|
621
|
+
{meta}
|
|
622
|
+
</span>
|
|
623
|
+
)}
|
|
624
|
+
</>
|
|
625
|
+
);
|
|
626
|
+
return (
|
|
627
|
+
<label
|
|
628
|
+
data-slot="choice"
|
|
629
|
+
data-checked={checked}
|
|
630
|
+
className={cn(
|
|
631
|
+
"group/choice press relative flex w-full cursor-pointer rounded-choice bg-(--choice-bg) p-4 text-left shadow-choice transition-[background-color,box-shadow,color] duration-(--state-ms)",
|
|
632
|
+
media !== undefined ? "flex-col gap-4" : "items-start gap-3.5",
|
|
633
|
+
"hover:bg-(--choice-hover-bg) has-[:focus-visible]:ring-3 has-[:focus-visible]:ring-ring/40",
|
|
634
|
+
"data-[checked=true]:bg-(--choice-selected-bg) data-[checked=true]:text-(--choice-selected-fg) data-[checked=true]:shadow-(--choice-selected-shadow) data-[checked=true]:hover:bg-(--choice-selected-hover-bg)",
|
|
635
|
+
className,
|
|
636
|
+
)}
|
|
637
|
+
>
|
|
638
|
+
<input
|
|
639
|
+
type={type}
|
|
640
|
+
name={name}
|
|
641
|
+
value={value}
|
|
642
|
+
checked={checked}
|
|
643
|
+
onChange={onChange}
|
|
644
|
+
aria-describedby={description !== undefined ? descriptionId : undefined}
|
|
645
|
+
className="absolute inset-0 z-10 m-0 size-full cursor-pointer appearance-none rounded-[inherit] opacity-0"
|
|
646
|
+
/>
|
|
647
|
+
{media !== undefined ? (
|
|
648
|
+
<>
|
|
649
|
+
<span data-slot="choice-media" className="block w-full">
|
|
650
|
+
{media}
|
|
651
|
+
</span>
|
|
652
|
+
<span className="flex items-start gap-3.5">{body}</span>
|
|
653
|
+
</>
|
|
654
|
+
) : (
|
|
655
|
+
body
|
|
656
|
+
)}
|
|
657
|
+
</label>
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/** The mark of an answer: a giallo dot that drops in, the theme's signature move (D181 item 1). */
|
|
662
|
+
function AnswerMark({
|
|
663
|
+
shape = "circle",
|
|
664
|
+
}: {
|
|
665
|
+
readonly shape?: "circle" | "square";
|
|
666
|
+
}): React.JSX.Element {
|
|
667
|
+
return (
|
|
668
|
+
<span
|
|
669
|
+
data-slot="answer-mark"
|
|
670
|
+
aria-hidden
|
|
671
|
+
className={cn(
|
|
672
|
+
"relative mt-px grid size-5 shrink-0 place-items-center border-[1.5px] border-input bg-card transition-colors duration-(--state-ms)",
|
|
673
|
+
"group-data-[checked=true]/choice:border-foreground group-data-[checked=true]/choice:bg-answer",
|
|
674
|
+
shape === "circle" ? "rounded-full" : "rounded-[5px]",
|
|
675
|
+
)}
|
|
676
|
+
>
|
|
677
|
+
{shape === "circle" ? (
|
|
678
|
+
<span className="animate-answer hidden size-2 rounded-full bg-foreground group-data-[checked=true]/choice:block" />
|
|
679
|
+
) : (
|
|
680
|
+
<Check
|
|
681
|
+
strokeWidth={3}
|
|
682
|
+
className="animate-answer hidden size-3.5 text-foreground group-data-[checked=true]/choice:block"
|
|
683
|
+
/>
|
|
684
|
+
)}
|
|
685
|
+
</span>
|
|
686
|
+
);
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
/* -------------------------------------------------------------------------------------------- */
|
|
690
|
+
/* Scales */
|
|
691
|
+
|
|
692
|
+
interface ScaleProps {
|
|
693
|
+
readonly min?: number;
|
|
694
|
+
readonly max?: number;
|
|
695
|
+
/** The words at either end ("Airy", "Dense"). */
|
|
696
|
+
readonly labels?: readonly [string, string];
|
|
697
|
+
readonly className?: string;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
function Scale({ min = 1, max = 5, labels, className }: ScaleProps): React.JSX.Element {
|
|
701
|
+
const question = useQuestion("Scale");
|
|
702
|
+
const steps = Array.from({ length: Math.max(0, max - min + 1) }, (_, index) => min + index);
|
|
703
|
+
const [value, setValue] = useField<number | null>(question.field, null, {
|
|
704
|
+
...(question.label !== undefined ? { label: question.label } : {}),
|
|
705
|
+
required: question.required,
|
|
706
|
+
options: steps.map((step, index) => ({
|
|
707
|
+
value: step,
|
|
708
|
+
label:
|
|
709
|
+
labels !== undefined && index === 0
|
|
710
|
+
? `${step} (${labels[0]})`
|
|
711
|
+
: labels !== undefined && index === steps.length - 1
|
|
712
|
+
? `${step} (${labels[1]})`
|
|
713
|
+
: String(step),
|
|
714
|
+
})),
|
|
715
|
+
});
|
|
716
|
+
useAnswered(question.report, value !== null);
|
|
717
|
+
return (
|
|
718
|
+
<div data-slot="scale" className={cn("flex flex-col gap-2.5", className)}>
|
|
719
|
+
<div
|
|
720
|
+
role="radiogroup"
|
|
721
|
+
aria-labelledby={question.titleId}
|
|
722
|
+
className="grid gap-1.5"
|
|
723
|
+
style={{ gridTemplateColumns: `repeat(${steps.length}, minmax(0, 1fr))` }}
|
|
724
|
+
>
|
|
725
|
+
{steps.map((step, index) => (
|
|
726
|
+
<label
|
|
727
|
+
key={step}
|
|
728
|
+
data-slot="scale-step"
|
|
729
|
+
data-checked={value === step}
|
|
730
|
+
className={cn(
|
|
731
|
+
"press relative grid h-12 cursor-pointer place-items-center rounded-control bg-(--choice-bg) text-[0.9375rem] font-medium tabular-nums shadow-choice transition-[background-color,box-shadow] duration-(--state-ms)",
|
|
732
|
+
"hover:bg-(--choice-hover-bg) has-[:focus-visible]:ring-3 has-[:focus-visible]:ring-ring/40",
|
|
733
|
+
"data-[checked=true]:bg-(--scale-selected-bg) data-[checked=true]:text-(--scale-selected-fg) data-[checked=true]:shadow-(--scale-selected-shadow)",
|
|
734
|
+
)}
|
|
735
|
+
>
|
|
736
|
+
<input
|
|
737
|
+
type="radio"
|
|
738
|
+
name={question.field}
|
|
739
|
+
value={step}
|
|
740
|
+
checked={value === step}
|
|
741
|
+
onChange={() => setValue(step)}
|
|
742
|
+
aria-label={
|
|
743
|
+
labels !== undefined && index === 0
|
|
744
|
+
? `${step}, ${labels[0]}`
|
|
745
|
+
: labels !== undefined && index === steps.length - 1
|
|
746
|
+
? `${step}, ${labels[1]}`
|
|
747
|
+
: String(step)
|
|
748
|
+
}
|
|
749
|
+
className="absolute inset-0 z-10 m-0 size-full cursor-pointer appearance-none rounded-[inherit] opacity-0"
|
|
750
|
+
/>
|
|
751
|
+
<span aria-hidden>{step}</span>
|
|
752
|
+
</label>
|
|
753
|
+
))}
|
|
754
|
+
</div>
|
|
755
|
+
{labels !== undefined && (
|
|
756
|
+
<div aria-hidden className="flex justify-between text-sm text-muted-foreground">
|
|
757
|
+
<span>{labels[0]}</span>
|
|
758
|
+
<span>{labels[1]}</span>
|
|
759
|
+
</div>
|
|
760
|
+
)}
|
|
761
|
+
</div>
|
|
762
|
+
);
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
/* -------------------------------------------------------------------------------------------- */
|
|
766
|
+
/* Rankings */
|
|
767
|
+
|
|
768
|
+
type RankItem = string | { readonly value: string; readonly label: string };
|
|
769
|
+
|
|
770
|
+
interface RankProps {
|
|
771
|
+
/** What to put in order, in the order shown first. */
|
|
772
|
+
readonly items: readonly RankItem[];
|
|
773
|
+
readonly className?: string;
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
function Rank({ items, className }: RankProps): React.JSX.Element {
|
|
777
|
+
const question = useQuestion("Rank");
|
|
778
|
+
const all = items.map((item) => (typeof item === "string" ? { value: item, label: item } : item));
|
|
779
|
+
const [order, setOrder] = useField<string[] | null>(question.field, null, {
|
|
780
|
+
...(question.label !== undefined ? { label: question.label } : {}),
|
|
781
|
+
required: question.required,
|
|
782
|
+
options: all.map((item) => ({ value: item.value, label: item.label })),
|
|
783
|
+
});
|
|
784
|
+
useAnswered(question.report, order !== null);
|
|
785
|
+
// An order kept from before keeps what is still offered, then adds what is new at the end.
|
|
786
|
+
const shown =
|
|
787
|
+
order === null
|
|
788
|
+
? all
|
|
789
|
+
: [
|
|
790
|
+
...order.flatMap((value) => all.filter((item) => item.value === value)),
|
|
791
|
+
...all.filter((item) => !order.includes(item.value)),
|
|
792
|
+
];
|
|
793
|
+
const move = (from: number, to: number) => {
|
|
794
|
+
if (to < 0 || to >= shown.length) return;
|
|
795
|
+
const next = shown.map((item) => item.value);
|
|
796
|
+
const [moved] = next.splice(from, 1);
|
|
797
|
+
next.splice(to, 0, moved as string);
|
|
798
|
+
setOrder(next);
|
|
799
|
+
};
|
|
800
|
+
return (
|
|
801
|
+
<div data-slot="rank" className={cn("flex flex-col gap-3", className)}>
|
|
802
|
+
<ol aria-labelledby={question.titleId} className="flex flex-col gap-2">
|
|
803
|
+
{shown.map((item, index) => (
|
|
804
|
+
<li
|
|
805
|
+
key={item.value}
|
|
806
|
+
data-slot="rank-item"
|
|
807
|
+
className="flex items-center gap-3 rounded-choice bg-(--choice-bg) py-2 pr-2 pl-4 shadow-choice"
|
|
808
|
+
>
|
|
809
|
+
<span className="w-5 text-sm font-semibold text-muted-foreground tabular-nums">
|
|
810
|
+
{index + 1}
|
|
811
|
+
</span>
|
|
812
|
+
<span className="flex-1 font-medium">{item.label}</span>
|
|
813
|
+
<span className="flex gap-1">
|
|
814
|
+
<button
|
|
815
|
+
type="button"
|
|
816
|
+
aria-label={`Move ${item.label} up`}
|
|
817
|
+
disabled={index === 0}
|
|
818
|
+
onClick={() => move(index, index - 1)}
|
|
819
|
+
className="press grid size-9 place-items-center rounded-button text-muted-foreground outline-none hover:bg-muted hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/40 disabled:opacity-30"
|
|
820
|
+
>
|
|
821
|
+
<ArrowUp aria-hidden className="size-4" />
|
|
822
|
+
</button>
|
|
823
|
+
<button
|
|
824
|
+
type="button"
|
|
825
|
+
aria-label={`Move ${item.label} down`}
|
|
826
|
+
disabled={index === shown.length - 1}
|
|
827
|
+
onClick={() => move(index, index + 1)}
|
|
828
|
+
className="press grid size-9 place-items-center rounded-button text-muted-foreground outline-none hover:bg-muted hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/40 disabled:opacity-30"
|
|
829
|
+
>
|
|
830
|
+
<ArrowDown aria-hidden className="size-4" />
|
|
831
|
+
</button>
|
|
832
|
+
</span>
|
|
833
|
+
</li>
|
|
834
|
+
))}
|
|
835
|
+
</ol>
|
|
836
|
+
{order === null && (
|
|
837
|
+
<button
|
|
838
|
+
type="button"
|
|
839
|
+
onClick={() => setOrder(shown.map((item) => item.value))}
|
|
840
|
+
className={cn(stepButton("secondary"), "w-fit")}
|
|
841
|
+
>
|
|
842
|
+
Keep this order
|
|
843
|
+
</button>
|
|
844
|
+
)}
|
|
845
|
+
</div>
|
|
846
|
+
);
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/* -------------------------------------------------------------------------------------------- */
|
|
850
|
+
/* Text */
|
|
851
|
+
|
|
852
|
+
const fieldClasses =
|
|
853
|
+
"w-full min-w-0 rounded-control border border-input bg-(--field) px-3.5 text-base shadow-control outline-none transition-[border-color,box-shadow] duration-(--state-ms) placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/30 h-(--control-h)";
|
|
854
|
+
|
|
855
|
+
interface TextAnswerProps {
|
|
856
|
+
/** Several lines (the default), or one. */
|
|
857
|
+
readonly multiline?: boolean;
|
|
858
|
+
/** For one line: what kind of text, so phones offer the right keys. */
|
|
859
|
+
readonly type?: "text" | "email" | "number" | "url" | "tel" | "date" | "time";
|
|
860
|
+
readonly placeholder?: string;
|
|
861
|
+
readonly className?: string;
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
function TextAnswer({
|
|
865
|
+
multiline = true,
|
|
866
|
+
type = "text",
|
|
867
|
+
placeholder,
|
|
868
|
+
className,
|
|
869
|
+
}: TextAnswerProps): React.JSX.Element {
|
|
870
|
+
const question = useQuestion("TextAnswer");
|
|
871
|
+
const [text, setText] = useField<string>(question.field, "", {
|
|
872
|
+
...(question.label !== undefined ? { label: question.label } : {}),
|
|
873
|
+
required: question.required,
|
|
874
|
+
});
|
|
875
|
+
useAnswered(question.report, text.trim() !== "");
|
|
876
|
+
const shared = {
|
|
877
|
+
"aria-labelledby": question.titleId,
|
|
878
|
+
"aria-describedby": question.descriptionId,
|
|
879
|
+
"aria-required": question.required || undefined,
|
|
880
|
+
value: text,
|
|
881
|
+
placeholder,
|
|
882
|
+
};
|
|
883
|
+
return multiline ? (
|
|
884
|
+
<textarea
|
|
885
|
+
data-slot="text-answer"
|
|
886
|
+
{...shared}
|
|
887
|
+
onChange={(event) => setText(event.currentTarget.value)}
|
|
888
|
+
className={cn(
|
|
889
|
+
fieldClasses,
|
|
890
|
+
"h-auto min-h-28 py-3 leading-relaxed field-sizing-content",
|
|
891
|
+
className,
|
|
892
|
+
)}
|
|
893
|
+
/>
|
|
894
|
+
) : (
|
|
895
|
+
<input
|
|
896
|
+
data-slot="text-answer"
|
|
897
|
+
type={type}
|
|
898
|
+
{...shared}
|
|
899
|
+
onChange={(event) => setText(event.currentTarget.value)}
|
|
900
|
+
className={cn(fieldClasses, className)}
|
|
901
|
+
/>
|
|
902
|
+
);
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
/* -------------------------------------------------------------------------------------------- */
|
|
906
|
+
/* Comments */
|
|
907
|
+
|
|
908
|
+
function Comment() {
|
|
909
|
+
const question = useQuestion("Question");
|
|
910
|
+
const [comment, setComment] = useField<string>(`${question.field}.comment`, "", {
|
|
911
|
+
label: question.label !== undefined ? `${question.label}: comment` : "Comment",
|
|
912
|
+
});
|
|
913
|
+
const [open, setOpen] = React.useState(false);
|
|
914
|
+
if (!open && comment === "") {
|
|
915
|
+
return (
|
|
916
|
+
<button
|
|
917
|
+
type="button"
|
|
918
|
+
onClick={() => setOpen(true)}
|
|
919
|
+
className="press inline-flex w-fit items-center rounded-button px-2 py-1 text-sm text-muted-foreground underline-offset-4 outline-none hover:text-foreground hover:underline focus-visible:ring-3 focus-visible:ring-ring/40"
|
|
920
|
+
>
|
|
921
|
+
Add a comment
|
|
922
|
+
</button>
|
|
923
|
+
);
|
|
924
|
+
}
|
|
925
|
+
return (
|
|
926
|
+
<textarea
|
|
927
|
+
data-slot="question-comment"
|
|
928
|
+
aria-label={question.label !== undefined ? `Comment on “${question.label}”` : "Comment"}
|
|
929
|
+
value={comment}
|
|
930
|
+
placeholder="Anything to add?"
|
|
931
|
+
// biome-ignore lint/a11y/noAutofocus: it opens because the person asked to write.
|
|
932
|
+
autoFocus={open && comment === ""}
|
|
933
|
+
onChange={(event) => setComment(event.currentTarget.value)}
|
|
934
|
+
className={cn(
|
|
935
|
+
fieldClasses,
|
|
936
|
+
"h-auto min-h-20 py-2.5 text-[0.9375rem] leading-relaxed field-sizing-content",
|
|
937
|
+
)}
|
|
938
|
+
/>
|
|
939
|
+
);
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
/* -------------------------------------------------------------------------------------------- */
|
|
943
|
+
/* Progress */
|
|
944
|
+
|
|
945
|
+
interface AnswerProgressProps extends React.ComponentProps<"div"> {
|
|
946
|
+
readonly answered: number;
|
|
947
|
+
readonly total: number;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
/** How many questions are answered: a dot for each, giallo once answered. */
|
|
951
|
+
function AnswerProgress({
|
|
952
|
+
answered,
|
|
953
|
+
total,
|
|
954
|
+
className,
|
|
955
|
+
...props
|
|
956
|
+
}: AnswerProgressProps): React.JSX.Element {
|
|
957
|
+
return (
|
|
958
|
+
<div
|
|
959
|
+
data-slot="answer-progress"
|
|
960
|
+
className={cn("flex items-center gap-3 text-sm text-muted-foreground", className)}
|
|
961
|
+
{...props}
|
|
962
|
+
>
|
|
963
|
+
<span aria-hidden className="flex flex-wrap gap-1.5">
|
|
964
|
+
{Array.from({ length: total }, (_, index) => (
|
|
965
|
+
<span
|
|
966
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: the dots are positions, not items.
|
|
967
|
+
key={index}
|
|
968
|
+
className={cn(
|
|
969
|
+
"size-2.5 rounded-full border-[1.5px] border-input transition-colors duration-(--state-ms)",
|
|
970
|
+
index < answered && "animate-answer border-foreground bg-answer",
|
|
971
|
+
)}
|
|
972
|
+
/>
|
|
973
|
+
))}
|
|
974
|
+
</span>
|
|
975
|
+
<span aria-live="polite" className="tabular-nums">
|
|
976
|
+
{answered} of {total} answered
|
|
977
|
+
</span>
|
|
978
|
+
</div>
|
|
979
|
+
);
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
export type {
|
|
983
|
+
AnswerProgressProps,
|
|
984
|
+
ChoiceProps,
|
|
985
|
+
OtherProps,
|
|
986
|
+
QuestionnaireProps,
|
|
987
|
+
QuestionProps,
|
|
988
|
+
RankItem,
|
|
989
|
+
RankProps,
|
|
990
|
+
ScaleProps,
|
|
991
|
+
SectionProps,
|
|
992
|
+
TextAnswerProps,
|
|
993
|
+
};
|
|
994
|
+
export {
|
|
995
|
+
AnswerMark,
|
|
996
|
+
AnswerProgress,
|
|
997
|
+
Choice,
|
|
998
|
+
Other,
|
|
999
|
+
Question,
|
|
1000
|
+
Questionnaire,
|
|
1001
|
+
Rank,
|
|
1002
|
+
Scale,
|
|
1003
|
+
Section,
|
|
1004
|
+
TextAnswer,
|
|
1005
|
+
};
|