@composable-os/pack-forms 0.0.0-stage → 0.1.0-beta.12
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/LICENSE +109 -0
- package/dist/automations.d.ts +22 -0
- package/dist/automations.js +100 -0
- package/dist/blocks/forms-form/form.d.ts +46 -0
- package/dist/blocks/forms-form/form.js +196 -0
- package/dist/blocks/forms-native-form/form-screen.d.ts +35 -0
- package/dist/blocks/forms-native-form/form-screen.js +161 -0
- package/dist/client/index.d.ts +302 -0
- package/dist/client/index.js +431 -0
- package/dist/client/page.d.ts +67 -0
- package/dist/client/page.js +634 -0
- package/dist/index.d.ts +605 -0
- package/dist/index.js +406 -0
- package/dist/routine-mail.d.ts +23 -0
- package/dist/routine-mail.js +25 -0
- package/dist/tools.d.ts +92 -0
- package/dist/tools.js +288 -0
- package/package.json +40 -4
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, ALv2 Future License
|
|
2
|
+
|
|
3
|
+
> This licence covers the repository except the directories listed in NOTICE. In particular, the
|
|
4
|
+
> assistant packages `packages/agent-*` (agent-core, agent-a2ui, agent-voice, agent-plugins) are
|
|
5
|
+
> licensed under the MIT License; see the LICENSE file in each of those directories (ADR 0350).
|
|
6
|
+
|
|
7
|
+
## Abbreviation
|
|
8
|
+
|
|
9
|
+
FSL-1.1-ALv2
|
|
10
|
+
|
|
11
|
+
## Notice
|
|
12
|
+
|
|
13
|
+
Copyright 2026 Tim O'Connell
|
|
14
|
+
|
|
15
|
+
## Terms and Conditions
|
|
16
|
+
|
|
17
|
+
### Licensor ("We")
|
|
18
|
+
|
|
19
|
+
The party offering the Software under these Terms and Conditions.
|
|
20
|
+
|
|
21
|
+
### The Software
|
|
22
|
+
|
|
23
|
+
The "Software" is each version of the software that we make available under
|
|
24
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
25
|
+
Conditions with the Software.
|
|
26
|
+
|
|
27
|
+
### License Grant
|
|
28
|
+
|
|
29
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
30
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
31
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
32
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
33
|
+
|
|
34
|
+
### Permitted Purpose
|
|
35
|
+
|
|
36
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
37
|
+
means making the Software available to others in a commercial product or
|
|
38
|
+
service that:
|
|
39
|
+
|
|
40
|
+
1. substitutes for the Software;
|
|
41
|
+
|
|
42
|
+
2. substitutes for any other product or service we offer using the Software
|
|
43
|
+
that exists as of the date we make the Software available; or
|
|
44
|
+
|
|
45
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
46
|
+
|
|
47
|
+
Permitted Purposes specifically include using the Software:
|
|
48
|
+
|
|
49
|
+
1. for your internal use and access;
|
|
50
|
+
|
|
51
|
+
2. for non-commercial education;
|
|
52
|
+
|
|
53
|
+
3. for non-commercial research; and
|
|
54
|
+
|
|
55
|
+
4. in connection with professional services that you provide to a licensee
|
|
56
|
+
using the Software in accordance with these Terms and Conditions.
|
|
57
|
+
|
|
58
|
+
### Patents
|
|
59
|
+
|
|
60
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
61
|
+
patents, the license grant above includes a license under our patents. If you
|
|
62
|
+
make a claim against any party that the Software infringes or contributes to
|
|
63
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
64
|
+
immediately.
|
|
65
|
+
|
|
66
|
+
### Redistribution
|
|
67
|
+
|
|
68
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
69
|
+
the Software.
|
|
70
|
+
|
|
71
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
72
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
73
|
+
remove any copyright notices provided in or with the Software.
|
|
74
|
+
|
|
75
|
+
### Disclaimer
|
|
76
|
+
|
|
77
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
78
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
79
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
80
|
+
|
|
81
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
82
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
83
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
84
|
+
|
|
85
|
+
### Trademarks
|
|
86
|
+
|
|
87
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
88
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
89
|
+
trademarks, trade names, service marks or product names.
|
|
90
|
+
|
|
91
|
+
## Grant of Future License
|
|
92
|
+
|
|
93
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
94
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
95
|
+
the date we make the Software available. On or after that date, you may use the
|
|
96
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
97
|
+
will apply:
|
|
98
|
+
|
|
99
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
100
|
+
this file except in compliance with the License.
|
|
101
|
+
|
|
102
|
+
You may obtain a copy of the License at
|
|
103
|
+
|
|
104
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
105
|
+
|
|
106
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
107
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
108
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
109
|
+
specific language governing permissions and limitations under the License.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export declare const lowRating: import("@composable-os/sdk").AutomationSpec<{
|
|
2
|
+
readonly type: "record";
|
|
3
|
+
readonly object: "forms.submission";
|
|
4
|
+
readonly on: readonly ["created"];
|
|
5
|
+
}>;
|
|
6
|
+
export declare const receipt: import("@composable-os/sdk").AutomationSpec<{
|
|
7
|
+
readonly type: "record";
|
|
8
|
+
readonly object: "forms.submission";
|
|
9
|
+
readonly on: readonly ["created"];
|
|
10
|
+
}>;
|
|
11
|
+
export declare const formsAutomations: {
|
|
12
|
+
lowRating: import("@composable-os/sdk").AutomationSpec<{
|
|
13
|
+
readonly type: "record";
|
|
14
|
+
readonly object: "forms.submission";
|
|
15
|
+
readonly on: readonly ["created"];
|
|
16
|
+
}>;
|
|
17
|
+
receipt: import("@composable-os/sdk").AutomationSpec<{
|
|
18
|
+
readonly type: "record";
|
|
19
|
+
readonly object: "forms.submission";
|
|
20
|
+
readonly on: readonly ["created"];
|
|
21
|
+
}>;
|
|
22
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// The Forms automations. The notice of each new submission is the host's own (the door tells the
|
|
2
|
+
// form's chosen person, else the admins, ADR 0720); this one adds the low-rating notice: a first
|
|
3
|
+
// rating at or under the form's "tell at a rating of" tells the answers' owner (assignment rules),
|
|
4
|
+
// or the admins when they have none.
|
|
5
|
+
import { defineAutomation } from "@composable-os/sdk";
|
|
6
|
+
import { FORM } from "./tools.js";
|
|
7
|
+
const num = (value) => typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
8
|
+
export const lowRating = defineAutomation({
|
|
9
|
+
label: "Tell of a low rating",
|
|
10
|
+
description: "When answers arrive with a rating at or under the form's 'tell at a rating of', tells the " +
|
|
11
|
+
"answers' owner, or the admins when they have none. Answers kept as spam tell nobody.",
|
|
12
|
+
trigger: { type: "record", object: "forms.submission", on: ["created"] },
|
|
13
|
+
conditions: {
|
|
14
|
+
and: [
|
|
15
|
+
{ field: "rating", op: "is_not_null" },
|
|
16
|
+
{ field: "status", op: "neq", value: "spam" },
|
|
17
|
+
],
|
|
18
|
+
},
|
|
19
|
+
steps: [
|
|
20
|
+
{
|
|
21
|
+
name: "tell",
|
|
22
|
+
label: "Tell the owner, or the admins",
|
|
23
|
+
reads: [FORM],
|
|
24
|
+
notifies: [
|
|
25
|
+
{ to: { field: "owner" }, message: "A low rating: {record}" },
|
|
26
|
+
{ to: "admins", message: "A low rating: {record}" },
|
|
27
|
+
],
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
async run(event, step, ctx) {
|
|
31
|
+
const submission = event.record;
|
|
32
|
+
if (!submission)
|
|
33
|
+
return { skipped: "no submission" };
|
|
34
|
+
const rating = num(submission.data?.rating);
|
|
35
|
+
const formId = submission.data?.form;
|
|
36
|
+
if (rating === null || typeof formId !== "string")
|
|
37
|
+
return { skipped: "no rating" };
|
|
38
|
+
return step.do("tell", async () => {
|
|
39
|
+
const form = await ctx.ops.call("record.get", {
|
|
40
|
+
object: FORM,
|
|
41
|
+
id: formId,
|
|
42
|
+
});
|
|
43
|
+
const level = num(form.values.lowRating);
|
|
44
|
+
if (level === null || rating > level)
|
|
45
|
+
return { told: false };
|
|
46
|
+
const owner = submission.data?.owner;
|
|
47
|
+
await ctx.notify(typeof owner === "string" && owner ? { field: "owner" } : "admins", {
|
|
48
|
+
title: "A low rating: {record}",
|
|
49
|
+
body: `Rated ${rating}. Open the answers to see why.`,
|
|
50
|
+
link: "record",
|
|
51
|
+
});
|
|
52
|
+
return { told: true };
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
/**
|
|
57
|
+
* The email-send connection the host sends receipts from: a person binds it to the automation,
|
|
58
|
+
* which is their consent to the pack's mail going out from that address (ADR 0590).
|
|
59
|
+
*/
|
|
60
|
+
const MAIL = {
|
|
61
|
+
id: "mail",
|
|
62
|
+
gatekeeper: "email-send",
|
|
63
|
+
purpose: "Email a receipt to whoever sent answers",
|
|
64
|
+
};
|
|
65
|
+
export const receipt = defineAutomation({
|
|
66
|
+
label: "Send a receipt",
|
|
67
|
+
description: "When answers arrive to a form whose receipt is on, emails whoever sent them a short " +
|
|
68
|
+
"receipt naming the form. The host decides who it goes to and refuses it for answers kept " +
|
|
69
|
+
"as spam, entered by staff or sent anonymously, and after three to one address in a day. " +
|
|
70
|
+
"Routine email: Receipts.",
|
|
71
|
+
trigger: { type: "record", object: "forms.submission", on: ["created"] },
|
|
72
|
+
conditions: {
|
|
73
|
+
and: [
|
|
74
|
+
{ field: "status", op: "eq", value: "new" },
|
|
75
|
+
{ field: "person", op: "is_not_null" },
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
connections: [MAIL],
|
|
79
|
+
steps: [
|
|
80
|
+
{ name: "check", label: "Check the form's receipt is on", reads: [FORM] },
|
|
81
|
+
{ name: "send", label: "Email the receipt" },
|
|
82
|
+
],
|
|
83
|
+
async run(event, step, ctx) {
|
|
84
|
+
const submission = event.record;
|
|
85
|
+
const formId = submission?.data?.form;
|
|
86
|
+
if (!submission || typeof formId !== "string")
|
|
87
|
+
return { skipped: "no submission" };
|
|
88
|
+
const on = await step.do("check", async () => {
|
|
89
|
+
const form = await ctx.ops.call("record.get", {
|
|
90
|
+
object: FORM,
|
|
91
|
+
id: formId,
|
|
92
|
+
});
|
|
93
|
+
return form.values.receipt !== false && form.values.anonymous !== true;
|
|
94
|
+
});
|
|
95
|
+
if (!on)
|
|
96
|
+
return { sent: 0 };
|
|
97
|
+
return step.do("send", () => ctx.routineMail.send({ kind: "receipt", record: submission.id, message: "received" }));
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
export const formsAutomations = { lowRating, receipt };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { type DraftValue, type Filler, type FormsHooks, type OpenedForm, type QuestionItem } from "@composable-os/pack-forms/client";
|
|
2
|
+
import { type ComponentType } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* The app's Turnstile widget (for example `@marsidev/react-turnstile` with the site key of the
|
|
5
|
+
* app's hostname): it calls `onToken` with a token once Cloudflare has decided, and null when the
|
|
6
|
+
* token expires.
|
|
7
|
+
*/
|
|
8
|
+
export type TurnstileWidget = ComponentType<{
|
|
9
|
+
onToken: (token: string | null) => void;
|
|
10
|
+
}>;
|
|
11
|
+
/** One question (or a heading or paragraph), drawn by its kind. */
|
|
12
|
+
export declare function Question({ item, value, error, onChange, }: {
|
|
13
|
+
item: QuestionItem;
|
|
14
|
+
value: DraftValue | undefined;
|
|
15
|
+
error?: string;
|
|
16
|
+
onChange: (value: DraftValue) => void;
|
|
17
|
+
}): any;
|
|
18
|
+
/** A form being filled: its questions, what to fix, the spam checks and the send. */
|
|
19
|
+
export declare function FormFields({ form, filler, Turnstile, sendLabel, }: {
|
|
20
|
+
form: OpenedForm;
|
|
21
|
+
filler: Filler;
|
|
22
|
+
Turnstile?: TurnstileWidget | undefined;
|
|
23
|
+
sendLabel?: string;
|
|
24
|
+
}): any;
|
|
25
|
+
/**
|
|
26
|
+
* A form by its address (`slug`), or by a personal link's token (`link`, read once from the page's
|
|
27
|
+
* address with `takeLinkToken` and kept in memory). `via` says where it is: the business's website
|
|
28
|
+
* (`site`, the default) or its app. `Turnstile`: the app's widget, for a visitor on a public form.
|
|
29
|
+
*/
|
|
30
|
+
export declare function FormBlock({ forms, slug, link, via, Turnstile, onSent, }: {
|
|
31
|
+
forms: Pick<FormsHooks, "useForm" | "useAnswer">;
|
|
32
|
+
slug?: string;
|
|
33
|
+
link?: string;
|
|
34
|
+
via?: "site" | "app";
|
|
35
|
+
Turnstile?: TurnstileWidget;
|
|
36
|
+
onSent?: () => void;
|
|
37
|
+
}): any;
|
|
38
|
+
/**
|
|
39
|
+
* A signed-in member's own personal link (or a child's they are a guardian of), opened from Needs
|
|
40
|
+
* your attention by the link's id. Answered once; the item goes from Needs your attention once sent.
|
|
41
|
+
*/
|
|
42
|
+
export declare function LinkedFormBlock({ forms, linkId, onSent, }: {
|
|
43
|
+
forms: Pick<FormsHooks, "useLinkedForm" | "useAnswerLinked">;
|
|
44
|
+
linkId: string;
|
|
45
|
+
onSent?: () => void;
|
|
46
|
+
}): any;
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
// A form to fill in, for the business's own website (stage 29 step 4): any published form, with
|
|
3
|
+
// every question kind the question schema has, then its thank-you. `FormBlock` opens a form by its
|
|
4
|
+
// address (or a personal link's token); `LinkedFormBlock` opens a signed-in member's own personal
|
|
5
|
+
// link from Needs your attention (`me.attention`, kind form_to_fill, by the link's id).
|
|
6
|
+
//
|
|
7
|
+
// It holds no rules. Whether the form takes answers from this visitor, who may answer, once only,
|
|
8
|
+
// the spam checks and the caps are the host's; required questions, lengths and links are checked
|
|
9
|
+
// before sending with the host's own check (the headless entry), and the host's answer is the one
|
|
10
|
+
// shown: a refusal stays on the screen with the answers as typed. The spam checks travel with the
|
|
11
|
+
// send: the honeypot is a field a person never sees, the time the form was shown is the host's
|
|
12
|
+
// token from opening it, and a visitor's Turnstile token comes from the widget the app passes as
|
|
13
|
+
// `Turnstile` (the block loads no script of its own). A personal link skips Turnstile at the host,
|
|
14
|
+
// so no widget is drawn for one. What a person types stays in this component's state and nowhere
|
|
15
|
+
// else. A page showing `FormBlock` for a personal link's token keeps the link page's three rules
|
|
16
|
+
// itself (ADR 0728): `Referrer-Policy: no-referrer`, no third-party script (one could read the
|
|
17
|
+
// address before the token is out), and the token taken out at once with `takeLinkToken`.
|
|
18
|
+
import { HONEYPOT_FIELD, isAsked, ratingLabel, ratingValues, refusalOf, } from "@composable-os/pack-forms/client";
|
|
19
|
+
import { useEffect, useRef, useState, } from "react";
|
|
20
|
+
import { Button } from "../ui-button/button.js";
|
|
21
|
+
import { Input } from "../ui-input/input.js";
|
|
22
|
+
import { Label } from "../ui-label/label.js";
|
|
23
|
+
const fieldClass = "w-full rounded-md border border-input bg-transparent px-3 py-2 text-base shadow-xs outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 aria-invalid:border-destructive md:text-sm dark:bg-input/30";
|
|
24
|
+
/** Rich text's inline runs, drawn as text: bold, italic, code and links. */
|
|
25
|
+
function Inlines({ content }) {
|
|
26
|
+
return (_jsx(_Fragment, { children: content.map((run, i) => {
|
|
27
|
+
const r = run;
|
|
28
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: runs have no ids; the list never reorders.
|
|
29
|
+
if (r.type === "break")
|
|
30
|
+
return _jsx("br", {}, i);
|
|
31
|
+
if (r.type !== "text" || !r.text)
|
|
32
|
+
return null;
|
|
33
|
+
let node = r.text;
|
|
34
|
+
if (r.code)
|
|
35
|
+
node = _jsx("code", { children: node });
|
|
36
|
+
if (r.italic)
|
|
37
|
+
node = _jsx("em", { children: node });
|
|
38
|
+
if (r.bold)
|
|
39
|
+
node = _jsx("strong", { children: node });
|
|
40
|
+
if (r.href && /^(?:https?:|mailto:)/i.test(r.href)) {
|
|
41
|
+
node = (_jsx("a", { href: r.href, rel: "noopener noreferrer nofollow", className: "underline", children: node }));
|
|
42
|
+
}
|
|
43
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: as above.
|
|
44
|
+
return _jsx("span", { children: node }, i);
|
|
45
|
+
}) }));
|
|
46
|
+
}
|
|
47
|
+
const fieldId = (key) => `form-${key}`;
|
|
48
|
+
const helpId = (key) => `form-${key}-help`;
|
|
49
|
+
const errorId = (key) => `form-${key}-error`;
|
|
50
|
+
/** The help and the error under a question, tied to its field by `aria-describedby`. */
|
|
51
|
+
function describedBy(item, error) {
|
|
52
|
+
const ids = [item.help ? helpId(item.key) : null, error ? errorId(item.key) : null].filter(Boolean);
|
|
53
|
+
return ids.length ? ids.join(" ") : undefined;
|
|
54
|
+
}
|
|
55
|
+
function Notes({ item, error }) {
|
|
56
|
+
return (_jsxs(_Fragment, { children: [item.help ? (_jsx("p", { id: helpId(item.key), className: "text-sm text-muted-foreground", children: item.help })) : null, error ? (_jsx("p", { id: errorId(item.key), className: "text-sm font-medium text-destructive", children: error })) : null] }));
|
|
57
|
+
}
|
|
58
|
+
function Required({ on }) {
|
|
59
|
+
return on ? _jsx("span", { className: "font-normal text-muted-foreground", children: " (required)" }) : null;
|
|
60
|
+
}
|
|
61
|
+
/** One question (or a heading or paragraph), drawn by its kind. */
|
|
62
|
+
export function Question({ item, value, error, onChange, }) {
|
|
63
|
+
if (item.kind === "heading")
|
|
64
|
+
return _jsx("h2", { className: "text-lg font-semibold", children: item.text });
|
|
65
|
+
if (item.kind === "paragraph") {
|
|
66
|
+
return (_jsx("p", { className: "text-sm", children: _jsx(Inlines, { content: item.content }) }));
|
|
67
|
+
}
|
|
68
|
+
const id = fieldId(item.key);
|
|
69
|
+
const invalid = error ? true : undefined;
|
|
70
|
+
const describes = describedBy(item, error);
|
|
71
|
+
if (item.kind === "consent") {
|
|
72
|
+
return (_jsxs("div", { className: "grid gap-1", children: [_jsxs("div", { className: "flex items-start gap-2", children: [_jsx("input", { type: "checkbox", id: id, className: "mt-1 size-4", checked: value === true, "aria-invalid": invalid, "aria-describedby": describes, "aria-required": item.required || undefined, onChange: (e) => onChange(e.target.checked) }), _jsxs("label", { htmlFor: id, className: "whitespace-pre-line text-sm", children: [item.words, _jsx(Required, { on: item.required })] })] }), _jsx(Notes, { item: item, error: error })] }));
|
|
73
|
+
}
|
|
74
|
+
if (item.kind === "choice" || item.kind === "rating" || item.kind === "yesNo") {
|
|
75
|
+
const options = item.kind === "choice"
|
|
76
|
+
? item.options.map((o) => ({ value: o, label: o }))
|
|
77
|
+
: item.kind === "rating"
|
|
78
|
+
? ratingValues(item).map((n) => ({ value: n, label: ratingLabel(item, n) }))
|
|
79
|
+
: [
|
|
80
|
+
{ value: true, label: "Yes" },
|
|
81
|
+
{ value: false, label: "No" },
|
|
82
|
+
];
|
|
83
|
+
const several = item.kind === "choice" && item.multiple;
|
|
84
|
+
const chosen = (v) => several ? Array.isArray(value) && value.includes(v) : value === v;
|
|
85
|
+
const pick = (v, on) => {
|
|
86
|
+
if (!several)
|
|
87
|
+
return onChange(v);
|
|
88
|
+
const now = Array.isArray(value) ? value : [];
|
|
89
|
+
onChange(on ? [...now, v] : now.filter((x) => x !== v));
|
|
90
|
+
};
|
|
91
|
+
return (_jsxs("fieldset", { className: "grid gap-2", "aria-describedby": describes, children: [_jsxs("legend", { className: "mb-1 text-sm font-medium", children: [item.label, _jsx(Required, { on: item.required })] }), _jsx(Notes, { item: item, error: error }), _jsx("div", { className: item.kind === "choice" ? "grid gap-1" : "flex flex-wrap gap-2", children: options.map((option, i) => (_jsxs("label", { htmlFor: `${id}-${i}`, className: item.kind === "choice"
|
|
92
|
+
? "flex min-h-11 items-center gap-2 text-sm"
|
|
93
|
+
: "flex min-h-11 items-center gap-2 rounded-md border border-input px-3 text-sm has-[:checked]:border-primary has-[:checked]:bg-accent", children: [_jsx("input", { type: several ? "checkbox" : "radio", id: `${id}-${i}`, name: id, className: "size-4", checked: chosen(option.value), "aria-invalid": invalid, onChange: (e) => pick(option.value, e.target.checked) }), option.label] }, String(option.value)))) })] }));
|
|
94
|
+
}
|
|
95
|
+
const text = typeof value === "string" ? value : "";
|
|
96
|
+
const common = {
|
|
97
|
+
id,
|
|
98
|
+
value: text,
|
|
99
|
+
"aria-invalid": invalid,
|
|
100
|
+
"aria-describedby": describes,
|
|
101
|
+
"aria-required": item.required || undefined,
|
|
102
|
+
};
|
|
103
|
+
const auto = item.map?.to === "person"
|
|
104
|
+
? { name: "name", email: "email", phone: "tel" }[item.map.field]
|
|
105
|
+
: undefined;
|
|
106
|
+
return (_jsxs("div", { className: "grid gap-2", children: [_jsx(Label, { htmlFor: id, children: _jsxs("span", { children: [item.label, _jsx(Required, { on: item.required })] }) }), _jsx(Notes, { item: item, error: error }), item.kind === "text" && item.long ? (_jsx("textarea", { ...common, rows: 6, maxLength: item.max ?? 5000, className: fieldClass, onChange: (e) => onChange(e.target.value) })) : (_jsx(Input, { ...common, type: item.kind === "email"
|
|
107
|
+
? "email"
|
|
108
|
+
: item.kind === "phone"
|
|
109
|
+
? "tel"
|
|
110
|
+
: item.kind === "number"
|
|
111
|
+
? "number"
|
|
112
|
+
: item.kind === "date"
|
|
113
|
+
? "date"
|
|
114
|
+
: "text", inputMode: item.kind === "email"
|
|
115
|
+
? "email"
|
|
116
|
+
: item.kind === "phone"
|
|
117
|
+
? "tel"
|
|
118
|
+
: item.kind === "number"
|
|
119
|
+
? "decimal"
|
|
120
|
+
: undefined, autoComplete: auto ?? (item.kind === "email" ? "email" : item.kind === "phone" ? "tel" : undefined), maxLength: item.kind === "text" ? (item.max ?? 200) : undefined, step: item.kind === "number" ? (item.integer ? 1 : "any") : undefined, min: item.kind === "number" ? item.min : undefined, max: item.kind === "number" ? item.max : undefined, onChange: (e) => onChange(e.target.value) }))] }));
|
|
121
|
+
}
|
|
122
|
+
/** What the person reads a question by, in the list of what to fix. */
|
|
123
|
+
function nameOf(item) {
|
|
124
|
+
if (!isAsked(item))
|
|
125
|
+
return "";
|
|
126
|
+
return (item.kind === "consent" ? item.words : item.label).slice(0, 120);
|
|
127
|
+
}
|
|
128
|
+
/** A form being filled: its questions, what to fix, the spam checks and the send. */
|
|
129
|
+
export function FormFields({ form, filler, Turnstile, sendLabel = "Send", }) {
|
|
130
|
+
const [token, setToken] = useState(null);
|
|
131
|
+
const honeypot = useRef(null);
|
|
132
|
+
const summary = useRef(null);
|
|
133
|
+
const [tried, setTried] = useState(0);
|
|
134
|
+
const faults = filler.items.filter((item) => filler.errors[item.key]);
|
|
135
|
+
const needsCheck = form.turnstile;
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
// After a send that did not go, the list of what to fix takes the focus.
|
|
138
|
+
if (tried && (faults.length || filler.problem))
|
|
139
|
+
summary.current?.focus();
|
|
140
|
+
}, [tried, faults.length, filler.problem]);
|
|
141
|
+
async function onSubmit(e) {
|
|
142
|
+
e.preventDefault();
|
|
143
|
+
await filler.send({
|
|
144
|
+
...(needsCheck ? { turnstileToken: token } : {}),
|
|
145
|
+
honeypot: honeypot.current?.value ?? "",
|
|
146
|
+
});
|
|
147
|
+
setTried((n) => n + 1);
|
|
148
|
+
}
|
|
149
|
+
return (_jsxs("form", { onSubmit: onSubmit, noValidate: true, className: "grid gap-6", children: [faults.length || filler.problem ? (_jsxs("div", { ref: summary, tabIndex: -1, role: "alert", className: "rounded-md border-2 border-destructive p-4 text-sm outline-none", children: [_jsx("p", { className: "font-semibold", children: faults.length ? "Some answers need another look" : "Your answers were not sent" }), filler.problem && filler.problem.kind !== "answers" ? (_jsx("p", { children: filler.problem.message })) : null, faults.length ? (_jsx("ul", { className: "mt-2 list-disc pl-5", children: faults.map((item) => (_jsx("li", { children: _jsxs("a", { className: "underline", href: `#${fieldId(item.key)}${["choice", "rating", "yesNo"].includes(item.kind) ? "-0" : ""}`, children: [nameOf(item), ": ", filler.errors[item.key]] }) }, item.key))) })) : null] })) : null, _jsxs("div", { "aria-hidden": "true", className: "absolute -left-[10000px] h-px w-px overflow-hidden", children: [_jsx("label", { htmlFor: `form-${HONEYPOT_FIELD}`, children: "Leave this empty" }), _jsx("input", { ref: honeypot, type: "text", id: `form-${HONEYPOT_FIELD}`, name: HONEYPOT_FIELD, tabIndex: -1, autoComplete: "off", defaultValue: "" })] }), filler.items.map((item) => (_jsx(Question, { item: item, value: filler.draft[item.key], ...(filler.errors[item.key] ? { error: filler.errors[item.key] } : {}), onChange: (v) => filler.set(item.key, v) }, item.key))), needsCheck ? (Turnstile ? (_jsx(Turnstile, { onToken: setToken })) : (_jsx("p", { className: "text-sm text-muted-foreground", role: "note", children: "This form cannot be sent from this page just now. Please try again later." }))) : null, _jsx("div", { children: _jsx(Button, { type: "submit", disabled: filler.status === "sending" || (needsCheck && !Turnstile), children: filler.status === "sending" ? "Sending…" : sendLabel }) })] }));
|
|
150
|
+
}
|
|
151
|
+
function Shell({ title, children }) {
|
|
152
|
+
return (_jsxs("section", { className: "mx-auto grid w-full max-w-xl gap-6", children: [title ? _jsx("h1", { className: "text-2xl font-semibold", children: title }) : null, children] }));
|
|
153
|
+
}
|
|
154
|
+
function Opened({ form, filler, Turnstile, onSent, }) {
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
if (filler.status === "sent")
|
|
157
|
+
onSent?.();
|
|
158
|
+
}, [filler.status, onSent]);
|
|
159
|
+
return (_jsx(Shell, { title: form.title, children: filler.status === "sent" ? (_jsx("p", { role: "status", className: "whitespace-pre-line", children: filler.thanks })) : (_jsx(FormFields, { form: form, filler: filler, Turnstile: Turnstile })) }));
|
|
160
|
+
}
|
|
161
|
+
/** Loading, or why the form cannot be opened, in the host's words. */
|
|
162
|
+
function NotOpen({ error, link }) {
|
|
163
|
+
if (!error) {
|
|
164
|
+
return (_jsx("p", { className: "text-sm text-muted-foreground", role: "status", children: "Loading\u2026" }));
|
|
165
|
+
}
|
|
166
|
+
const refusal = refusalOf(error);
|
|
167
|
+
const words = refusal.kind === "offline" || refusal.kind === "other" || refusal.kind === "later"
|
|
168
|
+
? "This form could not be opened just now. Try again shortly."
|
|
169
|
+
: link
|
|
170
|
+
? "This link cannot be used: it may have been used already or have expired."
|
|
171
|
+
: "This form is not taking answers just now.";
|
|
172
|
+
return (_jsx("p", { className: "text-sm", role: "alert", children: words }));
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* A form by its address (`slug`), or by a personal link's token (`link`, read once from the page's
|
|
176
|
+
* address with `takeLinkToken` and kept in memory). `via` says where it is: the business's website
|
|
177
|
+
* (`site`, the default) or its app. `Turnstile`: the app's widget, for a visitor on a public form.
|
|
178
|
+
*/
|
|
179
|
+
export function FormBlock({ forms, slug, link, via = "site", Turnstile, onSent, }) {
|
|
180
|
+
const opened = forms.useForm(link ? { link } : slug ? { slug } : null);
|
|
181
|
+
const filler = forms.useAnswer(opened.data, { via, ...(link ? { link } : {}) });
|
|
182
|
+
if (!opened.data)
|
|
183
|
+
return _jsx(NotOpen, { error: opened.error, link: Boolean(link) });
|
|
184
|
+
return (_jsx(Opened, { form: opened.data, filler: filler, Turnstile: link ? undefined : Turnstile, onSent: onSent }));
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* A signed-in member's own personal link (or a child's they are a guardian of), opened from Needs
|
|
188
|
+
* your attention by the link's id. Answered once; the item goes from Needs your attention once sent.
|
|
189
|
+
*/
|
|
190
|
+
export function LinkedFormBlock({ forms, linkId, onSent, }) {
|
|
191
|
+
const opened = forms.useLinkedForm(linkId);
|
|
192
|
+
const filler = forms.useAnswerLinked(opened.data, linkId);
|
|
193
|
+
if (!opened.data)
|
|
194
|
+
return _jsx(NotOpen, { error: opened.error, link: true });
|
|
195
|
+
return _jsx(Opened, { form: opened.data, filler: filler, onSent: onSent });
|
|
196
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type DraftValue, type Filler, type FormsHooks, type OpenedForm, type QuestionItem } from "@composable-os/pack-forms/client";
|
|
2
|
+
import { type ComponentType } from "react";
|
|
3
|
+
/** The app's Turnstile widget (in a web view): `onToken` with a token, null when it expires. */
|
|
4
|
+
export type NativeTurnstile = ComponentType<{
|
|
5
|
+
onToken: (token: string | null) => void;
|
|
6
|
+
}>;
|
|
7
|
+
/** One question (or a heading or paragraph), drawn by its kind. */
|
|
8
|
+
export declare function NativeQuestion({ item, value, error, onChange, }: {
|
|
9
|
+
item: QuestionItem;
|
|
10
|
+
value: DraftValue | undefined;
|
|
11
|
+
error?: string | undefined;
|
|
12
|
+
onChange: (value: DraftValue) => void;
|
|
13
|
+
}): any;
|
|
14
|
+
/** A form being filled: its questions, what to fix, the spam check where asked, and the send. */
|
|
15
|
+
export declare function NativeFormFields({ form, filler, Turnstile, }: {
|
|
16
|
+
form: OpenedForm;
|
|
17
|
+
filler: Filler;
|
|
18
|
+
Turnstile?: NativeTurnstile | undefined;
|
|
19
|
+
}): any;
|
|
20
|
+
/** A form by its address, in the app. `Turnstile`: the app's widget, for a visitor's public form. */
|
|
21
|
+
export declare function FormScreen({ forms, slug, Turnstile, onDone, }: {
|
|
22
|
+
forms: Pick<FormsHooks, "useForm" | "useAnswer">;
|
|
23
|
+
slug: string;
|
|
24
|
+
Turnstile?: NativeTurnstile;
|
|
25
|
+
onDone?: () => void;
|
|
26
|
+
}): any;
|
|
27
|
+
/**
|
|
28
|
+
* A signed-in member's own personal link (or a child's they are a guardian of), opened from Needs
|
|
29
|
+
* your attention by the link's id. Answered once; the item goes once sent.
|
|
30
|
+
*/
|
|
31
|
+
export declare function LinkedFormScreen({ forms, linkId, onDone, }: {
|
|
32
|
+
forms: Pick<FormsHooks, "useLinkedForm" | "useAnswerLinked">;
|
|
33
|
+
linkId: string;
|
|
34
|
+
onDone?: () => void;
|
|
35
|
+
}): any;
|