hugo-theme/layouts/shortcodes/form.html

97 lines
5.5 KiB
HTML
Raw Permalink Normal View History

2024-10-21 21:29:11 +02:00
<!-- layouts/shortcodes/contact-form.html -->
{{ $action := .Get "action" | default "" }}
{{ $method := .Get "method" | default "post" }}
{{ if not (.Page.Scratch.Get "recaptchaLoaded") }}
{{ with .Get "recaptcha_site_key" }}
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
{{ $.Page.Scratch.Set "recaptchaLoaded" true }}
{{ end }}
{{ end }}
2024-10-22 16:19:29 +02:00
{{ $seed := now.Unix }}
{{ $random := delimit (shuffle (split (md5 $seed) "" )) "" }}
2024-10-22 19:07:42 +02:00
<form action="{{ $action }}" method="{{ $method }}" id="form-{{ $random }}" class="space-y-4 dark:bg-gray-800 dark:text-gray-100 p-4" enctype="multipart/form-data" accept-charset="UTF-8">
2024-10-21 21:29:11 +02:00
{{ $fieldCount := 0 }}
{{ range $key, $value := .Params }}
{{ if hasPrefix $key "field" }}
{{ $fieldIndex := int (replaceRE "field(\\d+)_.*" "$1" $key) }}
{{ if gt $fieldIndex $fieldCount }}
{{ $fieldCount = $fieldIndex }}
{{ end }}
{{ end }}
{{ end }}
{{ range $i := seq 0 $fieldCount }}
{{ $fieldType := $.Get (printf "field%d_type" $i) }}
{{ $fieldName := $.Get (printf "field%d_name" $i) }}
{{ $fieldLabel := $.Get (printf "field%d_label" $i) }}
{{ $fieldRequired := $.Get (printf "field%d_required" $i) }}
{{ $fieldPlaceholder := $.Get (printf "field%d_placeholder" $i) }}
{{ $fieldOptions := $.Get (printf "field%d_options" $i) }}
{{ if and $fieldType $fieldName $fieldLabel }}
{{ if eq $fieldType "text" }}
<div>
<label for="{{ $fieldName }}" class="block text-sm font-medium text-gray-700 dark:text-gray-300">{{ $fieldLabel }}</label>
<input type="text" name="{{ $fieldName }}" id="{{ $fieldName }}" {{ if eq $fieldRequired "true" }}required{{ end }} {{ if $fieldPlaceholder }}placeholder="{{ $fieldPlaceholder }}"{{ end }} class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-indigo-500 dark:focus:border-indigo-500">
</div>
{{ else if eq $fieldType "email" }}
<div>
<label for="{{ $fieldName }}" class="block text-sm font-medium text-gray-700 dark:text-gray-300">{{ $fieldLabel }}</label>
<input type="email" name="{{ $fieldName }}" id="{{ $fieldName }}" {{ if eq $fieldRequired "true" }}required{{ end }} {{ if $fieldPlaceholder }}placeholder="{{ $fieldPlaceholder }}"{{ end }} class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-indigo-500 dark:focus:border-indigo-500">
</div>
{{ else if eq $fieldType "select" }}
<div>
<label for="{{ $fieldName }}" class="block text-sm font-medium text-gray-700 dark:text-gray-300">{{ $fieldLabel }}</label>
<select name="{{ $fieldName }}" id="{{ $fieldName }}" {{ if eq $fieldRequired "true" }}required{{ end }} class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-indigo-500 dark:focus:border-indigo-500">
{{ range split $fieldOptions "," }}
<option value="{{ . }}">{{ . }}</option>
{{ end }}
</select>
</div>
{{ else if eq $fieldType "textarea" }}
<div>
<label for="{{ $fieldName }}" class="block text-sm font-medium text-gray-700 dark:text-gray-300">{{ $fieldLabel }}</label>
<textarea name="{{ $fieldName }}" id="{{ $fieldName }}" {{ if eq $fieldRequired "true" }}required{{ end }} {{ if $fieldPlaceholder }}placeholder="{{ $fieldPlaceholder }}"{{ end }} rows="4" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-indigo-500 dark:focus:border-indigo-500"></textarea>
</div>
{{ end }}
{{ end }}
{{ end }}
<input type="text" name="honey_pot" style="display: none" class="hidden">
2024-10-22 16:19:29 +02:00
2024-10-22 16:55:42 +02:00
{{ with .Get "recaptcha_site_key" }}
<div class="g-recaptcha" data-sitekey="{{ . }}" data-callback="submitForm{{ $random }}" data-size="invisible"></div>
{{ end }}
2024-10-21 21:29:11 +02:00
<div>
2024-10-22 16:19:29 +02:00
<button
2024-10-22 16:55:42 +02:00
id="formSubmit-{{ $random }}"
2024-10-22 16:19:29 +02:00
class="inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 dark:bg-indigo-500 dark:hover:bg-indigo-600 dark:focus:ring-offset-gray-800" >
2024-10-21 21:29:11 +02:00
{{ .Get "submit_text" | default "Send" }}
</button>
</div>
2024-10-22 16:19:29 +02:00
</form>
<script type="text/javascript">
2024-10-22 16:55:42 +02:00
const recaptchaSiteKey = '{{.Get "recaptcha_site_key" }}';
window["submitForm{{ $random }}"] = (token) => {
const form = document.getElementById("form-{{ $random }}");
console.log("form submitted");
form.submit();
2024-10-22 16:19:29 +02:00
}
2024-10-22 16:55:42 +02:00
document.addEventListener("DOMContentLoaded", () => {
const form = document.getElementById("form-{{ $random }}");
const submit = document.getElementById("formSubmit-{{ $random }}");
submit.onclick = (e) => {
e.preventDefault();
if (form.reportValidity()) {
if (recaptchaSiteKey) grecaptcha.execute();
else window["submitForm{{ $random }}"]();
}
};
});
2024-10-22 16:19:29 +02:00
</script>