svelte-typeahead 4.2.4 → 4.3.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-typeahead",
3
- "version": "4.2.4",
3
+ "version": "4.3.2",
4
4
  "license": "MIT",
5
5
  "description": "Accessible, fuzzy search typeahead component",
6
6
  "author": "Eric Liu (https://github.com/metonym)",
@@ -8,7 +8,6 @@
8
8
  "main": "./lib/index.js",
9
9
  "module": "./lib/index.mjs",
10
10
  "types": "./types/index.d.ts",
11
- "sideEffects": false,
12
11
  "scripts": {
13
12
  "dev": "rollup -cw",
14
13
  "predeploy": "rollup -c",
@@ -22,7 +21,7 @@
22
21
  },
23
22
  "devDependencies": {
24
23
  "@rollup/plugin-commonjs": "^21.0.2",
25
- "svelte": "^3.48.0",
24
+ "svelte": "^3.49.0",
26
25
  "svelte-check": "^2.8.0",
27
26
  "svelte-readme": "^3.6.3"
28
27
  },
@@ -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,10 +53,11 @@
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) {
56
- selectedIndex = 0;
60
+ selectedIndex = getNextNonDisabledIndex();
57
61
  }
58
62
 
59
63
  if (prevResults !== resultsId && !$$slots["no-results"]) {
@@ -85,8 +89,25 @@
85
89
  await tick();
86
90
 
87
91
  if (focusAfterSelect) searchRef.focus();
92
+ close();
93
+ }
88
94
 
89
- hideDropdown = true;
95
+ /** @type {() => number} */
96
+ function getNextNonDisabledIndex() {
97
+ let index = 0;
98
+ let disabled = results[index]?.disabled ?? false;
99
+
100
+ while (disabled) {
101
+ if (index === results.length) {
102
+ index = 0;
103
+ } else {
104
+ index += 1;
105
+ }
106
+
107
+ disabled = results[index]?.disabled ?? false;
108
+ }
109
+
110
+ return index;
90
111
  }
91
112
 
92
113
  /** @type {(direction: -1 | 1) => void} */
@@ -112,6 +133,9 @@
112
133
  selectedIndex = index;
113
134
  }
114
135
 
136
+ const open = () => (hideDropdown = false);
137
+ const close = () => (hideDropdown = true);
138
+
115
139
  $: options = { pre: "<mark>", post: "</mark>", extract };
116
140
  $: results = fuzzy
117
141
  .filter(value, data, options)
@@ -121,12 +145,15 @@
121
145
  .map((result) => ({ ...result, disabled: disable(result.original) }));
122
146
  $: resultsId = results.map((result) => extract(result.original)).join("");
123
147
  $: showResults = !hideDropdown && results.length > 0;
148
+ $: if (showDropdownOnFocus) {
149
+ showResults = showResults && isFocused;
150
+ }
124
151
  </script>
125
152
 
126
153
  <svelte:window
127
154
  on:click={({ target }) => {
128
- if (!hideDropdown && comboboxRef && !comboboxRef.contains(target)) {
129
- hideDropdown = true;
155
+ if (!hideDropdown && !comboboxRef?.contains(target)) {
156
+ close();
130
157
  }
131
158
  }}
132
159
  />
@@ -160,12 +187,14 @@
160
187
  on:change
161
188
  on:focus
162
189
  on:focus={() => {
163
- hideDropdown = false;
190
+ open();
191
+ if (showDropdownOnFocus) {
192
+ showResults = true;
193
+ isFocused = true;
194
+ }
164
195
  }}
165
196
  on:clear
166
- on:clear={() => {
167
- hideDropdown = false;
168
- }}
197
+ on:clear={open}
169
198
  on:blur
170
199
  on:keydown
171
200
  on:keydown={(e) => {
@@ -186,8 +215,8 @@
186
215
  case "Escape":
187
216
  e.preventDefault();
188
217
  value = "";
189
- searchRef.focus();
190
- hideDropdown = true;
218
+ searchRef?.focus();
219
+ close();
191
220
  break;
192
221
  }
193
222
  }}
@@ -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