@atelic-action/ui 8.4.0 → 8.5.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 +50 -5
- package/dist/lib/contactLinks.d.ts +31 -0
- package/dist/lib/contactLinks.js +55 -0
- package/dist/scripts/check-contacts.d.ts +17 -0
- package/dist/scripts/check-contacts.js +162 -0
- package/package.json +10 -13
- package/src/chrome/Footer.tsx +4 -3
- package/src/chrome/SiteHeader.tsx +7 -6
- package/src/chrome/SiteMenu.tsx +11 -9
- package/src/chrome/StickyCTABar.tsx +3 -2
- package/src/components/GateLogin.tsx +2 -1
- package/src/components/MultiSelect.tsx +225 -0
- package/src/components/index.ts +1 -0
- package/src/lib/contactForm.ts +184 -0
- package/src/lib/contactLinks.ts +57 -0
- package/src/lib/googlePlaces.ts +392 -0
- package/src/lib/latestOnly.ts +47 -0
- package/src/primitives/AddressSearch.tsx +341 -0
- package/src/primitives/EmailLink.tsx +55 -0
- package/src/primitives/ExpandableText.tsx +137 -0
- package/src/primitives/GoogleMapsAttribution.tsx +14 -0
- package/src/primitives/PhoneLink.tsx +61 -0
- package/src/primitives/index.ts +18 -0
- package/src/scripts/check-base-path.ts +15 -0
- package/src/scripts/check-contacts.ts +177 -0
- package/src/sections/ContactForm.tsx +683 -87
- package/src/sections/ContactInfoCard.tsx +20 -5
- package/src/sections/InfoRows.tsx +103 -0
- package/src/sections/LocationSection.tsx +2 -1
- package/src/sections/Locations.tsx +3 -2
- package/src/sections/PageHero.tsx +22 -5
- package/src/sections/index.ts +23 -1
- package/src/styles/components.css +123 -0
- package/src/styles/defaults.css +31 -1
- package/src/styles/fields.css +33 -0
- package/src/styles/primitives.css +165 -0
- package/src/styles/sections.css +306 -19
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Phone as PhoneIcon } from "lucide-react";
|
|
2
2
|
import { useEffect, useRef, useState } from "react";
|
|
3
3
|
import { newTabProps } from "../lib/newTabProps";
|
|
4
|
+
import { PhoneLink } from "../primitives/PhoneLink";
|
|
4
5
|
import type { CallToAction, Phone } from "../types";
|
|
5
6
|
|
|
6
7
|
export interface StickyCTABarProps {
|
|
@@ -157,9 +158,9 @@ export function StickyCTABar({
|
|
|
157
158
|
{primaryCTA.label}
|
|
158
159
|
</a>
|
|
159
160
|
{phone && (
|
|
160
|
-
<
|
|
161
|
+
<PhoneLink className="btn btn-ghost btn-call" phone={phone} aria-label={callLabel}>
|
|
161
162
|
<PhoneIcon size={18} aria-hidden="true" />
|
|
162
|
-
</
|
|
163
|
+
</PhoneLink>
|
|
163
164
|
)}
|
|
164
165
|
</div>
|
|
165
166
|
);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type FormEvent, useEffect, useId, useRef, useState } from "react";
|
|
2
|
+
import { EmailLink } from "../primitives/EmailLink";
|
|
2
3
|
|
|
3
4
|
type Status = "idle" | "sending" | "sent" | "failed";
|
|
4
5
|
|
|
@@ -179,7 +180,7 @@ export function GateLogin({
|
|
|
179
180
|
<p className="gate-fine">
|
|
180
181
|
{builtBy && `Built by ${builtBy}`}
|
|
181
182
|
{builtBy && contactEmail && " · "}
|
|
182
|
-
{contactEmail && <
|
|
183
|
+
{contactEmail && <EmailLink email={contactEmail}>{contactEmail}</EmailLink>}
|
|
183
184
|
</p>
|
|
184
185
|
)}
|
|
185
186
|
</>
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import * as Checkbox from "@radix-ui/react-checkbox";
|
|
2
|
+
import * as Popover from "@radix-ui/react-popover";
|
|
3
|
+
import { Check, ChevronDown } from "lucide-react";
|
|
4
|
+
import { type KeyboardEvent, type ReactNode, useEffect, useId, useRef, useState } from "react";
|
|
5
|
+
|
|
6
|
+
/** One choice: its submitted value, and the words shown when they differ. */
|
|
7
|
+
export interface MultiSelectOption {
|
|
8
|
+
value: string;
|
|
9
|
+
label?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface MultiSelectProps {
|
|
13
|
+
/** The field's label, and the first half of the button's accessible name. */
|
|
14
|
+
label: ReactNode;
|
|
15
|
+
/** A quiet note beside the label, such as "(check all that apply)". */
|
|
16
|
+
hint?: ReactNode;
|
|
17
|
+
/** The choices, as plain strings or as values with their own words. */
|
|
18
|
+
options: (string | MultiSelectOption)[];
|
|
19
|
+
/**
|
|
20
|
+
* The name each chosen value is submitted under in a form, once per value,
|
|
21
|
+
* as checkboxes are, so `FormData.getAll(name)` reads them in the options'
|
|
22
|
+
* order. Nothing is submitted without it.
|
|
23
|
+
*/
|
|
24
|
+
name?: string;
|
|
25
|
+
/** Chosen on first render, and again when the form resets. */
|
|
26
|
+
defaultValue?: string[];
|
|
27
|
+
/** The button's words while nothing is chosen. */
|
|
28
|
+
placeholder?: string;
|
|
29
|
+
/** How many chosen labels the button lists before it counts them instead. Defaults to 2. */
|
|
30
|
+
listed?: number;
|
|
31
|
+
/** The button's words for more chosen than `listed`. */
|
|
32
|
+
countLabel?: (count: number) => string;
|
|
33
|
+
/** Called with the chosen values, in the options' order, on every change. */
|
|
34
|
+
onChange?: (values: string[]) => void;
|
|
35
|
+
/** The button's id; minted when left out. */
|
|
36
|
+
id?: string;
|
|
37
|
+
className?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const optionOf = (option: string | MultiSelectOption) =>
|
|
41
|
+
typeof option === "string"
|
|
42
|
+
? { value: option, label: option }
|
|
43
|
+
: { value: option.value, label: option.label ?? option.value };
|
|
44
|
+
|
|
45
|
+
const defaultCount = (count: number) => `${count} chosen`;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A closed control that names what is chosen and opens to a list of
|
|
49
|
+
* checkboxes, any number of which may be ticked: a form question with more
|
|
50
|
+
* than one answer ("Which services?").
|
|
51
|
+
*
|
|
52
|
+
* Built on Radix: the button is a Popover trigger (`aria-expanded`, and a
|
|
53
|
+
* popup it controls), and each choice is a Radix Checkbox (`role="checkbox"`
|
|
54
|
+
* with `aria-checked`). Checkboxes and not a multi select listbox, because a
|
|
55
|
+
* screen reader announces a checkbox's state the same way everywhere, where
|
|
56
|
+
* a listbox's `aria-multiselectable` selected state is read unevenly; and
|
|
57
|
+
* not cmdk's list, whose options mark the highlighted one `aria-selected`,
|
|
58
|
+
* which in a multi select says a choice is chosen when it is only under the
|
|
59
|
+
* cursor. Radix closes the list on a click outside (leaving focus where the
|
|
60
|
+
* press put it) and on Escape (focus back on the button); Enter in the list
|
|
61
|
+
* closes it too, back on the button, and never sends the form. There is no
|
|
62
|
+
* Done button. Opening lands on the first choice, Space ticks one, the
|
|
63
|
+
* arrows, Home, and End move between them, and Tab keeps to the open list,
|
|
64
|
+
* the last choice wrapping to the first, as the popover's focus scope does.
|
|
65
|
+
*
|
|
66
|
+
* The list renders in place, not in a portal, so it stays under the page's
|
|
67
|
+
* `.atelic-ui` root and its rules reach it. The chosen values are submitted
|
|
68
|
+
* as hidden inputs beside the button, which stay in the form while the list
|
|
69
|
+
* is closed, and a form's reset puts back `defaultValue`. The button lists
|
|
70
|
+
* the chosen labels, up to `listed`, then counts them.
|
|
71
|
+
*/
|
|
72
|
+
export function MultiSelect({
|
|
73
|
+
label,
|
|
74
|
+
hint,
|
|
75
|
+
options,
|
|
76
|
+
name,
|
|
77
|
+
defaultValue = [],
|
|
78
|
+
placeholder = "Choose one or more",
|
|
79
|
+
listed = 2,
|
|
80
|
+
countLabel = defaultCount,
|
|
81
|
+
onChange,
|
|
82
|
+
id,
|
|
83
|
+
className,
|
|
84
|
+
}: MultiSelectProps) {
|
|
85
|
+
const minted = useId();
|
|
86
|
+
const buttonId = id ?? `${minted}-button`;
|
|
87
|
+
const labelTextId = `${minted}-label`;
|
|
88
|
+
const hintId = `${minted}-hint`;
|
|
89
|
+
// The label's words and its hint are named apart, so the name joins them
|
|
90
|
+
// with a space even where a page's HTML has dropped the one between them.
|
|
91
|
+
const labelledBy = hint ? `${labelTextId} ${hintId}` : labelTextId;
|
|
92
|
+
const valueId = `${minted}-value`;
|
|
93
|
+
const buttonRef = useRef<HTMLButtonElement>(null);
|
|
94
|
+
const listRef = useRef<HTMLDivElement>(null);
|
|
95
|
+
const [open, setOpen] = useState(false);
|
|
96
|
+
const choices = options.map(optionOf);
|
|
97
|
+
// Chosen values are always held in the options' order, whatever order they came in.
|
|
98
|
+
const inOrder = (values: string[]) =>
|
|
99
|
+
choices.flatMap((choice) => (values.includes(choice.value) ? [choice.value] : []));
|
|
100
|
+
const [chosen, setChosen] = useState<string[]>(() => inOrder(defaultValue));
|
|
101
|
+
// Read by the handlers, so a reset and a tick in one turn never see a stale list.
|
|
102
|
+
const chosenNow = useRef(chosen);
|
|
103
|
+
chosenNow.current = chosen;
|
|
104
|
+
// The values the form resets to, as first rendered.
|
|
105
|
+
const resetTo = useRef(chosen);
|
|
106
|
+
|
|
107
|
+
useEffect(() => {
|
|
108
|
+
const form = buttonRef.current?.form;
|
|
109
|
+
if (!form) return;
|
|
110
|
+
const onReset = () => {
|
|
111
|
+
setOpen(false);
|
|
112
|
+
chosenNow.current = resetTo.current;
|
|
113
|
+
setChosen(resetTo.current);
|
|
114
|
+
};
|
|
115
|
+
form.addEventListener("reset", onReset);
|
|
116
|
+
return () => form.removeEventListener("reset", onReset);
|
|
117
|
+
}, []);
|
|
118
|
+
|
|
119
|
+
const toggle = (value: string, on: boolean) => {
|
|
120
|
+
const set = new Set(chosenNow.current);
|
|
121
|
+
if (set.has(value) === on) return;
|
|
122
|
+
if (on) set.add(value);
|
|
123
|
+
else set.delete(value);
|
|
124
|
+
const values = inOrder([...set]);
|
|
125
|
+
chosenNow.current = values;
|
|
126
|
+
setChosen(values);
|
|
127
|
+
onChange?.(values);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const onListKey = (event: KeyboardEvent<HTMLDivElement>) => {
|
|
131
|
+
const all = Array.from(
|
|
132
|
+
listRef.current?.querySelectorAll<HTMLButtonElement>('[role="checkbox"]') ?? [],
|
|
133
|
+
);
|
|
134
|
+
const at = all.indexOf(event.target as HTMLButtonElement);
|
|
135
|
+
if (at < 0) return;
|
|
136
|
+
const moveTo = (index: number) => {
|
|
137
|
+
event.preventDefault();
|
|
138
|
+
all[(index + all.length) % all.length]?.focus();
|
|
139
|
+
};
|
|
140
|
+
if (event.key === "ArrowDown") moveTo(at + 1);
|
|
141
|
+
else if (event.key === "ArrowUp") moveTo(at - 1);
|
|
142
|
+
else if (event.key === "Home") moveTo(0);
|
|
143
|
+
else if (event.key === "End") moveTo(all.length - 1);
|
|
144
|
+
else if (event.key === "Enter") {
|
|
145
|
+
// A checkbox never takes Enter; here it means done, and never sends the form.
|
|
146
|
+
event.preventDefault();
|
|
147
|
+
setOpen(false);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const chosenLabels = choices.filter((c) => chosen.includes(c.value)).map((c) => c.label);
|
|
152
|
+
const summary =
|
|
153
|
+
chosenLabels.length === 0
|
|
154
|
+
? placeholder
|
|
155
|
+
: chosenLabels.length <= listed
|
|
156
|
+
? chosenLabels.join(", ")
|
|
157
|
+
: countLabel(chosenLabels.length);
|
|
158
|
+
|
|
159
|
+
return (
|
|
160
|
+
<div className={["multi-select", className].filter(Boolean).join(" ")}>
|
|
161
|
+
<label htmlFor={buttonId} className="multi-select-label">
|
|
162
|
+
<span id={labelTextId}>{label}</span>
|
|
163
|
+
{hint && (
|
|
164
|
+
<>
|
|
165
|
+
{" "}
|
|
166
|
+
<small id={hintId}>{hint}</small>
|
|
167
|
+
</>
|
|
168
|
+
)}
|
|
169
|
+
</label>
|
|
170
|
+
<Popover.Root open={open} onOpenChange={setOpen}>
|
|
171
|
+
<Popover.Trigger asChild>
|
|
172
|
+
<button
|
|
173
|
+
ref={buttonRef}
|
|
174
|
+
id={buttonId}
|
|
175
|
+
type="button"
|
|
176
|
+
className="multi-select-button"
|
|
177
|
+
data-empty={chosenLabels.length === 0 || undefined}
|
|
178
|
+
aria-labelledby={`${labelledBy} ${valueId}`}
|
|
179
|
+
onKeyDown={(event) => {
|
|
180
|
+
if (event.key === "ArrowDown" && !open) {
|
|
181
|
+
event.preventDefault();
|
|
182
|
+
setOpen(true);
|
|
183
|
+
}
|
|
184
|
+
}}
|
|
185
|
+
>
|
|
186
|
+
<span id={valueId} className="multi-select-value">
|
|
187
|
+
{summary}
|
|
188
|
+
</span>
|
|
189
|
+
<ChevronDown className="multi-select-chevron" size={18} aria-hidden="true" />
|
|
190
|
+
</button>
|
|
191
|
+
</Popover.Trigger>
|
|
192
|
+
<Popover.Content
|
|
193
|
+
ref={listRef}
|
|
194
|
+
className="multi-select-list"
|
|
195
|
+
side="bottom"
|
|
196
|
+
align="start"
|
|
197
|
+
sideOffset={4}
|
|
198
|
+
aria-labelledby={labelledBy}
|
|
199
|
+
onKeyDown={onListKey}
|
|
200
|
+
>
|
|
201
|
+
{choices.map((choice, index) => {
|
|
202
|
+
// By position, not value: a value may hold a space or repeat.
|
|
203
|
+
const boxId = `${minted}-choice-${index}`;
|
|
204
|
+
return (
|
|
205
|
+
<div key={boxId} className="multi-select-option">
|
|
206
|
+
<Checkbox.Root
|
|
207
|
+
id={boxId}
|
|
208
|
+
className="multi-select-box"
|
|
209
|
+
checked={chosen.includes(choice.value)}
|
|
210
|
+
onCheckedChange={(state) => toggle(choice.value, state === true)}
|
|
211
|
+
>
|
|
212
|
+
<Checkbox.Indicator className="multi-select-tick">
|
|
213
|
+
<Check size={14} strokeWidth={3} aria-hidden="true" />
|
|
214
|
+
</Checkbox.Indicator>
|
|
215
|
+
</Checkbox.Root>
|
|
216
|
+
<label htmlFor={boxId}>{choice.label}</label>
|
|
217
|
+
</div>
|
|
218
|
+
);
|
|
219
|
+
})}
|
|
220
|
+
</Popover.Content>
|
|
221
|
+
</Popover.Root>
|
|
222
|
+
{name && chosen.map((value) => <input key={value} type="hidden" name={name} value={value} />)}
|
|
223
|
+
</div>
|
|
224
|
+
);
|
|
225
|
+
}
|
package/src/components/index.ts
CHANGED
|
@@ -25,6 +25,7 @@ export {
|
|
|
25
25
|
export { GateLogin, type GateLoginProps } from "./GateLogin";
|
|
26
26
|
export { Hint, type HintProps } from "./Hint";
|
|
27
27
|
export { IconList, type IconListItem, type IconListProps } from "./IconList";
|
|
28
|
+
export { MultiSelect, type MultiSelectOption, type MultiSelectProps } from "./MultiSelect";
|
|
28
29
|
export { NotFound, type NotFoundProps } from "./NotFound";
|
|
29
30
|
export { Rank, type RankProps } from "./Rank";
|
|
30
31
|
export {
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import type { AddressParts } from "../primitives/AddressSearch";
|
|
2
|
+
import { EMAIL_PATTERN, toE164 } from "./contactLinks";
|
|
3
|
+
|
|
4
|
+
/** Which of the contact form's opt in fields are drawn, and which must be filled. */
|
|
5
|
+
export interface ContactFormShape {
|
|
6
|
+
/** First and last name in place of the one name. */
|
|
7
|
+
splitName: boolean;
|
|
8
|
+
phone: "off" | "optional" | "required";
|
|
9
|
+
address: "off" | "optional" | "required";
|
|
10
|
+
/** The services multi select in place of the topic select. */
|
|
11
|
+
services: boolean;
|
|
12
|
+
consent: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The shape the form had before 8.5.0, and still has with no opt in prop. */
|
|
16
|
+
export const PLAIN_SHAPE: ContactFormShape = {
|
|
17
|
+
splitName: false,
|
|
18
|
+
phone: "off",
|
|
19
|
+
address: "off",
|
|
20
|
+
services: false,
|
|
21
|
+
consent: false,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** What a visitor wrote, read from the form, trimmed. */
|
|
25
|
+
export interface ContactFormValues {
|
|
26
|
+
/** The one name, or the first and last joined by a space. */
|
|
27
|
+
name: string;
|
|
28
|
+
firstName: string;
|
|
29
|
+
lastName: string;
|
|
30
|
+
email: string;
|
|
31
|
+
phone: string;
|
|
32
|
+
/** The topic chosen, or the chosen services joined by commas. */
|
|
33
|
+
topic: string;
|
|
34
|
+
services: string[];
|
|
35
|
+
address: AddressParts;
|
|
36
|
+
message: string;
|
|
37
|
+
consent: boolean;
|
|
38
|
+
/** The honeypot, verbatim. */
|
|
39
|
+
company: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A field the form can refuse, in the order the form draws them. */
|
|
43
|
+
export type ContactFormField =
|
|
44
|
+
| "name"
|
|
45
|
+
| "firstName"
|
|
46
|
+
| "lastName"
|
|
47
|
+
| "phone"
|
|
48
|
+
| "email"
|
|
49
|
+
| "street"
|
|
50
|
+
| "city"
|
|
51
|
+
| "state"
|
|
52
|
+
| "zip"
|
|
53
|
+
| "message";
|
|
54
|
+
|
|
55
|
+
/** Every refusable field in the page's order, so focus goes to the first refused. */
|
|
56
|
+
export const FIELD_ORDER: ContactFormField[] = [
|
|
57
|
+
"name",
|
|
58
|
+
"firstName",
|
|
59
|
+
"lastName",
|
|
60
|
+
"phone",
|
|
61
|
+
"email",
|
|
62
|
+
"street",
|
|
63
|
+
"city",
|
|
64
|
+
"state",
|
|
65
|
+
"zip",
|
|
66
|
+
"message",
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
export type ContactFormRefusals = Partial<Record<ContactFormField, true>>;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Whether a typed number is one to dial: one that reads into E.164
|
|
73
|
+
* (`toE164`), the same check the phone link makes. The form's default
|
|
74
|
+
* phone check.
|
|
75
|
+
*/
|
|
76
|
+
export function isPhoneNumber(value: string): boolean {
|
|
77
|
+
return toE164(value) !== null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** A US ZIP code, five digits or ZIP+4. */
|
|
81
|
+
export function isZIP(value: string): boolean {
|
|
82
|
+
return /^\d{5}(-?\d{4})?$/.test(value.trim());
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** An address on one line: "418 Alder Street, Fairhaven, CO 80000", skipping empty parts. */
|
|
86
|
+
export function addressLine(address: AddressParts): string {
|
|
87
|
+
const place = [address.state, address.zip].filter(Boolean).join(" ");
|
|
88
|
+
return [address.street, address.city, place].filter(Boolean).join(", ");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Reads what the visitor wrote from the form's data, for the fields the shape draws. */
|
|
92
|
+
export function readContactForm(data: FormData, shape: ContactFormShape): ContactFormValues {
|
|
93
|
+
const text = (key: string) => String(data.get(key) ?? "").trim();
|
|
94
|
+
const firstName = shape.splitName ? text("firstName") : "";
|
|
95
|
+
const lastName = shape.splitName ? text("lastName") : "";
|
|
96
|
+
const services = shape.services ? data.getAll("services").map(String) : [];
|
|
97
|
+
const street = text("street");
|
|
98
|
+
const city = text("city");
|
|
99
|
+
const zip = text("zip");
|
|
100
|
+
// An optional address nobody wrote is no address: a state select's
|
|
101
|
+
// default alone is not the visitor's answer, so it goes too.
|
|
102
|
+
const unwritten = shape.address === "optional" && !street && !city && !zip;
|
|
103
|
+
return {
|
|
104
|
+
name: shape.splitName ? [firstName, lastName].filter(Boolean).join(" ") : text("name"),
|
|
105
|
+
firstName,
|
|
106
|
+
lastName,
|
|
107
|
+
email: text("email"),
|
|
108
|
+
phone: shape.phone === "off" ? "" : text("phone"),
|
|
109
|
+
// The topic is read untrimmed, as it always was.
|
|
110
|
+
topic: shape.services ? services.join(", ") : String(data.get("topic") ?? ""),
|
|
111
|
+
services,
|
|
112
|
+
address:
|
|
113
|
+
shape.address === "off"
|
|
114
|
+
? { street: "", city: "", state: "", zip: "" }
|
|
115
|
+
: { street, city, state: unwritten ? "" : text("state"), zip },
|
|
116
|
+
message: text("message"),
|
|
117
|
+
consent: shape.consent && data.get("consent") !== null,
|
|
118
|
+
company: String(data.get("company") ?? ""),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The fields to refuse. The name (or the first and the last), a valid email,
|
|
124
|
+
* and the message are always required, as they always were. A required
|
|
125
|
+
* phone must be a number `validPhone` accepts; an optional one is refused
|
|
126
|
+
* only when something is written that it does not accept. A required
|
|
127
|
+
* address needs every part and a ZIP code; an optional one is not checked.
|
|
128
|
+
*/
|
|
129
|
+
export function checkContactForm(
|
|
130
|
+
values: ContactFormValues,
|
|
131
|
+
shape: ContactFormShape,
|
|
132
|
+
validPhone: (value: string) => boolean = isPhoneNumber,
|
|
133
|
+
): ContactFormRefusals {
|
|
134
|
+
const refused: ContactFormRefusals = {};
|
|
135
|
+
if (shape.splitName) {
|
|
136
|
+
if (!values.firstName) refused.firstName = true;
|
|
137
|
+
if (!values.lastName) refused.lastName = true;
|
|
138
|
+
} else if (!values.name) refused.name = true;
|
|
139
|
+
if (shape.phone === "required" && !validPhone(values.phone)) refused.phone = true;
|
|
140
|
+
if (shape.phone === "optional" && values.phone && !validPhone(values.phone)) refused.phone = true;
|
|
141
|
+
if (!EMAIL_PATTERN.test(values.email)) refused.email = true;
|
|
142
|
+
if (shape.address === "required") {
|
|
143
|
+
if (!values.address.street) refused.street = true;
|
|
144
|
+
if (!values.address.city) refused.city = true;
|
|
145
|
+
if (!values.address.state) refused.state = true;
|
|
146
|
+
if (!isZIP(values.address.zip)) refused.zip = true;
|
|
147
|
+
}
|
|
148
|
+
if (!values.message) refused.message = true;
|
|
149
|
+
return refused;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** The first field to fix, in the page's order, or none. */
|
|
153
|
+
export function firstRefused(refused: ContactFormRefusals): ContactFormField | undefined {
|
|
154
|
+
return FIELD_ORDER.find((field) => refused[field]);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The JSON body the form posts. It always carries `name`, `email`, `topic`,
|
|
159
|
+
* `message`, and `company` (the honeypot, verbatim), so a relay that reads
|
|
160
|
+
* only those keeps working whatever the form draws: with the split name,
|
|
161
|
+
* `name` is the first and last joined; with the services, `topic` is the
|
|
162
|
+
* chosen services joined by commas. Each opt in field adds its own keys
|
|
163
|
+
* only when it is drawn: `firstName` and `lastName`; `phone`; `address`, an
|
|
164
|
+
* object of `street`, `city`, `state`, and `zip`; `services`, an array of
|
|
165
|
+
* the chosen values; `consent`, a boolean.
|
|
166
|
+
*/
|
|
167
|
+
export function contactFormBody(
|
|
168
|
+
values: ContactFormValues,
|
|
169
|
+
shape: ContactFormShape,
|
|
170
|
+
): Record<string, unknown> {
|
|
171
|
+
const { name, email, topic, message, company } = values;
|
|
172
|
+
return {
|
|
173
|
+
name,
|
|
174
|
+
email,
|
|
175
|
+
topic,
|
|
176
|
+
message,
|
|
177
|
+
company,
|
|
178
|
+
...(shape.splitName && { firstName: values.firstName, lastName: values.lastName }),
|
|
179
|
+
...(shape.phone !== "off" && { phone: values.phone }),
|
|
180
|
+
...(shape.address !== "off" && { address: values.address }),
|
|
181
|
+
...(shape.services && { services: values.services }),
|
|
182
|
+
...(shape.consent && { consent: values.consent }),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a phone number or an email address is well formed enough to link.
|
|
3
|
+
* One check each, shared by the contact form (what it accepts) and the link
|
|
4
|
+
* primitives (what they link), so the two never disagree. Light by design:
|
|
5
|
+
* the build guard checks a site's configured numbers against Google's full
|
|
6
|
+
* numbering plan (`checkSiteContacts`, in the scripts); a page carries none
|
|
7
|
+
* of that.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** An email address: something, an @, something, a dot, something; no spaces. */
|
|
11
|
+
export const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
12
|
+
|
|
13
|
+
/** Whether an address is one to send to, ignoring the space around it. */
|
|
14
|
+
export function isEmail(value: string): boolean {
|
|
15
|
+
return EMAIL_PATTERN.test(value.trim());
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** A number in E.164: a + and 8 to 15 digits, never opening on 0. */
|
|
19
|
+
export function isE164(value: string): boolean {
|
|
20
|
+
return /^\+[1-9]\d{7,14}$/.test(value);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** An extension after the number ("x12", "ext. 12", "extension 12"), which no dialer takes in E.164. */
|
|
24
|
+
export const EXTENSION = /\s*(?:,|;|#|x|ext\.?|extension)\s*\d+\s*$/i;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A typed number in E.164, or null when it is not one to dial. A number
|
|
28
|
+
* typed with a + keeps its country code; ten digits, typed any common way,
|
|
29
|
+
* take the North American +1 (an area code never opens on 0 or 1); eleven
|
|
30
|
+
* that open on 1 take the +. An extension is dropped, so the link dials the
|
|
31
|
+
* main line while the words still show it. Anything else, letters included,
|
|
32
|
+
* is null.
|
|
33
|
+
*/
|
|
34
|
+
export function toE164(value: string): string | null {
|
|
35
|
+
const number = value.trim().replace(EXTENSION, "");
|
|
36
|
+
if (!number || /[^\d\s().+-]/.test(number) || number.lastIndexOf("+") > 0) return null;
|
|
37
|
+
const digits = number.replace(/\D/g, "");
|
|
38
|
+
if (number.startsWith("+")) {
|
|
39
|
+
const e164 = `+${digits}`;
|
|
40
|
+
return isE164(e164) ? e164 : null;
|
|
41
|
+
}
|
|
42
|
+
if (/^[2-9]\d{9}$/.test(digits)) return `+1${digits}`;
|
|
43
|
+
if (/^1[2-9]\d{9}$/.test(digits)) return `+${digits}`;
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* What a dialer can still take from a typed number `toE164` cannot read:
|
|
49
|
+
* its digits, its extension dropped and a typed leading + kept. Null when
|
|
50
|
+
* there is no digit to dial.
|
|
51
|
+
*/
|
|
52
|
+
export function dialable(value: string): string | null {
|
|
53
|
+
const number = value.trim().replace(EXTENSION, "");
|
|
54
|
+
const digits = number.replace(/\D/g, "");
|
|
55
|
+
if (!digits) return null;
|
|
56
|
+
return number.startsWith("+") ? `+${digits}` : digits;
|
|
57
|
+
}
|