@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
|
@@ -2,8 +2,39 @@
|
|
|
2
2
|
import { randomString } from '@splendidlabz/utils/dom'
|
|
3
3
|
import { useActions } from '../../actions/use-actions'
|
|
4
4
|
import { init } from '../../utils'
|
|
5
|
-
import
|
|
5
|
+
import FieldMeta from './FieldMeta.svelte'
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* @import { Snippet } from 'svelte'
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {object} TextInputProps
|
|
13
|
+
* @property {string} name
|
|
14
|
+
* @property {string} [value]
|
|
15
|
+
* @property {HTMLInputElement | null} [ref]
|
|
16
|
+
* @property {string} [class]
|
|
17
|
+
* @property {string} [style]
|
|
18
|
+
* @property {string} [labelClass]
|
|
19
|
+
* @property {string} [hintClass]
|
|
20
|
+
* @property {string} [errorClass]
|
|
21
|
+
* @property {string} [inputGroupClass]
|
|
22
|
+
* @property {string} [fieldClass]
|
|
23
|
+
* @property {Snippet} [before] Content before the input
|
|
24
|
+
* @property {Snippet} [after] Content after the input
|
|
25
|
+
* @property {Snippet} [children]
|
|
26
|
+
* @property {string} [id]
|
|
27
|
+
* @property {'text' | 'email' | 'password' | 'search' | 'url' | 'tel'} [type]
|
|
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 {boolean | null} [readonly]
|
|
34
|
+
* @property {{ fn: Function, props?: any }[]} [actions] Actions run on the input
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/** @type {TextInputProps & Record<string, any>} */
|
|
7
38
|
let {
|
|
8
39
|
value = $bindable(''),
|
|
9
40
|
ref = $bindable(null),
|
|
@@ -78,27 +109,16 @@
|
|
|
78
109
|
class:disabled
|
|
79
110
|
class:readonly
|
|
80
111
|
>
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
<output
|
|
92
|
-
id={errorId}
|
|
93
|
-
class="error input-meta {errorClass}"
|
|
94
|
-
class:sr-only={!error}
|
|
95
|
-
for={id}
|
|
96
|
-
role="alert"
|
|
97
|
-
>
|
|
98
|
-
{#if error}
|
|
99
|
-
<Markdown content={error} inline />
|
|
100
|
-
{/if}
|
|
101
|
-
</output>
|
|
112
|
+
<FieldMeta
|
|
113
|
+
{id}
|
|
114
|
+
{error}
|
|
115
|
+
{errorClass}
|
|
116
|
+
{hint}
|
|
117
|
+
{hintClass}
|
|
118
|
+
{label}
|
|
119
|
+
{labelClass}
|
|
120
|
+
{labelVisible}
|
|
121
|
+
/>
|
|
102
122
|
|
|
103
123
|
{#if before || after}
|
|
104
124
|
<div class="input-group {inputGroupClass}">
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
<script>
|
|
2
2
|
import { randomString } from '@splendidlabz/utils/dom'
|
|
3
3
|
import { useActions } from '../../actions/use-actions'
|
|
4
|
+
import FieldMeta from './FieldMeta.svelte'
|
|
4
5
|
|
|
5
6
|
let {
|
|
6
7
|
value = $bindable(''),
|
|
@@ -15,6 +16,7 @@
|
|
|
15
16
|
id = randomString(8),
|
|
16
17
|
name,
|
|
17
18
|
label = '',
|
|
19
|
+
labelVisible = true,
|
|
18
20
|
hint = null,
|
|
19
21
|
error = null,
|
|
20
22
|
disabled = null,
|
|
@@ -36,21 +38,8 @@
|
|
|
36
38
|
const errorId = $derived(id + '-error')
|
|
37
39
|
const describedBy = $derived(`${hintId} ${errorId}`.trim() || null)
|
|
38
40
|
|
|
39
|
-
//
|
|
40
|
-
|
|
41
|
-
// ========================================
|
|
42
|
-
// Think about auto-growing textarea
|
|
43
|
-
let dummyRef = $state(null)
|
|
44
|
-
function autoGrow(event) {
|
|
45
|
-
if (autogrow) {
|
|
46
|
-
dummyRef.textContent = event.target.value
|
|
47
|
-
const last = event.target.value.slice(-1)
|
|
48
|
-
if (last === '\n') dummyRef.textContent += ' '
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Fire oninput event too... if any
|
|
52
|
-
if (oninput) oninput(event)
|
|
53
|
-
}
|
|
41
|
+
// Mirrors the text, and fills a trailing newline so its line counts
|
|
42
|
+
const replicaText = $derived(value + (value.endsWith('\n') ? '\u00a0' : ''))
|
|
54
43
|
</script>
|
|
55
44
|
|
|
56
45
|
<!-- Snippets -->
|
|
@@ -63,12 +52,12 @@
|
|
|
63
52
|
class="input {fieldClass}"
|
|
64
53
|
aria-describedby={describedBy}
|
|
65
54
|
aria-invalid={error ? 'true' : null}
|
|
55
|
+
aria-label={!labelVisible && label ? label : null}
|
|
66
56
|
{disabled}
|
|
67
|
-
oninput
|
|
57
|
+
{oninput}
|
|
68
58
|
{readonly}
|
|
69
59
|
bind:value
|
|
70
|
-
use:useActions={actions}
|
|
71
|
-
></textarea>
|
|
60
|
+
use:useActions={actions}></textarea>
|
|
72
61
|
{/snippet}
|
|
73
62
|
|
|
74
63
|
<div
|
|
@@ -78,21 +67,16 @@
|
|
|
78
67
|
class:readonly
|
|
79
68
|
class:textarea-autogrow={autogrow}
|
|
80
69
|
>
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
class:sr-only={!error}
|
|
92
|
-
role="alert"
|
|
93
|
-
>
|
|
94
|
-
{#if error}{error}{/if}
|
|
95
|
-
</output>
|
|
70
|
+
<FieldMeta
|
|
71
|
+
{id}
|
|
72
|
+
{error}
|
|
73
|
+
{errorClass}
|
|
74
|
+
{hint}
|
|
75
|
+
{hintClass}
|
|
76
|
+
{label}
|
|
77
|
+
{labelClass}
|
|
78
|
+
{labelVisible}
|
|
79
|
+
/>
|
|
96
80
|
|
|
97
81
|
{#if before || after}
|
|
98
82
|
<div class="input-group {inputGroupClass}">
|
|
@@ -111,7 +95,11 @@
|
|
|
111
95
|
{:else}
|
|
112
96
|
<div class="stack">
|
|
113
97
|
<!-- Autogrow textarea cannot be used together with input-group cos it makes the CSS very complex -->
|
|
114
|
-
|
|
98
|
+
{#if autogrow}
|
|
99
|
+
<div class="replica-field field {fieldClass}" aria-hidden="true">
|
|
100
|
+
{replicaText}
|
|
101
|
+
</div>
|
|
102
|
+
{/if}
|
|
115
103
|
{@render textarea()}
|
|
116
104
|
</div>
|
|
117
105
|
{/if}
|
package/components/Form/index.js
CHANGED
|
@@ -8,8 +8,8 @@ export { default as Hidden } from './Hidden.svelte'
|
|
|
8
8
|
export { default as HiddenInput } from './HiddenInput.svelte'
|
|
9
9
|
export { default as Honeypot } from './Honeypot.svelte'
|
|
10
10
|
export { default as Radio } from './Radio.svelte'
|
|
11
|
+
export { default as NativeSelect } from './NativeSelect.svelte'
|
|
11
12
|
export { default as Radios } from './Radios.svelte'
|
|
12
|
-
export { default as Select } from './Select.svelte'
|
|
13
13
|
export { default as Switch } from './Switch.svelte'
|
|
14
14
|
export { default as Textarea } from './Textarea.svelte'
|
|
15
15
|
export { default as TextInput } from './TextInput.svelte'
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@splendidlabz/svelte",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "4.
|
|
4
|
+
"version": "4.17.0",
|
|
5
5
|
"license": "Elastic-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"homepage": "https://splendidlabz.com/docs/svelte",
|
|
@@ -34,11 +34,10 @@
|
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@floating-ui/dom": "^1.7.6",
|
|
36
36
|
"@iconify/svelte": "^5.2.1",
|
|
37
|
-
"@splendidlabz/utils": "*"
|
|
38
|
-
"autosuggest-highlight": "^3.3.4"
|
|
37
|
+
"@splendidlabz/utils": "*"
|
|
39
38
|
},
|
|
40
39
|
"devDependencies": {
|
|
41
|
-
"@splendidlabz/eslint-config": "2.
|
|
40
|
+
"@splendidlabz/eslint-config": "2.3.1",
|
|
42
41
|
"@sveltejs/vite-plugin-svelte": "^7.0.0",
|
|
43
42
|
"happy-dom": "^20.9.0",
|
|
44
43
|
"svelte": "^5.55.4",
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import { autocompleteInput } from './autocomplete.svelte.js'
|
|
3
|
-
import ComboboxInput from './ComboboxInput.svelte'
|
|
4
|
-
|
|
5
|
-
let {
|
|
6
|
-
value = $bindable(''),
|
|
7
|
-
ref = $bindable(null),
|
|
8
|
-
search,
|
|
9
|
-
debounceMs,
|
|
10
|
-
onselect,
|
|
11
|
-
...rest
|
|
12
|
-
} = $props()
|
|
13
|
-
</script>
|
|
14
|
-
|
|
15
|
-
<ComboboxInput
|
|
16
|
-
{...rest}
|
|
17
|
-
actions={[
|
|
18
|
-
{
|
|
19
|
-
fn: autocompleteInput,
|
|
20
|
-
props: { options: { search, debounceMs, onselect } },
|
|
21
|
-
},
|
|
22
|
-
]}
|
|
23
|
-
aria-autocomplete="list"
|
|
24
|
-
bind:value
|
|
25
|
-
bind:ref
|
|
26
|
-
/>
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import { init } from '../../../utils'
|
|
3
|
-
import TextInput from '../TextInput.svelte'
|
|
4
|
-
import { comboboxInput } from './combobox.svelte.js'
|
|
5
|
-
|
|
6
|
-
let {
|
|
7
|
-
value = $bindable(''),
|
|
8
|
-
ref = $bindable(null),
|
|
9
|
-
target,
|
|
10
|
-
loop,
|
|
11
|
-
onselect,
|
|
12
|
-
actions = [],
|
|
13
|
-
...rest
|
|
14
|
-
} = $props()
|
|
15
|
-
|
|
16
|
-
init(() => {
|
|
17
|
-
if (!target) throw new Error('ComboboxInput requires a target')
|
|
18
|
-
})
|
|
19
|
-
</script>
|
|
20
|
-
|
|
21
|
-
<TextInput
|
|
22
|
-
{...rest}
|
|
23
|
-
actions={[
|
|
24
|
-
{
|
|
25
|
-
fn: comboboxInput,
|
|
26
|
-
props: { options: { loop, onselect } },
|
|
27
|
-
},
|
|
28
|
-
...actions,
|
|
29
|
-
]}
|
|
30
|
-
aria-controls="{target}-listbox"
|
|
31
|
-
aria-expanded="false"
|
|
32
|
-
autocomplete="off"
|
|
33
|
-
role="combobox"
|
|
34
|
-
{target}
|
|
35
|
-
bind:value
|
|
36
|
-
bind:ref
|
|
37
|
-
/>
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Matches option texts against a typed query.
|
|
3
|
-
*
|
|
4
|
-
* autosuggest-highlight strips diacritics before matching, so "cafe" matches "Café".
|
|
5
|
-
*/
|
|
6
|
-
import aMatch from 'autosuggest-highlight/match/index.js'
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* getMatches
|
|
10
|
-
* Finds the texts a query matches, and where each match sits.
|
|
11
|
-
*
|
|
12
|
-
* @typedef {[number, number]} Range Start and end index of a matched part.
|
|
13
|
-
* @typedef {{ index: number, ranges: Range[] }} Match `index` points into the texts passed in.
|
|
14
|
-
*
|
|
15
|
-
* @param {object} opts
|
|
16
|
-
* @param {string[]} opts.texts Texts to search
|
|
17
|
-
* @param {string} opts.query Typed query
|
|
18
|
-
* @returns {Match[]} Word-start matches first, then inside-word matches.
|
|
19
|
-
*/
|
|
20
|
-
export function getMatches({ texts, query }) {
|
|
21
|
-
const wordStartIndexes = getMatchingIndexes({
|
|
22
|
-
texts,
|
|
23
|
-
query,
|
|
24
|
-
insideWords: false,
|
|
25
|
-
})
|
|
26
|
-
const insideWordIndexes = getMatchingIndexes({
|
|
27
|
-
texts,
|
|
28
|
-
query,
|
|
29
|
-
insideWords: true,
|
|
30
|
-
})
|
|
31
|
-
const indexes = new Set([...wordStartIndexes, ...insideWordIndexes])
|
|
32
|
-
|
|
33
|
-
return Array.from(indexes, index => ({
|
|
34
|
-
index,
|
|
35
|
-
ranges: getRanges({ text: texts[index], query, insideWords: true }),
|
|
36
|
-
}))
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* hasPerfectMatch
|
|
41
|
-
* Checks whether the query equals one of the texts, ignoring case and diacritics.
|
|
42
|
-
*
|
|
43
|
-
* @param {object} opts
|
|
44
|
-
* @param {string[]} opts.texts Texts to search
|
|
45
|
-
* @param {string} opts.query Typed query
|
|
46
|
-
* @returns {boolean}
|
|
47
|
-
*/
|
|
48
|
-
export function hasPerfectMatch({ texts, query }) {
|
|
49
|
-
return texts.some(text => isPerfectMatch({ text, query }))
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function getMatchingIndexes({ texts, query, insideWords }) {
|
|
53
|
-
return texts.flatMap((text, index) => {
|
|
54
|
-
const ranges = getRanges({ text, query, insideWords })
|
|
55
|
-
return ranges.length ? [index] : []
|
|
56
|
-
})
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function getRanges({ text, query, insideWords }) {
|
|
60
|
-
return aMatch(text, query, { insideWords, requireMatchAll: true })
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function isPerfectMatch({ text, query }) {
|
|
64
|
-
if (text.length !== query.length) return false
|
|
65
|
-
|
|
66
|
-
const ranges = getRanges({ text, query, insideWords: true })
|
|
67
|
-
if (!ranges.length) return false
|
|
68
|
-
return ranges[0][0] === 0 && ranges.at(-1)[1] === text.length
|
|
69
|
-
}
|