@emmorts/formsnap 0.0.0-stage → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +115 -2
- package/dist/attrs.types.d.ts +138 -0
- package/dist/attrs.types.js +1 -0
- package/dist/components/control.svelte +34 -0
- package/dist/components/control.svelte.d.ts +22 -0
- package/dist/components/description.svelte +40 -0
- package/dist/components/description.svelte.d.ts +9 -0
- package/dist/components/element-field.svelte +37 -0
- package/dist/components/element-field.svelte.d.ts +34 -0
- package/dist/components/field-errors.svelte +55 -0
- package/dist/components/field-errors.svelte.d.ts +16 -0
- package/dist/components/field.svelte +37 -0
- package/dist/components/field.svelte.d.ts +34 -0
- package/dist/components/fieldset.svelte +79 -0
- package/dist/components/fieldset.svelte.d.ts +36 -0
- package/dist/components/index.d.ts +9 -0
- package/dist/components/index.js +8 -0
- package/dist/components/label.svelte +54 -0
- package/dist/components/label.svelte.d.ts +24 -0
- package/dist/components/legend.svelte +43 -0
- package/dist/components/legend.svelte.d.ts +12 -0
- package/dist/components/types.d.ts +137 -0
- package/dist/components/types.js +1 -0
- package/dist/formsnap.svelte.d.ts +199 -0
- package/dist/formsnap.svelte.js +339 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +2 -0
- package/dist/internal/create-context.d.ts +1 -0
- package/dist/internal/create-context.js +56 -0
- package/dist/internal/types.d.ts +12 -0
- package/dist/internal/types.js +1 -0
- package/dist/internal/utils/attributes.d.ts +32 -0
- package/dist/internal/utils/attributes.js +37 -0
- package/dist/internal/utils/errors.d.ts +5 -0
- package/dist/internal/utils/errors.js +12 -0
- package/dist/internal/utils/id.d.ts +4 -0
- package/dist/internal/utils/id.js +8 -0
- package/dist/internal/utils/index.d.ts +3 -0
- package/dist/internal/utils/index.js +3 -0
- package/dist/internal/utils/path.d.ts +1 -0
- package/dist/internal/utils/path.js +12 -0
- package/package.json +70 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Hunter Johnston <https://github.com/huntabyte>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,116 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Formsnap
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The goal of this library is to make working with the already incredible [sveltekit-superforms](https://github.com/ciscoheat/sveltekit-superforms) even more pleasant, by wrapping it with accessible form components.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i formsnap sveltekit-superforms <your-schema-library>
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
You'll handle the initial Superforms setup just as you normally would, where you define a schema and return the form from your load function.
|
|
14
|
+
|
|
15
|
+
#### 1. Define a Zod schema
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
// schemas.ts
|
|
19
|
+
import { z } from "zod";
|
|
20
|
+
export const settingsFormSchema = z.object({
|
|
21
|
+
email: z.string().email(),
|
|
22
|
+
bio: z.string().max(250).optional(),
|
|
23
|
+
marketingEmails: z.boolean().default(true),
|
|
24
|
+
language: z.enum(["en", "es", "fr"]).default(["en"]),
|
|
25
|
+
theme: z.enum(["light", "dark"]).default(["light"]),
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
#### 2. Return the form from your load function
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
// +page.server.ts
|
|
33
|
+
import { superValidate } from "sveltekit-superforms";
|
|
34
|
+
import { zod } from "sveltekit-superforms/adapters";
|
|
35
|
+
import type { PageServerLoad } from "./$types";
|
|
36
|
+
import { settingsFormSchema } from "./schemas";
|
|
37
|
+
|
|
38
|
+
export const load: PageServerLoad = async () => {
|
|
39
|
+
return {
|
|
40
|
+
form: await superValidate(zod(settingsFormSchema)),
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
#### 3. Construct the form in your page
|
|
46
|
+
|
|
47
|
+
```svelte
|
|
48
|
+
<script lang="ts">
|
|
49
|
+
import { Field, Label, FieldErrors, Control, Description, Fieldset, Legend } from "formsnap";
|
|
50
|
+
import { settingsFormSchema } from "./schemas";
|
|
51
|
+
import { superForm } from "sveltekit-superforms";
|
|
52
|
+
import { zodClient } from "sveltekit-superforms/adapters";
|
|
53
|
+
|
|
54
|
+
export let data;
|
|
55
|
+
|
|
56
|
+
const form = superForm(data.form, {
|
|
57
|
+
validators: zodClient(settingsFormSchema),
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const { form: formData, enhance } = form;
|
|
61
|
+
</script>
|
|
62
|
+
|
|
63
|
+
<form method="POST" use:enhance>
|
|
64
|
+
<Field {form} name="email">
|
|
65
|
+
<Control let:attrs>
|
|
66
|
+
<Label>Email</Label>
|
|
67
|
+
<input type="email" {...attrs} bind:value={$formData.email} />
|
|
68
|
+
</Control>
|
|
69
|
+
<Description>We'll provide critical updates about your account via email.</Description>
|
|
70
|
+
<FieldErrors />
|
|
71
|
+
</Field>
|
|
72
|
+
|
|
73
|
+
<Field {form} name="bio">
|
|
74
|
+
<Control let:attrs>
|
|
75
|
+
<Label>Bio</Label>
|
|
76
|
+
<textarea bind:value={$formData.bio} {...attrs} />
|
|
77
|
+
</Control>
|
|
78
|
+
<FieldErrors />
|
|
79
|
+
</Field>
|
|
80
|
+
|
|
81
|
+
<Field {form} name="language">
|
|
82
|
+
<Control let:attrs>
|
|
83
|
+
<Label>Language</Label>
|
|
84
|
+
<select {...attrs} bind:value={$formData.language}>
|
|
85
|
+
<option value="en">English</option>
|
|
86
|
+
<option value="es">Spanish</option>
|
|
87
|
+
<option value="fr">French</option>
|
|
88
|
+
</select>
|
|
89
|
+
</Control>
|
|
90
|
+
<FieldErrors />
|
|
91
|
+
</Field>
|
|
92
|
+
|
|
93
|
+
<Field {form} name="marketingEmails">
|
|
94
|
+
<Control let:attrs>
|
|
95
|
+
<Label>Receive marketing emails from us</Label>
|
|
96
|
+
<input type="checkbox" {...attrs} bind:checked={$formData.marketingEmails} />
|
|
97
|
+
</Control>
|
|
98
|
+
<FieldErrors />
|
|
99
|
+
</Field>
|
|
100
|
+
|
|
101
|
+
<Fieldset {form} name="theme">
|
|
102
|
+
<Legend>Select your theme</Legend>
|
|
103
|
+
{#each ["light", "dark"] as theme}
|
|
104
|
+
<Control let:attrs>
|
|
105
|
+
<input {...attrs} type="radio" bind:group={$formData.theme} value={theme} />
|
|
106
|
+
<Label>{theme}</Label>
|
|
107
|
+
</Control>
|
|
108
|
+
{/each}
|
|
109
|
+
<FieldErrors />
|
|
110
|
+
</Fieldset>
|
|
111
|
+
|
|
112
|
+
<button type="submit">Submit</button>
|
|
113
|
+
</form>
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Check out [Formsnap.dev](https://formsnap.dev) for more documentation.
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attributes provided via the props snippet prop of the
|
|
3
|
+
* [Control](https://formsnap.dev/docs/components/control) component to
|
|
4
|
+
* spread onto the form control element.
|
|
5
|
+
*
|
|
6
|
+
* @category Control
|
|
7
|
+
*
|
|
8
|
+
* @see {@link https://formsnap.dev/docs/components/control Control Documentation}
|
|
9
|
+
*/
|
|
10
|
+
export type ControlAttrs = {
|
|
11
|
+
/** The name of the control used for form submission. */
|
|
12
|
+
name: string;
|
|
13
|
+
/** The ID of the control, used for label association. */
|
|
14
|
+
id: string;
|
|
15
|
+
/** Present when a validation error exists on the field. */
|
|
16
|
+
"data-fs-error": string | undefined;
|
|
17
|
+
/** Present when description or validation exists. */
|
|
18
|
+
"aria-describedby": string | undefined;
|
|
19
|
+
/** Present when a validation error exists on the field. */
|
|
20
|
+
"aria-invalid": "true" | undefined;
|
|
21
|
+
/** Present when the field is required. */
|
|
22
|
+
"aria-required": "true" | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Used for selection during styling or otherwise
|
|
25
|
+
* @see {@link https://formsnap.dev/docs/styling Styling Documentation}
|
|
26
|
+
*/
|
|
27
|
+
"data-fs-control": string;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Attributes provided via the `child` snippet props of the
|
|
31
|
+
* [Label](https://formsnap.dev/docs/components/label) component to spread onto the label element.
|
|
32
|
+
*/
|
|
33
|
+
export type LabelAttrs = {
|
|
34
|
+
/** The ID of the control, used for label association. */
|
|
35
|
+
for: string;
|
|
36
|
+
/** Present when a validation error exists on the field. */
|
|
37
|
+
"data-fs-error": string | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Used for selection during styling or otherwise
|
|
40
|
+
* @see {@link https://formsnap.dev/docs/styling Styling Documentation}
|
|
41
|
+
*/
|
|
42
|
+
"data-fs-label": string;
|
|
43
|
+
/** Any additional props provided to the `<Form.Label />` component */
|
|
44
|
+
[key: string]: any;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Attributes provided via the `child` snippet props to spread onto the field error
|
|
48
|
+
* container element.
|
|
49
|
+
*/
|
|
50
|
+
export type FieldErrorsAttrs = {
|
|
51
|
+
/** The ID of the validation element, used to describe the control. */
|
|
52
|
+
id: string;
|
|
53
|
+
/** Present when a validation error exists on the field. */
|
|
54
|
+
"data-fs-error": string | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* Used for selection during styling or otherwise
|
|
57
|
+
* @see {@link https://formsnap.dev/docs/styling Styling Documentation}
|
|
58
|
+
*/
|
|
59
|
+
"data-fs-field-errors": string;
|
|
60
|
+
/** Notifies screen readers when a validation error occurs */
|
|
61
|
+
"aria-live": "assertive" | "polite";
|
|
62
|
+
/** Any additional props provided to `<Form.Validation />` */
|
|
63
|
+
[key: string]: any;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Attributes provided via the snippet props to be spread onto the individual error elements.
|
|
67
|
+
*/
|
|
68
|
+
export type ErrorAttrs = {
|
|
69
|
+
/**
|
|
70
|
+
* Used for selection during styling or otherwise
|
|
71
|
+
* @see {@link https://formsnap.dev/docs/styling Styling Documentation}
|
|
72
|
+
*/
|
|
73
|
+
"data-fs-field-error": string;
|
|
74
|
+
/** Present when a validation error exists on the field. */
|
|
75
|
+
"data-fs-error": string | undefined;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Attributes provided via the {@link LegendSlotProps legendAttrs} slot prop of
|
|
79
|
+
* the [Legend](https://formsnap.dev/docs/components/legend) component to be spread
|
|
80
|
+
* onto the legend element when used with the {@link LegendProps asChild} prop.
|
|
81
|
+
*
|
|
82
|
+
* @category Legend
|
|
83
|
+
*
|
|
84
|
+
* @see {@link https://formsnap.dev/docs/composition/aschild asChild Documentation}
|
|
85
|
+
* @see {@link https://formsnap.dev/docs/components/legend Legend Documentation}
|
|
86
|
+
*/
|
|
87
|
+
export type LegendAttrs = {
|
|
88
|
+
/**
|
|
89
|
+
* Used for selection during styling or otherwise
|
|
90
|
+
* @see {@link https://formsnap.dev/docs/styling Styling Documentation}
|
|
91
|
+
*/
|
|
92
|
+
"data-fs-legend": string;
|
|
93
|
+
/** Present when a validation error exists on the field. */
|
|
94
|
+
"data-fs-error": string | undefined;
|
|
95
|
+
[key: string]: any;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Attributes provided via the {@link DescriptionSlotProps descriptionAttrs} slot prop
|
|
99
|
+
* of the [Description](https://formsnap.dev/docs/components/description) component to be
|
|
100
|
+
* spread onto the description element when used with the {@link DescriptionProps asChild} prop.
|
|
101
|
+
*
|
|
102
|
+
* @category Description
|
|
103
|
+
*
|
|
104
|
+
* @see {@link https://formsnap.dev/docs/composition/aschild asChild Documentation}
|
|
105
|
+
* @see {@link https://formsnap.dev/docs/components/description Description Documentation}
|
|
106
|
+
*/
|
|
107
|
+
export type DescriptionAttrs = {
|
|
108
|
+
/** The ID of the description element, used to describe the control. */
|
|
109
|
+
id: string;
|
|
110
|
+
/**
|
|
111
|
+
* Used for selection during styling or otherwise
|
|
112
|
+
* @see {@link https://formsnap.dev/docs/styling Styling Documentation}
|
|
113
|
+
*/
|
|
114
|
+
"data-fs-description": string;
|
|
115
|
+
/** Present when a validation error exists on the field. */
|
|
116
|
+
"data-fs-error": string | undefined;
|
|
117
|
+
[key: string]: any;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Attributes provided via the {@link FieldsetSlotProps props} snippet prop
|
|
121
|
+
* of the [Fieldset](https://formsnap.dev/docs/components/fieldset) component to be
|
|
122
|
+
* spread onto the `<fieldset>` element when used with the {@link FieldsetProps child} snippet.
|
|
123
|
+
*
|
|
124
|
+
* @category Fieldset
|
|
125
|
+
*
|
|
126
|
+
* @see {@link https://formsnap.dev/docs/composition/aschild asChild Documentation}
|
|
127
|
+
* @see {@link https://formsnap.dev/docs/components/fieldset Fieldset Documentation}
|
|
128
|
+
*/
|
|
129
|
+
export type FieldsetAttrs = {
|
|
130
|
+
/**
|
|
131
|
+
* Used for selection during styling or otherwise
|
|
132
|
+
* @see {@link https://formsnap.dev/docs/styling Styling Documentation}
|
|
133
|
+
*/
|
|
134
|
+
"data-fs-fieldset": string;
|
|
135
|
+
/** Present when a validation error exists on the field. */
|
|
136
|
+
"data-fs-error": string | undefined;
|
|
137
|
+
[key: string]: any;
|
|
138
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { box } from "svelte-toolbelt";
|
|
3
|
+
import type { ControlProps } from "./types.js";
|
|
4
|
+
import { useId } from "../internal/utils/index.js";
|
|
5
|
+
import { useControl } from "../formsnap.svelte.js";
|
|
6
|
+
|
|
7
|
+
let { id = useId(), children }: ControlProps = $props();
|
|
8
|
+
|
|
9
|
+
const controlState = useControl({
|
|
10
|
+
id: box.with(() => id),
|
|
11
|
+
});
|
|
12
|
+
</script>
|
|
13
|
+
|
|
14
|
+
<!--
|
|
15
|
+
@component
|
|
16
|
+
## Control
|
|
17
|
+
Associates a [Label](https://formsnap.dev/docs/components/label) with and provides necessary attributes for a form control.
|
|
18
|
+
|
|
19
|
+
- [Control Documentation](https://formsnap.dev/docs/components/label)
|
|
20
|
+
|
|
21
|
+
@example
|
|
22
|
+
```svelte
|
|
23
|
+
<Control>
|
|
24
|
+
{#snippet children({ props })}
|
|
25
|
+
<Label>Name</Label>
|
|
26
|
+
<input type="text" {...props} bind:value={$formData.name} />
|
|
27
|
+
{/snippet}
|
|
28
|
+
</Control>
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Snippet Props
|
|
32
|
+
- `props` - A spreadable object of attributes that must be applied to the form control element.
|
|
33
|
+
-->
|
|
34
|
+
{@render children?.({ props: controlState.props })}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ControlProps } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* ## Control
|
|
4
|
+
* Associates a [Label](https://formsnap.dev/docs/components/label) with and provides necessary attributes for a form control.
|
|
5
|
+
*
|
|
6
|
+
* - [Control Documentation](https://formsnap.dev/docs/components/label)
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```svelte
|
|
10
|
+
* <Control>
|
|
11
|
+
* {#snippet children({ props })}
|
|
12
|
+
* <Label>Name</Label>
|
|
13
|
+
* <input type="text" {...props} bind:value={$formData.name} />
|
|
14
|
+
* {/snippet}
|
|
15
|
+
* </Control>
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* ### Snippet Props
|
|
19
|
+
* - `props` - A spreadable object of attributes that must be applied to the form control element.
|
|
20
|
+
*/
|
|
21
|
+
declare const Control: import("svelte").Component<ControlProps, {}, "">;
|
|
22
|
+
export default Control;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { box, mergeProps } from "svelte-toolbelt";
|
|
3
|
+
import type { DescriptionProps } from "./types.js";
|
|
4
|
+
import { useId } from "../internal/utils/index.js";
|
|
5
|
+
import { useDescription } from "../formsnap.svelte.js";
|
|
6
|
+
|
|
7
|
+
let {
|
|
8
|
+
id = useId(),
|
|
9
|
+
ref = $bindable(null),
|
|
10
|
+
children,
|
|
11
|
+
child,
|
|
12
|
+
...restProps
|
|
13
|
+
}: DescriptionProps = $props();
|
|
14
|
+
|
|
15
|
+
const descriptionState = useDescription({
|
|
16
|
+
id: box.with(() => id),
|
|
17
|
+
ref: box.with(
|
|
18
|
+
() => ref,
|
|
19
|
+
(v) => (ref = v)
|
|
20
|
+
),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const mergedProps = $derived(mergeProps(restProps, descriptionState.props));
|
|
24
|
+
</script>
|
|
25
|
+
|
|
26
|
+
<!--
|
|
27
|
+
@component
|
|
28
|
+
## Description
|
|
29
|
+
A component that provides additional context for a field and is associated with the control using the `aria-describedby` attribute.
|
|
30
|
+
|
|
31
|
+
- [Description Documentation](https://formsnap.dev/docs/components/description)
|
|
32
|
+
-->
|
|
33
|
+
|
|
34
|
+
{#if child}
|
|
35
|
+
{@render child({ props: mergedProps })}
|
|
36
|
+
{:else}
|
|
37
|
+
<div {...mergedProps}>
|
|
38
|
+
{@render children?.()}
|
|
39
|
+
</div>
|
|
40
|
+
{/if}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { DescriptionProps } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* ## Description
|
|
4
|
+
* A component that provides additional context for a field and is associated with the control using the `aria-describedby` attribute.
|
|
5
|
+
*
|
|
6
|
+
* - [Description Documentation](https://formsnap.dev/docs/components/description)
|
|
7
|
+
*/
|
|
8
|
+
declare const Description: import("svelte").Component<DescriptionProps, {}, "ref">;
|
|
9
|
+
export default Description;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
<script lang="ts" module>
|
|
2
|
+
import type { FormPathLeaves as _FormPathLeaves } from "sveltekit-superforms";
|
|
3
|
+
type T = unknown;
|
|
4
|
+
type U = unknown;
|
|
5
|
+
</script>
|
|
6
|
+
|
|
7
|
+
<script lang="ts" generics="T extends Record<string, unknown>, U extends _FormPathLeaves<T>">
|
|
8
|
+
import { box } from "svelte-toolbelt";
|
|
9
|
+
import type { ElementFieldProps } from "./types.js";
|
|
10
|
+
import { useElementField } from "../formsnap.svelte.js";
|
|
11
|
+
|
|
12
|
+
let { form, name, children }: ElementFieldProps<T, U> = $props();
|
|
13
|
+
|
|
14
|
+
const elementFieldState = useElementField({
|
|
15
|
+
form: box.with(() => form),
|
|
16
|
+
name: box.with(() => name),
|
|
17
|
+
});
|
|
18
|
+
</script>
|
|
19
|
+
|
|
20
|
+
<!--
|
|
21
|
+
@component
|
|
22
|
+
## ElementField
|
|
23
|
+
A component that provides the necessary context for a form field that represents a single element in an array.
|
|
24
|
+
|
|
25
|
+
- [ElementField Documentation](https://formsnap.dev/docs/components/element-field)
|
|
26
|
+
|
|
27
|
+
### Snippet Props
|
|
28
|
+
- `value` - The value of the field.
|
|
29
|
+
- `errors` - The errors of the field.
|
|
30
|
+
- `tainted` - The tainted state of the field.
|
|
31
|
+
- `constraints` - The constraints of the field.
|
|
32
|
+
|
|
33
|
+
@param {SuperForm} form - The form object.
|
|
34
|
+
@param {FormPathLeaves<T>} name - The name and index of the field. For example, `urls[0]`.
|
|
35
|
+
-->
|
|
36
|
+
|
|
37
|
+
{@render children?.(elementFieldState.snippetProps)}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { FormPathLeaves as _FormPathLeaves } from "sveltekit-superforms";
|
|
2
|
+
import type { ElementFieldProps } from "./types.js";
|
|
3
|
+
declare class __sveltets_Render<T extends Record<string, unknown>, U extends _FormPathLeaves<T>> {
|
|
4
|
+
props(): ElementFieldProps<T, U>;
|
|
5
|
+
events(): {};
|
|
6
|
+
slots(): {};
|
|
7
|
+
bindings(): "";
|
|
8
|
+
exports(): {};
|
|
9
|
+
}
|
|
10
|
+
interface $$IsomorphicComponent {
|
|
11
|
+
new <T extends Record<string, unknown>, U extends _FormPathLeaves<T>>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<T, U>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<T, U>['props']>, ReturnType<__sveltets_Render<T, U>['events']>, ReturnType<__sveltets_Render<T, U>['slots']>> & {
|
|
12
|
+
$$bindings?: ReturnType<__sveltets_Render<T, U>['bindings']>;
|
|
13
|
+
} & ReturnType<__sveltets_Render<T, U>['exports']>;
|
|
14
|
+
<T extends Record<string, unknown>, U extends _FormPathLeaves<T>>(internal: unknown, props: ReturnType<__sveltets_Render<T, U>['props']> & {}): ReturnType<__sveltets_Render<T, U>['exports']>;
|
|
15
|
+
z_$$bindings?: ReturnType<__sveltets_Render<any, any>['bindings']>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* ## ElementField
|
|
19
|
+
* A component that provides the necessary context for a form field that represents a single element in an array.
|
|
20
|
+
*
|
|
21
|
+
* - [ElementField Documentation](https://formsnap.dev/docs/components/element-field)
|
|
22
|
+
*
|
|
23
|
+
* ### Snippet Props
|
|
24
|
+
* - `value` - The value of the field.
|
|
25
|
+
* - `errors` - The errors of the field.
|
|
26
|
+
* - `tainted` - The tainted state of the field.
|
|
27
|
+
* - `constraints` - The constraints of the field.
|
|
28
|
+
*
|
|
29
|
+
* @param {SuperForm} form - The form object.
|
|
30
|
+
* @param {FormPathLeaves<T>} name - The name and index of the field. For example, `urls[0]`.
|
|
31
|
+
*/
|
|
32
|
+
declare const ElementField: $$IsomorphicComponent;
|
|
33
|
+
type ElementField<T extends Record<string, unknown>, U extends _FormPathLeaves<T>> = InstanceType<typeof ElementField<T, U>>;
|
|
34
|
+
export default ElementField;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { box, mergeProps } from "svelte-toolbelt";
|
|
3
|
+
import type { FieldErrorsProps } from "./types.js";
|
|
4
|
+
import { useId } from "../internal/utils/index.js";
|
|
5
|
+
import { useFieldErrors } from "../formsnap.svelte.js";
|
|
6
|
+
|
|
7
|
+
let {
|
|
8
|
+
id = useId(),
|
|
9
|
+
ref = $bindable(null),
|
|
10
|
+
children,
|
|
11
|
+
child,
|
|
12
|
+
...restProps
|
|
13
|
+
}: FieldErrorsProps = $props();
|
|
14
|
+
|
|
15
|
+
const fieldErrorsState = useFieldErrors({
|
|
16
|
+
id: box.with(() => id),
|
|
17
|
+
ref: box.with(
|
|
18
|
+
() => ref,
|
|
19
|
+
(v) => (ref = v)
|
|
20
|
+
),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const mergedProps = $derived(mergeProps(restProps, fieldErrorsState.fieldErrorsProps));
|
|
24
|
+
</script>
|
|
25
|
+
|
|
26
|
+
<!--
|
|
27
|
+
@component
|
|
28
|
+
## FieldErrors
|
|
29
|
+
A component that renders the container for validation errors for a [Field](https://formsnap.dev/docs/components/field), [Fieldset](https://formsnap.dev/docs/components/fieldset), or [ElementField](https://formsnap.dev/docs/components/element-field).
|
|
30
|
+
|
|
31
|
+
- [FieldErrors Documentation](https://formsnap.dev/docs/components/field-errors)
|
|
32
|
+
|
|
33
|
+
### Snippet Props
|
|
34
|
+
- `errors` - An array of errors for the associated field.
|
|
35
|
+
- `fieldErrorsAttrs` - A spreadable object of attributes for the container element if `child` snippet is used.
|
|
36
|
+
- `errorAttrs` - A spreadable object of attributes for the individual error elements if `child` snippet is used.
|
|
37
|
+
|
|
38
|
+
@param {string} [id] - The id of the field errors container.
|
|
39
|
+
-->
|
|
40
|
+
{#if child}
|
|
41
|
+
{@render child({
|
|
42
|
+
props: mergedProps,
|
|
43
|
+
...fieldErrorsState.snippetProps,
|
|
44
|
+
})}
|
|
45
|
+
{:else}
|
|
46
|
+
<div {...mergedProps}>
|
|
47
|
+
{#if children}
|
|
48
|
+
{@render children(fieldErrorsState.snippetProps)}
|
|
49
|
+
{:else}
|
|
50
|
+
{#each fieldErrorsState.field.errors as error}
|
|
51
|
+
<div {...fieldErrorsState.errorProps}>{error}</div>
|
|
52
|
+
{/each}
|
|
53
|
+
{/if}
|
|
54
|
+
</div>
|
|
55
|
+
{/if}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { FieldErrorsProps } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* ## FieldErrors
|
|
4
|
+
* A component that renders the container for validation errors for a [Field](https://formsnap.dev/docs/components/field), [Fieldset](https://formsnap.dev/docs/components/fieldset), or [ElementField](https://formsnap.dev/docs/components/element-field).
|
|
5
|
+
*
|
|
6
|
+
* - [FieldErrors Documentation](https://formsnap.dev/docs/components/field-errors)
|
|
7
|
+
*
|
|
8
|
+
* ### Snippet Props
|
|
9
|
+
* - `errors` - An array of errors for the associated field.
|
|
10
|
+
* - `fieldErrorsAttrs` - A spreadable object of attributes for the container element if `child` snippet is used.
|
|
11
|
+
* - `errorAttrs` - A spreadable object of attributes for the individual error elements if `child` snippet is used.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} [id] - The id of the field errors container.
|
|
14
|
+
*/
|
|
15
|
+
declare const FieldErrors: import("svelte").Component<FieldErrorsProps, {}, "ref">;
|
|
16
|
+
export default FieldErrors;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
<script lang="ts" module>
|
|
2
|
+
import type { FormPath as _FormPath } from "sveltekit-superforms";
|
|
3
|
+
type T = unknown;
|
|
4
|
+
type U = unknown;
|
|
5
|
+
</script>
|
|
6
|
+
|
|
7
|
+
<script lang="ts" generics="T extends Record<string, unknown>, U extends _FormPath<T>">
|
|
8
|
+
import { box } from "svelte-toolbelt";
|
|
9
|
+
import type { FieldProps } from "./types.js";
|
|
10
|
+
import { useField } from "../formsnap.svelte.js";
|
|
11
|
+
|
|
12
|
+
let { form, name, children }: FieldProps<T, U> = $props();
|
|
13
|
+
|
|
14
|
+
const fieldState = useField({
|
|
15
|
+
form: box.with(() => form),
|
|
16
|
+
name: box.with(() => name),
|
|
17
|
+
});
|
|
18
|
+
</script>
|
|
19
|
+
|
|
20
|
+
<!--
|
|
21
|
+
@component
|
|
22
|
+
## Field
|
|
23
|
+
A component that provides the necessary context for a form field.
|
|
24
|
+
|
|
25
|
+
- [Field Documentation](https://formsnap.dev/docs/components/field)
|
|
26
|
+
|
|
27
|
+
### Snippet Props
|
|
28
|
+
- `value` - The value of the field.
|
|
29
|
+
- `errors` - The errors of the field.
|
|
30
|
+
- `tainted` - The tainted state of the field.
|
|
31
|
+
- `constraints` - The constraints of the field.
|
|
32
|
+
|
|
33
|
+
@param {SuperForm} form - The form object.
|
|
34
|
+
@param {FormPath<T>} name - The name of the field.
|
|
35
|
+
-->
|
|
36
|
+
|
|
37
|
+
{@render children?.(fieldState.snippetProps)}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { FormPath as _FormPath } from "sveltekit-superforms";
|
|
2
|
+
import type { FieldProps } from "./types.js";
|
|
3
|
+
declare class __sveltets_Render<T extends Record<string, unknown>, U extends _FormPath<T>> {
|
|
4
|
+
props(): FieldProps<T, U>;
|
|
5
|
+
events(): {};
|
|
6
|
+
slots(): {};
|
|
7
|
+
bindings(): "";
|
|
8
|
+
exports(): {};
|
|
9
|
+
}
|
|
10
|
+
interface $$IsomorphicComponent {
|
|
11
|
+
new <T extends Record<string, unknown>, U extends _FormPath<T>>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<T, U>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<T, U>['props']>, ReturnType<__sveltets_Render<T, U>['events']>, ReturnType<__sveltets_Render<T, U>['slots']>> & {
|
|
12
|
+
$$bindings?: ReturnType<__sveltets_Render<T, U>['bindings']>;
|
|
13
|
+
} & ReturnType<__sveltets_Render<T, U>['exports']>;
|
|
14
|
+
<T extends Record<string, unknown>, U extends _FormPath<T>>(internal: unknown, props: ReturnType<__sveltets_Render<T, U>['props']> & {}): ReturnType<__sveltets_Render<T, U>['exports']>;
|
|
15
|
+
z_$$bindings?: ReturnType<__sveltets_Render<any, any>['bindings']>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* ## Field
|
|
19
|
+
* A component that provides the necessary context for a form field.
|
|
20
|
+
*
|
|
21
|
+
* - [Field Documentation](https://formsnap.dev/docs/components/field)
|
|
22
|
+
*
|
|
23
|
+
* ### Snippet Props
|
|
24
|
+
* - `value` - The value of the field.
|
|
25
|
+
* - `errors` - The errors of the field.
|
|
26
|
+
* - `tainted` - The tainted state of the field.
|
|
27
|
+
* - `constraints` - The constraints of the field.
|
|
28
|
+
*
|
|
29
|
+
* @param {SuperForm} form - The form object.
|
|
30
|
+
* @param {FormPath<T>} name - The name of the field.
|
|
31
|
+
*/
|
|
32
|
+
declare const Field: $$IsomorphicComponent;
|
|
33
|
+
type Field<T extends Record<string, unknown>, U extends _FormPath<T>> = InstanceType<typeof Field<T, U>>;
|
|
34
|
+
export default Field;
|