@privaty/ui-forms 0.1.0
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 +202 -0
- package/NOTICE +2 -0
- package/README.md +107 -0
- package/dist/components/form-error.svelte +49 -0
- package/dist/components/form-error.svelte.d.ts +15 -0
- package/dist/components/reset.svelte +49 -0
- package/dist/components/reset.svelte.d.ts +19 -0
- package/dist/components/submit.svelte +82 -0
- package/dist/components/submit.svelte.d.ts +27 -0
- package/dist/context.d.ts +23 -0
- package/dist/context.js +21 -0
- package/dist/form-state.svelte.d.ts +89 -0
- package/dist/form-state.svelte.js +139 -0
- package/dist/form.svelte +219 -0
- package/dist/form.svelte.d.ts +62 -0
- package/dist/inputs/checkbox-input.svelte +119 -0
- package/dist/inputs/checkbox-input.svelte.d.ts +45 -0
- package/dist/inputs/date-input.svelte +132 -0
- package/dist/inputs/date-input.svelte.d.ts +61 -0
- package/dist/inputs/number-input.svelte +141 -0
- package/dist/inputs/number-input.svelte.d.ts +63 -0
- package/dist/inputs/select-input.svelte +156 -0
- package/dist/inputs/select-input.svelte.d.ts +57 -0
- package/dist/inputs/text-input.svelte +126 -0
- package/dist/inputs/text-input.svelte.d.ts +59 -0
- package/dist/inputs/textarea-input.svelte +135 -0
- package/dist/inputs/textarea-input.svelte.d.ts +57 -0
- package/dist/inputs/wire-field.d.ts +44 -0
- package/dist/inputs/wire-field.js +45 -0
- package/dist/testing/fakes.svelte.d.ts +205 -0
- package/dist/testing/fakes.svelte.js +346 -0
- package/dist/types/field.d.ts +156 -0
- package/dist/types/field.js +1 -0
- package/dist/types/form.d.ts +27 -0
- package/dist/types/form.js +1 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Privaty
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# @privaty/ui-forms
|
|
2
|
+
|
|
3
|
+
Form components built on **SvelteKit remote functions** (experimental):
|
|
4
|
+
`Form` wraps a `form(schema, handler)` instance, inputs bind to its fields,
|
|
5
|
+
and a small `FormState` layer adds the display semantics Kit doesn't ship —
|
|
6
|
+
value-based dirty tracking, touch-gated error display, submit gating.
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pnpm add @privaty/ui @privaty/ui-forms
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
> Requires `@privaty/ui` as a **peerDependency** at the same lockstep version
|
|
13
|
+
> (single instance — Symbol-keyed contexts), `@sveltejs/kit` with
|
|
14
|
+
> `experimental.remoteFunctions` + `compilerOptions.experimental.async`, and
|
|
15
|
+
> Tailwind v4 (`@source` the package, see the core README).
|
|
16
|
+
|
|
17
|
+
## Quickstart
|
|
18
|
+
|
|
19
|
+
```svelte
|
|
20
|
+
<script>
|
|
21
|
+
import Form from "@privaty/ui-forms/form.svelte";
|
|
22
|
+
import TextInput from "@privaty/ui-forms/inputs/text-input.svelte";
|
|
23
|
+
import Submit from "@privaty/ui-forms/components/submit.svelte";
|
|
24
|
+
import FormError from "@privaty/ui-forms/components/form-error.svelte";
|
|
25
|
+
import { createItem } from "./data.remote";
|
|
26
|
+
import { createItemSchema } from "./schema";
|
|
27
|
+
</script>
|
|
28
|
+
|
|
29
|
+
<Form form={createItem} schema={createItemSchema}>
|
|
30
|
+
<TextInput field={createItem.fields.name} label="Name" required />
|
|
31
|
+
<FormError />
|
|
32
|
+
<Submit />
|
|
33
|
+
</Form>
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Inputs: `TextInput` (text/email/password/search/url/tel), `TextareaInput`,
|
|
37
|
+
`NumberInput`, `DateInput` (date/month/week/time/datetime-local — one
|
|
38
|
+
component, all string-valued), `SelectInput`, `CheckboxInput`. Plus
|
|
39
|
+
`Submit`, `Reset`, `FormError`. Icon-style Submit/Reset: pass children; the
|
|
40
|
+
label stays as the accessible name.
|
|
41
|
+
|
|
42
|
+
## The validation model
|
|
43
|
+
|
|
44
|
+
- **With `schema`** (a Standard Schema, e.g. valibot): validation runs
|
|
45
|
+
client-side via Kit's `preflight` — immediately on input, and on submit.
|
|
46
|
+
Kit swallows invalid submits before the enhance callback runs; the Form's
|
|
47
|
+
own submit listener still opens the error gates, so a rejected click shows
|
|
48
|
+
its issues.
|
|
49
|
+
- **Without `schema`**: every validation is a server round-trip — typing is
|
|
50
|
+
debounced (`validationDebounce`, default 400 ms), submits validate
|
|
51
|
+
server-side. Transform schemas (Output ≠ Input) are accepted.
|
|
52
|
+
- Issues per field appear once the field is touched or a submit was
|
|
53
|
+
attempted; `FormError` shows path-less issues and submit failures
|
|
54
|
+
(`labels.form.generalError`).
|
|
55
|
+
- **Server issues** (a rejected submission, a server validation round-trip)
|
|
56
|
+
are persisted by Kit through every client-side validation pass — no edit
|
|
57
|
+
can refresh them, only another round-trip. The Form handles both
|
|
58
|
+
consequences: a schema'd resubmission is never gated on them (the
|
|
59
|
+
submission re-judges them authoritatively), and while they linger, input
|
|
60
|
+
revalidation escalates to full validation — client schema first, then the
|
|
61
|
+
server round-trip that replaces the whole issue set — debounced like
|
|
62
|
+
schema-less typing. Rules that depend on server data (a cross-field cap, a
|
|
63
|
+
uniqueness check) therefore belong in the **server schema** (an async check
|
|
64
|
+
is fine) if they should refresh live while the user edits; issues raised in
|
|
65
|
+
the _handler_ (`invalid()`) clear optimistically on the next round-trip and
|
|
66
|
+
are re-judged at submit.
|
|
67
|
+
|
|
68
|
+
## Schema recipes (the footguns)
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
// Checkboxes: unchecked submits NOTHING — the schema supplies the false.
|
|
72
|
+
inStock: v.optional(v.boolean(), false),
|
|
73
|
+
|
|
74
|
+
// Placeholder selects: the disabled placeholder is skipped by submission —
|
|
75
|
+
// default "" so YOUR message fires instead of a raw missing-key error.
|
|
76
|
+
category: v.pipe(v.optional(v.string(), ""), v.picklist(categories, "required")),
|
|
77
|
+
|
|
78
|
+
// Month inputs submit "YYYY-MM"; empty submits "".
|
|
79
|
+
availableFrom: v.pipe(v.string(), v.nonEmpty("required"), v.regex(/^\d{4}-\d{2}$/, "invalid-month")),
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Rules learned the hard way
|
|
83
|
+
|
|
84
|
+
- **Never disable controls while submitting** — disabled controls are
|
|
85
|
+
excluded from `FormData`, and Kit reads live form data mid-submission. The
|
|
86
|
+
inputs lock via `readonly` / pointer+keyboard locks instead. Keep that
|
|
87
|
+
contract in custom inputs.
|
|
88
|
+
- **Queries: `await getItems()` bare.** `query().current` never
|
|
89
|
+
server-renders (hard-coded `undefined` on the server), and a
|
|
90
|
+
`<svelte:boundary>` with a `pending` snippet makes the server render the
|
|
91
|
+
snippet INSTEAD of the children. Await, no pending boundary.
|
|
92
|
+
- Required/optional markers follow the majority rule (the minority gets
|
|
93
|
+
marked) and wait for `FormState.settled` — they cannot be SSR'd with
|
|
94
|
+
self-registering fields.
|
|
95
|
+
- No-JS submissions are out of scope for v1 (Submit renders its gate state
|
|
96
|
+
into SSR HTML).
|
|
97
|
+
- Browser support: Firefox has no `type="month"`/`"week"` pickers (falls
|
|
98
|
+
back to a text input) — a library-owned cross-browser date picker is
|
|
99
|
+
planned post-v1.
|
|
100
|
+
|
|
101
|
+
## Testing
|
|
102
|
+
|
|
103
|
+
`testing/fakes.svelte.ts` ships Kit-faithful fakes: `fakeRemoteForm`
|
|
104
|
+
preflight-gates its enhance callback like Kit does, and the field fakes'
|
|
105
|
+
`edit()` stores raw DOM values (`"5"`, `"on"`) while `set()` stores typed
|
|
106
|
+
ones — mirroring Kit's mid-edit behavior so dirty-tracking tests mean
|
|
107
|
+
something.
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
<!-- @component
|
|
2
|
+
Form-level error list — must render inside a <Form> (throws otherwise).
|
|
3
|
+
Shows the configured `labels.form.generalError` line when a submission threw,
|
|
4
|
+
plus every validation issue without a path (e.g. from `invalid("...")` in the
|
|
5
|
+
handler), translated through the configured `resolveMessage`; field-level
|
|
6
|
+
issues render at their inputs instead. Renders nothing while there are no
|
|
7
|
+
messages.
|
|
8
|
+
-->
|
|
9
|
+
<script lang="ts">
|
|
10
|
+
import { cn } from "@privaty/ui/cn.js";
|
|
11
|
+
import { getUiConfig } from "@privaty/ui/config/context.js";
|
|
12
|
+
import { getFormContext } from "../context";
|
|
13
|
+
|
|
14
|
+
interface Props {
|
|
15
|
+
/** Extra classes for the <ul> element. */
|
|
16
|
+
class?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const { class: classes }: Props = $props();
|
|
20
|
+
|
|
21
|
+
const { form, state } = getFormContext();
|
|
22
|
+
const config = getUiConfig();
|
|
23
|
+
|
|
24
|
+
const messages = $derived.by(() => {
|
|
25
|
+
const collected: string[] = [];
|
|
26
|
+
|
|
27
|
+
if (state.submitError !== undefined)
|
|
28
|
+
collected.push(config.labels.form.generalError);
|
|
29
|
+
|
|
30
|
+
// Form-level issues (e.g. from `invalid("...")` in the handler) carry no
|
|
31
|
+
// path; field-level issues render at their fields instead.
|
|
32
|
+
for (const issue of form.fields.allIssues() ?? []) {
|
|
33
|
+
if (!issue.path?.length) collected.push(config.resolveMessage(issue));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return collected;
|
|
37
|
+
});
|
|
38
|
+
</script>
|
|
39
|
+
|
|
40
|
+
{#if messages.length}
|
|
41
|
+
<ul
|
|
42
|
+
class={cn("text-sm text-red-700 dark:text-red-500", classes)}
|
|
43
|
+
role="alert"
|
|
44
|
+
>
|
|
45
|
+
{#each messages as message, index (index)}
|
|
46
|
+
<li>{message}</li>
|
|
47
|
+
{/each}
|
|
48
|
+
</ul>
|
|
49
|
+
{/if}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
interface Props {
|
|
2
|
+
/** Extra classes for the <ul> element. */
|
|
3
|
+
class?: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Form-level error list — must render inside a <Form> (throws otherwise).
|
|
7
|
+
* Shows the configured `labels.form.generalError` line when a submission threw,
|
|
8
|
+
* plus every validation issue without a path (e.g. from `invalid("...")` in the
|
|
9
|
+
* handler), translated through the configured `resolveMessage`; field-level
|
|
10
|
+
* issues render at their inputs instead. Renders nothing while there are no
|
|
11
|
+
* messages.
|
|
12
|
+
*/
|
|
13
|
+
declare const FormError: import("svelte").Component<Props, {}, "">;
|
|
14
|
+
type FormError = ReturnType<typeof FormError>;
|
|
15
|
+
export default FormError;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
<!-- @component
|
|
2
|
+
Reset button — must render inside a <Form> (throws otherwise). Disabled
|
|
3
|
+
while the form is pristine or submitting; clicking fires a native reset,
|
|
4
|
+
which the Form's onreset handler turns into a full state reset (initial
|
|
5
|
+
values restored, issues and error gates cleared).
|
|
6
|
+
-->
|
|
7
|
+
<script lang="ts">
|
|
8
|
+
import Button from "@privaty/ui/components/button.svelte";
|
|
9
|
+
import { getUiConfig } from "@privaty/ui/config/context.js";
|
|
10
|
+
import { getFormContext } from "../context";
|
|
11
|
+
|
|
12
|
+
import type { Snippet } from "svelte";
|
|
13
|
+
|
|
14
|
+
interface Props {
|
|
15
|
+
/** Button label — defaults to the configured `labels.form.reset`. */
|
|
16
|
+
label?: string;
|
|
17
|
+
|
|
18
|
+
/** Extra classes for the button. */
|
|
19
|
+
class?: string;
|
|
20
|
+
|
|
21
|
+
/** Icon-style content rendered instead of the label text — the label
|
|
22
|
+
* stays as the accessible name (sr-only + tooltip). */
|
|
23
|
+
children?: Snippet;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const { label, class: classes, children }: Props = $props();
|
|
27
|
+
|
|
28
|
+
const { state } = getFormContext();
|
|
29
|
+
const config = getUiConfig();
|
|
30
|
+
|
|
31
|
+
const resolvedLabel = $derived(label ?? config.labels.form.reset);
|
|
32
|
+
</script>
|
|
33
|
+
|
|
34
|
+
<!-- Nothing to reset while pristine; the native reset event triggers
|
|
35
|
+
FormState.reset() via the Form component's onreset handler. -->
|
|
36
|
+
<Button
|
|
37
|
+
type="reset"
|
|
38
|
+
variant="secondary"
|
|
39
|
+
disabled={!state.isDirty || state.isSubmitting}
|
|
40
|
+
title={children ? resolvedLabel : undefined}
|
|
41
|
+
class={classes}
|
|
42
|
+
>
|
|
43
|
+
{#if children}
|
|
44
|
+
{@render children()}
|
|
45
|
+
<span class="sr-only">{resolvedLabel}</span>
|
|
46
|
+
{:else}
|
|
47
|
+
{resolvedLabel}
|
|
48
|
+
{/if}
|
|
49
|
+
</Button>
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Snippet } from "svelte";
|
|
2
|
+
interface Props {
|
|
3
|
+
/** Button label — defaults to the configured `labels.form.reset`. */
|
|
4
|
+
label?: string;
|
|
5
|
+
/** Extra classes for the button. */
|
|
6
|
+
class?: string;
|
|
7
|
+
/** Icon-style content rendered instead of the label text — the label
|
|
8
|
+
* stays as the accessible name (sr-only + tooltip). */
|
|
9
|
+
children?: Snippet;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Reset button — must render inside a <Form> (throws otherwise). Disabled
|
|
13
|
+
* while the form is pristine or submitting; clicking fires a native reset,
|
|
14
|
+
* which the Form's onreset handler turns into a full state reset (initial
|
|
15
|
+
* values restored, issues and error gates cleared).
|
|
16
|
+
*/
|
|
17
|
+
declare const Reset: import("svelte").Component<Props, {}, "">;
|
|
18
|
+
type Reset = ReturnType<typeof Reset>;
|
|
19
|
+
export default Reset;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
<!-- @component
|
|
2
|
+
Submit button — must render inside a <Form> (throws otherwise). Disabled
|
|
3
|
+
per `disabledUntil` (default "dirty-and-valid") and always while submitting,
|
|
4
|
+
when it shows a spinner, sets aria-busy, and swaps to `submittingLabel` when
|
|
5
|
+
one is given.
|
|
6
|
+
-->
|
|
7
|
+
<script lang="ts">
|
|
8
|
+
import { cn } from "@privaty/ui/cn.js";
|
|
9
|
+
import Button from "@privaty/ui/components/button.svelte";
|
|
10
|
+
import Spinner from "@privaty/ui/components/spinner.svelte";
|
|
11
|
+
import { getUiConfig } from "@privaty/ui/config/context.js";
|
|
12
|
+
import type { Snippet } from "svelte";
|
|
13
|
+
import { getFormContext } from "../context";
|
|
14
|
+
|
|
15
|
+
interface Props {
|
|
16
|
+
/** Button label — defaults to the configured `labels.form.submit`. */
|
|
17
|
+
label?: string;
|
|
18
|
+
/** Label shown next to the spinner while submitting. Without it only the
|
|
19
|
+
* spinner is visible and the label stays as the sr-only accessible name. */
|
|
20
|
+
submittingLabel?: string;
|
|
21
|
+
|
|
22
|
+
/** Validity gate: "dirty-and-valid" (default) disables the button until
|
|
23
|
+
* the form is both dirty and issue-free, "valid" only requires issue-free,
|
|
24
|
+
* "none" removes the gate. The button is always disabled while submitting
|
|
25
|
+
* regardless. */
|
|
26
|
+
disabledUntil?: "dirty-and-valid" | "valid" | "none";
|
|
27
|
+
|
|
28
|
+
/** Extra classes for the button. */
|
|
29
|
+
class?: string;
|
|
30
|
+
|
|
31
|
+
/** Icon-style content rendered instead of the label text — the label
|
|
32
|
+
* stays as the accessible name (sr-only + tooltip). */
|
|
33
|
+
children?: Snippet;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const {
|
|
37
|
+
label,
|
|
38
|
+
submittingLabel,
|
|
39
|
+
|
|
40
|
+
disabledUntil = "dirty-and-valid",
|
|
41
|
+
|
|
42
|
+
class: classes,
|
|
43
|
+
|
|
44
|
+
children,
|
|
45
|
+
}: Props = $props();
|
|
46
|
+
|
|
47
|
+
const { state } = getFormContext();
|
|
48
|
+
const config = getUiConfig();
|
|
49
|
+
|
|
50
|
+
const resolvedLabel = $derived(label ?? config.labels.form.submit);
|
|
51
|
+
|
|
52
|
+
const gateBlocked = $derived(
|
|
53
|
+
disabledUntil === "dirty-and-valid"
|
|
54
|
+
? !state.isDirty || !state.isValid
|
|
55
|
+
: disabledUntil === "valid"
|
|
56
|
+
? !state.isValid
|
|
57
|
+
: false,
|
|
58
|
+
);
|
|
59
|
+
</script>
|
|
60
|
+
|
|
61
|
+
<Button
|
|
62
|
+
type="submit"
|
|
63
|
+
disabled={gateBlocked || state.isSubmitting}
|
|
64
|
+
aria-busy={state.isSubmitting}
|
|
65
|
+
title={children ? resolvedLabel : undefined}
|
|
66
|
+
class={cn("inline-flex items-center justify-center gap-2", classes)}
|
|
67
|
+
>
|
|
68
|
+
{#if state.isSubmitting}
|
|
69
|
+
<Spinner />
|
|
70
|
+
{#if submittingLabel}
|
|
71
|
+
{submittingLabel}
|
|
72
|
+
{:else}
|
|
73
|
+
<!-- Keep the accessible name when only the spinner is visible. -->
|
|
74
|
+
<span class="sr-only">{resolvedLabel}</span>
|
|
75
|
+
{/if}
|
|
76
|
+
{:else if children}
|
|
77
|
+
{@render children()}
|
|
78
|
+
<span class="sr-only">{resolvedLabel}</span>
|
|
79
|
+
{:else}
|
|
80
|
+
{resolvedLabel}
|
|
81
|
+
{/if}
|
|
82
|
+
</Button>
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Snippet } from "svelte";
|
|
2
|
+
interface Props {
|
|
3
|
+
/** Button label — defaults to the configured `labels.form.submit`. */
|
|
4
|
+
label?: string;
|
|
5
|
+
/** Label shown next to the spinner while submitting. Without it only the
|
|
6
|
+
* spinner is visible and the label stays as the sr-only accessible name. */
|
|
7
|
+
submittingLabel?: string;
|
|
8
|
+
/** Validity gate: "dirty-and-valid" (default) disables the button until
|
|
9
|
+
* the form is both dirty and issue-free, "valid" only requires issue-free,
|
|
10
|
+
* "none" removes the gate. The button is always disabled while submitting
|
|
11
|
+
* regardless. */
|
|
12
|
+
disabledUntil?: "dirty-and-valid" | "valid" | "none";
|
|
13
|
+
/** Extra classes for the button. */
|
|
14
|
+
class?: string;
|
|
15
|
+
/** Icon-style content rendered instead of the label text — the label
|
|
16
|
+
* stays as the accessible name (sr-only + tooltip). */
|
|
17
|
+
children?: Snippet;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Submit button — must render inside a <Form> (throws otherwise). Disabled
|
|
21
|
+
* per `disabledUntil` (default "dirty-and-valid") and always while submitting,
|
|
22
|
+
* when it shows a spinner, sets aria-busy, and swaps to `submittingLabel` when
|
|
23
|
+
* one is given.
|
|
24
|
+
*/
|
|
25
|
+
declare const Submit: import("svelte").Component<Props, {}, "">;
|
|
26
|
+
type Submit = ReturnType<typeof Submit>;
|
|
27
|
+
export default Submit;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { FormState } from "./form-state.svelte";
|
|
2
|
+
import type { ValidatableForm } from "./types/form";
|
|
3
|
+
/** What a <Form> shares with its descendants. */
|
|
4
|
+
interface FormContext {
|
|
5
|
+
/** The enhanced remote form instance (the preflighted one when the Form
|
|
6
|
+
* was given a schema). */
|
|
7
|
+
form: ValidatableForm;
|
|
8
|
+
/** The form's client-side display state — dirty/touched tracking and
|
|
9
|
+
* error-display gating. */
|
|
10
|
+
state: FormState;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Shares a form instance and its FormState with descendant components. Called
|
|
14
|
+
* by <Form>; must run during component init. A custom form wrapper can call
|
|
15
|
+
* it to host this library's inputs and buttons.
|
|
16
|
+
*/
|
|
17
|
+
declare function setFormContext(context: FormContext): void;
|
|
18
|
+
/**
|
|
19
|
+
* Returns the nearest enclosing <Form>'s context. Must be called during
|
|
20
|
+
* component init; throws when no ancestor has set the form context.
|
|
21
|
+
*/
|
|
22
|
+
declare function getFormContext(): FormContext;
|
|
23
|
+
export { getFormContext, setFormContext };
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { getContext, setContext } from "svelte";
|
|
2
|
+
const formContextKey = Symbol("privaty-ui-form-context");
|
|
3
|
+
/**
|
|
4
|
+
* Shares a form instance and its FormState with descendant components. Called
|
|
5
|
+
* by <Form>; must run during component init. A custom form wrapper can call
|
|
6
|
+
* it to host this library's inputs and buttons.
|
|
7
|
+
*/
|
|
8
|
+
function setFormContext(context) {
|
|
9
|
+
setContext(formContextKey, context);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Returns the nearest enclosing <Form>'s context. Must be called during
|
|
13
|
+
* component init; throws when no ancestor has set the form context.
|
|
14
|
+
*/
|
|
15
|
+
function getFormContext() {
|
|
16
|
+
const context = getContext(formContextKey);
|
|
17
|
+
if (!context)
|
|
18
|
+
throw new Error("FormContext: form components must be used inside a <Form>");
|
|
19
|
+
return context;
|
|
20
|
+
}
|
|
21
|
+
export { getFormContext, setFormContext };
|