@lavalogic/scoria 0.40.1 → 0.40.2
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 +58 -58
- package/dist/Components/AccordionGroup.svelte +46 -46
- package/dist/Components/AccordionGroupButton.svelte +76 -76
- package/dist/Components/Action.svelte +136 -136
- package/dist/Components/AlertModal.svelte +195 -195
- package/dist/Components/AppointmentCalendar/AppointmentCalendar.svelte +729 -729
- package/dist/Components/Base/BaseModal.svelte +125 -125
- package/dist/Components/Base/ContextWrapper.svelte +72 -72
- package/dist/Components/Base/Pagination.svelte +198 -198
- package/dist/Components/Base/Snippets.svelte +36 -36
- package/dist/Components/BigRadioSet.svelte +38 -38
- package/dist/Components/Bubble.svelte +101 -101
- package/dist/Components/Button.svelte +209 -209
- package/dist/Components/Checkbox.svelte +86 -86
- package/dist/Components/CollapsibleCard.svelte +78 -78
- package/dist/Components/Contexts/ContextMenu.svelte +164 -164
- package/dist/Components/Contexts/ContextMenuDivider.svelte +15 -15
- package/dist/Components/Contexts/ContextMenuOption.svelte +60 -60
- package/dist/Components/Contexts/Toast.svelte +86 -86
- package/dist/Components/Contexts/ToastContainer.svelte +65 -65
- package/dist/Components/Contexts/Tooltip.svelte +37 -37
- package/dist/Components/DataMatrixIcon.svelte +112 -112
- package/dist/Components/DateInput.svelte +148 -148
- package/dist/Components/DatePicker.svelte +300 -300
- package/dist/Components/DesktopModal.svelte +90 -90
- package/dist/Components/Dial.svelte +204 -204
- package/dist/Components/DimensionInput.svelte +170 -170
- package/dist/Components/DragMenu/DragMenuHolder.svelte +119 -119
- package/dist/Components/DragMenu/DraggableCard.svelte +77 -77
- package/dist/Components/FileCard.svelte +24 -24
- package/dist/Components/FileUpload.svelte +236 -236
- package/dist/Components/GridInput.svelte +196 -196
- package/dist/Components/GridInputSet.svelte +29 -29
- package/dist/Components/HorizontalTabGroup.svelte +82 -82
- package/dist/Components/HorizontalTabGroupButton.svelte +44 -44
- package/dist/Components/Icon.svelte +128 -128
- package/dist/Components/Icons.js +357 -357
- package/dist/Components/InfoAlert.svelte +35 -35
- package/dist/Components/LoadingAnimation.svelte +29 -29
- package/dist/Components/LoadingModal.svelte +45 -45
- package/dist/Components/LoadingOverlay.svelte +25 -25
- package/dist/Components/Modal.svelte +258 -258
- package/dist/Components/MultiSelect.svelte +220 -220
- package/dist/Components/Nav/Nav.svelte +110 -110
- package/dist/Components/Nav/NavServiceMenuList.svelte +43 -43
- package/dist/Components/Nav/NavTab.svelte +62 -62
- package/dist/Components/Nav/ServiceMenuItem.svelte +112 -112
- package/dist/Components/Nav/ServicesNav.svelte +80 -80
- package/dist/Components/NumberInput.svelte +157 -157
- package/dist/Components/PageHeading.svelte +25 -25
- package/dist/Components/PasswordInput.svelte +88 -88
- package/dist/Components/Portal.svelte +36 -36
- package/dist/Components/ProgressBubble.svelte +107 -107
- package/dist/Components/QuerySelect.svelte +366 -366
- package/dist/Components/SingleSelect.svelte +250 -250
- package/dist/Components/StepButton.svelte +70 -70
- package/dist/Components/StepForm.svelte +140 -140
- package/dist/Components/Switch.svelte +69 -69
- package/dist/Components/Table/ARCHITECTURE.md +298 -298
- package/dist/Components/Table/Body/QuickSearchCell.svelte +679 -679
- package/dist/Components/Table/Body/Rows/ColumnHighlight.svelte +51 -51
- package/dist/Components/Table/Body/Rows/ColumnList.svelte +59 -59
- package/dist/Components/Table/Body/Rows/ColumnListRow.svelte +454 -454
- package/dist/Components/Table/Body/Rows/Columns/AccessorComponent.svelte +79 -79
- package/dist/Components/Table/Body/Rows/Columns/ActionsCell.svelte +185 -185
- package/dist/Components/Table/Body/Rows/Columns/BubbleCell.svelte +124 -124
- package/dist/Components/Table/Body/Rows/Columns/ExpandCell.svelte +131 -120
- package/dist/Components/Table/Body/Rows/Columns/FocusableImmutableCell.svelte +234 -234
- package/dist/Components/Table/Body/Rows/Columns/ProgressCell.svelte +105 -105
- package/dist/Components/Table/Body/Rows/Columns/TableAction.svelte +87 -87
- package/dist/Components/Table/Body/Rows/Columns/TableCheckbox.svelte +126 -126
- package/dist/Components/Table/Body/Rows/Columns/TableColumn.svelte +159 -159
- package/dist/Components/Table/Body/Rows/Columns/TableDatePicker.svelte +246 -246
- package/dist/Components/Table/Body/Rows/Columns/TableNumberInput.svelte +216 -216
- package/dist/Components/Table/Body/Rows/Columns/TableQuerySelect.svelte +252 -252
- package/dist/Components/Table/Body/Rows/Columns/TableRowSelectionCheckbox.svelte +179 -179
- package/dist/Components/Table/Body/Rows/Columns/TableSelect.svelte +221 -221
- package/dist/Components/Table/Body/Rows/Columns/TableTextInput.svelte +197 -197
- package/dist/Components/Table/Body/Rows/Columns/ValidityCell.svelte +190 -190
- package/dist/Components/Table/Body/Rows/TableRow.svelte +290 -290
- package/dist/Components/Table/Body/TableBody.svelte +155 -155
- package/dist/Components/Table/ColumnFactory.svelte.js +1 -0
- package/dist/Components/Table/Headers/HighlightAllHeader.svelte +41 -41
- package/dist/Components/Table/Headers/SelectAllHeader.svelte +30 -30
- package/dist/Components/Table/Headers/TableHead.svelte +415 -415
- package/dist/Components/Table/Misc/ColumnPanel.svelte +39 -39
- package/dist/Components/Table/Misc/ColumnPanelModal.svelte +39 -39
- package/dist/Components/Table/Misc/FilterInput.svelte +641 -641
- package/dist/Components/Table/Misc/FilterPanel.svelte +190 -190
- package/dist/Components/Table/Misc/FilterPanelModal.svelte +31 -31
- package/dist/Components/Table/Misc/ShareViewModal.svelte +291 -291
- package/dist/Components/Table/Misc/SidePanel.svelte +35 -35
- package/dist/Components/Table/Misc/TableConfigurationModal.svelte +644 -644
- package/dist/Components/Table/Misc/TableFooter.svelte +340 -340
- package/dist/Components/Table/Misc/TableHorizontalBar.svelte +140 -140
- package/dist/Components/Table/Misc/TableSidebar.svelte +200 -200
- package/dist/Components/Table/Misc/TableViewDropdown.svelte +105 -105
- package/dist/Components/Table/Misc/ToolboxPanel.svelte +100 -100
- package/dist/Components/Table/Misc/ToolboxPanelModal.svelte +107 -107
- package/dist/Components/Table/Misc/UpdateViewOptionsModal.svelte +192 -192
- package/dist/Components/Table/NestedTable.svelte +130 -130
- package/dist/Components/Table/Table.svelte +692 -692
- package/dist/Components/Table/Types/Columns/Definitions/Expand/ExpandDef.svelte.d.ts +5 -0
- package/dist/Components/Table/Types/Columns/Definitions/Expand/ExpandDef.svelte.js +3 -0
- package/dist/Components/Table/Types/Public/ColumnOptions.d.ts +10 -0
- package/dist/Components/Test/TestContextConsumer.svelte +13 -13
- package/dist/Components/TextArea.svelte +130 -130
- package/dist/Components/TextInput.svelte +176 -176
- package/dist/Components/ThreeStateRadio.svelte +131 -131
- package/dist/Components/Touch/BoolCard.svelte +45 -45
- package/dist/Components/Touch/DateModal.svelte +209 -209
- package/dist/Components/Touch/EditCard.svelte +59 -59
- package/dist/Components/Touch/InfoCard.svelte +31 -31
- package/dist/Components/Touch/InfoTextCard.svelte +28 -28
- package/dist/Components/Touch/ModalExplanation.svelte +17 -17
- package/dist/Components/Touch/NumberKeyboard.svelte +225 -225
- package/dist/Components/Touch/NumberModal.svelte +79 -79
- package/dist/Components/Touch/PlusMinusCard.svelte +46 -46
- package/dist/Components/Touch/SelectModal.svelte +127 -127
- package/dist/Components/Touch/SummaryCard.svelte +93 -93
- package/dist/Components/Touch/TextAreaModal.svelte +95 -95
- package/dist/Components/Touch/TextModal.svelte +106 -106
- package/dist/Components/Touch/TouchCard.svelte +33 -33
- package/dist/Components/Touch/TouchModal.svelte +274 -274
- package/dist/Components/Touch/UtilityButton.svelte +109 -109
- package/dist/Components/VerticalTabGroup.svelte +79 -79
- package/dist/Components/VerticalTabGroupButton.svelte +58 -58
- package/dist/Helpers/Datamatrix.d.ts +19 -19
- package/dist/Helpers/Datamatrix.js +291 -291
- package/dist/Types/Examples/ExampleModal.svelte +35 -35
- package/dist/scss/_basics.scss +12 -12
- package/dist/scss/_colours.scss +134 -134
- package/dist/scss/_mixins.scss +3392 -3392
- package/dist/scss/_motion.scss +57 -57
- package/dist/scss/_sizing.scss +85 -85
- package/dist/scss/_transitions.scss +8 -8
- package/package.json +1 -1
|
@@ -1,368 +1,368 @@
|
|
|
1
|
-
<svelte:options runes />
|
|
2
|
-
|
|
3
|
-
<script
|
|
4
|
-
lang="ts"
|
|
5
|
-
generics="V extends object, VP extends ValueProp<V>"
|
|
6
|
-
>
|
|
7
|
-
import { ToastContext } from '../Contexts/ToastContext.svelte.js';
|
|
8
|
-
|
|
9
|
-
import { dev } from '$app/environment';
|
|
10
|
-
import { devCatch, generateWeakId, vowels } from '../Helpers/Helpers.svelte.js';
|
|
11
|
-
import { attachDropdownPortal } from '../Helpers/dropdownPortal.js';
|
|
12
|
-
import { Colour, ColourSet } from '../scss/colours.js';
|
|
13
|
-
import type { ValueProp } from '../Types/Internal/Misc.js';
|
|
14
|
-
import { Size } from '../Types/Internal/Size.js';
|
|
15
|
-
import Svelecte from 'svelecte';
|
|
16
|
-
import { getContext, onMount, type Snippet, tick, untrack } from 'svelte';
|
|
17
|
-
import Action from './Action.svelte';
|
|
18
|
-
import type { QuerySelectProps } from './QuerySelectProps.js';
|
|
19
|
-
|
|
20
|
-
let {
|
|
21
|
-
label,
|
|
22
|
-
labelProp,
|
|
23
|
-
valueProp,
|
|
24
|
-
clearable = false,
|
|
25
|
-
required = false,
|
|
26
|
-
invalid = false,
|
|
27
|
-
valueAsObject,
|
|
28
|
-
name = 'Item',
|
|
29
|
-
disabled,
|
|
30
|
-
query,
|
|
31
|
-
width,
|
|
32
|
-
height,
|
|
33
|
-
focusOnLoad = false,
|
|
34
|
-
inputId = generateWeakId(),
|
|
35
|
-
value: selectedValue,
|
|
36
|
-
options = $bindable<Array<V>>([]),
|
|
37
|
-
isValid,
|
|
38
|
-
onchange,
|
|
39
|
-
onfocus,
|
|
40
|
-
}: QuerySelectProps<V, VP> = $props();
|
|
41
|
-
|
|
42
|
-
export function forceQuery() {
|
|
43
|
-
clearTimeout(timeoutHandler);
|
|
44
|
-
|
|
45
|
-
const input = document.getElementById(inputId);
|
|
46
|
-
const searchValue = (input as HTMLInputElement).value;
|
|
47
|
-
|
|
48
|
-
const currentHandler = (timeoutHandler = setTimeout(() => {
|
|
49
|
-
tryQuery(currentHandler, searchValue).catch(devCatch);
|
|
50
|
-
}, 500) as unknown as number);
|
|
51
|
-
tryQuery(currentHandler, searchValue).catch(devCatch);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
let ref: Svelecte | undefined = $state();
|
|
55
|
-
let hasAutoFocused = $state(false);
|
|
56
|
-
let loadingDiv: HTMLDivElement | undefined = $state();
|
|
57
|
-
let isLoading = $state(false);
|
|
58
|
-
let hasRegisteredListeners = $state(false);
|
|
59
|
-
// Abort controller used to remove input listeners on unmount.
|
|
60
|
-
let listenerAbort: AbortController | undefined;
|
|
61
|
-
|
|
62
|
-
function isObjectValue(value: unknown): value is V {
|
|
63
|
-
return value !== null && typeof value == 'object' && labelProp in value && valueProp in value;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
$effect.pre(() => {
|
|
67
|
-
void selectedValue;
|
|
68
|
-
untrack(() => {
|
|
69
|
-
if (selectedValue != null) {
|
|
70
|
-
if (valueAsObject) {
|
|
71
|
-
if (isObjectValue(selectedValue)) {
|
|
72
|
-
if (!options.some((opt: V) => opt[valueProp] === selectedValue[valueProp])) {
|
|
73
|
-
// Immutable update so the bindable propagates even when the
|
|
74
|
-
// parent did not declare `$state` on its side.
|
|
75
|
-
options = [...options, selectedValue];
|
|
76
|
-
}
|
|
77
|
-
} else if (dev) {
|
|
78
|
-
console.warn(`valueAsObject is true but the value doesn't match`, selectedValue);
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
} else if (!options.some((opt: V) => opt[valueProp] === selectedValue)) {
|
|
82
|
-
options = [
|
|
83
|
-
...options,
|
|
84
|
-
{
|
|
85
|
-
//best-effort solution. Not a good one
|
|
86
|
-
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
|
87
|
-
[labelProp]: selectedValue.toString(),
|
|
88
|
-
[valueProp]: selectedValue,
|
|
89
|
-
} as V,
|
|
90
|
-
];
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
const isInvalid = $derived(
|
|
97
|
-
(required && !selectedValue) ||
|
|
98
|
-
invalid ||
|
|
99
|
-
isValid?.(
|
|
100
|
-
valueAsObject
|
|
101
|
-
? (selectedValue as V)
|
|
102
|
-
: (options.find((a) => a[valueProp] === selectedValue) ?? null)
|
|
103
|
-
) === false
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
const toastContext = getContext<ToastContext>(ToastContext.identifier);
|
|
107
|
-
|
|
108
|
-
function forceShowDropdown(e: HTMLInputElement) {
|
|
109
|
-
if (focusOnLoad && !hasAutoFocused) {
|
|
110
|
-
hasAutoFocused = true;
|
|
111
|
-
e.click();
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// HACK svelecte type shenanigans
|
|
116
|
-
function isFocusable(ref: unknown): ref is HTMLInputElement {
|
|
117
|
-
return ref != null;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
onMount(() => {
|
|
121
|
-
if (focusOnLoad) {
|
|
122
|
-
tick()
|
|
123
|
-
.then(() => {
|
|
124
|
-
if (isFocusable(ref)) {
|
|
125
|
-
ref.focus();
|
|
126
|
-
}
|
|
127
|
-
})
|
|
128
|
-
.catch(devCatch);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const input = document.getElementById(inputId);
|
|
132
|
-
const svelecteRoot = input?.closest('div.svelecte') as HTMLElement | null;
|
|
133
|
-
const dropdownRef =
|
|
134
|
-
(svelecteRoot?.querySelector('.sv_dropdown') as HTMLElement | null) ?? undefined;
|
|
135
|
-
|
|
136
|
-
// HACK callback for is-open on/off; drives the chevron rotation
|
|
137
|
-
// in the action snippet further down.
|
|
138
|
-
const observer = new MutationObserver((mutations: Array<MutationRecord>) => {
|
|
139
|
-
for (let i = mutations.length - 1; i > -1; i--) {
|
|
140
|
-
const mutation = mutations[i];
|
|
141
|
-
if (mutation.attributeName === 'class') {
|
|
142
|
-
isExpanded = dropdownRef?.classList.contains('is-open') ?? false;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
if (dropdownRef) {
|
|
148
|
-
observer.observe(dropdownRef, { attributes: true });
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// Teleport the Svelecte dropdown to `document.body` while open
|
|
152
|
-
// so it escapes every ancestor `overflow` / `transform` /
|
|
153
|
-
// `z-index` stacking context. See `Helpers/dropdownPortal.ts`.
|
|
154
|
-
const detachPortal = attachDropdownPortal({
|
|
155
|
-
getTrigger: () => svelecteRoot,
|
|
156
|
-
getDropdown: () => dropdownRef,
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
return () => {
|
|
160
|
-
observer.disconnect();
|
|
161
|
-
detachPortal();
|
|
162
|
-
clearTimeout(timeoutHandler);
|
|
163
|
-
listenerAbort?.abort();
|
|
164
|
-
};
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
let timeoutHandler = -1;
|
|
168
|
-
|
|
169
|
-
$effect(() => {
|
|
170
|
-
if (loadingDiv && isLoading) {
|
|
171
|
-
loadingDiv.classList.add('is-loading');
|
|
172
|
-
} else {
|
|
173
|
-
loadingDiv?.classList.remove('is-loading');
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
function registerInputListeners(e: HTMLInputElement) {
|
|
178
|
-
if (!hasRegisteredListeners) {
|
|
179
|
-
hasRegisteredListeners = true;
|
|
180
|
-
listenerAbort = new AbortController();
|
|
181
|
-
const signal = listenerAbort.signal;
|
|
182
|
-
|
|
183
|
-
const inputElement = e;
|
|
184
|
-
|
|
185
|
-
loadingDiv = inputElement.parentElement?.parentElement?.nextElementSibling as
|
|
186
|
-
| HTMLDivElement
|
|
187
|
-
| undefined;
|
|
188
|
-
|
|
189
|
-
const currentHandler = (timeoutHandler = setTimeout(() => {
|
|
190
|
-
tryQuery(currentHandler, '').catch(devCatch);
|
|
191
|
-
}, 500) as unknown as number);
|
|
192
|
-
|
|
193
|
-
inputElement.addEventListener(
|
|
194
|
-
'input',
|
|
195
|
-
(e) => {
|
|
196
|
-
clearTimeout(timeoutHandler);
|
|
197
|
-
|
|
198
|
-
const searchValue = (e.target as HTMLInputElement).value;
|
|
199
|
-
|
|
200
|
-
const currentHandler = (timeoutHandler = setTimeout(() => {
|
|
201
|
-
tryQuery(currentHandler, searchValue).catch(devCatch);
|
|
202
|
-
}, 500) as unknown as number);
|
|
203
|
-
},
|
|
204
|
-
{ signal }
|
|
205
|
-
);
|
|
206
|
-
|
|
207
|
-
inputElement.addEventListener(
|
|
208
|
-
'keydown',
|
|
209
|
-
(e) => {
|
|
210
|
-
if (e.key === 'Enter') {
|
|
211
|
-
tick()
|
|
212
|
-
.then(() => {
|
|
213
|
-
const searchValue = (e.target as HTMLInputElement).value;
|
|
214
|
-
if (searchValue) {
|
|
215
|
-
clearTimeout(timeoutHandler);
|
|
216
|
-
|
|
217
|
-
const currentHandler = (timeoutHandler = setTimeout(() => {
|
|
218
|
-
tryQuery(currentHandler, searchValue, true).catch(devCatch);
|
|
219
|
-
}, 1) as unknown as number);
|
|
220
|
-
}
|
|
221
|
-
})
|
|
222
|
-
.catch(devCatch);
|
|
223
|
-
}
|
|
224
|
-
},
|
|
225
|
-
{ signal }
|
|
226
|
-
);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
async function tryQuery(handler: number, value: string, andSelectIt = false) {
|
|
231
|
-
if (timeoutHandler === handler) {
|
|
232
|
-
isLoading = true;
|
|
233
|
-
try {
|
|
234
|
-
const newValues = await query(value);
|
|
235
|
-
isLoading = false;
|
|
236
|
-
|
|
237
|
-
if (timeoutHandler === handler) {
|
|
238
|
-
options = newValues;
|
|
239
|
-
|
|
240
|
-
if (andSelectIt) {
|
|
241
|
-
if (newValues.length === 1) {
|
|
242
|
-
onChange(newValues[0]);
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
} catch (e) {
|
|
247
|
-
toastContext.toast(e);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
function onChange(newValue: V | null | undefined) {
|
|
253
|
-
if (clearable || newValue != null) {
|
|
254
|
-
// svelecte always passes the full option object; normalise to
|
|
255
|
-
// the shape the variant promised. AsPrimitive callbacks
|
|
256
|
-
// receive the primitive key (`V[VP]`), not the object.
|
|
257
|
-
if (valueAsObject) {
|
|
258
|
-
void (onchange as (v: V | null) => void | Promise<void>)(newValue ?? null);
|
|
259
|
-
} else {
|
|
260
|
-
const primitive = newValue == null ? null : newValue[valueProp];
|
|
261
|
-
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
262
|
-
void (onchange as (v: V[VP] | null) => void | Promise<void>)(primitive);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
const an = $derived(vowels.has(name[0].toLocaleLowerCase()) ? 'an' : 'a');
|
|
268
|
-
|
|
269
|
-
let isExpanded = $state(false);
|
|
270
|
-
</script>
|
|
271
|
-
|
|
272
|
-
<div
|
|
273
|
-
class="select-wrapper"
|
|
274
|
-
class:invalid={isInvalid}
|
|
275
|
-
style={`${width ? `--input-width: ${width};` : ''}${height ? `--input-height: ${height};` : ''}`}
|
|
276
|
-
>
|
|
277
|
-
{#if label}
|
|
278
|
-
<label for={inputId}>
|
|
279
|
-
<span
|
|
280
|
-
>{label}{#if !required} - <i>optional</i>{/if}</span
|
|
281
|
-
>
|
|
282
|
-
<span class="border"
|
|
283
|
-
>{label}{#if !required} - <i>optional</i>{/if}</span
|
|
284
|
-
>
|
|
285
|
-
</label>
|
|
286
|
-
{/if}
|
|
287
|
-
<!-- strict mode is used to stop svelecte from rejecting perfectly valid values -->
|
|
288
|
-
<Svelecte
|
|
289
|
-
strictMode={false}
|
|
290
|
-
keepCreated
|
|
291
|
-
onFocus={(e: HTMLInputElement) => {
|
|
292
|
-
registerInputListeners(e);
|
|
293
|
-
forceShowDropdown(e);
|
|
294
|
-
onfocus?.(e);
|
|
295
|
-
}}
|
|
296
|
-
placeholder="Please Select{name ? ` ${an} ${name}` : ''}"
|
|
297
|
-
bind:this={ref}
|
|
298
|
-
{inputId}
|
|
299
|
-
{disabled}
|
|
300
|
-
valueField={valueProp}
|
|
301
|
-
labelField={labelProp}
|
|
302
|
-
virtualList={false}
|
|
303
|
-
searchable
|
|
304
|
-
searchProps={{ keepSelectionInList: true }}
|
|
305
|
-
lazyDropdown
|
|
306
|
-
{valueAsObject}
|
|
307
|
-
{options}
|
|
308
|
-
value={selectedValue as Array<unknown>}
|
|
309
|
-
{onChange}
|
|
310
|
-
selectOnTab
|
|
311
|
-
deselectMode={required && !clearable ? 'none' : 'toggle'}
|
|
312
|
-
keepSelectionInList
|
|
313
|
-
{name}
|
|
314
|
-
selection={selection as (selectedOptions: Array<object>) => ReturnType<Snippet>}
|
|
315
|
-
{append}
|
|
316
|
-
>
|
|
317
|
-
{#snippet toggleIcon()}
|
|
318
|
-
<div class="nothing"></div>
|
|
319
|
-
{/snippet}
|
|
320
|
-
</Svelecte>
|
|
321
|
-
|
|
322
|
-
{#snippet append()}
|
|
323
|
-
<div class="icons">
|
|
324
|
-
{#if clearable && selectedValue}
|
|
325
|
-
<Action
|
|
326
|
-
{disabled}
|
|
327
|
-
noTabIndex
|
|
328
|
-
colourSet={ColourSet.Auxiliary}
|
|
329
|
-
size={Size.Small}
|
|
330
|
-
onclick={(e: MouseEvent) => {
|
|
331
|
-
if (isExpanded) {
|
|
332
|
-
e.stopPropagation();
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
onChange(null);
|
|
336
|
-
}}
|
|
337
|
-
icon={{
|
|
338
|
-
name: 'close-menu-cross',
|
|
339
|
-
colour: Colour['$ui-blue-9'],
|
|
340
|
-
}}
|
|
341
|
-
/>
|
|
342
|
-
{/if}
|
|
343
|
-
|
|
344
|
-
<Action
|
|
345
|
-
{disabled}
|
|
346
|
-
noTabIndex
|
|
347
|
-
colourSet={ColourSet.Auxiliary}
|
|
348
|
-
size={Size.Small}
|
|
349
|
-
icon={{
|
|
350
|
-
name: isExpanded ? 'chevron-up' : 'chevron-down',
|
|
351
|
-
colour: Colour['$ui-blue-9'],
|
|
352
|
-
}}
|
|
353
|
-
/>
|
|
354
|
-
</div>
|
|
355
|
-
{/snippet}
|
|
356
|
-
|
|
357
|
-
{#snippet selection(selectedOptions: Array<V>)}
|
|
358
|
-
{#if selectedOptions.length == 0}
|
|
359
|
-
<span class="selection">Please Select {an} {name}</span>
|
|
360
|
-
{:else}
|
|
361
|
-
<span class="selection">{selectedOptions[0][labelProp]}</span>
|
|
362
|
-
{/if}
|
|
363
|
-
{/snippet}
|
|
364
|
-
</div>
|
|
365
|
-
|
|
1
|
+
<svelte:options runes />
|
|
2
|
+
|
|
3
|
+
<script
|
|
4
|
+
lang="ts"
|
|
5
|
+
generics="V extends object, VP extends ValueProp<V>"
|
|
6
|
+
>
|
|
7
|
+
import { ToastContext } from '../Contexts/ToastContext.svelte.js';
|
|
8
|
+
|
|
9
|
+
import { dev } from '$app/environment';
|
|
10
|
+
import { devCatch, generateWeakId, vowels } from '../Helpers/Helpers.svelte.js';
|
|
11
|
+
import { attachDropdownPortal } from '../Helpers/dropdownPortal.js';
|
|
12
|
+
import { Colour, ColourSet } from '../scss/colours.js';
|
|
13
|
+
import type { ValueProp } from '../Types/Internal/Misc.js';
|
|
14
|
+
import { Size } from '../Types/Internal/Size.js';
|
|
15
|
+
import Svelecte from 'svelecte';
|
|
16
|
+
import { getContext, onMount, type Snippet, tick, untrack } from 'svelte';
|
|
17
|
+
import Action from './Action.svelte';
|
|
18
|
+
import type { QuerySelectProps } from './QuerySelectProps.js';
|
|
19
|
+
|
|
20
|
+
let {
|
|
21
|
+
label,
|
|
22
|
+
labelProp,
|
|
23
|
+
valueProp,
|
|
24
|
+
clearable = false,
|
|
25
|
+
required = false,
|
|
26
|
+
invalid = false,
|
|
27
|
+
valueAsObject,
|
|
28
|
+
name = 'Item',
|
|
29
|
+
disabled,
|
|
30
|
+
query,
|
|
31
|
+
width,
|
|
32
|
+
height,
|
|
33
|
+
focusOnLoad = false,
|
|
34
|
+
inputId = generateWeakId(),
|
|
35
|
+
value: selectedValue,
|
|
36
|
+
options = $bindable<Array<V>>([]),
|
|
37
|
+
isValid,
|
|
38
|
+
onchange,
|
|
39
|
+
onfocus,
|
|
40
|
+
}: QuerySelectProps<V, VP> = $props();
|
|
41
|
+
|
|
42
|
+
export function forceQuery() {
|
|
43
|
+
clearTimeout(timeoutHandler);
|
|
44
|
+
|
|
45
|
+
const input = document.getElementById(inputId);
|
|
46
|
+
const searchValue = (input as HTMLInputElement).value;
|
|
47
|
+
|
|
48
|
+
const currentHandler = (timeoutHandler = setTimeout(() => {
|
|
49
|
+
tryQuery(currentHandler, searchValue).catch(devCatch);
|
|
50
|
+
}, 500) as unknown as number);
|
|
51
|
+
tryQuery(currentHandler, searchValue).catch(devCatch);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let ref: Svelecte | undefined = $state();
|
|
55
|
+
let hasAutoFocused = $state(false);
|
|
56
|
+
let loadingDiv: HTMLDivElement | undefined = $state();
|
|
57
|
+
let isLoading = $state(false);
|
|
58
|
+
let hasRegisteredListeners = $state(false);
|
|
59
|
+
// Abort controller used to remove input listeners on unmount.
|
|
60
|
+
let listenerAbort: AbortController | undefined;
|
|
61
|
+
|
|
62
|
+
function isObjectValue(value: unknown): value is V {
|
|
63
|
+
return value !== null && typeof value == 'object' && labelProp in value && valueProp in value;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
$effect.pre(() => {
|
|
67
|
+
void selectedValue;
|
|
68
|
+
untrack(() => {
|
|
69
|
+
if (selectedValue != null) {
|
|
70
|
+
if (valueAsObject) {
|
|
71
|
+
if (isObjectValue(selectedValue)) {
|
|
72
|
+
if (!options.some((opt: V) => opt[valueProp] === selectedValue[valueProp])) {
|
|
73
|
+
// Immutable update so the bindable propagates even when the
|
|
74
|
+
// parent did not declare `$state` on its side.
|
|
75
|
+
options = [...options, selectedValue];
|
|
76
|
+
}
|
|
77
|
+
} else if (dev) {
|
|
78
|
+
console.warn(`valueAsObject is true but the value doesn't match`, selectedValue);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
} else if (!options.some((opt: V) => opt[valueProp] === selectedValue)) {
|
|
82
|
+
options = [
|
|
83
|
+
...options,
|
|
84
|
+
{
|
|
85
|
+
//best-effort solution. Not a good one
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
|
87
|
+
[labelProp]: selectedValue.toString(),
|
|
88
|
+
[valueProp]: selectedValue,
|
|
89
|
+
} as V,
|
|
90
|
+
];
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const isInvalid = $derived(
|
|
97
|
+
(required && !selectedValue) ||
|
|
98
|
+
invalid ||
|
|
99
|
+
isValid?.(
|
|
100
|
+
valueAsObject
|
|
101
|
+
? (selectedValue as V)
|
|
102
|
+
: (options.find((a) => a[valueProp] === selectedValue) ?? null)
|
|
103
|
+
) === false
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
const toastContext = getContext<ToastContext>(ToastContext.identifier);
|
|
107
|
+
|
|
108
|
+
function forceShowDropdown(e: HTMLInputElement) {
|
|
109
|
+
if (focusOnLoad && !hasAutoFocused) {
|
|
110
|
+
hasAutoFocused = true;
|
|
111
|
+
e.click();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// HACK svelecte type shenanigans
|
|
116
|
+
function isFocusable(ref: unknown): ref is HTMLInputElement {
|
|
117
|
+
return ref != null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
onMount(() => {
|
|
121
|
+
if (focusOnLoad) {
|
|
122
|
+
tick()
|
|
123
|
+
.then(() => {
|
|
124
|
+
if (isFocusable(ref)) {
|
|
125
|
+
ref.focus();
|
|
126
|
+
}
|
|
127
|
+
})
|
|
128
|
+
.catch(devCatch);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const input = document.getElementById(inputId);
|
|
132
|
+
const svelecteRoot = input?.closest('div.svelecte') as HTMLElement | null;
|
|
133
|
+
const dropdownRef =
|
|
134
|
+
(svelecteRoot?.querySelector('.sv_dropdown') as HTMLElement | null) ?? undefined;
|
|
135
|
+
|
|
136
|
+
// HACK callback for is-open on/off; drives the chevron rotation
|
|
137
|
+
// in the action snippet further down.
|
|
138
|
+
const observer = new MutationObserver((mutations: Array<MutationRecord>) => {
|
|
139
|
+
for (let i = mutations.length - 1; i > -1; i--) {
|
|
140
|
+
const mutation = mutations[i];
|
|
141
|
+
if (mutation.attributeName === 'class') {
|
|
142
|
+
isExpanded = dropdownRef?.classList.contains('is-open') ?? false;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
if (dropdownRef) {
|
|
148
|
+
observer.observe(dropdownRef, { attributes: true });
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Teleport the Svelecte dropdown to `document.body` while open
|
|
152
|
+
// so it escapes every ancestor `overflow` / `transform` /
|
|
153
|
+
// `z-index` stacking context. See `Helpers/dropdownPortal.ts`.
|
|
154
|
+
const detachPortal = attachDropdownPortal({
|
|
155
|
+
getTrigger: () => svelecteRoot,
|
|
156
|
+
getDropdown: () => dropdownRef,
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
return () => {
|
|
160
|
+
observer.disconnect();
|
|
161
|
+
detachPortal();
|
|
162
|
+
clearTimeout(timeoutHandler);
|
|
163
|
+
listenerAbort?.abort();
|
|
164
|
+
};
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
let timeoutHandler = -1;
|
|
168
|
+
|
|
169
|
+
$effect(() => {
|
|
170
|
+
if (loadingDiv && isLoading) {
|
|
171
|
+
loadingDiv.classList.add('is-loading');
|
|
172
|
+
} else {
|
|
173
|
+
loadingDiv?.classList.remove('is-loading');
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
function registerInputListeners(e: HTMLInputElement) {
|
|
178
|
+
if (!hasRegisteredListeners) {
|
|
179
|
+
hasRegisteredListeners = true;
|
|
180
|
+
listenerAbort = new AbortController();
|
|
181
|
+
const signal = listenerAbort.signal;
|
|
182
|
+
|
|
183
|
+
const inputElement = e;
|
|
184
|
+
|
|
185
|
+
loadingDiv = inputElement.parentElement?.parentElement?.nextElementSibling as
|
|
186
|
+
| HTMLDivElement
|
|
187
|
+
| undefined;
|
|
188
|
+
|
|
189
|
+
const currentHandler = (timeoutHandler = setTimeout(() => {
|
|
190
|
+
tryQuery(currentHandler, '').catch(devCatch);
|
|
191
|
+
}, 500) as unknown as number);
|
|
192
|
+
|
|
193
|
+
inputElement.addEventListener(
|
|
194
|
+
'input',
|
|
195
|
+
(e) => {
|
|
196
|
+
clearTimeout(timeoutHandler);
|
|
197
|
+
|
|
198
|
+
const searchValue = (e.target as HTMLInputElement).value;
|
|
199
|
+
|
|
200
|
+
const currentHandler = (timeoutHandler = setTimeout(() => {
|
|
201
|
+
tryQuery(currentHandler, searchValue).catch(devCatch);
|
|
202
|
+
}, 500) as unknown as number);
|
|
203
|
+
},
|
|
204
|
+
{ signal }
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
inputElement.addEventListener(
|
|
208
|
+
'keydown',
|
|
209
|
+
(e) => {
|
|
210
|
+
if (e.key === 'Enter') {
|
|
211
|
+
tick()
|
|
212
|
+
.then(() => {
|
|
213
|
+
const searchValue = (e.target as HTMLInputElement).value;
|
|
214
|
+
if (searchValue) {
|
|
215
|
+
clearTimeout(timeoutHandler);
|
|
216
|
+
|
|
217
|
+
const currentHandler = (timeoutHandler = setTimeout(() => {
|
|
218
|
+
tryQuery(currentHandler, searchValue, true).catch(devCatch);
|
|
219
|
+
}, 1) as unknown as number);
|
|
220
|
+
}
|
|
221
|
+
})
|
|
222
|
+
.catch(devCatch);
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
{ signal }
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async function tryQuery(handler: number, value: string, andSelectIt = false) {
|
|
231
|
+
if (timeoutHandler === handler) {
|
|
232
|
+
isLoading = true;
|
|
233
|
+
try {
|
|
234
|
+
const newValues = await query(value);
|
|
235
|
+
isLoading = false;
|
|
236
|
+
|
|
237
|
+
if (timeoutHandler === handler) {
|
|
238
|
+
options = newValues;
|
|
239
|
+
|
|
240
|
+
if (andSelectIt) {
|
|
241
|
+
if (newValues.length === 1) {
|
|
242
|
+
onChange(newValues[0]);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
} catch (e) {
|
|
247
|
+
toastContext.toast(e);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function onChange(newValue: V | null | undefined) {
|
|
253
|
+
if (clearable || newValue != null) {
|
|
254
|
+
// svelecte always passes the full option object; normalise to
|
|
255
|
+
// the shape the variant promised. AsPrimitive callbacks
|
|
256
|
+
// receive the primitive key (`V[VP]`), not the object.
|
|
257
|
+
if (valueAsObject) {
|
|
258
|
+
void (onchange as (v: V | null) => void | Promise<void>)(newValue ?? null);
|
|
259
|
+
} else {
|
|
260
|
+
const primitive = newValue == null ? null : newValue[valueProp];
|
|
261
|
+
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
262
|
+
void (onchange as (v: V[VP] | null) => void | Promise<void>)(primitive);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const an = $derived(vowels.has(name[0].toLocaleLowerCase()) ? 'an' : 'a');
|
|
268
|
+
|
|
269
|
+
let isExpanded = $state(false);
|
|
270
|
+
</script>
|
|
271
|
+
|
|
272
|
+
<div
|
|
273
|
+
class="select-wrapper"
|
|
274
|
+
class:invalid={isInvalid}
|
|
275
|
+
style={`${width ? `--input-width: ${width};` : ''}${height ? `--input-height: ${height};` : ''}`}
|
|
276
|
+
>
|
|
277
|
+
{#if label}
|
|
278
|
+
<label for={inputId}>
|
|
279
|
+
<span
|
|
280
|
+
>{label}{#if !required} - <i>optional</i>{/if}</span
|
|
281
|
+
>
|
|
282
|
+
<span class="border"
|
|
283
|
+
>{label}{#if !required} - <i>optional</i>{/if}</span
|
|
284
|
+
>
|
|
285
|
+
</label>
|
|
286
|
+
{/if}
|
|
287
|
+
<!-- strict mode is used to stop svelecte from rejecting perfectly valid values -->
|
|
288
|
+
<Svelecte
|
|
289
|
+
strictMode={false}
|
|
290
|
+
keepCreated
|
|
291
|
+
onFocus={(e: HTMLInputElement) => {
|
|
292
|
+
registerInputListeners(e);
|
|
293
|
+
forceShowDropdown(e);
|
|
294
|
+
onfocus?.(e);
|
|
295
|
+
}}
|
|
296
|
+
placeholder="Please Select{name ? ` ${an} ${name}` : ''}"
|
|
297
|
+
bind:this={ref}
|
|
298
|
+
{inputId}
|
|
299
|
+
{disabled}
|
|
300
|
+
valueField={valueProp}
|
|
301
|
+
labelField={labelProp}
|
|
302
|
+
virtualList={false}
|
|
303
|
+
searchable
|
|
304
|
+
searchProps={{ keepSelectionInList: true }}
|
|
305
|
+
lazyDropdown
|
|
306
|
+
{valueAsObject}
|
|
307
|
+
{options}
|
|
308
|
+
value={selectedValue as Array<unknown>}
|
|
309
|
+
{onChange}
|
|
310
|
+
selectOnTab
|
|
311
|
+
deselectMode={required && !clearable ? 'none' : 'toggle'}
|
|
312
|
+
keepSelectionInList
|
|
313
|
+
{name}
|
|
314
|
+
selection={selection as (selectedOptions: Array<object>) => ReturnType<Snippet>}
|
|
315
|
+
{append}
|
|
316
|
+
>
|
|
317
|
+
{#snippet toggleIcon()}
|
|
318
|
+
<div class="nothing"></div>
|
|
319
|
+
{/snippet}
|
|
320
|
+
</Svelecte>
|
|
321
|
+
|
|
322
|
+
{#snippet append()}
|
|
323
|
+
<div class="icons">
|
|
324
|
+
{#if clearable && selectedValue}
|
|
325
|
+
<Action
|
|
326
|
+
{disabled}
|
|
327
|
+
noTabIndex
|
|
328
|
+
colourSet={ColourSet.Auxiliary}
|
|
329
|
+
size={Size.Small}
|
|
330
|
+
onclick={(e: MouseEvent) => {
|
|
331
|
+
if (isExpanded) {
|
|
332
|
+
e.stopPropagation();
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
onChange(null);
|
|
336
|
+
}}
|
|
337
|
+
icon={{
|
|
338
|
+
name: 'close-menu-cross',
|
|
339
|
+
colour: Colour['$ui-blue-9'],
|
|
340
|
+
}}
|
|
341
|
+
/>
|
|
342
|
+
{/if}
|
|
343
|
+
|
|
344
|
+
<Action
|
|
345
|
+
{disabled}
|
|
346
|
+
noTabIndex
|
|
347
|
+
colourSet={ColourSet.Auxiliary}
|
|
348
|
+
size={Size.Small}
|
|
349
|
+
icon={{
|
|
350
|
+
name: isExpanded ? 'chevron-up' : 'chevron-down',
|
|
351
|
+
colour: Colour['$ui-blue-9'],
|
|
352
|
+
}}
|
|
353
|
+
/>
|
|
354
|
+
</div>
|
|
355
|
+
{/snippet}
|
|
356
|
+
|
|
357
|
+
{#snippet selection(selectedOptions: Array<V>)}
|
|
358
|
+
{#if selectedOptions.length == 0}
|
|
359
|
+
<span class="selection">Please Select {an} {name}</span>
|
|
360
|
+
{:else}
|
|
361
|
+
<span class="selection">{selectedOptions[0][labelProp]}</span>
|
|
362
|
+
{/if}
|
|
363
|
+
{/snippet}
|
|
364
|
+
</div>
|
|
365
|
+
|
|
366
366
|
<style>.select-wrapper {
|
|
367
367
|
min-width: var(--input-min-width, 12rem);
|
|
368
368
|
width: var(--input-width, 100%);
|
|
@@ -525,4 +525,4 @@ div.icons {
|
|
|
525
525
|
}
|
|
526
526
|
:global(.dropdown-item) .toggle-icon.yes {
|
|
527
527
|
opacity: 1;
|
|
528
|
-
}</style>
|
|
528
|
+
}</style>
|