svelte-typeahead 4.2.0 → 4.2.3
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 +26 -0
- package/README.md +43 -59
- package/lib/index.js +167 -107
- package/lib/index.mjs +163 -103
- package/package.json +5 -5
- package/src/Typeahead.svelte +31 -9
- package/types/Typeahead.svelte.d.ts +1 -0
package/src/Typeahead.svelte
CHANGED
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
*/
|
|
28
28
|
export let inputAfterSelect = "update";
|
|
29
29
|
|
|
30
|
-
/** @type {{ original: TItem; index: number; score: number; string: string; }[]} */
|
|
30
|
+
/** @type {{ original: TItem; index: number; score: number; string: string; disabled?: boolean; }[]} */
|
|
31
31
|
export let results = [];
|
|
32
32
|
|
|
33
33
|
/** Set to `true` to re-focus the input after selecting a result */
|
|
@@ -65,6 +65,9 @@
|
|
|
65
65
|
|
|
66
66
|
async function select() {
|
|
67
67
|
const result = results[selectedIndex];
|
|
68
|
+
|
|
69
|
+
if (result.disabled) return;
|
|
70
|
+
|
|
68
71
|
const selectedValue = extract(result.original);
|
|
69
72
|
const searchedValue = value;
|
|
70
73
|
|
|
@@ -86,6 +89,27 @@
|
|
|
86
89
|
hideDropdown = true;
|
|
87
90
|
}
|
|
88
91
|
|
|
92
|
+
/** @type {(direction: -1 | 1) => void} */
|
|
93
|
+
function change(direction) {
|
|
94
|
+
let index =
|
|
95
|
+
direction === 1 && selectedIndex === results.length - 1 ? 0 : selectedIndex + direction;
|
|
96
|
+
if (index < 0) index = results.length - 1;
|
|
97
|
+
|
|
98
|
+
let disabled = results[index].disabled;
|
|
99
|
+
|
|
100
|
+
while (disabled) {
|
|
101
|
+
if (index === results.length) {
|
|
102
|
+
index = 0;
|
|
103
|
+
} else {
|
|
104
|
+
index += direction;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
disabled = results[index].disabled;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
selectedIndex = index;
|
|
111
|
+
}
|
|
112
|
+
|
|
89
113
|
$: options = { pre: "<mark>", post: "</mark>", extract };
|
|
90
114
|
$: results = fuzzy
|
|
91
115
|
.filter(value, data, options)
|
|
@@ -150,17 +174,11 @@
|
|
|
150
174
|
break;
|
|
151
175
|
case "ArrowDown":
|
|
152
176
|
e.preventDefault();
|
|
153
|
-
|
|
154
|
-
if (selectedIndex === results.length) {
|
|
155
|
-
selectedIndex = 0;
|
|
156
|
-
}
|
|
177
|
+
change(1);
|
|
157
178
|
break;
|
|
158
179
|
case "ArrowUp":
|
|
159
180
|
e.preventDefault();
|
|
160
|
-
|
|
161
|
-
if (selectedIndex < 0) {
|
|
162
|
-
selectedIndex = results.length - 1;
|
|
163
|
-
}
|
|
181
|
+
change(-1);
|
|
164
182
|
break;
|
|
165
183
|
case "Escape":
|
|
166
184
|
e.preventDefault();
|
|
@@ -191,6 +209,10 @@
|
|
|
191
209
|
select();
|
|
192
210
|
}
|
|
193
211
|
}}
|
|
212
|
+
on:mouseenter={() => {
|
|
213
|
+
if (result.disabled) return;
|
|
214
|
+
selectedIndex = i;
|
|
215
|
+
}}
|
|
194
216
|
>
|
|
195
217
|
<slot {result} index={i} {value}>
|
|
196
218
|
{@html result.string}
|