@splendidlabz/svelte 4.15.3 → 4.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -0
- package/components/Form/Combobox/Autocomplete.svelte +208 -0
- package/components/Form/Combobox/AutocompleteOptions.svelte +99 -14
- package/components/Form/Combobox/Chips.svelte +58 -0
- package/components/Form/Combobox/ComboboxOptions.svelte +44 -1
- package/components/Form/Combobox/Select.svelte +195 -0
- package/components/Form/Combobox/SelectOptions.svelte +57 -0
- package/components/Form/Combobox/autocomplete.svelte.js +517 -96
- package/components/Form/Combobox/combobox.svelte.js +395 -49
- package/components/Form/Combobox/index.js +6 -2
- package/components/Form/Combobox/listbox.js +119 -22
- package/components/Form/Combobox/match.js +405 -0
- package/components/Form/Combobox/matching.js +83 -0
- package/components/Form/Combobox/select.svelte.js +533 -0
- package/components/Form/FieldMeta.svelte +50 -0
- package/components/Form/Honeypot.svelte +3 -1
- package/components/Form/{Select.svelte → NativeSelect.svelte} +20 -15
- package/components/Form/SearchInput.svelte +6 -0
- package/components/Form/TextInput.svelte +42 -22
- package/components/Form/Textarea.svelte +22 -34
- package/components/Form/index.js +1 -1
- package/package.json +3 -4
- package/components/Form/Combobox/AutocompleteInput.svelte +0 -26
- package/components/Form/Combobox/ComboboxInput.svelte +0 -37
- package/components/Form/Combobox/autocomplete-matching.js +0 -69
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import { randomString } from '@splendidlabz/utils/dom'
|
|
3
|
+
import { useActions } from '../../../actions/use-actions'
|
|
4
|
+
import { init } from '../../../utils'
|
|
5
|
+
import Icon from '../../Icon/Icon.svelte'
|
|
6
|
+
import FieldMeta from '../FieldMeta.svelte'
|
|
7
|
+
import Chips from './Chips.svelte'
|
|
8
|
+
import { comboboxInput, getCombobox } from './combobox.svelte.js'
|
|
9
|
+
import { selectTrigger, toSelectedOptions } from './select.svelte.js'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @import { Snippet } from 'svelte'
|
|
13
|
+
* @import { SelectedOption, SelectEntry } from './select.svelte.js'
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {object} SelectProps
|
|
18
|
+
* @property {string} name Name the selection submits under
|
|
19
|
+
* @property {string} target Id of the options
|
|
20
|
+
* @property {SelectEntry | SelectEntry[] | null} [selected] One entry, or an array with `multiple`. Bindable.
|
|
21
|
+
* @property {string} [placeholder] Shows while nothing is selected
|
|
22
|
+
* @property {HTMLButtonElement | null} [ref]
|
|
23
|
+
* @property {boolean} [multiple] Let more than one option be selected
|
|
24
|
+
* @property {number} [max] Most options a multi select takes
|
|
25
|
+
* @property {(item: any) => void} [onselect] Receives the item, or the option element
|
|
26
|
+
* @property {boolean} [loop] Wrap past either end
|
|
27
|
+
* @property {string} [id]
|
|
28
|
+
* @property {string} [label]
|
|
29
|
+
* @property {boolean} [labelVisible]
|
|
30
|
+
* @property {string | null} [hint]
|
|
31
|
+
* @property {string | null} [error]
|
|
32
|
+
* @property {boolean | null} [disabled]
|
|
33
|
+
* @property {string} [class]
|
|
34
|
+
* @property {string} [style]
|
|
35
|
+
* @property {string} [labelClass]
|
|
36
|
+
* @property {string} [hintClass]
|
|
37
|
+
* @property {string} [errorClass]
|
|
38
|
+
* @property {string} [inputGroupClass]
|
|
39
|
+
* @property {string} [fieldClass]
|
|
40
|
+
* @property {string} [chipsClass]
|
|
41
|
+
* @property {string | null} [icon] Icon name that replaces the caret
|
|
42
|
+
* @property {string | null} [iconClass]
|
|
43
|
+
* @property {Snippet<[SelectedOption]>} [chip] Replaces a chip's label
|
|
44
|
+
* @property {{ fn: Function, props?: any }[]} [actions] Actions run on the button
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
/** @type {SelectProps & Record<string, any>} */
|
|
48
|
+
let {
|
|
49
|
+
selected = $bindable(),
|
|
50
|
+
ref = $bindable(null),
|
|
51
|
+
// Classes
|
|
52
|
+
class: klass,
|
|
53
|
+
style,
|
|
54
|
+
labelClass,
|
|
55
|
+
hintClass,
|
|
56
|
+
errorClass,
|
|
57
|
+
inputGroupClass,
|
|
58
|
+
fieldClass,
|
|
59
|
+
chipsClass,
|
|
60
|
+
iconClass = null,
|
|
61
|
+
// Slots
|
|
62
|
+
chip,
|
|
63
|
+
// Options and props
|
|
64
|
+
id = randomString(8),
|
|
65
|
+
name,
|
|
66
|
+
target,
|
|
67
|
+
placeholder = '',
|
|
68
|
+
icon = null,
|
|
69
|
+
label = '',
|
|
70
|
+
labelVisible = true,
|
|
71
|
+
hint = null,
|
|
72
|
+
error = null,
|
|
73
|
+
disabled = null,
|
|
74
|
+
multiple = false,
|
|
75
|
+
max,
|
|
76
|
+
onselect,
|
|
77
|
+
loop = true,
|
|
78
|
+
actions = [],
|
|
79
|
+
...rest
|
|
80
|
+
} = $props()
|
|
81
|
+
|
|
82
|
+
init(() => {
|
|
83
|
+
if (!name) throw new Error('Select requires a name')
|
|
84
|
+
if (!target) throw new Error('Select requires a target')
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
const combobox = $derived(getCombobox(target))
|
|
88
|
+
const selectedOptions = $derived(
|
|
89
|
+
combobox?.state.selected ?? toSelectedOptions(selected),
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
// A single select shows its selection on the button, so only a multi select shows chips
|
|
93
|
+
const chips = $derived(multiple ? selectedOptions : [])
|
|
94
|
+
const buttonLabel = $derived(multiple ? '' : selectedOptions[0]?.label)
|
|
95
|
+
|
|
96
|
+
// A multi select submits an array, the way NativeSelect does
|
|
97
|
+
const hiddenName = $derived(multiple ? `${name}[]` : name)
|
|
98
|
+
|
|
99
|
+
// Only a selection passed in can go over the cap, since the field refuses the option past it
|
|
100
|
+
const maxError = $derived(
|
|
101
|
+
max && selectedOptions.length > max
|
|
102
|
+
? `Choose no more than ${max} option${max === 1 ? '' : 's'}.`
|
|
103
|
+
: null,
|
|
104
|
+
)
|
|
105
|
+
const fieldError = $derived(error ?? maxError)
|
|
106
|
+
|
|
107
|
+
// IDs for aria-describedby
|
|
108
|
+
const hintId = $derived(hint ? id + '-hint' : '')
|
|
109
|
+
const errorId = $derived(id + '-error')
|
|
110
|
+
const describedBy = $derived(`${hintId} ${errorId}`.trim() || null)
|
|
111
|
+
</script>
|
|
112
|
+
|
|
113
|
+
<div {style} class="input-container combobox select {klass}" class:disabled>
|
|
114
|
+
<FieldMeta
|
|
115
|
+
{id}
|
|
116
|
+
error={fieldError}
|
|
117
|
+
{errorClass}
|
|
118
|
+
{hint}
|
|
119
|
+
{hintClass}
|
|
120
|
+
{label}
|
|
121
|
+
{labelClass}
|
|
122
|
+
{labelVisible}
|
|
123
|
+
/>
|
|
124
|
+
|
|
125
|
+
<div class="input-group {inputGroupClass}">
|
|
126
|
+
<Chips class={chipsClass} {chip} {disabled} selected={chips} {target} />
|
|
127
|
+
|
|
128
|
+
<button
|
|
129
|
+
{...rest}
|
|
130
|
+
bind:this={ref}
|
|
131
|
+
{id}
|
|
132
|
+
class="input select-trigger {fieldClass}"
|
|
133
|
+
aria-controls="{target}-listbox"
|
|
134
|
+
aria-describedby={describedBy}
|
|
135
|
+
aria-expanded="false"
|
|
136
|
+
aria-haspopup="listbox"
|
|
137
|
+
aria-invalid={fieldError ? 'true' : null}
|
|
138
|
+
aria-label={!labelVisible && label ? label : null}
|
|
139
|
+
{disabled}
|
|
140
|
+
role="combobox"
|
|
141
|
+
{target}
|
|
142
|
+
type="button"
|
|
143
|
+
use:useActions={[
|
|
144
|
+
{
|
|
145
|
+
fn: comboboxInput,
|
|
146
|
+
props: {
|
|
147
|
+
options: {
|
|
148
|
+
loop,
|
|
149
|
+
onselectedchange:
|
|
150
|
+
/** @param {SelectedOption | SelectedOption[] | null} next */ next =>
|
|
151
|
+
(selected = next),
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
fn: selectTrigger,
|
|
157
|
+
props: { options: { multiple, max, onselect, selected } },
|
|
158
|
+
},
|
|
159
|
+
...actions,
|
|
160
|
+
]}
|
|
161
|
+
>
|
|
162
|
+
{#if buttonLabel}
|
|
163
|
+
<span class="select-trigger-text">{buttonLabel}</span>
|
|
164
|
+
{:else}
|
|
165
|
+
<span class="select-trigger-text select-placeholder">{placeholder}</span
|
|
166
|
+
>
|
|
167
|
+
{/if}
|
|
168
|
+
|
|
169
|
+
{#if icon}
|
|
170
|
+
<Icon class={iconClass} {icon} />
|
|
171
|
+
{:else}
|
|
172
|
+
<svg
|
|
173
|
+
class={iconClass}
|
|
174
|
+
aria-hidden="true"
|
|
175
|
+
fill="none"
|
|
176
|
+
height="24"
|
|
177
|
+
viewBox="0 0 24 24"
|
|
178
|
+
width="24"
|
|
179
|
+
>
|
|
180
|
+
<path
|
|
181
|
+
d="M6 9L12 15L18 9"
|
|
182
|
+
stroke="currentcolor"
|
|
183
|
+
stroke-linecap="round"
|
|
184
|
+
stroke-linejoin="round"
|
|
185
|
+
stroke-width="2"
|
|
186
|
+
/>
|
|
187
|
+
</svg>
|
|
188
|
+
{/if}
|
|
189
|
+
</button>
|
|
190
|
+
</div>
|
|
191
|
+
|
|
192
|
+
{#each selectedOptions as option (option.value)}
|
|
193
|
+
<input name={hiddenName} {disabled} type="hidden" value={option.value} />
|
|
194
|
+
{/each}
|
|
195
|
+
</div>
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import { getCombobox } from './combobox.svelte.js'
|
|
3
|
+
import ComboboxOptions from './ComboboxOptions.svelte'
|
|
4
|
+
import { getValueAndLabel, selectOptions } from './select.svelte.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @import { Snippet } from 'svelte'
|
|
8
|
+
* @import { SelectEntry } from './select.svelte.js'
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {object} SelectOptionsProps
|
|
13
|
+
* @property {string} id Matches the Select's `target`
|
|
14
|
+
* @property {SelectEntry[]} [items] Options to render. A string is both value and label.
|
|
15
|
+
* @property {HTMLDivElement | null} [ref]
|
|
16
|
+
* @property {string} [class]
|
|
17
|
+
* @property {number} [size] How many options to show before scrolling
|
|
18
|
+
* @property {string} [aria-label]
|
|
19
|
+
* @property {string} [aria-labelledby]
|
|
20
|
+
* @property {Snippet<[any]>} [option] Replaces an option's contents
|
|
21
|
+
* @property {Snippet} [check] Replaces the check a selected option shows
|
|
22
|
+
* @property {Snippet} [children] Options written as `<li>` elements
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** @type {SelectOptionsProps & Record<string, any>} */
|
|
26
|
+
let { ref = $bindable(null), id, items, option, children, ...rest } = $props()
|
|
27
|
+
|
|
28
|
+
// The button part registers it, and it can hydrate after this one
|
|
29
|
+
const multiple = $derived(getCombobox(id)?.options.multiple ?? false)
|
|
30
|
+
</script>
|
|
31
|
+
|
|
32
|
+
<ComboboxOptions
|
|
33
|
+
{...rest}
|
|
34
|
+
{id}
|
|
35
|
+
actions={[{ fn: selectOptions, props: { items } }]}
|
|
36
|
+
inline={false}
|
|
37
|
+
{multiple}
|
|
38
|
+
bind:ref
|
|
39
|
+
>
|
|
40
|
+
{#if items}
|
|
41
|
+
{#each items as item, index (index)}
|
|
42
|
+
{@const { value, label } = getValueAndLabel(item)}
|
|
43
|
+
<!-- svelte-ignore a11y_role_has_required_aria_props -->
|
|
44
|
+
<li
|
|
45
|
+
id="{id}-option-{index}"
|
|
46
|
+
data-index={index}
|
|
47
|
+
data-label={label}
|
|
48
|
+
data-value={value}
|
|
49
|
+
role="option"
|
|
50
|
+
>
|
|
51
|
+
{#if option}{@render option(item)}{:else}{label}{/if}
|
|
52
|
+
</li>
|
|
53
|
+
{/each}
|
|
54
|
+
{:else}
|
|
55
|
+
{@render children?.()}
|
|
56
|
+
{/if}
|
|
57
|
+
</ComboboxOptions>
|