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.
@@ -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
- selectedIndex += 1;
154
- if (selectedIndex === results.length) {
155
- selectedIndex = 0;
156
- }
177
+ change(1);
157
178
  break;
158
179
  case "ArrowUp":
159
180
  e.preventDefault();
160
- selectedIndex -= 1;
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}
@@ -53,6 +53,7 @@ export interface TypeaheadProps<TItem> extends SearchProps {
53
53
  index: number;
54
54
  score: number;
55
55
  string: string;
56
+ disabled?: boolean;
56
57
  }[];
57
58
 
58
59
  /**