From 5719ffdeb48894c36467e071db6ee8f7fa4d9ebc Mon Sep 17 00:00:00 2001 From: Hubbe <57641199+HubbeDev@users.noreply.github.com> Date: Sun, 7 Jan 2024 02:39:14 +0100 Subject: [PATCH] feat: add textarea & email support (#6) * Move form field to a seperate comp & started to work on textarea * More textarea work & default confirmation text * Fix linting --- .../src/lib/components/field/field.svelte | 71 +++++++++++++++++ .../src/lib/components/field/index.ts | 38 ++++++++++ .../src/lib/components/form/index.ts | 16 ++-- .../src/lib/components/index.ts | 2 +- .../src/lib/components/input/input.svelte | 8 ++ .../src/lib/components/root.svelte | 64 +++++++--------- .../src/lib/components/textarea/index.ts | 13 ++++ .../lib/components/textarea/textarea.svelte | 29 +++++++ .../components/validation/validation.svelte | 2 +- .../src/lib/internal/gf.ts | 76 +++++++++++++++++-- .../src/lib/internal/types.ts | 17 ++++- 11 files changed, 280 insertions(+), 56 deletions(-) create mode 100644 apps/svelte-gravity-forms/src/lib/components/field/field.svelte create mode 100644 apps/svelte-gravity-forms/src/lib/components/field/index.ts create mode 100644 apps/svelte-gravity-forms/src/lib/components/textarea/index.ts create mode 100644 apps/svelte-gravity-forms/src/lib/components/textarea/textarea.svelte diff --git a/apps/svelte-gravity-forms/src/lib/components/field/field.svelte b/apps/svelte-gravity-forms/src/lib/components/field/field.svelte new file mode 100644 index 0000000..02cb814 --- /dev/null +++ b/apps/svelte-gravity-forms/src/lib/components/field/field.svelte @@ -0,0 +1,71 @@ + + +
+ + + {#if label && showFieldLabel(labelPosition, 'above')} + + {label} + {#if isRequired} + {#if $formRequiredIndicator == 'asterisk'} + * + {:else if $formRequiredIndicator == 'text'} + (required) + {/if} + {/if} + + {/if} + + {#if description && showDescription(descriptionPosition, 'above')} + {description} + {/if} + + {#if type === 'text'} + + {:else if type === 'email'} + + {:else if type === 'textarea'} + + {/if} + + {#if description && showDescription(descriptionPosition, 'below')} + {description} + {/if} + + + + +
diff --git a/apps/svelte-gravity-forms/src/lib/components/field/index.ts b/apps/svelte-gravity-forms/src/lib/components/field/index.ts new file mode 100644 index 0000000..4a3a4e0 --- /dev/null +++ b/apps/svelte-gravity-forms/src/lib/components/field/index.ts @@ -0,0 +1,38 @@ +import Root from './field.svelte'; +import type { HTMLInputAttributes } from 'svelte/elements'; +import type { InputEvents } from '../input/index.js'; +import type { SuperForm } from 'sveltekit-superforms/client'; +import type { ZodValidation } from 'sveltekit-superforms'; +import type { AnyZodObject } from 'zod'; + +export type Form = { + schema: T; + form: SuperForm; +}; + +export type Arrayable = T | T[]; + +export type FormValidation = ZodValidation; + +type Props = HTMLInputAttributes & { + fieldId: number; + config: Form; + label?: string; + labelPosition?: string; + description?: string; + descriptionPosition?: string; + isRequired?: boolean; + defaultValue?: string; + columnSpan?: number; + placeholder?: string; + index?: number; + type?: string; +}; + +export { + Root, + type Props, + type InputEvents, + // + Root as FormField +}; diff --git a/apps/svelte-gravity-forms/src/lib/components/form/index.ts b/apps/svelte-gravity-forms/src/lib/components/form/index.ts index 6c6be64..20ab64d 100644 --- a/apps/svelte-gravity-forms/src/lib/components/form/index.ts +++ b/apps/svelte-gravity-forms/src/lib/components/form/index.ts @@ -1,13 +1,13 @@ -import { Form as FormPrimitive, getFormField } from 'formsnap'; +import { Form as FormPrimitive } from 'formsnap'; import * as RadioGroupComp from '$lib/components/ui/radio-group/index.js'; import * as SelectComp from '$lib/components/ui/select/index.js'; -import type { Writable } from 'svelte/store'; import { Item } from '$components/item/index.js'; import { Input } from '$components/input/index.js'; +import { Textarea } from '$components/textarea/index.js'; import { Description } from '$components/description/index.js'; import { Label } from '$components/label/index.js'; import { Validation } from '$components/validation/index.js'; - +import { FormField } from '$components/field/index.js'; import { Button } from '$components/button/index.js'; const Root = FormPrimitive.Root; @@ -21,10 +21,6 @@ const SelectGroup = SelectComp.Group; const SelectItem = SelectComp.Item; const SelectSeparator = SelectComp.Separator; -export type TextareaGetFormField = Omit, 'value'> & { - value: Writable; -}; - export type Props = { formId?: number; }; @@ -32,9 +28,11 @@ export type Props = { export { Root, Field, + FormField, Control, Item, Input, + Textarea, Label, Button, Validation, @@ -48,10 +46,12 @@ export { NativeRadio, // Root as Form, - Field as FormField, + Field as BitsField, + FormField as GFFormField, Control as FormControl, Item as FormItem, Input as FormInput, + Textarea as FormTextarea, Description as FormDescription, Label as FormLabel, Validation as FormValidation, diff --git a/apps/svelte-gravity-forms/src/lib/components/index.ts b/apps/svelte-gravity-forms/src/lib/components/index.ts index 921427e..95a96bf 100644 --- a/apps/svelte-gravity-forms/src/lib/components/index.ts +++ b/apps/svelte-gravity-forms/src/lib/components/index.ts @@ -1,3 +1,3 @@ import { default as GFButton } from './button/button.svelte'; -export { GFForm, GFButton }; +export { GFButton }; diff --git a/apps/svelte-gravity-forms/src/lib/components/input/input.svelte b/apps/svelte-gravity-forms/src/lib/components/input/input.svelte index 19f8017..7b61599 100644 --- a/apps/svelte-gravity-forms/src/lib/components/input/input.svelte +++ b/apps/svelte-gravity-forms/src/lib/components/input/input.svelte @@ -10,10 +10,18 @@ /* const { attrStore, value } = getFormField(); */ const { attrStore, value } = getFormField(); let className: $$Props['class'] = undefined; + export let type: $$Props['type'] = 'text'; + export { className as class }; + + function typeAction(node: HTMLInputElement) { + if (!type) return; + node.type = type; + } - {#if $formFields} - {#each $formFields as field, i} -
- - - {#if field.label && showFieldLabel(field.labelPlacement, 'above')} - - {field.label} - {#if field.isRequired} - {#if $formRequiredIndicator == 'asterisk'} - * - {:else if $formRequiredIndicator == 'text'} - (required) - {/if} - {/if} - - {#if field.description && field.descriptionPlacement == 'above'} - {field.description} - {/if} - - {#if field.label && showFieldLabel(field.labelPlacement, 'below')} - {field.description} - {/if} - {/if} - - - -
- {/each} + {#if $isSubmitted} +
+ {@html $defaultConfirmation.message} +
+ {:else} + {#if $formFields} + {#each $formFields as field, i} + + {/each} + {/if} + {/if} - {/if} diff --git a/apps/svelte-gravity-forms/src/lib/components/textarea/index.ts b/apps/svelte-gravity-forms/src/lib/components/textarea/index.ts new file mode 100644 index 0000000..13a6f86 --- /dev/null +++ b/apps/svelte-gravity-forms/src/lib/components/textarea/index.ts @@ -0,0 +1,13 @@ +import Root from './textarea.svelte'; +import { getFormField } from 'formsnap'; +import type { Writable } from 'svelte/store'; + +export type TextareaGetFormField = Omit, 'value'> & { + value: Writable; +}; + +export { + Root, + // + Root as Textarea +}; diff --git a/apps/svelte-gravity-forms/src/lib/components/textarea/textarea.svelte b/apps/svelte-gravity-forms/src/lib/components/textarea/textarea.svelte new file mode 100644 index 0000000..a88419c --- /dev/null +++ b/apps/svelte-gravity-forms/src/lib/components/textarea/textarea.svelte @@ -0,0 +1,29 @@ + + +