Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form action optional config #536

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/components/ui/Form.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,30 @@
import type { Form as Props } from '~/types';
import Button from '~/components/ui/Button.astro';

const { inputs, textarea, disclaimer, button = 'Contact us', description = '' } = Astro.props;
const { inputs, textarea, disclaimer, button = 'Contact us', description = '', form } = Astro.props;
---

<form>
<script is:inline>
function handleSubmit(e) {
if (!document.getElementById('disclaimer').checked) {
e.preventDefault();
alert('Please accept the terms.');
}
}

document.addEventListener('DOMContentLoaded', () => {
const formElement = document.querySelector('form');
if (formElement) {
formElement.addEventListener('submit', handleSubmit);
}
});
</script>

<form {...form}>
{
inputs &&
inputs.map(
({ type = 'text', name, label = '', autocomplete = 'on', placeholder = '' }) =>
({ type = 'text', name, label = '', autocomplete = 'on', placeholder = '', required }) =>
name && (
<div class="mb-6">
{label && (
Expand All @@ -23,6 +39,7 @@ const { inputs, textarea, disclaimer, button = 'Contact us', description = '' }
id={name}
autocomplete={autocomplete}
placeholder={placeholder}
required={required}
class="py-3 px-4 block w-full text-md rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-slate-900"
/>
</div>
Expand All @@ -41,6 +58,7 @@ const { inputs, textarea, disclaimer, button = 'Contact us', description = '' }
name={textarea.name ? textarea.name : 'message'}
rows={textarea.rows ? textarea.rows : 4}
placeholder={textarea.placeholder}
required={textarea.required}
class="py-3 px-4 block w-full text-md rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-slate-900"
/>
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/components/widgets/Contact.astro
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
disclaimer,
button,
description,
form,

id,
isDark = false,
Expand All @@ -33,6 +34,7 @@ const {
disclaimer={disclaimer}
button={button}
description={description}
form={form}
/>
</div>
)
Expand Down
12 changes: 12 additions & 0 deletions src/pages/contact.astro
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,34 @@ const metadata = {

<ContactUs
id="form"
form={{
action: '#',
name: 'formName',
id: 'formId',
method: 'POST',
enctype: 'multipart/form-data',
}}
title="Drop us a message today!"
subtitle="For quicker answers, explore our FAQs section. You may find the solution you're looking for right there! If not, our support team is delighted to help you."
inputs={[
{
type: 'text',
name: 'name',
label: 'Name',
placeholder: 'Your Name',
required: true,
},
{
type: 'email',
name: 'email',
label: 'Email',
placeholder: 'your@email.com',
required: true,
},
]}
textarea={{
label: 'Message',
required: false,
}}
disclaimer={{
label:
Expand Down
12 changes: 12 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,15 @@ export interface Input {
label?: string;
autocomplete?: string;
placeholder?: string;
required?: boolean;
}

export interface Textarea {
label?: string;
name?: string;
placeholder?: string;
rows?: number;
required?: boolean;
}

export interface Disclaimer {
Expand Down Expand Up @@ -210,6 +212,16 @@ export interface Form {
disclaimer?: Disclaimer;
button?: string;
description?: string;
form?: FormProps;
}

export interface FormProps {
action?: string;
class?: string;
name?: string;
id?: string;
method?: 'GET' | 'POST';
enctype?: string;
}

// WIDGETS
Expand Down