improve form submission handling and frontend assets
Replace Alpine form bindings with accessible vanilla JavaScript submission states and unique field IDs. Add the bundled frontend script and Rolldown build configuration, update the generated stylesheet, and extend submission handler tests.
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
--font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono",
|
||||
"Courier New", monospace;
|
||||
--spacing: 0.25rem;
|
||||
--container-md: 28rem;
|
||||
--container-2xl: 42rem;
|
||||
--text-xs: 0.75rem;
|
||||
--text-xs--line-height: calc(1 / 0.75);
|
||||
@@ -24,12 +25,14 @@
|
||||
--text-3xl--line-height: calc(2.25 / 1.875);
|
||||
--text-4xl: 2.25rem;
|
||||
--text-4xl--line-height: calc(2.5 / 2.25);
|
||||
--text-6xl: 3.75rem;
|
||||
--text-6xl--line-height: 1;
|
||||
--font-weight-medium: 500;
|
||||
--font-weight-semibold: 600;
|
||||
--font-weight-bold: 700;
|
||||
--tracking-tight: -0.025em;
|
||||
--tracking-wide: 0.025em;
|
||||
--radius-lg: 0.5rem;
|
||||
--leading-relaxed: 1.625;
|
||||
--radius-xl: 0.75rem;
|
||||
--radius-2xl: 1rem;
|
||||
--default-transition-duration: 150ms;
|
||||
--default-transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
|
||||
--default-font-family: var(--font-sans);
|
||||
@@ -223,8 +226,8 @@
|
||||
.mb-2 {
|
||||
margin-bottom: calc(var(--spacing) * 2);
|
||||
}
|
||||
.mb-4 {
|
||||
margin-bottom: calc(var(--spacing) * 4);
|
||||
.mb-3 {
|
||||
margin-bottom: calc(var(--spacing) * 3);
|
||||
}
|
||||
.mb-5 {
|
||||
margin-bottom: calc(var(--spacing) * 5);
|
||||
@@ -235,15 +238,36 @@
|
||||
.ml-1 {
|
||||
margin-left: var(--spacing);
|
||||
}
|
||||
.line-clamp-3 {
|
||||
overflow: hidden;
|
||||
display: -webkit-box;
|
||||
-webkit-box-orient: vertical;
|
||||
-webkit-line-clamp: 3;
|
||||
}
|
||||
.flex {
|
||||
display: flex;
|
||||
}
|
||||
.grid {
|
||||
display: grid;
|
||||
}
|
||||
.hidden {
|
||||
display: none;
|
||||
}
|
||||
.table {
|
||||
display: table;
|
||||
}
|
||||
.size-10 {
|
||||
width: calc(var(--spacing) * 10);
|
||||
height: calc(var(--spacing) * 10);
|
||||
}
|
||||
.size-11 {
|
||||
width: calc(var(--spacing) * 11);
|
||||
height: calc(var(--spacing) * 11);
|
||||
}
|
||||
.size-14 {
|
||||
width: calc(var(--spacing) * 14);
|
||||
height: calc(var(--spacing) * 14);
|
||||
}
|
||||
.h-16 {
|
||||
height: calc(var(--spacing) * 16);
|
||||
}
|
||||
@@ -268,15 +292,30 @@
|
||||
.max-w-2xl {
|
||||
max-width: var(--container-2xl);
|
||||
}
|
||||
.max-w-72 {
|
||||
max-width: calc(var(--spacing) * 72);
|
||||
}
|
||||
.max-w-md {
|
||||
max-width: var(--container-md);
|
||||
}
|
||||
.min-w-40 {
|
||||
min-width: calc(var(--spacing) * 40);
|
||||
}
|
||||
.min-w-\[36rem\] {
|
||||
min-width: 36rem;
|
||||
}
|
||||
.min-w-\[48rem\] {
|
||||
min-width: 48rem;
|
||||
}
|
||||
.min-w-full {
|
||||
min-width: 100%;
|
||||
}
|
||||
.flex-1 {
|
||||
flex: 1;
|
||||
}
|
||||
.shrink-0 {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.cursor-grab {
|
||||
cursor: grab;
|
||||
}
|
||||
@@ -292,6 +331,9 @@
|
||||
.flex-wrap {
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
.place-items-center {
|
||||
place-items: center;
|
||||
}
|
||||
.items-center {
|
||||
align-items: center;
|
||||
}
|
||||
@@ -319,6 +361,9 @@
|
||||
.gap-4 {
|
||||
gap: calc(var(--spacing) * 4);
|
||||
}
|
||||
.gap-5 {
|
||||
gap: calc(var(--spacing) * 5);
|
||||
}
|
||||
:where(.space-y-1 > :not(:last-child)) {
|
||||
--tw-space-y-reverse: 0;
|
||||
margin-block-start: calc(var(--spacing) * var(--tw-space-y-reverse));
|
||||
@@ -329,11 +374,6 @@
|
||||
margin-block-start: calc(calc(var(--spacing) * 2) * var(--tw-space-y-reverse));
|
||||
margin-block-end: calc(calc(var(--spacing) * 2) * calc(1 - var(--tw-space-y-reverse)));
|
||||
}
|
||||
:where(.space-y-3 > :not(:last-child)) {
|
||||
--tw-space-y-reverse: 0;
|
||||
margin-block-start: calc(calc(var(--spacing) * 3) * var(--tw-space-y-reverse));
|
||||
margin-block-end: calc(calc(var(--spacing) * 3) * calc(1 - var(--tw-space-y-reverse)));
|
||||
}
|
||||
:where(.space-y-4 > :not(:last-child)) {
|
||||
--tw-space-y-reverse: 0;
|
||||
margin-block-start: calc(calc(var(--spacing) * 4) * var(--tw-space-y-reverse));
|
||||
@@ -344,17 +384,25 @@
|
||||
margin-block-start: calc(calc(var(--spacing) * 5) * var(--tw-space-y-reverse));
|
||||
margin-block-end: calc(calc(var(--spacing) * 5) * calc(1 - var(--tw-space-y-reverse)));
|
||||
}
|
||||
:where(.space-y-6 > :not(:last-child)) {
|
||||
--tw-space-y-reverse: 0;
|
||||
margin-block-start: calc(calc(var(--spacing) * 6) * var(--tw-space-y-reverse));
|
||||
margin-block-end: calc(calc(var(--spacing) * 6) * calc(1 - var(--tw-space-y-reverse)));
|
||||
}
|
||||
.self-start {
|
||||
align-self: flex-start;
|
||||
}
|
||||
.overflow-x-auto {
|
||||
overflow-x: auto;
|
||||
}
|
||||
.rounded {
|
||||
border-radius: 0.25rem;
|
||||
.rounded-2xl {
|
||||
border-radius: var(--radius-2xl);
|
||||
}
|
||||
.rounded-full {
|
||||
border-radius: calc(infinity * 1px);
|
||||
}
|
||||
.rounded-lg {
|
||||
border-radius: var(--radius-lg);
|
||||
.rounded-xl {
|
||||
border-radius: var(--radius-xl);
|
||||
}
|
||||
.border {
|
||||
border-style: var(--tw-border-style);
|
||||
@@ -364,6 +412,10 @@
|
||||
border-style: var(--tw-border-style);
|
||||
border-width: 2px;
|
||||
}
|
||||
.border-t {
|
||||
border-top-style: var(--tw-border-style);
|
||||
border-top-width: 1px;
|
||||
}
|
||||
.border-b {
|
||||
border-bottom-style: var(--tw-border-style);
|
||||
border-bottom-width: 1px;
|
||||
@@ -376,15 +428,12 @@
|
||||
--tw-border-style: dashed;
|
||||
border-style: dashed;
|
||||
}
|
||||
.p-0 {
|
||||
padding: 0px;
|
||||
}
|
||||
.p-2 {
|
||||
padding: calc(var(--spacing) * 2);
|
||||
}
|
||||
.p-3 {
|
||||
padding: calc(var(--spacing) * 3);
|
||||
}
|
||||
.p-4 {
|
||||
padding: calc(var(--spacing) * 4);
|
||||
}
|
||||
.px-0 {
|
||||
padding-inline: 0px;
|
||||
}
|
||||
@@ -394,26 +443,32 @@
|
||||
.px-4 {
|
||||
padding-inline: calc(var(--spacing) * 4);
|
||||
}
|
||||
.px-5 {
|
||||
padding-inline: calc(var(--spacing) * 5);
|
||||
}
|
||||
.py-2 {
|
||||
padding-block: calc(var(--spacing) * 2);
|
||||
}
|
||||
.py-8 {
|
||||
padding-block: calc(var(--spacing) * 8);
|
||||
}
|
||||
.py-16 {
|
||||
padding-block: calc(var(--spacing) * 16);
|
||||
.py-10 {
|
||||
padding-block: calc(var(--spacing) * 10);
|
||||
}
|
||||
.pt-2 {
|
||||
padding-top: calc(var(--spacing) * 2);
|
||||
.py-12 {
|
||||
padding-block: calc(var(--spacing) * 12);
|
||||
}
|
||||
.pt-4 {
|
||||
padding-top: calc(var(--spacing) * 4);
|
||||
}
|
||||
.pt-5 {
|
||||
padding-top: calc(var(--spacing) * 5);
|
||||
}
|
||||
.pb-4 {
|
||||
padding-bottom: calc(var(--spacing) * 4);
|
||||
}
|
||||
.pl-3 {
|
||||
padding-left: calc(var(--spacing) * 3);
|
||||
}
|
||||
.pl-8 {
|
||||
padding-left: calc(var(--spacing) * 8);
|
||||
.pb-5 {
|
||||
padding-bottom: calc(var(--spacing) * 5);
|
||||
}
|
||||
.text-center {
|
||||
text-align: center;
|
||||
@@ -436,10 +491,6 @@
|
||||
font-size: var(--text-4xl);
|
||||
line-height: var(--tw-leading, var(--text-4xl--line-height));
|
||||
}
|
||||
.text-6xl {
|
||||
font-size: var(--text-6xl);
|
||||
line-height: var(--tw-leading, var(--text-6xl--line-height));
|
||||
}
|
||||
.text-lg {
|
||||
font-size: var(--text-lg);
|
||||
line-height: var(--tw-leading, var(--text-lg--line-height));
|
||||
@@ -456,6 +507,10 @@
|
||||
font-size: var(--text-xs);
|
||||
line-height: var(--tw-leading, var(--text-xs--line-height));
|
||||
}
|
||||
.leading-relaxed {
|
||||
--tw-leading: var(--leading-relaxed);
|
||||
line-height: var(--leading-relaxed);
|
||||
}
|
||||
.font-bold {
|
||||
--tw-font-weight: var(--font-weight-bold);
|
||||
font-weight: var(--font-weight-bold);
|
||||
@@ -464,10 +519,25 @@
|
||||
--tw-font-weight: var(--font-weight-medium);
|
||||
font-weight: var(--font-weight-medium);
|
||||
}
|
||||
.font-semibold {
|
||||
--tw-font-weight: var(--font-weight-semibold);
|
||||
font-weight: var(--font-weight-semibold);
|
||||
}
|
||||
.tracking-\[0\.18em\] {
|
||||
--tw-tracking: 0.18em;
|
||||
letter-spacing: 0.18em;
|
||||
}
|
||||
.tracking-tight {
|
||||
--tw-tracking: var(--tracking-tight);
|
||||
letter-spacing: var(--tracking-tight);
|
||||
}
|
||||
.tracking-wide {
|
||||
--tw-tracking: var(--tracking-wide);
|
||||
letter-spacing: var(--tracking-wide);
|
||||
}
|
||||
.break-words {
|
||||
overflow-wrap: break-word;
|
||||
}
|
||||
.uppercase {
|
||||
text-transform: uppercase;
|
||||
}
|
||||
@@ -496,14 +566,37 @@
|
||||
.sm\:items-center {
|
||||
align-items: center;
|
||||
}
|
||||
.sm\:items-end {
|
||||
align-items: flex-end;
|
||||
}
|
||||
.sm\:justify-between {
|
||||
justify-content: space-between;
|
||||
}
|
||||
.sm\:justify-end {
|
||||
justify-content: flex-end;
|
||||
}
|
||||
.sm\:pl-4 {
|
||||
padding-left: calc(var(--spacing) * 4);
|
||||
.sm\:self-center {
|
||||
align-self: center;
|
||||
}
|
||||
.sm\:p-4 {
|
||||
padding: calc(var(--spacing) * 4);
|
||||
}
|
||||
.sm\:p-5 {
|
||||
padding: calc(var(--spacing) * 5);
|
||||
}
|
||||
.sm\:p-6 {
|
||||
padding: calc(var(--spacing) * 6);
|
||||
}
|
||||
.sm\:py-16 {
|
||||
padding-block: calc(var(--spacing) * 16);
|
||||
}
|
||||
.sm\:pl-9 {
|
||||
padding-left: calc(var(--spacing) * 9);
|
||||
}
|
||||
}
|
||||
@media (width >= 64rem) {
|
||||
.lg\:p-8 {
|
||||
padding: calc(var(--spacing) * 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -668,6 +761,10 @@
|
||||
inherits: false;
|
||||
initial-value: solid;
|
||||
}
|
||||
@property --tw-leading {
|
||||
syntax: "*";
|
||||
inherits: false;
|
||||
}
|
||||
@property --tw-font-weight {
|
||||
syntax: "*";
|
||||
inherits: false;
|
||||
@@ -746,6 +843,7 @@
|
||||
*, ::before, ::after, ::backdrop {
|
||||
--tw-space-y-reverse: 0;
|
||||
--tw-border-style: solid;
|
||||
--tw-leading: initial;
|
||||
--tw-font-weight: initial;
|
||||
--tw-tracking: initial;
|
||||
--tw-shadow: 0 0 #0000;
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
(()=>{let e=`Your submission could not be completed. Please try again.`;function t(e,t){e&&(e.hidden=!t,e.classList.toggle(`hidden`,!t))}function n(e){let t=Array.from(e.querySelectorAll(`input[type="checkbox"]`));t.length!==0&&(t[0].required=!t.some(e=>e.checked))}function r(e){let t={},n=Array.from(e.querySelectorAll(`input[name], select[name], textarea[name]`)).filter(e=>!e.matches(`:disabled`)),r=/* @__PURE__ */ new Map;return n.forEach(e=>{e instanceof HTMLInputElement&&[`button`,`submit`,`reset`,`image`].includes(e.type)||(r.has(e.name)||r.set(e.name,[]),r.get(e.name).push(e))}),r.forEach((e,n)=>{let r=e[0];if(r instanceof HTMLInputElement&&r.type===`checkbox`)t[n]=e.filter(e=>e.checked).map(e=>e.value);else if(r instanceof HTMLInputElement&&r.type===`radio`){let r=e.find(e=>e.checked);t[n]=r?r.value:``}else t[n]=r.value}),{fields:t}}function i(e,n){let r=e.querySelector(`[data-form-submit]`),i=e.querySelector(`[data-form-submit-idle]`),a=e.querySelector(`[data-form-submit-busy]`);e.setAttribute(`aria-busy`,String(n)),r&&(r.disabled=n,r.setAttribute(`aria-busy`,String(n))),t(i,!n),t(a,n)}function a(){document.querySelectorAll(`[data-field-type="checkbox"][data-field-required="true"]`).forEach(n)}document.readyState===`loading`?document.addEventListener(`DOMContentLoaded`,a,{once:!0}):a(),document.addEventListener(`change`,e=>{if(!(e.target instanceof HTMLInputElement)||e.target.type!==`checkbox`)return;let t=e.target.closest(`[data-field-type="checkbox"][data-field-required="true"]`);t&&n(t)}),document.addEventListener(`submit`,async n=>{let a=n.target;if(!(a instanceof HTMLFormElement)||!a.matches(`[data-form-wrapper]`)||(n.preventDefault(),a.dataset.submitting===`true`))return;a.dataset.submitting=`true`;let o=a.parentElement,s=o.querySelector(`[data-form-error]`),c=o.querySelector(`[data-form-success]`);t(s,!1),s&&(s.textContent=``),i(a,!0);try{let n=await fetch(`/ext--form/submit?id=${encodeURIComponent(a.dataset.formId)}`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify(r(a))}),i=await n.json();if(!n.ok||!i||!i.success){let t=i&&Array.isArray(i.errors)?i.errors.join(` `):``;throw Error(t||e)}a.hidden=!0,a.classList.add(`hidden`),t(c,!0)}catch(n){s&&(s.textContent=n instanceof Error&&n.message?n.message:e,t(s,!0))}finally{i(a,!1),delete a.dataset.submitting}})})();
|
||||
@@ -0,0 +1,145 @@
|
||||
(() => {
|
||||
const defaultErrorMessage = 'Your submission could not be completed. Please try again.';
|
||||
|
||||
function setVisible(element, visible) {
|
||||
if (!element) {
|
||||
return;
|
||||
}
|
||||
|
||||
element.hidden = !visible;
|
||||
element.classList.toggle('hidden', !visible);
|
||||
}
|
||||
|
||||
function updateCheckboxGroupRequired(group) {
|
||||
const checkboxes = Array.from(group.querySelectorAll('input[type="checkbox"]'));
|
||||
if (checkboxes.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
checkboxes[0].required = !checkboxes.some(checkbox => checkbox.checked);
|
||||
}
|
||||
|
||||
function collectFields(form) {
|
||||
const fields = {};
|
||||
const controls = Array.from(form.querySelectorAll('input[name], select[name], textarea[name]'))
|
||||
.filter(control => !control.matches(':disabled'));
|
||||
const groups = new Map();
|
||||
|
||||
controls.forEach(control => {
|
||||
if (control instanceof HTMLInputElement && ['button', 'submit', 'reset', 'image'].includes(control.type)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!groups.has(control.name)) {
|
||||
groups.set(control.name, []);
|
||||
}
|
||||
groups.get(control.name).push(control);
|
||||
});
|
||||
|
||||
groups.forEach((groupControls, name) => {
|
||||
const firstControl = groupControls[0];
|
||||
|
||||
if (firstControl instanceof HTMLInputElement && firstControl.type === 'checkbox') {
|
||||
fields[name] = groupControls
|
||||
.filter(control => control.checked)
|
||||
.map(control => control.value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (firstControl instanceof HTMLInputElement && firstControl.type === 'radio') {
|
||||
const selectedControl = groupControls.find(control => control.checked);
|
||||
fields[name] = selectedControl ? selectedControl.value : '';
|
||||
return;
|
||||
}
|
||||
|
||||
fields[name] = firstControl.value;
|
||||
});
|
||||
|
||||
return { fields };
|
||||
}
|
||||
|
||||
function setBusy(form, busy) {
|
||||
const submitButton = form.querySelector('[data-form-submit]');
|
||||
const idleContent = form.querySelector('[data-form-submit-idle]');
|
||||
const busyContent = form.querySelector('[data-form-submit-busy]');
|
||||
|
||||
form.setAttribute('aria-busy', String(busy));
|
||||
if (submitButton) {
|
||||
submitButton.disabled = busy;
|
||||
submitButton.setAttribute('aria-busy', String(busy));
|
||||
}
|
||||
setVisible(idleContent, !busy);
|
||||
setVisible(busyContent, busy);
|
||||
}
|
||||
|
||||
function initializeCheckboxGroups() {
|
||||
document.querySelectorAll('[data-field-type="checkbox"][data-field-required="true"]')
|
||||
.forEach(updateCheckboxGroupRequired);
|
||||
}
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', initializeCheckboxGroups, { once: true });
|
||||
} else {
|
||||
initializeCheckboxGroups();
|
||||
}
|
||||
|
||||
document.addEventListener('change', event => {
|
||||
if (!(event.target instanceof HTMLInputElement) || event.target.type !== 'checkbox') {
|
||||
return;
|
||||
}
|
||||
|
||||
const group = event.target.closest('[data-field-type="checkbox"][data-field-required="true"]');
|
||||
if (group) {
|
||||
updateCheckboxGroupRequired(group);
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener('submit', async event => {
|
||||
const form = event.target;
|
||||
if (!(form instanceof HTMLFormElement) || !form.matches('[data-form-wrapper]')) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
if (form.dataset.submitting === 'true') {
|
||||
return;
|
||||
}
|
||||
|
||||
form.dataset.submitting = 'true';
|
||||
const formContainer = form.parentElement;
|
||||
const errorMessage = formContainer.querySelector('[data-form-error]');
|
||||
const successMessage = formContainer.querySelector('[data-form-success]');
|
||||
setVisible(errorMessage, false);
|
||||
if (errorMessage) {
|
||||
errorMessage.textContent = '';
|
||||
}
|
||||
setBusy(form, true);
|
||||
|
||||
try {
|
||||
const response = await fetch(`/ext--form/submit?id=${encodeURIComponent(form.dataset.formId)}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(collectFields(form))
|
||||
});
|
||||
const result = await response.json();
|
||||
if (!response.ok || !result || !result.success) {
|
||||
const errors = result && Array.isArray(result.errors) ? result.errors.join(' ') : '';
|
||||
throw new Error(errors || defaultErrorMessage);
|
||||
}
|
||||
|
||||
form.hidden = true;
|
||||
form.classList.add('hidden');
|
||||
setVisible(successMessage, true);
|
||||
} catch (error) {
|
||||
if (errorMessage) {
|
||||
errorMessage.textContent = error instanceof Error && error.message
|
||||
? error.message
|
||||
: defaultErrorMessage;
|
||||
setVisible(errorMessage, true);
|
||||
}
|
||||
} finally {
|
||||
setBusy(form, false);
|
||||
delete form.dataset.submitting;
|
||||
}
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user