@zeyolabs/ui 0.0.0-stage → 2026.11.5
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/README.md +43 -2
- package/back-button/BackButton.svelte +26 -0
- package/badge/Badge.svelte +73 -0
- package/badge/PromoBadge.svelte +319 -0
- package/badge/color.ts +10 -0
- package/bar/Bar.svelte +63 -0
- package/base.css +47 -0
- package/boundary/Boundary.svelte +59 -0
- package/boundary/BoundaryFailed.svelte +40 -0
- package/boundary/PendingTracker.svelte +14 -0
- package/boundary/pending-context.ts +15 -0
- package/boundary/pending-flag.svelte.ts +33 -0
- package/boundary/report-boundary-error.ts +13 -0
- package/card/Card.svelte +239 -0
- package/card/CardBody.svelte +41 -0
- package/card/CardFooter.svelte +36 -0
- package/card/CardHeader.svelte +36 -0
- package/copy/CopyArea.svelte +37 -0
- package/copy/CopyButton.svelte +59 -0
- package/copy/copy-state.svelte.ts +23 -0
- package/copy/types.ts +1 -0
- package/dialog/ConfirmDialog.svelte +61 -0
- package/dialog/Dialog.svelte +31 -0
- package/dialog/DialogBody.svelte +88 -0
- package/dialog/DialogCloseButton.svelte +26 -0
- package/dialog/DialogFooter.svelte +23 -0
- package/dialog/DialogTrigger.svelte +15 -0
- package/distribution-bar/DistributionBar.svelte +37 -0
- package/drawer/Drawer.svelte +31 -0
- package/drawer/DrawerBody.svelte +61 -0
- package/empty-state/EmptyState.svelte +74 -0
- package/error/ErrorPage.svelte +54 -0
- package/eyebrow/Eyebrow.svelte +10 -0
- package/faq/FAQList.svelte +89 -0
- package/filter/FilterChipGroup.svelte +78 -0
- package/filter/FilterOptionList.svelte +188 -0
- package/filter/FilterPopover.svelte +122 -0
- package/filter/filter-draft.svelte.ts +25 -0
- package/form/Button.svelte +73 -0
- package/form/Field.svelte +111 -0
- package/form/FieldLabel.svelte +19 -0
- package/form/Form.svelte +133 -0
- package/form/PhoneField.svelte +214 -0
- package/form/UnsavedBar.svelte +22 -0
- package/form/field-view.svelte.ts +213 -0
- package/form/fields/CheckboxField.svelte +85 -0
- package/form/fields/Combobox.svelte +327 -0
- package/form/fields/ComboboxAction.svelte +16 -0
- package/form/fields/DateField.svelte +31 -0
- package/form/fields/DateRangeField.svelte +188 -0
- package/form/fields/DateRangePresetField.svelte +119 -0
- package/form/fields/EmailField.svelte +63 -0
- package/form/fields/FileField.svelte +145 -0
- package/form/fields/HexColorField.svelte +83 -0
- package/form/fields/IntervalField.svelte +100 -0
- package/form/fields/MessageField.svelte +99 -0
- package/form/fields/NumberField.svelte +64 -0
- package/form/fields/Option.svelte +48 -0
- package/form/fields/OptionRegistrar.svelte +15 -0
- package/form/fields/PasswordField.svelte +44 -0
- package/form/fields/RadioField.svelte +60 -0
- package/form/fields/RichTextField.svelte +221 -0
- package/form/fields/SelectField.svelte +216 -0
- package/form/fields/SliderField.svelte +182 -0
- package/form/fields/SwitchField.svelte +102 -0
- package/form/fields/TextField.svelte +72 -0
- package/form/fields/TimeField.svelte +31 -0
- package/form/fields/index.ts +19 -0
- package/form/fields/select-field-context.ts +21 -0
- package/form/index.ts +21 -0
- package/form/primitives/Checkbox.svelte +103 -0
- package/form/primitives/DateInput.svelte +51 -0
- package/form/primitives/IconInputWrapper.svelte +40 -0
- package/form/primitives/Input.svelte +47 -0
- package/form/primitives/SearchBar.svelte +37 -0
- package/form/primitives/Select.svelte +139 -0
- package/form/primitives/TextArea.svelte +51 -0
- package/form/primitives/index.ts +6 -0
- package/gallery/ImageGallery.svelte +133 -0
- package/gallery/flickity-fullscreen.d.ts +2 -0
- package/grouped-grid/GroupedGrid.svelte +143 -0
- package/grouped-grid/types.ts +25 -0
- package/layout/Cluster.svelte +92 -0
- package/layout/Container.svelte +9 -0
- package/layout/Grid.svelte +131 -0
- package/layout/Section.svelte +146 -0
- package/layout/Shell.svelte +32 -0
- package/layout/SplitShell.svelte +47 -0
- package/layout/Stack.svelte +78 -0
- package/list-row/ListRow.svelte +77 -0
- package/menu/MenuButton.svelte +101 -0
- package/mini-stat/MiniStat.svelte +13 -0
- package/package.json +169 -5
- package/page-hero/PageHero.svelte +35 -0
- package/pagination/Pagination.svelte +44 -0
- package/pagination/finder-pagination.svelte.ts +55 -0
- package/pagination/finder-search.svelte.ts +292 -0
- package/percentage-ring/PercentageRing.svelte +76 -0
- package/percentage-ring/types.ts +1 -0
- package/popover/InfoTip.svelte +36 -0
- package/popover/Popover.svelte +157 -0
- package/query-state/QueryState.svelte +42 -0
- package/schema.ts +20 -0
- package/skeleton/Skeleton.svelte +86 -0
- package/skeleton/SkeletonButton.svelte +27 -0
- package/skeleton/SkeletonCard.svelte +49 -0
- package/skeleton/SkeletonCircle.svelte +25 -0
- package/skeleton/SkeletonField.svelte +19 -0
- package/skeleton/SkeletonRow.svelte +47 -0
- package/skeleton/SkeletonText.svelte +46 -0
- package/skeleton/TableSkeleton.svelte +60 -0
- package/spacing.css +32 -0
- package/stat/NextPayoutCountdown.svelte +52 -0
- package/stat/Stat.svelte +103 -0
- package/stat/ValueBox.svelte +38 -0
- package/status-row/StatusRow.svelte +176 -0
- package/status-row/types.ts +5 -0
- package/table/SelectAllCell.svelte +34 -0
- package/table/SelectAllTableCell.svelte +25 -0
- package/table/SelectRowCell.svelte +31 -0
- package/table/SelectRowTableCell.svelte +25 -0
- package/table/SelectionBar.svelte +68 -0
- package/table/SelectionOverlay.svelte +83 -0
- package/table/Table.svelte +64 -0
- package/table/TableCell.svelte +185 -0
- package/table/TableRow.svelte +86 -0
- package/table/index.ts +9 -0
- package/table/selection.svelte.ts +79 -0
- package/tabs/TabBlock.svelte +56 -0
- package/tabs/TabButton.svelte +33 -0
- package/template.css +1393 -0
- package/text/Span.svelte +83 -0
- package/text/Text.svelte +94 -0
- package/text/Title.svelte +75 -0
- package/text/color.ts +41 -0
- package/theme/theme.svelte.ts +33 -0
- package/tokens.css +62 -0
- package/typography.css +164 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
<script lang="ts" module>
|
|
2
|
+
import { type CountryCode, getCountries, getCountryCallingCode } from 'libphonenumber-js';
|
|
3
|
+
|
|
4
|
+
type Country = { dial: string; flag: string; iso: CountryCode; name: string };
|
|
5
|
+
|
|
6
|
+
/** ISO 3166-1 alpha-2 → regional-indicator flag emoji. */
|
|
7
|
+
function flagEmoji(iso: string): string {
|
|
8
|
+
return iso.toUpperCase().replace(/./g, (c) => String.fromCodePoint(127397 + c.charCodeAt(0)));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// Locale-independent country data (ISO, calling code, flag) — built once at module load.
|
|
12
|
+
// Display names are locale-dependent, so they're resolved per-instance (see the script below).
|
|
13
|
+
type StaticCountry = Omit<Country, 'name'>;
|
|
14
|
+
const STATIC_COUNTRIES: StaticCountry[] = getCountries().map((iso) => ({
|
|
15
|
+
dial: `+${getCountryCallingCode(iso)}`,
|
|
16
|
+
flag: flagEmoji(iso),
|
|
17
|
+
iso,
|
|
18
|
+
}));
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
<script lang="ts">
|
|
22
|
+
import type { RemoteForm } from '$app/server';
|
|
23
|
+
import type { Snippet } from 'svelte';
|
|
24
|
+
|
|
25
|
+
import { isValidPhoneNumber, parsePhoneNumberFromString } from 'libphonenumber-js';
|
|
26
|
+
|
|
27
|
+
let {
|
|
28
|
+
children,
|
|
29
|
+
defaultCountry = 'US',
|
|
30
|
+
form,
|
|
31
|
+
locale = 'en',
|
|
32
|
+
name = 'phone',
|
|
33
|
+
optional = false,
|
|
34
|
+
placeholder = 'Phone number',
|
|
35
|
+
}: {
|
|
36
|
+
/** Label content. */
|
|
37
|
+
children?: Snippet;
|
|
38
|
+
defaultCountry?: CountryCode;
|
|
39
|
+
// Loose form typing so any remote form with a phone field can use it.
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
41
|
+
form: RemoteForm<any, unknown>;
|
|
42
|
+
/** BCP 47 locale for the country display names (e.g. 'en', 'fr'). */
|
|
43
|
+
locale?: string;
|
|
44
|
+
name?: string;
|
|
45
|
+
optional?: boolean;
|
|
46
|
+
placeholder?: string;
|
|
47
|
+
} = $props();
|
|
48
|
+
|
|
49
|
+
const field = $derived(form.fields[name]);
|
|
50
|
+
// Same source the other fields use — populated live by the parent <Form>'s `oninput`
|
|
51
|
+
// revalidation, so errors appear as you type rather than only after submit.
|
|
52
|
+
const issues = $derived(field.issues() ?? []);
|
|
53
|
+
const required = $derived(!optional);
|
|
54
|
+
|
|
55
|
+
// Initial country only — it intentionally doesn't react to later `defaultCountry` changes.
|
|
56
|
+
// svelte-ignore state_referenced_locally
|
|
57
|
+
let country = $state<CountryCode>(defaultCountry);
|
|
58
|
+
let national = $state('');
|
|
59
|
+
let open = $state(false);
|
|
60
|
+
let search = $state('');
|
|
61
|
+
|
|
62
|
+
// Display names are locale-dependent, so they're re-derived if `locale` changes. Deriving
|
|
63
|
+
// here (not in the module script) keeps names consistent between SSR and client — the same
|
|
64
|
+
// instance resolves them once — and lets a consumer localise the picker via the prop.
|
|
65
|
+
const regionNames = $derived(
|
|
66
|
+
typeof Intl !== 'undefined' && 'DisplayNames' in Intl
|
|
67
|
+
? new Intl.DisplayNames([locale], { type: 'region' })
|
|
68
|
+
: undefined
|
|
69
|
+
);
|
|
70
|
+
const COUNTRIES = $derived(
|
|
71
|
+
STATIC_COUNTRIES.map((c) => ({ ...c, name: regionNames?.of(c.iso) ?? c.iso })).sort((a, b) =>
|
|
72
|
+
a.name.localeCompare(b.name)
|
|
73
|
+
)
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
const selected = $derived(COUNTRIES.find((c) => c.iso === country) ?? COUNTRIES[0]);
|
|
77
|
+
|
|
78
|
+
const filtered = $derived.by(() => {
|
|
79
|
+
const q = search.trim().toLowerCase();
|
|
80
|
+
if (!q) return COUNTRIES;
|
|
81
|
+
return COUNTRIES.filter(
|
|
82
|
+
(c) => c.name.toLowerCase().includes(q) || c.dial.includes(q) || c.iso.toLowerCase() === q
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
/** National number + country → the canonical E.164 (or '' if unparseable). */
|
|
87
|
+
function toE164(nationalNumber: string, iso: CountryCode): string {
|
|
88
|
+
if (!nationalNumber.trim()) return '';
|
|
89
|
+
return parsePhoneNumberFromString(nationalNumber, iso)?.number ?? '';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// The value the form submits + validates against, carried by the hidden input below.
|
|
93
|
+
const e164 = $derived(toE164(national, country));
|
|
94
|
+
|
|
95
|
+
// Live as-you-type invalidity (shown while typing a non-empty, not-yet-valid number),
|
|
96
|
+
// plus any form-level issues (e.g. the required check on submit).
|
|
97
|
+
const invalid = $derived(national.trim() !== '' && !isValidPhoneNumber(national, country));
|
|
98
|
+
const showError = $derived(invalid || issues.length > 0);
|
|
99
|
+
|
|
100
|
+
function selectCountry(iso: CountryCode) {
|
|
101
|
+
country = iso;
|
|
102
|
+
open = false;
|
|
103
|
+
search = '';
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function focusOnMount(node: HTMLElement) {
|
|
107
|
+
node.focus();
|
|
108
|
+
}
|
|
109
|
+
</script>
|
|
110
|
+
|
|
111
|
+
<div class="form-field">
|
|
112
|
+
{#if children}
|
|
113
|
+
<label class="form-label" for="{name}-input">
|
|
114
|
+
{@render children()}
|
|
115
|
+
{#if required}<span class="form-required-star" aria-hidden="true">*</span>{/if}
|
|
116
|
+
</label>
|
|
117
|
+
{/if}
|
|
118
|
+
|
|
119
|
+
<div class="flex gap-2">
|
|
120
|
+
<div class="relative shrink-0">
|
|
121
|
+
<button
|
|
122
|
+
class="flex h-full cursor-pointer items-center gap-1.5 rounded-md border border-[var(--bd,#e4e2e0)] bg-[var(--bg,#fff)] px-2.5 text-[13px] text-[var(--tx,#121014)]"
|
|
123
|
+
aria-expanded={open}
|
|
124
|
+
aria-haspopup="listbox"
|
|
125
|
+
aria-label="Country code, {selected.name} {selected.dial}"
|
|
126
|
+
onclick={() => (open = !open)}
|
|
127
|
+
type="button"
|
|
128
|
+
>
|
|
129
|
+
<span class="lg">{selected.flag}</span>
|
|
130
|
+
<span>{selected.dial}</span>
|
|
131
|
+
<span class="sm opacity-60" aria-hidden="true">▾</span>
|
|
132
|
+
</button>
|
|
133
|
+
|
|
134
|
+
{#if open}
|
|
135
|
+
<button
|
|
136
|
+
class="fixed inset-0 z-40 cursor-default"
|
|
137
|
+
aria-label="Close country list"
|
|
138
|
+
onclick={() => {
|
|
139
|
+
open = false;
|
|
140
|
+
search = '';
|
|
141
|
+
}}
|
|
142
|
+
tabindex="-1"
|
|
143
|
+
type="button"
|
|
144
|
+
></button>
|
|
145
|
+
<div
|
|
146
|
+
class="absolute left-0 top-[calc(100%+4px)] z-50 flex w-[300px] max-w-[80vw] flex-col overflow-hidden rounded-md border border-[var(--bd,#e4e2e0)] bg-[var(--bg,#fff)] shadow-lg"
|
|
147
|
+
aria-label="Countries"
|
|
148
|
+
role="listbox"
|
|
149
|
+
>
|
|
150
|
+
<input
|
|
151
|
+
class="border-0 border-b border-[var(--bd,#e4e2e0)] bg-transparent px-3 py-2 text-[var(--tx,#121014)] outline-none"
|
|
152
|
+
onkeydown={(event) => {
|
|
153
|
+
if (event.key === 'Enter') {
|
|
154
|
+
event.preventDefault();
|
|
155
|
+
if (filtered.length > 0) selectCountry(filtered[0].iso);
|
|
156
|
+
} else if (event.key === 'Escape') {
|
|
157
|
+
event.preventDefault();
|
|
158
|
+
open = false;
|
|
159
|
+
search = '';
|
|
160
|
+
}
|
|
161
|
+
}}
|
|
162
|
+
placeholder="Search country"
|
|
163
|
+
type="text"
|
|
164
|
+
bind:value={search}
|
|
165
|
+
use:focusOnMount
|
|
166
|
+
/>
|
|
167
|
+
<div class="max-h-[260px] overflow-y-auto">
|
|
168
|
+
{#each filtered as c (c.iso)}
|
|
169
|
+
<button
|
|
170
|
+
class="flex w-full cursor-pointer items-center gap-2 px-3 py-2 text-left text-[var(--tx,#121014)] hover:bg-[var(--sf,#f3f2f1)] {c.iso ===
|
|
171
|
+
country
|
|
172
|
+
? 'bg-[var(--sf,#f3f2f1)]'
|
|
173
|
+
: ''}"
|
|
174
|
+
aria-selected={c.iso === country}
|
|
175
|
+
onclick={() => selectCountry(c.iso)}
|
|
176
|
+
role="option"
|
|
177
|
+
type="button"
|
|
178
|
+
>
|
|
179
|
+
<span class="lg">{c.flag}</span>
|
|
180
|
+
<span class="min-w-0 flex-1 truncate">{c.name}</span>
|
|
181
|
+
<span class="text-[var(--txs,#6b6a69)] tabular-nums">{c.dial}</span>
|
|
182
|
+
</button>
|
|
183
|
+
{/each}
|
|
184
|
+
{#if filtered.length === 0}
|
|
185
|
+
<p class="px-3 py-3 text-center text-[var(--txs,#6b6a69)]">No matches</p>
|
|
186
|
+
{/if}
|
|
187
|
+
</div>
|
|
188
|
+
</div>
|
|
189
|
+
{/if}
|
|
190
|
+
</div>
|
|
191
|
+
|
|
192
|
+
<input
|
|
193
|
+
id="{name}-input"
|
|
194
|
+
class="form-input min-w-0 flex-1"
|
|
195
|
+
aria-invalid={showError}
|
|
196
|
+
autocomplete="tel-national"
|
|
197
|
+
inputmode="tel"
|
|
198
|
+
{placeholder}
|
|
199
|
+
type="tel"
|
|
200
|
+
bind:value={national}
|
|
201
|
+
/>
|
|
202
|
+
</div>
|
|
203
|
+
|
|
204
|
+
<!-- Register the value with the form (web-svelte's mechanism). `as('hidden', …)`
|
|
205
|
+
throws on empty, so render it only when there's a parseable E.164; an empty or
|
|
206
|
+
unparseable number leaves the field unset → the schema's error shows below. -->
|
|
207
|
+
{#if e164}
|
|
208
|
+
<input {...field.as('hidden', e164)} />
|
|
209
|
+
{/if}
|
|
210
|
+
|
|
211
|
+
{#if showError}
|
|
212
|
+
<p class="form-error">{issues[0]?.message ?? 'Enter a valid phone number'}</p>
|
|
213
|
+
{/if}
|
|
214
|
+
</div>
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
|
|
4
|
+
import Span from '@zeyolabs/ui/text/Span.svelte';
|
|
5
|
+
|
|
6
|
+
interface Props {
|
|
7
|
+
/** Action buttons (Save, plus any secondary actions like Pause/Resume). */
|
|
8
|
+
children: Snippet;
|
|
9
|
+
show: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
let { children, show }: Props = $props();
|
|
13
|
+
</script>
|
|
14
|
+
|
|
15
|
+
{#if show}
|
|
16
|
+
<div
|
|
17
|
+
class="sticky bottom-0 z-10 flex items-center justify-end gap-2.5 border-t border-(--bds) bg-(--bg) px-6 py-3.5"
|
|
18
|
+
>
|
|
19
|
+
<Span class="mr-auto" color="warning" size="sm">Unsaved changes</Span>
|
|
20
|
+
{@render children()}
|
|
21
|
+
</div>
|
|
22
|
+
{/if}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import type { RemoteForm, RemoteFormInput } from '$app/server';
|
|
2
|
+
import type { ZodType } from 'zod';
|
|
3
|
+
|
|
4
|
+
import { getContext, setContext } from 'svelte';
|
|
5
|
+
|
|
6
|
+
/** Normalised validation issue shape consumed by every field template. */
|
|
7
|
+
export type FieldIssue = { message: string; path: Array<number | string> };
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Bridge between a form-bound field and its enclosing `<Form>`. Native inputs
|
|
11
|
+
* bubble a DOM `input` event that `<Form>` listens for (to re-validate + mark
|
|
12
|
+
* dirty), but bits-ui controls (`Switch`, `Slider`, `Select`) update their value
|
|
13
|
+
* programmatically via `view.set()` and emit no such event — so `<Form>` would
|
|
14
|
+
* never learn they changed. `<Form>` registers a listener here and every
|
|
15
|
+
* `createFormView().set` calls it, so those fields flag dirty + validate too.
|
|
16
|
+
*/
|
|
17
|
+
const FORM_CHANGE_KEY = Symbol('web-svelte:form-change');
|
|
18
|
+
|
|
19
|
+
/** Called by `<Form>` during init to receive programmatic field-change notifications. */
|
|
20
|
+
export function setFormChangeListener(listener: () => void): void {
|
|
21
|
+
setContext(FORM_CHANGE_KEY, listener);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The surface every field template consumes. Produced either from a `RemoteForm`
|
|
26
|
+
* field (`createFormView`) or from local `$bindable` state (`createLocal*View`).
|
|
27
|
+
* `attrs` is spread onto the underlying input/select/checkbox.
|
|
28
|
+
*/
|
|
29
|
+
export type FieldView = {
|
|
30
|
+
readonly attrs: Record<string, unknown> & { name: string };
|
|
31
|
+
issues: () => FieldIssue[];
|
|
32
|
+
set: (value: unknown) => void;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
type LooseFormField = {
|
|
36
|
+
as(type: string, ...args: unknown[]): Record<string, unknown> & { name: string };
|
|
37
|
+
issues(): FieldIssue[] | undefined;
|
|
38
|
+
set(input: unknown): unknown;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Wrap a `RemoteForm` field. `as()` is called ONLY here, so the standalone path
|
|
43
|
+
* never touches it. Must be called during component init (uses `$derived`).
|
|
44
|
+
*/
|
|
45
|
+
export function createFormView<Input extends RemoteFormInput>(
|
|
46
|
+
form: Omit<RemoteForm<Input, unknown>, 'for'> | RemoteForm<Input, unknown>,
|
|
47
|
+
name: string,
|
|
48
|
+
type: string,
|
|
49
|
+
asArgs: unknown[] = []
|
|
50
|
+
): FieldView {
|
|
51
|
+
const field = $derived((form.fields as Record<string, LooseFormField>)[name]);
|
|
52
|
+
const attrs = $derived(field.as(type, ...asArgs));
|
|
53
|
+
// Read once at init (getContext requirement). Undefined when the field is used
|
|
54
|
+
// outside a `<Form>`, in which case a programmatic set simply notifies no one.
|
|
55
|
+
const notifyChange = getContext<(() => void) | undefined>(FORM_CHANGE_KEY);
|
|
56
|
+
return {
|
|
57
|
+
get attrs() {
|
|
58
|
+
return attrs;
|
|
59
|
+
},
|
|
60
|
+
issues: () => field.issues() ?? [],
|
|
61
|
+
set: (value) => {
|
|
62
|
+
field.set(value);
|
|
63
|
+
notifyChange?.();
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function schemaIssues(schema: undefined | ZodType, value: unknown): FieldIssue[] {
|
|
69
|
+
if (!schema) return [];
|
|
70
|
+
const result = schema.safeParse(value);
|
|
71
|
+
if (result.success) return [];
|
|
72
|
+
return result.error.issues.map((issue) => ({
|
|
73
|
+
message: issue.message,
|
|
74
|
+
path: issue.path as Array<number | string>,
|
|
75
|
+
}));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
type LocalOpts<T> = {
|
|
79
|
+
/** Reads the parent `$bindable` value. */
|
|
80
|
+
get: () => T;
|
|
81
|
+
name: string;
|
|
82
|
+
onChange?: (value: T) => void;
|
|
83
|
+
onInput?: (value: T) => void;
|
|
84
|
+
schema?: ZodType;
|
|
85
|
+
/** Writes the parent `$bindable` value. */
|
|
86
|
+
write: (value: T) => void;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Text-like standalone view (text/number/date/time/email/password/etc.).
|
|
91
|
+
* Builds its own `oninput`/`onchange` since `as()` does not provide them.
|
|
92
|
+
*/
|
|
93
|
+
export function createLocalTextView<T>(opts: LocalOpts<T>): FieldView {
|
|
94
|
+
const issues = $derived.by(() => schemaIssues(opts.schema, opts.get()));
|
|
95
|
+
const attrs = $derived({
|
|
96
|
+
'aria-invalid': issues.length > 0 ? ('true' as const) : undefined,
|
|
97
|
+
name: opts.name,
|
|
98
|
+
onchange: (event: Event) => {
|
|
99
|
+
const value = (event.currentTarget as HTMLInputElement).value as unknown as T;
|
|
100
|
+
opts.onChange?.(value);
|
|
101
|
+
},
|
|
102
|
+
oninput: (event: Event) => {
|
|
103
|
+
const value = (event.currentTarget as HTMLInputElement).value as unknown as T;
|
|
104
|
+
opts.write(value);
|
|
105
|
+
opts.onInput?.(value);
|
|
106
|
+
},
|
|
107
|
+
value: (opts.get() ?? '') as unknown,
|
|
108
|
+
});
|
|
109
|
+
return {
|
|
110
|
+
get attrs() {
|
|
111
|
+
return attrs;
|
|
112
|
+
},
|
|
113
|
+
issues: () => issues,
|
|
114
|
+
set: (value) => {
|
|
115
|
+
opts.write(value as T);
|
|
116
|
+
opts.onInput?.(value as T);
|
|
117
|
+
opts.onChange?.(value as T);
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Checkbox/switch standalone view — `checked` instead of `value`, boolean state. */
|
|
123
|
+
export function createLocalCheckboxView(opts: LocalOpts<boolean>): FieldView {
|
|
124
|
+
const issues = $derived.by(() => schemaIssues(opts.schema, opts.get()));
|
|
125
|
+
const attrs = $derived({
|
|
126
|
+
'aria-invalid': issues.length > 0 ? ('true' as const) : undefined,
|
|
127
|
+
checked: opts.get() === true,
|
|
128
|
+
name: opts.name,
|
|
129
|
+
type: 'checkbox' as const,
|
|
130
|
+
});
|
|
131
|
+
return {
|
|
132
|
+
get attrs() {
|
|
133
|
+
return attrs;
|
|
134
|
+
},
|
|
135
|
+
issues: () => issues,
|
|
136
|
+
set: (value) => {
|
|
137
|
+
const next = value === true;
|
|
138
|
+
opts.write(next);
|
|
139
|
+
opts.onInput?.(next);
|
|
140
|
+
opts.onChange?.(next);
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* File standalone view — the native file input stays uncontrolled, so `set` is a
|
|
147
|
+
* no-op; `attrs` only carries the name + validity, and `issues()` validates the
|
|
148
|
+
* currently-selected files against the optional schema.
|
|
149
|
+
*/
|
|
150
|
+
export function createLocalFileView(opts: {
|
|
151
|
+
get: () => File[];
|
|
152
|
+
name: string;
|
|
153
|
+
schema?: ZodType;
|
|
154
|
+
}): FieldView {
|
|
155
|
+
const issues = $derived.by(() => schemaIssues(opts.schema, opts.get()));
|
|
156
|
+
const attrs = $derived({
|
|
157
|
+
'aria-invalid': issues.length > 0 ? ('true' as const) : undefined,
|
|
158
|
+
name: opts.name,
|
|
159
|
+
});
|
|
160
|
+
return {
|
|
161
|
+
get attrs() {
|
|
162
|
+
return attrs;
|
|
163
|
+
},
|
|
164
|
+
issues: () => issues,
|
|
165
|
+
set: () => {},
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Slider standalone view — value is a `number` (single) or `number[]` (range).
|
|
171
|
+
* The bits-ui Slider has no native input, so there are no DOM events here; `set`
|
|
172
|
+
* is driven by the component's `onValueChange`.
|
|
173
|
+
*/
|
|
174
|
+
export function createLocalSliderView<T extends number | number[]>(opts: LocalOpts<T>): FieldView {
|
|
175
|
+
const issues = $derived.by(() => schemaIssues(opts.schema, opts.get()));
|
|
176
|
+
const attrs = $derived({
|
|
177
|
+
'aria-invalid': issues.length > 0 ? ('true' as const) : undefined,
|
|
178
|
+
name: opts.name,
|
|
179
|
+
value: opts.get() as unknown,
|
|
180
|
+
});
|
|
181
|
+
return {
|
|
182
|
+
get attrs() {
|
|
183
|
+
return attrs;
|
|
184
|
+
},
|
|
185
|
+
issues: () => issues,
|
|
186
|
+
set: (value) => {
|
|
187
|
+
opts.write(value as T);
|
|
188
|
+
opts.onInput?.(value as T);
|
|
189
|
+
opts.onChange?.(value as T);
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Select standalone view — `set` receives the already-resolved typed value. */
|
|
195
|
+
export function createLocalSelectView<T>(opts: LocalOpts<T>): FieldView {
|
|
196
|
+
const issues = $derived.by(() => schemaIssues(opts.schema, opts.get()));
|
|
197
|
+
const attrs = $derived({
|
|
198
|
+
'aria-invalid': issues.length > 0 ? ('true' as const) : undefined,
|
|
199
|
+
name: opts.name,
|
|
200
|
+
value: (opts.get() ?? '') as unknown,
|
|
201
|
+
});
|
|
202
|
+
return {
|
|
203
|
+
get attrs() {
|
|
204
|
+
return attrs;
|
|
205
|
+
},
|
|
206
|
+
issues: () => issues,
|
|
207
|
+
set: (value) => {
|
|
208
|
+
opts.write(value as T);
|
|
209
|
+
opts.onInput?.(value as T);
|
|
210
|
+
opts.onChange?.(value as T);
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
<script generics="Input extends RemoteFormInput" lang="ts">
|
|
2
|
+
import type { RemoteForm, RemoteFormInput } from '$app/server';
|
|
3
|
+
import type { Snippet } from 'svelte';
|
|
4
|
+
import type { ZodType } from 'zod';
|
|
5
|
+
|
|
6
|
+
import CheckIcon from '@lucide/svelte/icons/check';
|
|
7
|
+
import { Checkbox } from 'bits-ui';
|
|
8
|
+
|
|
9
|
+
import { createFormView, createLocalCheckboxView, type FieldView } from '../field-view.svelte.js';
|
|
10
|
+
import FieldLabel from '../FieldLabel.svelte';
|
|
11
|
+
|
|
12
|
+
interface Props {
|
|
13
|
+
checked?: boolean;
|
|
14
|
+
children?: Snippet;
|
|
15
|
+
description?: string;
|
|
16
|
+
disabled?: boolean;
|
|
17
|
+
form?: Omit<RemoteForm<Input, unknown>, 'for'> | RemoteForm<Input, unknown>;
|
|
18
|
+
name: keyof Input & string;
|
|
19
|
+
onChange?: (value: boolean) => void;
|
|
20
|
+
optional?: boolean;
|
|
21
|
+
schema?: ZodType;
|
|
22
|
+
/** No named hidden input, so the checkbox can sit inside a remote `<Form>` without
|
|
23
|
+
* tripping kit's unbound-field check. */
|
|
24
|
+
unsubmitted?: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let {
|
|
28
|
+
checked = $bindable(false),
|
|
29
|
+
children,
|
|
30
|
+
description,
|
|
31
|
+
disabled,
|
|
32
|
+
form,
|
|
33
|
+
name,
|
|
34
|
+
onChange,
|
|
35
|
+
optional,
|
|
36
|
+
schema,
|
|
37
|
+
unsubmitted,
|
|
38
|
+
}: Props = $props();
|
|
39
|
+
|
|
40
|
+
// `as('checkbox')` is reached only via the `form` branch; standalone uses local state.
|
|
41
|
+
// svelte-ignore state_referenced_locally
|
|
42
|
+
const view: FieldView = form
|
|
43
|
+
? createFormView(form, name, 'checkbox')
|
|
44
|
+
: createLocalCheckboxView({
|
|
45
|
+
get: () => checked,
|
|
46
|
+
name,
|
|
47
|
+
onChange,
|
|
48
|
+
schema,
|
|
49
|
+
write: (v) => (checked = v),
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const attrs = $derived(view.attrs);
|
|
53
|
+
const required = $derived(!optional);
|
|
54
|
+
</script>
|
|
55
|
+
|
|
56
|
+
<div class="form-checkbox-wrapper">
|
|
57
|
+
<div class="form-checkbox-row">
|
|
58
|
+
<Checkbox.Root
|
|
59
|
+
id={attrs.name}
|
|
60
|
+
name={unsubmitted ? undefined : attrs.name}
|
|
61
|
+
class="form-checkbox"
|
|
62
|
+
aria-invalid={attrs['aria-invalid'] as 'false' | 'true' | boolean | undefined}
|
|
63
|
+
checked={attrs.checked as boolean}
|
|
64
|
+
{disabled}
|
|
65
|
+
onCheckedChange={(v) => view.set(v === true)}
|
|
66
|
+
>
|
|
67
|
+
{#snippet children({ checked: isChecked })}
|
|
68
|
+
{#if isChecked}
|
|
69
|
+
<CheckIcon class="form-checkbox-icon" />
|
|
70
|
+
{/if}
|
|
71
|
+
{/snippet}
|
|
72
|
+
</Checkbox.Root>
|
|
73
|
+
{#if children}
|
|
74
|
+
<FieldLabel class="form-checkbox-label" for={attrs.name} {required}>
|
|
75
|
+
{@render children()}
|
|
76
|
+
</FieldLabel>
|
|
77
|
+
{/if}
|
|
78
|
+
</div>
|
|
79
|
+
{#if description}
|
|
80
|
+
<p class="form-description form-description--indented">{description}</p>
|
|
81
|
+
{/if}
|
|
82
|
+
{#each view.issues() as issue (`${issue.path}`)}
|
|
83
|
+
<p class="form-error form-error--indented">{issue.message}</p>
|
|
84
|
+
{/each}
|
|
85
|
+
</div>
|