Skip to content

Commit

Permalink
Add MaybeProtectedForm to optionally skip turnstile
Browse files Browse the repository at this point in the history
Using `<svelte:component>` for this purpose caused a warning about an
unknown `turnstileToken` property when the component was Form rather
than ProtectedForm. To avoid this warning, we create a new component
that selects between Form and ProtectedForm, and sends turnstileToken
only to ProtectedForm.
  • Loading branch information
rmunn committed May 24, 2024
1 parent 5c40148 commit 43ae707
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
6 changes: 3 additions & 3 deletions frontend/src/lib/components/Users/CreateUser.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import PasswordStrengthMeter from '$lib/components/PasswordStrengthMeter.svelte';
import { SubmitButton, FormError, Input, Form, ProtectedForm, isEmail, lexSuperForm, passwordFormRules, DisplayLanguageSelect } from '$lib/forms';
import { SubmitButton, FormError, Input, MaybeProtectedForm, isEmail, lexSuperForm, passwordFormRules, DisplayLanguageSelect } from '$lib/forms';
import t, { getLanguageCodeFromNavigator, locale } from '$lib/i18n';
import { type RegisterResponse } from '$lib/user';
import { getSearchParamValues } from '$lib/util/query-params';
Expand Down Expand Up @@ -69,7 +69,7 @@
});
</script>

<svelte:component this={skipTurnstile ? Form : ProtectedForm} {enhance} bind:turnstileToken>
<MaybeProtectedForm {skipTurnstile} {enhance} bind:turnstileToken>
<Input autofocus id="name" label={$t('register.label_name')} bind:value={$form.name} error={$errors.name} />
<div class="contents email">
<Input
Expand All @@ -95,7 +95,7 @@
/>
<FormError error={$message} />
<SubmitButton loading={$submitting}>{submitButtonText}</SubmitButton>
</svelte:component>
</MaybeProtectedForm>

<style lang="postcss">
.email :global(.description) {
Expand Down
19 changes: 19 additions & 0 deletions frontend/src/lib/forms/MaybeProtectedForm.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script lang="ts">
import Form from './Form.svelte';
import ProtectedForm from './ProtectedForm.svelte';
import type { AnySuperForm } from './types';
export let enhance: AnySuperForm['enhance'] | undefined = undefined;
export let turnstileToken = '';
export let skipTurnstile = false;
</script>

{#if skipTurnstile}
<Form {enhance} on:submit>
<slot />
</Form>
{:else}
<ProtectedForm {enhance} on:submit bind:turnstileToken>
<slot />
</ProtectedForm>
{/if}
2 changes: 2 additions & 0 deletions frontend/src/lib/forms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Input from './Input.svelte';
import PlainInput from './PlainInput.svelte';
import Checkbox from './Checkbox.svelte';
import ProtectedForm, { type Token } from './ProtectedForm.svelte';
import MaybeProtectedForm from './MaybeProtectedForm.svelte';
import Select from './Select.svelte';
import TextArea from './TextArea.svelte';
import { lexSuperForm } from './superforms';
Expand All @@ -27,6 +28,7 @@ export {
Input,
PlainInput,
ProtectedForm,
MaybeProtectedForm,
Select,
TextArea,
lexSuperForm,
Expand Down

0 comments on commit 43ae707

Please sign in to comment.