@eldrajs/theme-cli 0.1.0 → 0.2.1
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/CHANGELOG.md +8 -0
- package/dist/cli.js +1 -1
- package/dist/index.js +1 -1
- package/dist/{validate-C6jB8_Oc.js → validate-DprFpjV7.js} +29 -4
- package/package.json +3 -3
- package/template/.eldra/block-types.d.ts +12 -3
- package/template/.eldra/manifest.json +317 -26
- package/template/.eldra/previews.json +35 -34
- package/template/.github/workflows/eldra-deploy.yml +2 -0
- package/template/.gitlab-ci.yml +2 -0
- package/template/.storybook/main.ts +3 -0
- package/template/.storybook/preview.ts +2 -1
- package/template/.storybook/virtual-eldra.d.ts +5 -0
- package/template/README.md +31 -14
- package/template/app/components/FormHoneypot.vue +21 -0
- package/template/app/composables/useFormSpam.ts +55 -0
- package/template/app/composables/usePageStructuredData.ts +112 -0
- package/template/app/composables/useTurnstile.ts +111 -0
- package/template/app/formSeeds.ts +93 -0
- package/template/app/pages/[...slug].vue +48 -12
- package/template/app/plugins/eldra-storefront.ts +19 -2
- package/template/app/plugins/eldra-ui-messages.ts +1 -1
- package/template/app/seededPages.ts +7 -0
- package/template/app/storefront/analytics.ts +202 -0
- package/template/app/storefront/cart.ts +7 -0
- package/template/app/storefront/demo.ts +48 -2
- package/template/app/storefront/forms.ts +133 -0
- package/template/app/storefront/gateway.ts +113 -7
- package/template/app/storefront/money.ts +18 -17
- package/template/app/storefront/structuredData.ts +161 -0
- package/template/app/storefront/types.ts +101 -2
- package/template/app/utils/seoInput.ts +172 -0
- package/template/bitbucket-pipelines.yml +3 -1
- package/template/blocks/breadcrumbs/Block.vue +1 -0
- package/template/blocks/cart/Block.vue +1 -0
- package/template/blocks/cart/parts/Summary.vue +1 -0
- package/template/blocks/contact/Block.vue +50 -21
- package/template/blocks/contact/__tests__/Block.spec.ts +89 -8
- package/template/blocks/contact/block.json +2 -9
- package/template/blocks/contact/mock.json +1 -2
- package/template/blocks/footer/Block.vue +58 -8
- package/template/blocks/footer/__tests__/Block.spec.ts +70 -16
- package/template/blocks/footer/preview.png +0 -0
- package/template/blocks/form/Block.vue +614 -0
- package/template/blocks/form/__tests__/Block.spec.ts +536 -0
- package/template/blocks/form/block.json +69 -0
- package/template/blocks/form/mock.json +25 -0
- package/template/blocks/form/preview.png +0 -0
- package/template/blocks/form/validate.ts +68 -0
- package/template/blocks/newsletter/Block.vue +57 -3
- package/template/blocks/newsletter/__tests__/Block.spec.ts +89 -31
- package/template/blocks/newsletter/block.json +4 -5
- package/template/blocks/newsletter/mock.json +1 -1
- package/template/blocks/newsletter/preview.png +0 -0
- package/template/blocks/product-detail/__tests__/Block.spec.ts +7 -10
- package/template/i18n/en-US.json +33 -2
- package/template/i18n/is-IS.json +33 -2
- package/template/nuxt.config.ts +15 -1
- package/template/package.json +1 -0
- package/template/public/apple-touch-icon.png +0 -0
- package/template/public/favicon.ico +0 -0
- package/template/stories/generated/form.stories.ts +68 -0
- package/template/test/formValidate.spec.ts +46 -0
- package/template/test/inlineEditable.spec.ts +2 -1
- package/template/test/moneyFormatting.spec.ts +4 -8
- package/template/test/pages/ssr.spec.ts +5 -11
- package/template/test/prerenderFailure.spec.ts +1 -1
- package/template/test/prerenderRefresh.browser.spec.ts +5 -1
- package/template/test/richText.spec.ts +12 -9
- package/template/test/seededPages.spec.ts +13 -0
- package/template/test/seoInput.spec.ts +119 -0
- package/template/test/slugPage.spec.ts +62 -26
- package/template/test/slugPageTemplate.spec.ts +5 -1
- package/template/test/starter.spec.ts +42 -4
- package/template/test/storefront/analytics.spec.ts +283 -0
- package/template/test/storefront/forms.spec.ts +385 -0
- package/template/test/storefront/money.spec.ts +11 -12
- package/template/test/storefront/structuredData.spec.ts +138 -0
- package/template/test/structuredData.generate.spec.ts +159 -0
- package/template/test/support/mockGateway.ts +29 -1
- package/template/test/support/turnstileForms.ts +53 -0
- package/template/test/useBlockData.spec.ts +7 -4
- package/template/app/utils/pageTitle.ts +0 -37
- package/template/test/pageTitle.spec.ts +0 -49
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* A form from the platform's Forms page, rendered from its definition: the fields, their labels in
|
|
4
|
+
* the page's locale, which are required, the spam protection and what a visitor is told afterwards
|
|
5
|
+
* are all the merchant's (Studio, `GET /site/v1/forms/{key}`), so one block serves a contact form,
|
|
6
|
+
* a newsletter sign-up or any form a merchant builds. This block owns only the copy around the
|
|
7
|
+
* form — heading, text, button label, consent line — and the look. `preset`:
|
|
8
|
+
* - `default`: the fields stacked in a two-column `FormLayout` (long fields and checkboxes span
|
|
9
|
+
* both columns), the button in the actions row.
|
|
10
|
+
* - `inline`: one field and the button on one row, centred under the copy — a sign-up. A form of
|
|
11
|
+
* several fields falls back to the stacked layout rather than squeezing them into one row.
|
|
12
|
+
*
|
|
13
|
+
* **Errors are field-keyed.** A failed local check (required, shape, length) and a `422` from the
|
|
14
|
+
* platform land in the same place — under the field they name, as `FieldWrapper`'s `error` — and
|
|
15
|
+
* focus moves to the first invalid control. Messages about the form as a whole (the `422`'s `form`
|
|
16
|
+
* list, a rate limit, the service being down) go in the form's error summary, a `role="alert"`
|
|
17
|
+
* region above the fields. Nothing reaches the platform from a form that fails its own checks, but
|
|
18
|
+
* the platform decides: its messages are shown verbatim and win over the local ones.
|
|
19
|
+
*
|
|
20
|
+
* **Spam.** The definition names a honeypot field: it is rendered off-screen and inert (a person
|
|
21
|
+
* never reaches it) and its value is sent as `hp`. When the merchant enabled Cloudflare Turnstile
|
|
22
|
+
* the widget is rendered under the fields (`useTurnstile`) and the token sent with the submission;
|
|
23
|
+
* a form on a site without it loads nothing third-party.
|
|
24
|
+
*
|
|
25
|
+
* **Success** replaces the form with a `role="status"` panel that takes focus (the submit button
|
|
26
|
+
* that held it is gone), the merchant's confirmation text under a title. A form that subscribes the
|
|
27
|
+
* visitor answers `pending: true` — a confirmation email is on its way — and the title says so. A
|
|
28
|
+
* merchant-set redirect sends the visitor on instead, outside the editor.
|
|
29
|
+
*
|
|
30
|
+
* **`FormLayout`'s own gate.** It refuses to emit `submit` while any control is `aria-invalid`, so
|
|
31
|
+
* an error clears itself the moment its field changes, and a server failure that marks no field
|
|
32
|
+
* (every `form`-level message) never blocks the visitor's retry.
|
|
33
|
+
*/
|
|
34
|
+
import { computed, nextTick, reactive, ref, watch } from 'vue';
|
|
35
|
+
import {
|
|
36
|
+
Button,
|
|
37
|
+
Checkbox,
|
|
38
|
+
Container,
|
|
39
|
+
EditorPlaceholder,
|
|
40
|
+
FieldWrapper,
|
|
41
|
+
FormLayout,
|
|
42
|
+
Input,
|
|
43
|
+
Section,
|
|
44
|
+
Select,
|
|
45
|
+
Textarea,
|
|
46
|
+
type FormLayoutSubmitPayload,
|
|
47
|
+
type SectionBackground,
|
|
48
|
+
} from '@eldrajs/ui';
|
|
49
|
+
import { EldraRichText } from '@eldrajs/theme-vue';
|
|
50
|
+
import { useBlockData } from '../../app/composables/useBlockData';
|
|
51
|
+
import { useEditing } from '../../app/composables/useEditing';
|
|
52
|
+
import { useRichTextScrollRegions } from '../../app/composables/useRichTextScrollRegions';
|
|
53
|
+
import { useStorefront } from '../../app/composables/useStorefront';
|
|
54
|
+
import { useTurnstile } from '../../app/composables/useTurnstile';
|
|
55
|
+
import { useI18n } from 'vue-i18n';
|
|
56
|
+
import { useUiId } from '../../app/composables/useUiId';
|
|
57
|
+
import EldraIcon from '../../app/components/EldraIcon.vue';
|
|
58
|
+
import FormHoneypot from '../../app/components/FormHoneypot.vue';
|
|
59
|
+
import { safeHref } from '../../app/utils/links';
|
|
60
|
+
import type { StorefrontFormField, StorefrontFormSubmitResult } from '../../app/storefront/types';
|
|
61
|
+
import { validateField } from './validate';
|
|
62
|
+
|
|
63
|
+
const props = defineProps<{ entry: EldraBlockEntry<'form'> }>();
|
|
64
|
+
const { data, entryId } = useBlockData(props, 'form');
|
|
65
|
+
const { t } = useI18n();
|
|
66
|
+
const isEditing = useEditing();
|
|
67
|
+
const storefront = useStorefront();
|
|
68
|
+
|
|
69
|
+
const uid = useUiId();
|
|
70
|
+
const headingId = `form-heading-${uid}`;
|
|
71
|
+
const fieldId = (id: string): string => `form-${uid}-${id}`;
|
|
72
|
+
const fieldErrorId = (id: string): string => `form-${uid}-${id}-error`;
|
|
73
|
+
|
|
74
|
+
const variant = computed<'default' | 'inline'>(() =>
|
|
75
|
+
data.value.preset === 'inline' ? 'inline' : 'default'
|
|
76
|
+
);
|
|
77
|
+
const sectionBackground = computed<SectionBackground>(() => data.value.sectionBackground ?? 'none');
|
|
78
|
+
const isInverted = computed(
|
|
79
|
+
() => sectionBackground.value === 'primary' || sectionBackground.value === 'accent'
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const formKey = computed(() => (data.value.formKey ?? '').trim());
|
|
83
|
+
const heading = computed(() => (data.value.heading ?? '').trim());
|
|
84
|
+
const text = computed(() => (data.value.text ?? '').trim());
|
|
85
|
+
const buttonLabelText = computed(() => data.value.buttonLabel || t('form.submit'));
|
|
86
|
+
const hasConsent = computed(
|
|
87
|
+
() => Array.isArray(data.value.consent?.content) && data.value.consent.content.length > 0
|
|
88
|
+
);
|
|
89
|
+
const showHeadingHint = computed(() => isEditing.value && heading.value === '');
|
|
90
|
+
const showFormHint = computed(() => isEditing.value && formKey.value === '');
|
|
91
|
+
|
|
92
|
+
const definition = storefront.forms.definition(formKey);
|
|
93
|
+
const form = computed(() => definition.data.value);
|
|
94
|
+
|
|
95
|
+
/** Spec "On primary / accent": text on the section switches to the contrast colour. */
|
|
96
|
+
const contrastTextClass = computed<string | undefined>(() => {
|
|
97
|
+
if (sectionBackground.value === 'primary') return 'text-primary-contrast';
|
|
98
|
+
if (sectionBackground.value === 'accent') return 'text-accent-contrast';
|
|
99
|
+
return undefined;
|
|
100
|
+
});
|
|
101
|
+
const mutedToneClass = computed(() => {
|
|
102
|
+
if (sectionBackground.value === 'primary') return 'text-primary-contrast/90';
|
|
103
|
+
if (sectionBackground.value === 'accent') return 'text-accent-contrast/90';
|
|
104
|
+
return 'text-muted';
|
|
105
|
+
});
|
|
106
|
+
/**
|
|
107
|
+
* The platform's consent sentence of a subscribing form (`{shop}` already filled in). Plain text,
|
|
108
|
+
* rendered as an escaped interpolation, never as HTML, and tied to the submit button with
|
|
109
|
+
* `aria-describedby`: pressing it is the consent, so a screen reader hears the wording with it.
|
|
110
|
+
*/
|
|
111
|
+
const consentSentence = computed(() => (form.value?.consentText ?? '').trim());
|
|
112
|
+
const consentSentenceId = `form-consent-sentence-${uid}`;
|
|
113
|
+
const consentSentenceToneClass = computed(() =>
|
|
114
|
+
sectionBackground.value === 'primary'
|
|
115
|
+
? 'text-primary-contrast/90'
|
|
116
|
+
: sectionBackground.value === 'accent'
|
|
117
|
+
? 'text-accent-contrast/90'
|
|
118
|
+
: 'text-muted'
|
|
119
|
+
);
|
|
120
|
+
const consentToneClass = computed(() => {
|
|
121
|
+
if (sectionBackground.value === 'primary') {
|
|
122
|
+
return 'text-primary-contrast/90 [&_a]:text-primary-contrast';
|
|
123
|
+
}
|
|
124
|
+
if (sectionBackground.value === 'accent') {
|
|
125
|
+
return 'text-accent-contrast/90 [&_a]:text-accent-contrast';
|
|
126
|
+
}
|
|
127
|
+
return 'text-muted [&_a]:text-muted';
|
|
128
|
+
});
|
|
129
|
+
const errorChipClass = computed<string | undefined>(() =>
|
|
130
|
+
isInverted.value ? 'w-fit rounded-sm bg-background px-2 py-1' : undefined
|
|
131
|
+
);
|
|
132
|
+
const fieldWrapperClasses = computed(() => ({
|
|
133
|
+
label: contrastTextClass.value,
|
|
134
|
+
error: errorChipClass.value,
|
|
135
|
+
}));
|
|
136
|
+
const checkboxClasses = computed(() => ({ label: contrastTextClass.value }));
|
|
137
|
+
|
|
138
|
+
/* ---------------------------------------------------------------------- */
|
|
139
|
+
/* State */
|
|
140
|
+
/* ---------------------------------------------------------------------- */
|
|
141
|
+
|
|
142
|
+
const values = reactive<Record<string, string | boolean>>({});
|
|
143
|
+
const fieldErrors = reactive<Record<string, string | undefined>>({});
|
|
144
|
+
const formErrors = ref<string[]>([]);
|
|
145
|
+
const honeypot = ref('');
|
|
146
|
+
const isSubmitting = ref(false);
|
|
147
|
+
const succeeded = ref<{ pending: boolean } | null>(null);
|
|
148
|
+
|
|
149
|
+
/** Seeds one value per field when the definition arrives (or the form it names changes). */
|
|
150
|
+
watch(
|
|
151
|
+
() => form.value?.key,
|
|
152
|
+
() => {
|
|
153
|
+
for (const key of Object.keys(values)) delete values[key];
|
|
154
|
+
for (const key of Object.keys(fieldErrors)) delete fieldErrors[key];
|
|
155
|
+
for (const field of form.value?.fields ?? []) {
|
|
156
|
+
values[field.id] = isBoolean(field) ? false : '';
|
|
157
|
+
}
|
|
158
|
+
formErrors.value = [];
|
|
159
|
+
succeeded.value = null;
|
|
160
|
+
},
|
|
161
|
+
{ immediate: true }
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
function setValue(field: StorefrontFormField, value: string | boolean): void {
|
|
165
|
+
values[field.id] = value;
|
|
166
|
+
// The gate described in the file doc: a field's error is gone the moment its value changes.
|
|
167
|
+
fieldErrors[field.id] = undefined;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const statusRoot = ref<HTMLElement | null>(null);
|
|
171
|
+
const consentRoot = ref<HTMLElement | null>(null);
|
|
172
|
+
const turnstileRoot = ref<HTMLElement | null>(null);
|
|
173
|
+
useRichTextScrollRegions(consentRoot, (caption) => caption ?? t('form.richTextTableLabel'));
|
|
174
|
+
|
|
175
|
+
const turnstileKey = computed(() =>
|
|
176
|
+
succeeded.value ? null : (form.value?.turnstileSiteKey ?? null)
|
|
177
|
+
);
|
|
178
|
+
const turnstile = useTurnstile(turnstileKey, turnstileRoot);
|
|
179
|
+
|
|
180
|
+
const errorCount = computed(() => Object.values(fieldErrors).filter(Boolean).length);
|
|
181
|
+
|
|
182
|
+
/** Inline only when the form is one text-like field, which is the one shape a row can hold. */
|
|
183
|
+
const isInlineRow = computed(() => {
|
|
184
|
+
const fields = form.value?.fields ?? [];
|
|
185
|
+
return (
|
|
186
|
+
variant.value === 'inline' &&
|
|
187
|
+
fields.length === 1 &&
|
|
188
|
+
fields[0] !== undefined &&
|
|
189
|
+
!isBoolean(fields[0]) &&
|
|
190
|
+
fields[0].type !== 'textarea' &&
|
|
191
|
+
fields[0].type !== 'select'
|
|
192
|
+
);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const layout = computed(() => (variant.value === 'default' ? 'two' : 'single'));
|
|
196
|
+
const fieldsClass = computed(() =>
|
|
197
|
+
isInlineRow.value
|
|
198
|
+
? 'grid gap-3 @two-col:grid-cols-[1fr_auto] @two-col:items-end'
|
|
199
|
+
: variant.value === 'inline'
|
|
200
|
+
? 'grid gap-4'
|
|
201
|
+
: undefined
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
const isSplitCentered = computed(() => variant.value === 'inline');
|
|
205
|
+
const outerClass = computed(() =>
|
|
206
|
+
isSplitCentered.value ? 'mx-auto max-w-[36rem] text-center' : 'mx-auto max-w-[40rem]'
|
|
207
|
+
);
|
|
208
|
+
const copyClass = 'grid gap-3';
|
|
209
|
+
|
|
210
|
+
const NUMERIC_INPUT_MODE: Partial<
|
|
211
|
+
Record<StorefrontFormField['type'], 'tel' | 'email' | 'decimal' | 'url'>
|
|
212
|
+
> = {
|
|
213
|
+
phone: 'tel',
|
|
214
|
+
email: 'email',
|
|
215
|
+
number: 'decimal',
|
|
216
|
+
url: 'url',
|
|
217
|
+
};
|
|
218
|
+
const AUTOCOMPLETE: Partial<Record<string, string>> = {
|
|
219
|
+
name: 'name',
|
|
220
|
+
email: 'email',
|
|
221
|
+
tel: 'tel',
|
|
222
|
+
phone: 'tel',
|
|
223
|
+
};
|
|
224
|
+
/** A checkbox and a consent are both a boolean control; `required` means it must be checked. */
|
|
225
|
+
function isBoolean(field: StorefrontFormField): boolean {
|
|
226
|
+
return field.type === 'checkbox' || field.type === 'consent';
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function autocompleteFor(field: StorefrontFormField): string | undefined {
|
|
230
|
+
return (
|
|
231
|
+
AUTOCOMPLETE[field.id] ??
|
|
232
|
+
(field.type === 'email'
|
|
233
|
+
? 'email'
|
|
234
|
+
: field.type === 'phone'
|
|
235
|
+
? 'tel'
|
|
236
|
+
: field.type === 'url'
|
|
237
|
+
? 'url'
|
|
238
|
+
: undefined)
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// A number field stays `type="text"` with `inputmode="decimal"`: a `type="number"` input blanks a
|
|
243
|
+
// value the browser's locale does not parse (a decimal comma), which would submit it empty.
|
|
244
|
+
function inputType(field: StorefrontFormField): 'text' | 'email' | 'tel' | 'url' {
|
|
245
|
+
if (field.type === 'phone') return 'tel';
|
|
246
|
+
if (field.type === 'email' || field.type === 'url') return field.type;
|
|
247
|
+
return 'text';
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/* ---------------------------------------------------------------------- */
|
|
251
|
+
/* Submit */
|
|
252
|
+
/* ---------------------------------------------------------------------- */
|
|
253
|
+
|
|
254
|
+
async function focusFirstInvalid(): Promise<void> {
|
|
255
|
+
await nextTick();
|
|
256
|
+
const first = (form.value?.fields ?? []).find((field) => fieldErrors[field.id]);
|
|
257
|
+
if (first === undefined) return;
|
|
258
|
+
const element = document.getElementById(fieldId(first.id));
|
|
259
|
+
element?.focus();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async function focusStatus(): Promise<void> {
|
|
263
|
+
await nextTick();
|
|
264
|
+
statusRoot.value?.focus();
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function formMessageFor(result: Extract<StorefrontFormSubmitResult, { ok: false }>): string[] {
|
|
268
|
+
if (result.reason === 'invalid') {
|
|
269
|
+
return result.form.length > 0 ? result.form : [];
|
|
270
|
+
}
|
|
271
|
+
switch (result.reason) {
|
|
272
|
+
case 'rate-limited':
|
|
273
|
+
return [t('form.rateLimited')];
|
|
274
|
+
case 'too-large':
|
|
275
|
+
return [t('form.tooLarge')];
|
|
276
|
+
case 'unsupported':
|
|
277
|
+
return [t('form.unsupported')];
|
|
278
|
+
default:
|
|
279
|
+
return [t('form.unavailable')];
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function redirectTarget(): string | null {
|
|
284
|
+
const target = form.value?.successRedirect;
|
|
285
|
+
return target ? safeHref(target) : null;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
async function onSubmit(payload: FormLayoutSubmitPayload): Promise<void> {
|
|
289
|
+
payload.event.preventDefault();
|
|
290
|
+
// Spec "Submitting": repeat submits are ignored. The submit Button stays clickable while loading,
|
|
291
|
+
// so this guard is what stops them.
|
|
292
|
+
if (isSubmitting.value) return;
|
|
293
|
+
const current = form.value;
|
|
294
|
+
if (current === null) return;
|
|
295
|
+
|
|
296
|
+
formErrors.value = [];
|
|
297
|
+
let valid = true;
|
|
298
|
+
for (const field of current.fields) {
|
|
299
|
+
const message = validateField(field, values[field.id], t);
|
|
300
|
+
fieldErrors[field.id] = message ?? undefined;
|
|
301
|
+
if (message !== null) valid = false;
|
|
302
|
+
}
|
|
303
|
+
if (!valid) {
|
|
304
|
+
await focusFirstInvalid();
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
if (current.turnstileSiteKey !== null && turnstile.token.value === null) {
|
|
308
|
+
formErrors.value = [
|
|
309
|
+
turnstile.failed.value ? t('form.turnstileFailed') : t('form.turnstileRequired'),
|
|
310
|
+
];
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
isSubmitting.value = true;
|
|
315
|
+
const result = await storefront.forms.submit(current.key, {
|
|
316
|
+
values: { ...values },
|
|
317
|
+
...(turnstile.token.value ? { turnstileToken: turnstile.token.value } : {}),
|
|
318
|
+
...(honeypot.value ? { hp: honeypot.value } : {}),
|
|
319
|
+
});
|
|
320
|
+
isSubmitting.value = false;
|
|
321
|
+
|
|
322
|
+
if (result.ok) {
|
|
323
|
+
succeeded.value = { pending: result.pending };
|
|
324
|
+
const redirect = isEditing.value ? null : redirectTarget();
|
|
325
|
+
if (redirect !== null) {
|
|
326
|
+
window.location.assign(redirect);
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
await focusStatus();
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// A token is single-use: whatever failed, the next attempt needs a fresh one.
|
|
334
|
+
turnstile.reset();
|
|
335
|
+
if (result.reason === 'invalid') {
|
|
336
|
+
for (const field of current.fields) {
|
|
337
|
+
const messages = result.errors[field.id];
|
|
338
|
+
fieldErrors[field.id] = messages?.[0];
|
|
339
|
+
}
|
|
340
|
+
// A message about a field this form does not render would otherwise vanish.
|
|
341
|
+
const orphaned = Object.entries(result.errors)
|
|
342
|
+
.filter(([id]) => !current.fields.some((field) => field.id === id))
|
|
343
|
+
.flatMap(([, messages]) => messages);
|
|
344
|
+
const summary = [...result.form, ...orphaned];
|
|
345
|
+
formErrors.value =
|
|
346
|
+
summary.length > 0 || Object.keys(result.errors).length > 0
|
|
347
|
+
? summary
|
|
348
|
+
: [t('form.invalidForm')];
|
|
349
|
+
if (errorCount.value > 0) await focusFirstInvalid();
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
formErrors.value = formMessageFor(result);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
async function onSendAnother(): Promise<void> {
|
|
356
|
+
for (const field of form.value?.fields ?? []) {
|
|
357
|
+
values[field.id] = isBoolean(field) ? false : '';
|
|
358
|
+
fieldErrors[field.id] = undefined;
|
|
359
|
+
}
|
|
360
|
+
honeypot.value = '';
|
|
361
|
+
formErrors.value = [];
|
|
362
|
+
succeeded.value = null;
|
|
363
|
+
await nextTick();
|
|
364
|
+
const first = form.value?.fields[0];
|
|
365
|
+
if (first) document.getElementById(fieldId(first.id))?.focus();
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const successTitle = computed(
|
|
369
|
+
() =>
|
|
370
|
+
data.value.successTitle ||
|
|
371
|
+
(succeeded.value?.pending ? t('form.successTitlePending') : t('form.successTitle'))
|
|
372
|
+
);
|
|
373
|
+
</script>
|
|
374
|
+
|
|
375
|
+
<template>
|
|
376
|
+
<Section
|
|
377
|
+
:background="sectionBackground"
|
|
378
|
+
spacing="md"
|
|
379
|
+
:labelled-by="heading ? headingId : undefined"
|
|
380
|
+
>
|
|
381
|
+
<Container width="content">
|
|
382
|
+
<div :class="outerClass">
|
|
383
|
+
<div :class="copyClass">
|
|
384
|
+
<h2
|
|
385
|
+
v-if="heading"
|
|
386
|
+
:id="headingId"
|
|
387
|
+
class="@tablet:text-[2rem] text-[1.625rem] leading-tight font-semibold text-balance"
|
|
388
|
+
>
|
|
389
|
+
{{ heading }}
|
|
390
|
+
</h2>
|
|
391
|
+
<EditorPlaceholder
|
|
392
|
+
v-else-if="showHeadingHint"
|
|
393
|
+
:id="headingId"
|
|
394
|
+
inline
|
|
395
|
+
:label="t('form.headingHintLabel')"
|
|
396
|
+
/>
|
|
397
|
+
<p v-if="text" class="text-base leading-[1.6]" :class="mutedToneClass">{{ text }}</p>
|
|
398
|
+
</div>
|
|
399
|
+
|
|
400
|
+
<div class="mt-6 text-left">
|
|
401
|
+
<EditorPlaceholder
|
|
402
|
+
v-if="showFormHint"
|
|
403
|
+
:label="t('form.formHintLabel')"
|
|
404
|
+
:help="t('form.formHintHelp')"
|
|
405
|
+
/>
|
|
406
|
+
|
|
407
|
+
<p
|
|
408
|
+
v-else-if="definition.pending.value"
|
|
409
|
+
role="status"
|
|
410
|
+
class="text-body-sm"
|
|
411
|
+
:class="mutedToneClass"
|
|
412
|
+
>
|
|
413
|
+
{{ t('form.loading') }}
|
|
414
|
+
</p>
|
|
415
|
+
|
|
416
|
+
<p
|
|
417
|
+
v-else-if="form === null"
|
|
418
|
+
role="alert"
|
|
419
|
+
class="text-body-sm"
|
|
420
|
+
:class="[mutedToneClass, errorChipClass]"
|
|
421
|
+
>
|
|
422
|
+
{{ t('form.loadFailed') }}
|
|
423
|
+
</p>
|
|
424
|
+
|
|
425
|
+
<FormLayout
|
|
426
|
+
v-else-if="!succeeded"
|
|
427
|
+
novalidate
|
|
428
|
+
:layout="layout"
|
|
429
|
+
:aria-label="form.name"
|
|
430
|
+
:submitting="isSubmitting"
|
|
431
|
+
:classes="fieldsClass ? { fields: fieldsClass } : undefined"
|
|
432
|
+
@submit="onSubmit"
|
|
433
|
+
>
|
|
434
|
+
<template v-if="formErrors.length > 0" #errorSummary>
|
|
435
|
+
<p v-for="message in formErrors" :key="message" class="font-medium">{{ message }}</p>
|
|
436
|
+
</template>
|
|
437
|
+
|
|
438
|
+
<template v-for="field in form.fields" :key="field.id">
|
|
439
|
+
<div v-if="isBoolean(field)" class="@two-col:col-span-2 grid gap-1">
|
|
440
|
+
<Checkbox
|
|
441
|
+
:id="fieldId(field.id)"
|
|
442
|
+
:model-value="values[field.id] === true"
|
|
443
|
+
:name="field.id"
|
|
444
|
+
:invalid="Boolean(fieldErrors[field.id])"
|
|
445
|
+
:described-by="fieldErrors[field.id] ? fieldErrorId(field.id) : undefined"
|
|
446
|
+
:classes="checkboxClasses"
|
|
447
|
+
@update:model-value="setValue(field, $event)"
|
|
448
|
+
>
|
|
449
|
+
{{ field.label }}
|
|
450
|
+
</Checkbox>
|
|
451
|
+
<p
|
|
452
|
+
v-if="fieldErrors[field.id]"
|
|
453
|
+
:id="fieldErrorId(field.id)"
|
|
454
|
+
class="text-field-note text-danger flex items-start gap-1.25 font-medium"
|
|
455
|
+
:class="errorChipClass"
|
|
456
|
+
>
|
|
457
|
+
<EldraIcon
|
|
458
|
+
name="alert-circle"
|
|
459
|
+
size="sm"
|
|
460
|
+
class="size-4 shrink-0 translate-y-[0.1em]"
|
|
461
|
+
aria-hidden="true"
|
|
462
|
+
/>
|
|
463
|
+
<span>{{ fieldErrors[field.id] }}</span>
|
|
464
|
+
</p>
|
|
465
|
+
</div>
|
|
466
|
+
|
|
467
|
+
<FieldWrapper
|
|
468
|
+
v-else
|
|
469
|
+
:id="fieldId(field.id)"
|
|
470
|
+
:label="field.label"
|
|
471
|
+
:required="field.required"
|
|
472
|
+
:optional="!field.required && form.fields.length > 1"
|
|
473
|
+
:full="field.type === 'textarea'"
|
|
474
|
+
:error="fieldErrors[field.id]"
|
|
475
|
+
:classes="fieldWrapperClasses"
|
|
476
|
+
>
|
|
477
|
+
<Textarea
|
|
478
|
+
v-if="field.type === 'textarea'"
|
|
479
|
+
:model-value="String(values[field.id] ?? '')"
|
|
480
|
+
:name="field.id"
|
|
481
|
+
:placeholder="field.placeholder"
|
|
482
|
+
min-height="8rem"
|
|
483
|
+
@update:model-value="setValue(field, $event)"
|
|
484
|
+
/>
|
|
485
|
+
<Select
|
|
486
|
+
v-else-if="field.type === 'select'"
|
|
487
|
+
:model-value="String(values[field.id] ?? '')"
|
|
488
|
+
:name="field.id"
|
|
489
|
+
:options="field.options ?? []"
|
|
490
|
+
:placeholder="field.placeholder ?? t('form.selectPlaceholder')"
|
|
491
|
+
clearable
|
|
492
|
+
@update:model-value="setValue(field, $event ?? '')"
|
|
493
|
+
/>
|
|
494
|
+
<Input
|
|
495
|
+
v-else
|
|
496
|
+
:model-value="String(values[field.id] ?? '')"
|
|
497
|
+
:type="inputType(field)"
|
|
498
|
+
:name="field.id"
|
|
499
|
+
:inputmode="NUMERIC_INPUT_MODE[field.type]"
|
|
500
|
+
:autocomplete="autocompleteFor(field)"
|
|
501
|
+
:size="isInlineRow ? 'lg' : undefined"
|
|
502
|
+
:placeholder="field.placeholder"
|
|
503
|
+
@update:model-value="setValue(field, $event)"
|
|
504
|
+
/>
|
|
505
|
+
</FieldWrapper>
|
|
506
|
+
</template>
|
|
507
|
+
|
|
508
|
+
<!-- The honeypot: off-screen and inert, so a person never reaches it; a bot that fills
|
|
509
|
+
every input does, and the platform drops the submission. -->
|
|
510
|
+
<FormHoneypot v-model="honeypot" :name="form.honeypotField" />
|
|
511
|
+
|
|
512
|
+
<div v-if="form.turnstileSiteKey" ref="turnstileRoot" class="@two-col:col-span-2" />
|
|
513
|
+
|
|
514
|
+
<Button
|
|
515
|
+
v-if="isInlineRow"
|
|
516
|
+
type="submit"
|
|
517
|
+
variant="primary"
|
|
518
|
+
size="lg"
|
|
519
|
+
class="@two-col:w-auto w-full"
|
|
520
|
+
:label="t('form.submitting')"
|
|
521
|
+
:aria-describedby="consentSentence ? consentSentenceId : undefined"
|
|
522
|
+
>
|
|
523
|
+
{{ buttonLabelText }}
|
|
524
|
+
</Button>
|
|
525
|
+
<p
|
|
526
|
+
v-if="isInlineRow && consentSentence"
|
|
527
|
+
:id="consentSentenceId"
|
|
528
|
+
data-part="consent-sentence"
|
|
529
|
+
class="text-body-sm @two-col:col-span-2"
|
|
530
|
+
:class="consentSentenceToneClass"
|
|
531
|
+
>
|
|
532
|
+
{{ consentSentence }}
|
|
533
|
+
</p>
|
|
534
|
+
|
|
535
|
+
<div ref="consentRoot" class="@two-col:col-span-2">
|
|
536
|
+
<!-- Under this block's own `h2`, so any heading in the consent copy starts at `h3`. -->
|
|
537
|
+
<EldraRichText
|
|
538
|
+
v-if="hasConsent"
|
|
539
|
+
class="prose-eldra text-body-sm"
|
|
540
|
+
:class="consentToneClass"
|
|
541
|
+
:entry-id="entryId"
|
|
542
|
+
field="consent"
|
|
543
|
+
:doc="data.consent"
|
|
544
|
+
api-id="form"
|
|
545
|
+
:min-heading-level="3"
|
|
546
|
+
/>
|
|
547
|
+
</div>
|
|
548
|
+
|
|
549
|
+
<template v-if="!isInlineRow" #actions>
|
|
550
|
+
<div v-if="consentSentence" class="grid w-full gap-2">
|
|
551
|
+
<Button
|
|
552
|
+
type="submit"
|
|
553
|
+
variant="primary"
|
|
554
|
+
size="lg"
|
|
555
|
+
class="@two-col:justify-self-end"
|
|
556
|
+
:label="t('form.submitting')"
|
|
557
|
+
:aria-describedby="consentSentenceId"
|
|
558
|
+
>
|
|
559
|
+
{{ buttonLabelText }}
|
|
560
|
+
</Button>
|
|
561
|
+
<p
|
|
562
|
+
:id="consentSentenceId"
|
|
563
|
+
data-part="consent-sentence"
|
|
564
|
+
class="text-body-sm"
|
|
565
|
+
:class="consentSentenceToneClass"
|
|
566
|
+
>
|
|
567
|
+
{{ consentSentence }}
|
|
568
|
+
</p>
|
|
569
|
+
</div>
|
|
570
|
+
<Button
|
|
571
|
+
v-else
|
|
572
|
+
type="submit"
|
|
573
|
+
variant="primary"
|
|
574
|
+
size="lg"
|
|
575
|
+
:label="t('form.submitting')"
|
|
576
|
+
>
|
|
577
|
+
{{ buttonLabelText }}
|
|
578
|
+
</Button>
|
|
579
|
+
</template>
|
|
580
|
+
</FormLayout>
|
|
581
|
+
|
|
582
|
+
<div
|
|
583
|
+
ref="statusRoot"
|
|
584
|
+
role="status"
|
|
585
|
+
tabindex="-1"
|
|
586
|
+
:class="
|
|
587
|
+
succeeded
|
|
588
|
+
? 'border-border bg-background text-text mt-3 flex items-start gap-3 rounded-lg border p-5'
|
|
589
|
+
: ''
|
|
590
|
+
"
|
|
591
|
+
>
|
|
592
|
+
<template v-if="succeeded && form">
|
|
593
|
+
<EldraIcon
|
|
594
|
+
name="circle-check"
|
|
595
|
+
size="lg"
|
|
596
|
+
class="text-success shrink-0"
|
|
597
|
+
aria-hidden="true"
|
|
598
|
+
/>
|
|
599
|
+
<div class="grid gap-1">
|
|
600
|
+
<p class="font-semibold">{{ successTitle }}</p>
|
|
601
|
+
<p v-if="form.successMessage" class="text-muted">{{ form.successMessage }}</p>
|
|
602
|
+
<div v-if="variant === 'default'" class="mt-2">
|
|
603
|
+
<Button variant="outline" type="button" @click="onSendAnother">{{
|
|
604
|
+
t('form.sendAnother')
|
|
605
|
+
}}</Button>
|
|
606
|
+
</div>
|
|
607
|
+
</div>
|
|
608
|
+
</template>
|
|
609
|
+
</div>
|
|
610
|
+
</div>
|
|
611
|
+
</div>
|
|
612
|
+
</Container>
|
|
613
|
+
</Section>
|
|
614
|
+
</template>
|