@zeyolabs/ui 0.0.0-stage → 2026.11.6
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 +171 -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/sidebar/Sidebar.svelte +73 -0
- package/sidebar/SidebarBrand.svelte +32 -0
- package/sidebar/SidebarButton.svelte +31 -0
- package/sidebar/SidebarGroup.svelte +39 -0
- package/sidebar/SidebarItem.svelte +48 -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,327 @@
|
|
|
1
|
+
<script generics="Input extends RemoteFormInput, V extends number | string" lang="ts">
|
|
2
|
+
import type { RemoteForm, RemoteFormInput } from '$app/server';
|
|
3
|
+
import type { Component, Snippet } from 'svelte';
|
|
4
|
+
import type { ZodType } from 'zod';
|
|
5
|
+
|
|
6
|
+
import ChevronsUpDown from '@lucide/svelte/icons/chevrons-up-down';
|
|
7
|
+
import Search from '@lucide/svelte/icons/search';
|
|
8
|
+
import X from '@lucide/svelte/icons/x';
|
|
9
|
+
import { fuzzySearch } from '@zeyolabs/kit/utils/strings/normalize';
|
|
10
|
+
import { Combobox as ComboboxPrimitive } from 'bits-ui';
|
|
11
|
+
import { setContext, tick } from 'svelte';
|
|
12
|
+
import { SvelteMap } from 'svelte/reactivity';
|
|
13
|
+
|
|
14
|
+
import { createFormView, createLocalSelectView, type FieldView } from '../field-view.svelte.js';
|
|
15
|
+
import FieldLabel from '../FieldLabel.svelte';
|
|
16
|
+
import OptionRegistrar from './OptionRegistrar.svelte';
|
|
17
|
+
import {
|
|
18
|
+
SELECT_FIELD_CONTEXT,
|
|
19
|
+
type SelectFieldContext,
|
|
20
|
+
type SelectFieldEntry,
|
|
21
|
+
} from './select-field-context.js';
|
|
22
|
+
|
|
23
|
+
type BaseProps<Input extends RemoteFormInput> = {
|
|
24
|
+
/** The `<Option>` list — declarative, native `<select><option>`-style. */
|
|
25
|
+
children: Snippet;
|
|
26
|
+
class?: string;
|
|
27
|
+
/** Overrides the "no results" row. Receives the current search query. */
|
|
28
|
+
empty?: Snippet<[query: string]>;
|
|
29
|
+
/** Rendered below the option list, inside the panel (e.g. an "add new…" action). */
|
|
30
|
+
footer?: Snippet;
|
|
31
|
+
form?: Omit<RemoteForm<Input, unknown>, 'for'> | RemoteForm<Input, unknown>;
|
|
32
|
+
/** Rendered above the option list, inside the panel (e.g. a hint line). */
|
|
33
|
+
header?: Snippet;
|
|
34
|
+
icon?: Component;
|
|
35
|
+
/** Field label text. Not `children` here — that's the option list. */
|
|
36
|
+
label?: string;
|
|
37
|
+
name: keyof Input & string;
|
|
38
|
+
/** Only meaningful for single-select — ignored when `multiple`. */
|
|
39
|
+
nullable?: boolean;
|
|
40
|
+
onOpenChange?: (open: boolean) => void;
|
|
41
|
+
/** Server-side search: called (debounced) as the user types, and with `''` when the panel
|
|
42
|
+
* reopens. Turns off the built-in client filtering and the built-in "no results" row, so
|
|
43
|
+
* the panel shows exactly the `<Option>`s you render — put your own empty state in the
|
|
44
|
+
* children. */
|
|
45
|
+
onSearch?: (query: string) => void;
|
|
46
|
+
open?: boolean;
|
|
47
|
+
optional?: boolean;
|
|
48
|
+
placeholder: string;
|
|
49
|
+
schema?: ZodType;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
type Props<Input extends RemoteFormInput, V> =
|
|
53
|
+
| (BaseProps<Input> & {
|
|
54
|
+
multiple: true;
|
|
55
|
+
onchange?: (value: V[]) => void;
|
|
56
|
+
value?: V[];
|
|
57
|
+
})
|
|
58
|
+
| (BaseProps<Input> & {
|
|
59
|
+
multiple?: false;
|
|
60
|
+
onchange?: (value: undefined | V) => void;
|
|
61
|
+
value?: undefined | V;
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
let {
|
|
65
|
+
children,
|
|
66
|
+
class: className,
|
|
67
|
+
empty,
|
|
68
|
+
footer,
|
|
69
|
+
form,
|
|
70
|
+
header,
|
|
71
|
+
icon: Icon,
|
|
72
|
+
label,
|
|
73
|
+
multiple = false,
|
|
74
|
+
name,
|
|
75
|
+
nullable = false,
|
|
76
|
+
onchange,
|
|
77
|
+
onOpenChange,
|
|
78
|
+
onSearch,
|
|
79
|
+
open = $bindable(false),
|
|
80
|
+
optional,
|
|
81
|
+
placeholder,
|
|
82
|
+
schema,
|
|
83
|
+
value = $bindable(),
|
|
84
|
+
}: Props<Input, V> = $props();
|
|
85
|
+
|
|
86
|
+
const registry = new SvelteMap<string, SelectFieldEntry>();
|
|
87
|
+
let query = $state('');
|
|
88
|
+
const context: SelectFieldContext = {
|
|
89
|
+
getQuery: () => (onSearch ? '' : query),
|
|
90
|
+
register: (key, entry) => registry.set(key, entry),
|
|
91
|
+
unregister: (key) => registry.delete(key),
|
|
92
|
+
};
|
|
93
|
+
setContext(SELECT_FIELD_CONTEXT, context);
|
|
94
|
+
|
|
95
|
+
let inputEl = $state<HTMLInputElement | null>(null);
|
|
96
|
+
let triggerEl = $state<HTMLElement | null>(null);
|
|
97
|
+
|
|
98
|
+
// The search box only exists while the panel is open. Start every open with an empty query and
|
|
99
|
+
// focus it — bits-ui writes the picked label into the input on select, so a stale value would
|
|
100
|
+
// filter the list down to that one item.
|
|
101
|
+
$effect(() => {
|
|
102
|
+
if (!open) return;
|
|
103
|
+
query = '';
|
|
104
|
+
sendSearch('');
|
|
105
|
+
tick().then(() => inputEl?.focus());
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const SEARCH_DEBOUNCE_MS = 250;
|
|
109
|
+
let searchTimer: ReturnType<typeof setTimeout> | undefined;
|
|
110
|
+
let lastSearch = '';
|
|
111
|
+
function sendSearch(value: string) {
|
|
112
|
+
clearTimeout(searchTimer);
|
|
113
|
+
if (!onSearch || value === lastSearch) return;
|
|
114
|
+
lastSearch = value;
|
|
115
|
+
onSearch(value);
|
|
116
|
+
}
|
|
117
|
+
function scheduleSearch(value: string) {
|
|
118
|
+
clearTimeout(searchTimer);
|
|
119
|
+
searchTimer = setTimeout(() => sendSearch(value), SEARCH_DEBOUNCE_MS);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// `as('select'|'select multiple')` is reached only via the `form` branch — SvelteKit's remote
|
|
123
|
+
// forms natively support both (RemoteFormFieldType resolves 'select multiple' for a string[]
|
|
124
|
+
// field), so no change to field-view.svelte.ts was needed for multi-select.
|
|
125
|
+
// svelte-ignore state_referenced_locally
|
|
126
|
+
const view: FieldView = form
|
|
127
|
+
? createFormView(form, name, multiple ? 'select multiple' : 'select')
|
|
128
|
+
: createLocalSelectView<undefined | V | V[]>({
|
|
129
|
+
get: () => value,
|
|
130
|
+
name,
|
|
131
|
+
schema,
|
|
132
|
+
write: (v) => (value = v as typeof value),
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const attrs = $derived(view.attrs);
|
|
136
|
+
const required = $derived(!optional);
|
|
137
|
+
const displayPlaceholder = $derived(
|
|
138
|
+
label || required || !placeholder ? placeholder : `${placeholder} (Optional)`
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
// bits-ui `Combobox` uses this {value,label} list for typeahead matching, same as `Select`.
|
|
142
|
+
const bitsItems = $derived(
|
|
143
|
+
[...registry.entries()].map(([key, entry]) => ({ label: entry.label, value: key }))
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
const hasVisibleOptions = $derived(
|
|
147
|
+
!!onSearch ||
|
|
148
|
+
[...registry.values()].some((entry) => query.trim() === '' || fuzzySearch(query, entry.label))
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
// Form mode stores the submitted key(s); standalone tracks the typed value(s), keyed the same
|
|
152
|
+
// way `<Option>` derives its own key (`String(value)`) — same split `SelectField` uses, just
|
|
153
|
+
// branched on `multiple`.
|
|
154
|
+
const selectedKey = $derived(
|
|
155
|
+
!multiple ? (form ? (attrs.value as string) : String(value ?? '')) : ''
|
|
156
|
+
);
|
|
157
|
+
const selectedKeys = $derived(
|
|
158
|
+
multiple
|
|
159
|
+
? form
|
|
160
|
+
? ((attrs.value as string[] | undefined) ?? [])
|
|
161
|
+
: ((value as undefined | V[]) ?? []).map((v) => String(v))
|
|
162
|
+
: []
|
|
163
|
+
);
|
|
164
|
+
const selectedLabel = $derived(registry.get(selectedKey)?.label);
|
|
165
|
+
const selectedChips = $derived(
|
|
166
|
+
selectedKeys
|
|
167
|
+
.map((key) => ({ key, label: registry.get(key)?.label ?? key }))
|
|
168
|
+
.filter((chip) => chip.label)
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
function handleSingleChange(key: string) {
|
|
172
|
+
const typedValue = registry.get(key)?.value as undefined | V;
|
|
173
|
+
view.set(typedValue);
|
|
174
|
+
(onchange as ((value: undefined | V) => void) | undefined)?.(typedValue);
|
|
175
|
+
tick().then(() => (query = ''));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function handleMultiChange(keys: string[]) {
|
|
179
|
+
const typedValues = keys
|
|
180
|
+
.map((key) => registry.get(key)?.value)
|
|
181
|
+
.filter((v): v is V => v !== undefined);
|
|
182
|
+
view.set(typedValues);
|
|
183
|
+
(onchange as ((value: V[]) => void) | undefined)?.(typedValues);
|
|
184
|
+
tick().then(() => (query = ''));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function removeChip(key: string) {
|
|
188
|
+
handleMultiChange(selectedKeys.filter((k) => k !== key));
|
|
189
|
+
}
|
|
190
|
+
</script>
|
|
191
|
+
|
|
192
|
+
<div class="form-field">
|
|
193
|
+
{#if label}
|
|
194
|
+
<FieldLabel for={attrs.name} {required}>{label}</FieldLabel>
|
|
195
|
+
{/if}
|
|
196
|
+
|
|
197
|
+
{#snippet panel()}
|
|
198
|
+
<ComboboxPrimitive.Portal>
|
|
199
|
+
<ComboboxPrimitive.Content
|
|
200
|
+
class="form-select-menu form-combobox-menu"
|
|
201
|
+
customAnchor={triggerEl}
|
|
202
|
+
sideOffset={4}
|
|
203
|
+
>
|
|
204
|
+
<div class="form-combobox-search">
|
|
205
|
+
<Search class="form-combobox-search-icon" size={14} />
|
|
206
|
+
<ComboboxPrimitive.Input bind:ref={inputEl}>
|
|
207
|
+
{#snippet child({ props })}
|
|
208
|
+
<input
|
|
209
|
+
{...props}
|
|
210
|
+
class="form-combobox-search-input"
|
|
211
|
+
oninput={(e) => {
|
|
212
|
+
query = e.currentTarget.value;
|
|
213
|
+
scheduleSearch(query);
|
|
214
|
+
(props.oninput as ((e: Event) => void) | undefined)?.(e);
|
|
215
|
+
}}
|
|
216
|
+
placeholder="Search…"
|
|
217
|
+
value={query}
|
|
218
|
+
/>
|
|
219
|
+
{/snippet}
|
|
220
|
+
</ComboboxPrimitive.Input>
|
|
221
|
+
</div>
|
|
222
|
+
{#if header}<div class="form-combobox-header">{@render header()}</div>{/if}
|
|
223
|
+
<ComboboxPrimitive.Viewport class="form-combobox-list">
|
|
224
|
+
{@render children()}
|
|
225
|
+
{#if !hasVisibleOptions}
|
|
226
|
+
{#if empty}
|
|
227
|
+
{@render empty(query)}
|
|
228
|
+
{:else}
|
|
229
|
+
<div class="form-select-empty">No results for "{query}"</div>
|
|
230
|
+
{/if}
|
|
231
|
+
{/if}
|
|
232
|
+
</ComboboxPrimitive.Viewport>
|
|
233
|
+
{#if footer}<div class="form-combobox-footer">{@render footer()}</div>{/if}
|
|
234
|
+
</ComboboxPrimitive.Content>
|
|
235
|
+
</ComboboxPrimitive.Portal>
|
|
236
|
+
{/snippet}
|
|
237
|
+
|
|
238
|
+
{#if multiple}
|
|
239
|
+
<ComboboxPrimitive.Root
|
|
240
|
+
items={bitsItems}
|
|
241
|
+
{onOpenChange}
|
|
242
|
+
onValueChange={(keys) => handleMultiChange(keys)}
|
|
243
|
+
{required}
|
|
244
|
+
type="multiple"
|
|
245
|
+
value={selectedKeys}
|
|
246
|
+
bind:open
|
|
247
|
+
>
|
|
248
|
+
<OptionRegistrar {context}>{@render children()}</OptionRegistrar>
|
|
249
|
+
<ComboboxPrimitive.Trigger bind:ref={triggerEl}>
|
|
250
|
+
{#snippet child({ props })}
|
|
251
|
+
<div
|
|
252
|
+
{...props}
|
|
253
|
+
class={['form-input', 'form-select', 'form-combobox', 'form-combobox-multi', className]
|
|
254
|
+
.filter(Boolean)
|
|
255
|
+
.join(' ')}
|
|
256
|
+
role="combobox"
|
|
257
|
+
tabindex="0"
|
|
258
|
+
>
|
|
259
|
+
{#if Icon}<Icon class="form-input-icon" />{/if}
|
|
260
|
+
<div class="form-combobox-chips">
|
|
261
|
+
{#each selectedChips as chip (chip.key)}
|
|
262
|
+
<span class="form-combobox-chip">
|
|
263
|
+
{chip.label}
|
|
264
|
+
<button
|
|
265
|
+
class="form-combobox-chip-remove"
|
|
266
|
+
aria-label="Remove {chip.label}"
|
|
267
|
+
onclick={(e) => {
|
|
268
|
+
e.stopPropagation();
|
|
269
|
+
removeChip(chip.key);
|
|
270
|
+
}}
|
|
271
|
+
onpointerdown={(e) => e.stopPropagation()}
|
|
272
|
+
type="button"
|
|
273
|
+
>
|
|
274
|
+
<X size={11} />
|
|
275
|
+
</button>
|
|
276
|
+
</span>
|
|
277
|
+
{/each}
|
|
278
|
+
{#if selectedChips.length === 0}
|
|
279
|
+
<span class="form-select-value form-select--placeholder">{displayPlaceholder}</span>
|
|
280
|
+
{/if}
|
|
281
|
+
</div>
|
|
282
|
+
<ChevronsUpDown class="form-select-chevron" size={14} />
|
|
283
|
+
</div>
|
|
284
|
+
{/snippet}
|
|
285
|
+
</ComboboxPrimitive.Trigger>
|
|
286
|
+
{@render panel()}
|
|
287
|
+
</ComboboxPrimitive.Root>
|
|
288
|
+
{#each selectedKeys as key (key)}
|
|
289
|
+
<input name={attrs.name} type="hidden" value={key} />
|
|
290
|
+
{/each}
|
|
291
|
+
{:else}
|
|
292
|
+
<ComboboxPrimitive.Root
|
|
293
|
+
allowDeselect={nullable}
|
|
294
|
+
items={bitsItems}
|
|
295
|
+
{onOpenChange}
|
|
296
|
+
onValueChange={(key) => handleSingleChange(key)}
|
|
297
|
+
{required}
|
|
298
|
+
type="single"
|
|
299
|
+
value={selectedKey}
|
|
300
|
+
bind:open
|
|
301
|
+
>
|
|
302
|
+
<OptionRegistrar {context}>{@render children()}</OptionRegistrar>
|
|
303
|
+
<ComboboxPrimitive.Trigger
|
|
304
|
+
class={[
|
|
305
|
+
'form-input',
|
|
306
|
+
'form-select',
|
|
307
|
+
'form-combobox',
|
|
308
|
+
!selectedLabel && 'form-select--placeholder',
|
|
309
|
+
className,
|
|
310
|
+
]
|
|
311
|
+
.filter(Boolean)
|
|
312
|
+
.join(' ')}
|
|
313
|
+
bind:ref={triggerEl}
|
|
314
|
+
>
|
|
315
|
+
{#if Icon}<Icon class="form-input-icon" />{/if}
|
|
316
|
+
<span class="form-select-value">{selectedLabel ?? displayPlaceholder}</span>
|
|
317
|
+
<ChevronsUpDown class="form-select-chevron" size={14} />
|
|
318
|
+
</ComboboxPrimitive.Trigger>
|
|
319
|
+
{@render panel()}
|
|
320
|
+
</ComboboxPrimitive.Root>
|
|
321
|
+
<input name={attrs.name} type="hidden" value={selectedKey} />
|
|
322
|
+
{/if}
|
|
323
|
+
|
|
324
|
+
{#each view.issues() as issue (`${issue.path}`)}
|
|
325
|
+
<p class="form-error">{issue.message}</p>
|
|
326
|
+
{/each}
|
|
327
|
+
</div>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Component, Snippet } from 'svelte';
|
|
3
|
+
|
|
4
|
+
interface Props {
|
|
5
|
+
children: Snippet;
|
|
6
|
+
icon?: Component;
|
|
7
|
+
onclick: () => void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let { children, icon: Icon, onclick }: Props = $props();
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
<button class="form-combobox-action" {onclick} type="button">
|
|
14
|
+
{#if Icon}<Icon size={13} />{/if}
|
|
15
|
+
{@render children()}
|
|
16
|
+
</button>
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<script generics="Input extends RemoteFormInput, T = string" 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 Field from '../Field.svelte';
|
|
7
|
+
|
|
8
|
+
interface Props {
|
|
9
|
+
children?: Snippet;
|
|
10
|
+
form?: Omit<RemoteForm<Input, unknown>, 'for'> | RemoteForm<Input, unknown>;
|
|
11
|
+
name: keyof Input & string;
|
|
12
|
+
onChange?: (value: T) => void;
|
|
13
|
+
onInput?: (value: T) => void;
|
|
14
|
+
optional?: boolean;
|
|
15
|
+
schema?: ZodType;
|
|
16
|
+
value?: T;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
let {
|
|
20
|
+
children,
|
|
21
|
+
form,
|
|
22
|
+
name,
|
|
23
|
+
onChange,
|
|
24
|
+
onInput,
|
|
25
|
+
optional,
|
|
26
|
+
schema,
|
|
27
|
+
value = $bindable(),
|
|
28
|
+
}: Props = $props();
|
|
29
|
+
</script>
|
|
30
|
+
|
|
31
|
+
<Field {name} {children} {form} {onChange} {onInput} {optional} {schema} type="date" bind:value />
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
<script generics="Input extends RemoteFormInput" lang="ts">
|
|
2
|
+
import type { RemoteForm, RemoteFormInput } from '$app/server';
|
|
3
|
+
import type { CalendarDate } from '@internationalized/date';
|
|
4
|
+
import type { Snippet } from 'svelte';
|
|
5
|
+
import type { ZodType } from 'zod';
|
|
6
|
+
|
|
7
|
+
import { type DateValue, parseDate } from '@internationalized/date';
|
|
8
|
+
import Calendar from '@lucide/svelte/icons/calendar';
|
|
9
|
+
import ChevronLeft from '@lucide/svelte/icons/chevron-left';
|
|
10
|
+
import ChevronRight from '@lucide/svelte/icons/chevron-right';
|
|
11
|
+
import { Datetime } from '@zeyolabs/kit/utils/datetime';
|
|
12
|
+
import { type DateRange, Popover, RangeCalendar } from 'bits-ui';
|
|
13
|
+
|
|
14
|
+
import { createFormView, createLocalSelectView, type FieldView } from '../field-view.svelte.js';
|
|
15
|
+
import FieldLabel from '../FieldLabel.svelte';
|
|
16
|
+
|
|
17
|
+
interface Props {
|
|
18
|
+
children?: Snippet;
|
|
19
|
+
endName: keyof Input & string;
|
|
20
|
+
endSchema?: ZodType;
|
|
21
|
+
endValue?: string;
|
|
22
|
+
form?: Omit<RemoteForm<Input, unknown>, 'for'> | RemoteForm<Input, unknown>;
|
|
23
|
+
onChange?: (range: { end: string; start: string }) => void;
|
|
24
|
+
optional?: boolean;
|
|
25
|
+
startName: keyof Input & string;
|
|
26
|
+
startSchema?: ZodType;
|
|
27
|
+
startValue?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let {
|
|
31
|
+
children,
|
|
32
|
+
endName,
|
|
33
|
+
endSchema,
|
|
34
|
+
endValue = $bindable(''),
|
|
35
|
+
form,
|
|
36
|
+
onChange,
|
|
37
|
+
optional,
|
|
38
|
+
startName,
|
|
39
|
+
startSchema,
|
|
40
|
+
startValue = $bindable(''),
|
|
41
|
+
}: Props = $props();
|
|
42
|
+
|
|
43
|
+
// `as('date')` is reached only via the `form` branch; standalone uses local state.
|
|
44
|
+
// svelte-ignore state_referenced_locally
|
|
45
|
+
const startView: FieldView = form
|
|
46
|
+
? createFormView(form, startName, 'date')
|
|
47
|
+
: createLocalSelectView<string>({
|
|
48
|
+
get: () => startValue,
|
|
49
|
+
name: startName,
|
|
50
|
+
schema: startSchema,
|
|
51
|
+
write: (v) => (startValue = v),
|
|
52
|
+
});
|
|
53
|
+
// svelte-ignore state_referenced_locally
|
|
54
|
+
const endView: FieldView = form
|
|
55
|
+
? createFormView(form, endName, 'date')
|
|
56
|
+
: createLocalSelectView<string>({
|
|
57
|
+
get: () => endValue,
|
|
58
|
+
name: endName,
|
|
59
|
+
schema: endSchema,
|
|
60
|
+
write: (v) => (endValue = v),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const required = $derived(!optional);
|
|
64
|
+
const startAttrs = $derived(startView.attrs);
|
|
65
|
+
const endAttrs = $derived(endView.attrs);
|
|
66
|
+
|
|
67
|
+
let open = $state(false);
|
|
68
|
+
|
|
69
|
+
function toDateValue(iso: unknown): DateValue | undefined {
|
|
70
|
+
if (typeof iso !== 'string' || !iso) return undefined;
|
|
71
|
+
try {
|
|
72
|
+
return parseDate(iso);
|
|
73
|
+
} catch {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function toIso(date: DateValue | undefined): string {
|
|
79
|
+
return date ? date.toString() : '';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const calendarValue: DateRange = $derived({
|
|
83
|
+
end: toDateValue(endAttrs.value),
|
|
84
|
+
start: toDateValue(startAttrs.value),
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const displayLabel = $derived(
|
|
88
|
+
calendarValue.start && calendarValue.end
|
|
89
|
+
? `${Datetime.formatDateFull(calendarValue.start as CalendarDate)} – ${Datetime.formatDateFull(calendarValue.end as CalendarDate)}`
|
|
90
|
+
: 'Select date range'
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
// Scratch copy the calendar edits freely while the range is incomplete, so
|
|
94
|
+
// picking a start day doesn't blank/validate the committed end field. Only
|
|
95
|
+
// a complete range gets written back via `.set()`.
|
|
96
|
+
let localRange: DateRange = $state({ end: undefined, start: undefined });
|
|
97
|
+
|
|
98
|
+
$effect(() => {
|
|
99
|
+
if (!open) localRange = calendarValue;
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
function handleRangeChange(range: DateRange) {
|
|
103
|
+
localRange = range;
|
|
104
|
+
if (!range.start || !range.end) return;
|
|
105
|
+
|
|
106
|
+
const startIso = toIso(range.start);
|
|
107
|
+
const endIso = toIso(range.end);
|
|
108
|
+
startView.set(startIso);
|
|
109
|
+
endView.set(endIso);
|
|
110
|
+
onChange?.({ end: endIso, start: startIso });
|
|
111
|
+
open = false;
|
|
112
|
+
}
|
|
113
|
+
</script>
|
|
114
|
+
|
|
115
|
+
<div class="form-field">
|
|
116
|
+
{#if children}
|
|
117
|
+
<FieldLabel for={startAttrs.name} {required}>{@render children()}</FieldLabel>
|
|
118
|
+
{/if}
|
|
119
|
+
<Popover.Root bind:open>
|
|
120
|
+
<Popover.Trigger
|
|
121
|
+
id={startAttrs.name}
|
|
122
|
+
class={[
|
|
123
|
+
'form-input',
|
|
124
|
+
'form-select',
|
|
125
|
+
'form-input--with-icon',
|
|
126
|
+
!(calendarValue.start && calendarValue.end) && 'form-select--placeholder',
|
|
127
|
+
]
|
|
128
|
+
.filter(Boolean)
|
|
129
|
+
.join(' ')}
|
|
130
|
+
>
|
|
131
|
+
<div class="form-input-icon"><Calendar size={14} /></div>
|
|
132
|
+
<span class="form-select-value">{displayLabel}</span>
|
|
133
|
+
</Popover.Trigger>
|
|
134
|
+
<Popover.Portal>
|
|
135
|
+
<Popover.Content class="form-daterange-menu" sideOffset={4}>
|
|
136
|
+
<RangeCalendar.Root
|
|
137
|
+
onValueChange={handleRangeChange}
|
|
138
|
+
value={localRange}
|
|
139
|
+
weekdayFormat="short"
|
|
140
|
+
>
|
|
141
|
+
{#snippet children({ months, weekdays })}
|
|
142
|
+
{#each months as month (month.value.toString())}
|
|
143
|
+
<div class="form-daterange-header">
|
|
144
|
+
<RangeCalendar.PrevButton class="form-daterange-nav">
|
|
145
|
+
<ChevronLeft size={14} />
|
|
146
|
+
</RangeCalendar.PrevButton>
|
|
147
|
+
<RangeCalendar.Heading class="form-daterange-heading" />
|
|
148
|
+
<RangeCalendar.NextButton class="form-daterange-nav">
|
|
149
|
+
<ChevronRight size={14} />
|
|
150
|
+
</RangeCalendar.NextButton>
|
|
151
|
+
</div>
|
|
152
|
+
<RangeCalendar.Grid class="form-daterange-grid">
|
|
153
|
+
<RangeCalendar.GridHead>
|
|
154
|
+
<RangeCalendar.GridRow class="form-daterange-row">
|
|
155
|
+
{#each weekdays as weekday (weekday)}
|
|
156
|
+
<RangeCalendar.HeadCell class="form-daterange-headcell">
|
|
157
|
+
{weekday.slice(0, 2)}
|
|
158
|
+
</RangeCalendar.HeadCell>
|
|
159
|
+
{/each}
|
|
160
|
+
</RangeCalendar.GridRow>
|
|
161
|
+
</RangeCalendar.GridHead>
|
|
162
|
+
<RangeCalendar.GridBody>
|
|
163
|
+
{#each month.weeks as weekDates (weekDates[0].toString())}
|
|
164
|
+
<RangeCalendar.GridRow class="form-daterange-row">
|
|
165
|
+
{#each weekDates as date (date.toString())}
|
|
166
|
+
<RangeCalendar.Cell class="form-daterange-cell" {date} month={month.value}>
|
|
167
|
+
<RangeCalendar.Day class="form-daterange-day" />
|
|
168
|
+
</RangeCalendar.Cell>
|
|
169
|
+
{/each}
|
|
170
|
+
</RangeCalendar.GridRow>
|
|
171
|
+
{/each}
|
|
172
|
+
</RangeCalendar.GridBody>
|
|
173
|
+
</RangeCalendar.Grid>
|
|
174
|
+
{/each}
|
|
175
|
+
{/snippet}
|
|
176
|
+
</RangeCalendar.Root>
|
|
177
|
+
</Popover.Content>
|
|
178
|
+
</Popover.Portal>
|
|
179
|
+
</Popover.Root>
|
|
180
|
+
<input name={startAttrs.name} type="hidden" value={startAttrs.value} />
|
|
181
|
+
<input name={endAttrs.name} type="hidden" value={endAttrs.value} />
|
|
182
|
+
{#each startView.issues() as issue (`start-${issue.path}`)}
|
|
183
|
+
<p class="form-error">{issue.message}</p>
|
|
184
|
+
{/each}
|
|
185
|
+
{#each endView.issues() as issue (`end-${issue.path}`)}
|
|
186
|
+
<p class="form-error">{issue.message}</p>
|
|
187
|
+
{/each}
|
|
188
|
+
</div>
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
<script lang="ts" module>
|
|
2
|
+
import type { CalendarDate } from '@internationalized/date';
|
|
3
|
+
|
|
4
|
+
export type DateRangePresetOption<Value extends string = string> = {
|
|
5
|
+
days: number;
|
|
6
|
+
label: string;
|
|
7
|
+
value: Value;
|
|
8
|
+
};
|
|
9
|
+
export type DateRangeValue<Preset extends string = string> =
|
|
10
|
+
{ from: CalendarDate; kind: 'custom'; to: CalendarDate } | { kind: 'preset'; preset: Preset };
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
<script generics="P extends string" lang="ts">
|
|
14
|
+
import { parseDate } from '@internationalized/date';
|
|
15
|
+
import { Datetime, ServerDt } from '@zeyolabs/kit/utils/datetime';
|
|
16
|
+
import { untrack } from 'svelte';
|
|
17
|
+
|
|
18
|
+
import DateRangeField from './DateRangeField.svelte';
|
|
19
|
+
import SelectField from './SelectField.svelte';
|
|
20
|
+
|
|
21
|
+
interface Props {
|
|
22
|
+
/** Preset selected on mount, and used as the fallback if a custom range is invalid.
|
|
23
|
+
* Defaults to the first preset. */
|
|
24
|
+
initial?: P;
|
|
25
|
+
/** Widest span (in days) a custom range may cover. A wider pick has its start date pulled
|
|
26
|
+
* forward to fit — the same silent-correction this field already applies to an inverted
|
|
27
|
+
* range — so the emitted value never violates a matching server-side schema cap. */
|
|
28
|
+
maxSpanDays?: number;
|
|
29
|
+
presets: DateRangePresetOption<P>[];
|
|
30
|
+
value?: DateRangeValue<P>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let { initial, maxSpanDays, presets, value = $bindable() }: Props = $props();
|
|
34
|
+
|
|
35
|
+
// svelte-ignore state_referenced_locally
|
|
36
|
+
const fallback = (initial ?? presets[0]?.value) as P;
|
|
37
|
+
let selected = $state<'custom' | P>(fallback);
|
|
38
|
+
// svelte-ignore state_referenced_locally
|
|
39
|
+
let customFrom = $state(
|
|
40
|
+
Datetime.formatDateISO(ServerDt.today().subtract({ days: presets[0]?.days ?? 7 }))
|
|
41
|
+
);
|
|
42
|
+
let customTo = $state(Datetime.formatDateISO(ServerDt.today()));
|
|
43
|
+
|
|
44
|
+
function applyPreset(v: 'custom' | P | undefined) {
|
|
45
|
+
const preset = presets.find((p) => p.value === v);
|
|
46
|
+
if (!preset) return;
|
|
47
|
+
customTo = Datetime.formatDateISO(ServerDt.today());
|
|
48
|
+
customFrom = Datetime.formatDateISO(ServerDt.today().subtract({ days: preset.days }));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const selectItems = $derived([
|
|
52
|
+
...presets.map((p) => ({ label: p.label, value: p.value as 'custom' | P })),
|
|
53
|
+
{ label: 'Custom range…', value: 'custom' as const },
|
|
54
|
+
]);
|
|
55
|
+
|
|
56
|
+
function sameRange(a: DateRangeValue<P> | undefined, b: DateRangeValue<P>): boolean {
|
|
57
|
+
if (!a) return false;
|
|
58
|
+
if (a.kind === 'preset') return b.kind === 'preset' && a.preset === b.preset;
|
|
59
|
+
return b.kind === 'custom' && a.from.compare(b.from) === 0 && a.to.compare(b.to) === 0;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
$effect(() => {
|
|
63
|
+
let next: DateRangeValue<P>;
|
|
64
|
+
if (selected === 'custom') {
|
|
65
|
+
let from = parseDate(customFrom);
|
|
66
|
+
const to = parseDate(customTo);
|
|
67
|
+
if (from.compare(to) > 0) {
|
|
68
|
+
next = { kind: 'preset', preset: fallback };
|
|
69
|
+
} else {
|
|
70
|
+
if (maxSpanDays !== undefined) {
|
|
71
|
+
const clamped = Datetime.clampSpanStart(from, to, maxSpanDays);
|
|
72
|
+
if (clamped.compare(from) !== 0) {
|
|
73
|
+
from = clamped;
|
|
74
|
+
// Reflect the applied start date in the field; the write settles on the next
|
|
75
|
+
// effect run (the clamped span no longer exceeds the limit).
|
|
76
|
+
customFrom = Datetime.formatDateISO(clamped);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
next = { from, kind: 'custom', to };
|
|
80
|
+
}
|
|
81
|
+
} else {
|
|
82
|
+
next = { kind: 'preset', preset: selected };
|
|
83
|
+
}
|
|
84
|
+
// Write only on a real change. An identical-content write is still a NEW object, and
|
|
85
|
+
// consumers deriving off the bound value re-run on identity — on mount that re-fired
|
|
86
|
+
// every query keyed on the range even though it never changed.
|
|
87
|
+
const current = untrack(() => value);
|
|
88
|
+
if (!sameRange(current, next)) value = next;
|
|
89
|
+
});
|
|
90
|
+
</script>
|
|
91
|
+
|
|
92
|
+
<div class="form-daterange-preset flex items-center gap-2">
|
|
93
|
+
<div class="min-w-40">
|
|
94
|
+
<SelectField
|
|
95
|
+
name="rangePreset"
|
|
96
|
+
getKey={(i) => i.value}
|
|
97
|
+
getLabel={(i) => i.label}
|
|
98
|
+
getValue={(i) => i.value}
|
|
99
|
+
items={selectItems}
|
|
100
|
+
onchange={applyPreset}
|
|
101
|
+
placeholder="Date range"
|
|
102
|
+
bind:value={selected}
|
|
103
|
+
/>
|
|
104
|
+
</div>
|
|
105
|
+
{#if selected === 'custom'}
|
|
106
|
+
<DateRangeField
|
|
107
|
+
endName="to"
|
|
108
|
+
startName="from"
|
|
109
|
+
bind:startValue={customFrom}
|
|
110
|
+
bind:endValue={customTo}
|
|
111
|
+
/>
|
|
112
|
+
{/if}
|
|
113
|
+
</div>
|
|
114
|
+
|
|
115
|
+
<style>
|
|
116
|
+
.form-daterange-preset :global(.form-field) {
|
|
117
|
+
margin: 0;
|
|
118
|
+
}
|
|
119
|
+
</style>
|