svelte-typeahead 4.2.4 → 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.
@@ -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,8 +89,7 @@
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} */
@@ -112,6 +115,9 @@
112
115
  selectedIndex = index;
113
116
  }
114
117
 
118
+ const open = () => (hideDropdown = false);
119
+ const close = () => (hideDropdown = true);
120
+
115
121
  $: options = { pre: "<mark>", post: "</mark>", extract };
116
122
  $: results = fuzzy
117
123
  .filter(value, data, options)
@@ -121,12 +127,15 @@
121
127
  .map((result) => ({ ...result, disabled: disable(result.original) }));
122
128
  $: resultsId = results.map((result) => extract(result.original)).join("");
123
129
  $: showResults = !hideDropdown && results.length > 0;
130
+ $: if (showDropdownOnFocus) {
131
+ showResults = showResults && isFocused;
132
+ }
124
133
  </script>
125
134
 
126
135
  <svelte:window
127
136
  on:click={({ target }) => {
128
- if (!hideDropdown && comboboxRef && !comboboxRef.contains(target)) {
129
- hideDropdown = true;
137
+ if (!hideDropdown && !comboboxRef?.contains(target)) {
138
+ close();
130
139
  }
131
140
  }}
132
141
  />
@@ -160,13 +169,15 @@
160
169
  on:change
161
170
  on:focus
162
171
  on:focus={() => {
163
- hideDropdown = false;
172
+ open();
173
+ isFocused = true;
164
174
  }}
165
175
  on:clear
166
- on:clear={() => {
167
- hideDropdown = false;
168
- }}
176
+ on:clear={open}
169
177
  on:blur
178
+ on:blur={() => {
179
+ isFocused = false;
180
+ }}
170
181
  on:keydown
171
182
  on:keydown={(e) => {
172
183
  if (results.length === 0) return;
@@ -186,8 +197,8 @@
186
197
  case "Escape":
187
198
  e.preventDefault();
188
199
  value = "";
189
- searchRef.focus();
190
- hideDropdown = true;
200
+ searchRef?.focus();
201
+ close();
191
202
  break;
192
203
  }
193
204
  }}
@@ -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