svelte-typeahead 4.2.2 → 4.3.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/CHANGELOG.md +18 -0
- package/README.md +35 -35
- package/lib/index.js +183 -133
- package/lib/index.mjs +183 -133
- package/package.json +3 -3
- package/src/Typeahead.svelte +43 -22
- package/types/Typeahead.svelte.d.ts +6 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svelte-typeahead",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Accessible, fuzzy search typeahead component",
|
|
6
6
|
"author": "Eric Liu (https://github.com/metonym)",
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@rollup/plugin-commonjs": "^21.0.2",
|
|
25
|
-
"svelte": "^3.
|
|
26
|
-
"svelte-check": "^2.
|
|
25
|
+
"svelte": "^3.49.0",
|
|
26
|
+
"svelte-check": "^2.8.0",
|
|
27
27
|
"svelte-readme": "^3.6.3"
|
|
28
28
|
},
|
|
29
29
|
"repository": {
|
package/src/Typeahead.svelte
CHANGED
|
@@ -33,6 +33,9 @@
|
|
|
33
33
|
/** Set to `true` to re-focus the input after selecting a result */
|
|
34
34
|
export let focusAfterSelect = false;
|
|
35
35
|
|
|
36
|
+
/** Set to `true` to only show results when the input is focused */
|
|
37
|
+
export let showDropdownOnFocus = false;
|
|
38
|
+
|
|
36
39
|
/**
|
|
37
40
|
* Specify the maximum number of results to return
|
|
38
41
|
* @type {number}
|
|
@@ -50,6 +53,7 @@
|
|
|
50
53
|
let hideDropdown = false;
|
|
51
54
|
let selectedIndex = -1;
|
|
52
55
|
let prevResults = "";
|
|
56
|
+
let isFocused = false;
|
|
53
57
|
|
|
54
58
|
afterUpdate(() => {
|
|
55
59
|
if (prevResults !== resultsId && autoselect) {
|
|
@@ -85,14 +89,15 @@
|
|
|
85
89
|
await tick();
|
|
86
90
|
|
|
87
91
|
if (focusAfterSelect) searchRef.focus();
|
|
88
|
-
|
|
89
|
-
hideDropdown = true;
|
|
92
|
+
close();
|
|
90
93
|
}
|
|
91
94
|
|
|
92
95
|
/** @type {(direction: -1 | 1) => void} */
|
|
93
96
|
function change(direction) {
|
|
94
97
|
let index =
|
|
95
|
-
direction === 1 && selectedIndex === results.length - 1
|
|
98
|
+
direction === 1 && selectedIndex === results.length - 1
|
|
99
|
+
? 0
|
|
100
|
+
: selectedIndex + direction;
|
|
96
101
|
if (index < 0) index = results.length - 1;
|
|
97
102
|
|
|
98
103
|
let disabled = results[index].disabled;
|
|
@@ -110,6 +115,9 @@
|
|
|
110
115
|
selectedIndex = index;
|
|
111
116
|
}
|
|
112
117
|
|
|
118
|
+
const open = () => (hideDropdown = false);
|
|
119
|
+
const close = () => (hideDropdown = true);
|
|
120
|
+
|
|
113
121
|
$: options = { pre: "<mark>", post: "</mark>", extract };
|
|
114
122
|
$: results = fuzzy
|
|
115
123
|
.filter(value, data, options)
|
|
@@ -118,12 +126,16 @@
|
|
|
118
126
|
.filter((result) => !filter(result.original))
|
|
119
127
|
.map((result) => ({ ...result, disabled: disable(result.original) }));
|
|
120
128
|
$: resultsId = results.map((result) => extract(result.original)).join("");
|
|
129
|
+
$: showResults = !hideDropdown && results.length > 0;
|
|
130
|
+
$: if (showDropdownOnFocus) {
|
|
131
|
+
showResults = showResults && isFocused;
|
|
132
|
+
}
|
|
121
133
|
</script>
|
|
122
134
|
|
|
123
135
|
<svelte:window
|
|
124
136
|
on:click={({ target }) => {
|
|
125
|
-
if (!hideDropdown &&
|
|
126
|
-
|
|
137
|
+
if (!hideDropdown && !comboboxRef?.contains(target)) {
|
|
138
|
+
close();
|
|
127
139
|
}
|
|
128
140
|
}}
|
|
129
141
|
/>
|
|
@@ -135,7 +147,7 @@
|
|
|
135
147
|
aria-haspopup="listbox"
|
|
136
148
|
aria-owns="{id}-listbox"
|
|
137
149
|
class:dropdown={results.length > 0}
|
|
138
|
-
aria-expanded={
|
|
150
|
+
aria-expanded={showResults}
|
|
139
151
|
id="{id}-typeahead"
|
|
140
152
|
>
|
|
141
153
|
<Search
|
|
@@ -157,13 +169,15 @@
|
|
|
157
169
|
on:change
|
|
158
170
|
on:focus
|
|
159
171
|
on:focus={() => {
|
|
160
|
-
|
|
172
|
+
open();
|
|
173
|
+
isFocused = true;
|
|
161
174
|
}}
|
|
162
175
|
on:clear
|
|
163
|
-
on:clear={
|
|
164
|
-
hideDropdown = false;
|
|
165
|
-
}}
|
|
176
|
+
on:clear={open}
|
|
166
177
|
on:blur
|
|
178
|
+
on:blur={() => {
|
|
179
|
+
isFocused = false;
|
|
180
|
+
}}
|
|
167
181
|
on:keydown
|
|
168
182
|
on:keydown={(e) => {
|
|
169
183
|
if (results.length === 0) return;
|
|
@@ -183,8 +197,8 @@
|
|
|
183
197
|
case "Escape":
|
|
184
198
|
e.preventDefault();
|
|
185
199
|
value = "";
|
|
186
|
-
searchRef
|
|
187
|
-
|
|
200
|
+
searchRef?.focus();
|
|
201
|
+
close();
|
|
188
202
|
break;
|
|
189
203
|
}
|
|
190
204
|
}}
|
|
@@ -195,22 +209,25 @@
|
|
|
195
209
|
aria-labelledby="{id}-label"
|
|
196
210
|
id="{id}-listbox"
|
|
197
211
|
>
|
|
198
|
-
{#if
|
|
199
|
-
{#each results as result,
|
|
212
|
+
{#if showResults}
|
|
213
|
+
{#each results as result, index}
|
|
200
214
|
<li
|
|
201
215
|
role="option"
|
|
202
|
-
id="{id}-result-{
|
|
203
|
-
class:selected={selectedIndex ===
|
|
216
|
+
id="{id}-result-{index}"
|
|
217
|
+
class:selected={selectedIndex === index}
|
|
204
218
|
class:disabled={result.disabled}
|
|
205
|
-
aria-selected={selectedIndex ===
|
|
219
|
+
aria-selected={selectedIndex === index}
|
|
206
220
|
on:click={() => {
|
|
207
|
-
if (
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
221
|
+
if (result.disabled) return;
|
|
222
|
+
selectedIndex = index;
|
|
223
|
+
select();
|
|
224
|
+
}}
|
|
225
|
+
on:mouseenter={() => {
|
|
226
|
+
if (result.disabled) return;
|
|
227
|
+
selectedIndex = index;
|
|
211
228
|
}}
|
|
212
229
|
>
|
|
213
|
-
<slot {result} index
|
|
230
|
+
<slot {result} {index} {value}>
|
|
214
231
|
{@html result.string}
|
|
215
232
|
</slot>
|
|
216
233
|
</li>
|
|
@@ -241,6 +258,10 @@
|
|
|
241
258
|
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
|
242
259
|
}
|
|
243
260
|
|
|
261
|
+
[aria-expanded="true"] ul {
|
|
262
|
+
z-index: 1;
|
|
263
|
+
}
|
|
264
|
+
|
|
244
265
|
li,
|
|
245
266
|
.no-results {
|
|
246
267
|
padding: 0.25rem 1rem;
|
|
@@ -62,6 +62,12 @@ export interface TypeaheadProps<TItem> extends SearchProps {
|
|
|
62
62
|
*/
|
|
63
63
|
focusAfterSelect?: boolean;
|
|
64
64
|
|
|
65
|
+
/**
|
|
66
|
+
* Set to `true` to only show results when the input is focused
|
|
67
|
+
* @default false
|
|
68
|
+
*/
|
|
69
|
+
showDropdownOnFocus?: boolean;
|
|
70
|
+
|
|
65
71
|
/**
|
|
66
72
|
* Specify the maximum number of results to return
|
|
67
73
|
* @default Infinity
|