svelte-typeahead 4.3.0 → 4.3.1

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 CHANGED
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [4.3.1](https://github.com/metonym/svelte-typeahead/releases/tag/v4.3.1) - 2022-08-29
9
+
10
+ **Fixes**
11
+
12
+ - skip disabled results when auto-selecting a result
13
+
8
14
  ## [4.3.0](https://github.com/metonym/svelte-typeahead/releases/tag/v4.3.0) - 2022-08-29
9
15
 
10
16
  **Features**
package/README.md CHANGED
@@ -153,20 +153,20 @@ Set `showDropdownOnFocus` to `true` to only show the dropdown when the search in
153
153
 
154
154
  ### Props
155
155
 
156
- | Prop name | Value | Description |
157
- | :----------------- | :------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------ |
158
- | value | `string` (default: `""`) | Input search value |
159
- | data | `TItem[]` (default: `[]`) | Items to search |
160
- | extract | `(TItem) => any` | Target an item key if `data` is an object array |
161
- | disable | `(TItem) => boolean` | Pass in a function to disable items. They can be displayed in the results but will not be selectable. |
162
- | filter | `(TItem) => boolean` | Pass in a function to filter items. They will be hidden and are not displayed in the results. |
163
- | autoselect | `boolean` (default: `true`) | Automatically select the first (top) result |
164
- | inputAfterSelect | `"update" or "clear" or "keep"`(default: `"update"`) | Set to `"clear"` to clear the `value` after selecting a result. Set to `"keep"` to keep the search field unchanged after a selection. |
165
- | results | `FuzzyResult[]` (default: `[]`) | Raw fuzzy results from the [fuzzy](https://github.com/mattyork/fuzzy) module |
166
- | focusAfterSelect | `boolean` (default: `false`) | Set to `true` to re-focus the input after selecting a result. |
156
+ | Prop name | Value | Description |
157
+ | :------------------ | :------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------ |
158
+ | value | `string` (default: `""`) | Input search value |
159
+ | data | `TItem[]` (default: `[]`) | Items to search |
160
+ | extract | `(TItem) => any` | Target an item key if `data` is an object array |
161
+ | disable | `(TItem) => boolean` | Pass in a function to disable items. They can be displayed in the results but will not be selectable. |
162
+ | filter | `(TItem) => boolean` | Pass in a function to filter items. They will be hidden and are not displayed in the results. |
163
+ | autoselect | `boolean` (default: `true`) | Automatically select the first (top) result |
164
+ | inputAfterSelect | `"update" or "clear" or "keep"`(default: `"update"`) | Set to `"clear"` to clear the `value` after selecting a result. Set to `"keep"` to keep the search field unchanged after a selection. |
165
+ | results | `FuzzyResult[]` (default: `[]`) | Raw fuzzy results from the [fuzzy](https://github.com/mattyork/fuzzy) module |
166
+ | focusAfterSelect | `boolean` (default: `false`) | Set to `true` to re-focus the input after selecting a result. |
167
167
  | showDropdownOnFocus | `boolean` (default: `false`) | Set to `true` to only show results when the input is focused. |
168
- | limit | `number` (default: `Infinity`) | Specify the maximum number of results to display. |
169
- | `...$$restProps` | (forwarded to [`svelte-search`](https://github.com/metonym/svelte-search)) | All other props are forwarded to the input element. |
168
+ | limit | `number` (default: `Infinity`) | Specify the maximum number of results to display. |
169
+ | `...$$restProps` | (forwarded to [`svelte-search`](https://github.com/metonym/svelte-search)) | All other props are forwarded to the input element. |
170
170
 
171
171
  ### Dispatched events
172
172
 
package/lib/index.js CHANGED
@@ -1084,14 +1084,14 @@
1084
1084
 
1085
1085
  const get_no_results_slot_context = ctx => ({
1086
1086
  value: /*value*/ ctx[0],
1087
- result: /*result*/ ctx[46],
1088
- index: /*index*/ ctx[48]
1087
+ result: /*result*/ ctx[47],
1088
+ index: /*index*/ ctx[49]
1089
1089
  });
1090
1090
 
1091
1091
  function get_each_context(ctx, list, i) {
1092
1092
  const child_ctx = ctx.slice();
1093
- child_ctx[46] = list[i];
1094
- child_ctx[48] = i;
1093
+ child_ctx[47] = list[i];
1094
+ child_ctx[49] = i;
1095
1095
  return child_ctx;
1096
1096
  }
1097
1097
 
@@ -1101,12 +1101,12 @@
1101
1101
  });
1102
1102
 
1103
1103
  const get_default_slot_context = ctx => ({
1104
- result: /*result*/ ctx[46],
1105
- index: /*index*/ ctx[48],
1104
+ result: /*result*/ ctx[47],
1105
+ index: /*index*/ ctx[49],
1106
1106
  value: /*value*/ ctx[0]
1107
1107
  });
1108
1108
 
1109
- // (212:4) {#if showResults}
1109
+ // (230:4) {#if showResults}
1110
1110
  function create_if_block_1(ctx) {
1111
1111
  let each_1_anchor;
1112
1112
  let current;
@@ -1190,10 +1190,10 @@
1190
1190
  };
1191
1191
  }
1192
1192
 
1193
- // (230:41)
1193
+ // (248:41)
1194
1194
  function fallback_block(ctx) {
1195
1195
  let html_tag;
1196
- let raw_value = /*result*/ ctx[46].string + "";
1196
+ let raw_value = /*result*/ ctx[47].string + "";
1197
1197
  let html_anchor;
1198
1198
 
1199
1199
  return {
@@ -1207,7 +1207,7 @@
1207
1207
  insert(target, html_anchor, anchor);
1208
1208
  },
1209
1209
  p(ctx, dirty) {
1210
- if (dirty[0] & /*results*/ 2 && raw_value !== (raw_value = /*result*/ ctx[46].string + "")) html_tag.p(raw_value);
1210
+ if (dirty[0] & /*results*/ 2 && raw_value !== (raw_value = /*result*/ ctx[47].string + "")) html_tag.p(raw_value);
1211
1211
  },
1212
1212
  d(detaching) {
1213
1213
  if (detaching) detach(html_anchor);
@@ -1216,7 +1216,7 @@
1216
1216
  };
1217
1217
  }
1218
1218
 
1219
- // (213:6) {#each results as result, index}
1219
+ // (231:6) {#each results as result, index}
1220
1220
  function create_each_block(ctx) {
1221
1221
  let li;
1222
1222
  let t;
@@ -1230,11 +1230,11 @@
1230
1230
  const default_slot_or_fallback = default_slot || fallback_block(ctx);
1231
1231
 
1232
1232
  function click_handler_1() {
1233
- return /*click_handler_1*/ ctx[40](/*result*/ ctx[46], /*index*/ ctx[48]);
1233
+ return /*click_handler_1*/ ctx[40](/*result*/ ctx[47], /*index*/ ctx[49]);
1234
1234
  }
1235
1235
 
1236
1236
  function mouseenter_handler() {
1237
- return /*mouseenter_handler*/ ctx[41](/*result*/ ctx[46], /*index*/ ctx[48]);
1237
+ return /*mouseenter_handler*/ ctx[41](/*result*/ ctx[47], /*index*/ ctx[49]);
1238
1238
  }
1239
1239
 
1240
1240
  return {
@@ -1243,11 +1243,11 @@
1243
1243
  if (default_slot_or_fallback) default_slot_or_fallback.c();
1244
1244
  t = space();
1245
1245
  attr(li, "role", "option");
1246
- attr(li, "id", li_id_value = "" + (/*id*/ ctx[2] + "-result-" + /*index*/ ctx[48]));
1247
- attr(li, "aria-selected", li_aria_selected_value = /*selectedIndex*/ ctx[8] === /*index*/ ctx[48]);
1246
+ attr(li, "id", li_id_value = "" + (/*id*/ ctx[2] + "-result-" + /*index*/ ctx[49]));
1247
+ attr(li, "aria-selected", li_aria_selected_value = /*selectedIndex*/ ctx[8] === /*index*/ ctx[49]);
1248
1248
  attr(li, "class", "svelte-9wywte");
1249
- toggle_class(li, "selected", /*selectedIndex*/ ctx[8] === /*index*/ ctx[48]);
1250
- toggle_class(li, "disabled", /*result*/ ctx[46].disabled);
1249
+ toggle_class(li, "selected", /*selectedIndex*/ ctx[8] === /*index*/ ctx[49]);
1250
+ toggle_class(li, "disabled", /*result*/ ctx[47].disabled);
1251
1251
  },
1252
1252
  m(target, anchor) {
1253
1253
  insert(target, li, anchor);
@@ -1290,20 +1290,20 @@
1290
1290
  }
1291
1291
  }
1292
1292
 
1293
- if (!current || dirty[0] & /*id*/ 4 && li_id_value !== (li_id_value = "" + (/*id*/ ctx[2] + "-result-" + /*index*/ ctx[48]))) {
1293
+ if (!current || dirty[0] & /*id*/ 4 && li_id_value !== (li_id_value = "" + (/*id*/ ctx[2] + "-result-" + /*index*/ ctx[49]))) {
1294
1294
  attr(li, "id", li_id_value);
1295
1295
  }
1296
1296
 
1297
- if (!current || dirty[0] & /*selectedIndex*/ 256 && li_aria_selected_value !== (li_aria_selected_value = /*selectedIndex*/ ctx[8] === /*index*/ ctx[48])) {
1297
+ if (!current || dirty[0] & /*selectedIndex*/ 256 && li_aria_selected_value !== (li_aria_selected_value = /*selectedIndex*/ ctx[8] === /*index*/ ctx[49])) {
1298
1298
  attr(li, "aria-selected", li_aria_selected_value);
1299
1299
  }
1300
1300
 
1301
1301
  if (dirty[0] & /*selectedIndex*/ 256) {
1302
- toggle_class(li, "selected", /*selectedIndex*/ ctx[8] === /*index*/ ctx[48]);
1302
+ toggle_class(li, "selected", /*selectedIndex*/ ctx[8] === /*index*/ ctx[49]);
1303
1303
  }
1304
1304
 
1305
1305
  if (dirty[0] & /*results*/ 2) {
1306
- toggle_class(li, "disabled", /*result*/ ctx[46].disabled);
1306
+ toggle_class(li, "disabled", /*result*/ ctx[47].disabled);
1307
1307
  }
1308
1308
  },
1309
1309
  i(local) {
@@ -1324,7 +1324,7 @@
1324
1324
  };
1325
1325
  }
1326
1326
 
1327
- // (236:4) {#if $$slots["no-results"] && !hideDropdown && value.length > 0 && results.length === 0}
1327
+ // (254:4) {#if $$slots["no-results"] && !hideDropdown && value.length > 0 && results.length === 0}
1328
1328
  function create_if_block(ctx) {
1329
1329
  let div;
1330
1330
  let current;
@@ -1655,7 +1655,7 @@
1655
1655
 
1656
1656
  afterUpdate(() => {
1657
1657
  if (prevResults !== resultsId && autoselect) {
1658
- $$invalidate(8, selectedIndex = 0);
1658
+ $$invalidate(8, selectedIndex = getNextNonDisabledIndex());
1659
1659
  }
1660
1660
 
1661
1661
  if (prevResults !== resultsId && !$$slots["no-results"]) {
@@ -1686,6 +1686,24 @@
1686
1686
  close();
1687
1687
  }
1688
1688
 
1689
+ /** @type {() => number} */
1690
+ function getNextNonDisabledIndex() {
1691
+ let index = 0;
1692
+ let disabled = results[index]?.disabled ?? false;
1693
+
1694
+ while (disabled) {
1695
+ if (index === results.length) {
1696
+ index = 0;
1697
+ } else {
1698
+ index += 1;
1699
+ }
1700
+
1701
+ disabled = results[index]?.disabled ?? false;
1702
+ }
1703
+
1704
+ return index;
1705
+ }
1706
+
1689
1707
  /** @type {(direction: -1 | 1) => void} */
1690
1708
  function change(direction) {
1691
1709
  let index = direction === 1 && selectedIndex === results.length - 1
@@ -1928,9 +1946,7 @@
1928
1946
  }
1929
1947
  }
1930
1948
 
1931
- var Typeahead$1 = Typeahead;
1932
-
1933
- exports["default"] = Typeahead$1;
1949
+ exports["default"] = Typeahead;
1934
1950
 
1935
1951
  Object.defineProperty(exports, '__esModule', { value: true });
1936
1952
 
package/lib/index.mjs CHANGED
@@ -1078,14 +1078,14 @@ const get_no_results_slot_changes = dirty => ({
1078
1078
 
1079
1079
  const get_no_results_slot_context = ctx => ({
1080
1080
  value: /*value*/ ctx[0],
1081
- result: /*result*/ ctx[46],
1082
- index: /*index*/ ctx[48]
1081
+ result: /*result*/ ctx[47],
1082
+ index: /*index*/ ctx[49]
1083
1083
  });
1084
1084
 
1085
1085
  function get_each_context(ctx, list, i) {
1086
1086
  const child_ctx = ctx.slice();
1087
- child_ctx[46] = list[i];
1088
- child_ctx[48] = i;
1087
+ child_ctx[47] = list[i];
1088
+ child_ctx[49] = i;
1089
1089
  return child_ctx;
1090
1090
  }
1091
1091
 
@@ -1095,12 +1095,12 @@ const get_default_slot_changes = dirty => ({
1095
1095
  });
1096
1096
 
1097
1097
  const get_default_slot_context = ctx => ({
1098
- result: /*result*/ ctx[46],
1099
- index: /*index*/ ctx[48],
1098
+ result: /*result*/ ctx[47],
1099
+ index: /*index*/ ctx[49],
1100
1100
  value: /*value*/ ctx[0]
1101
1101
  });
1102
1102
 
1103
- // (212:4) {#if showResults}
1103
+ // (230:4) {#if showResults}
1104
1104
  function create_if_block_1(ctx) {
1105
1105
  let each_1_anchor;
1106
1106
  let current;
@@ -1184,10 +1184,10 @@ function create_if_block_1(ctx) {
1184
1184
  };
1185
1185
  }
1186
1186
 
1187
- // (230:41)
1187
+ // (248:41)
1188
1188
  function fallback_block(ctx) {
1189
1189
  let html_tag;
1190
- let raw_value = /*result*/ ctx[46].string + "";
1190
+ let raw_value = /*result*/ ctx[47].string + "";
1191
1191
  let html_anchor;
1192
1192
 
1193
1193
  return {
@@ -1201,7 +1201,7 @@ function fallback_block(ctx) {
1201
1201
  insert(target, html_anchor, anchor);
1202
1202
  },
1203
1203
  p(ctx, dirty) {
1204
- if (dirty[0] & /*results*/ 2 && raw_value !== (raw_value = /*result*/ ctx[46].string + "")) html_tag.p(raw_value);
1204
+ if (dirty[0] & /*results*/ 2 && raw_value !== (raw_value = /*result*/ ctx[47].string + "")) html_tag.p(raw_value);
1205
1205
  },
1206
1206
  d(detaching) {
1207
1207
  if (detaching) detach(html_anchor);
@@ -1210,7 +1210,7 @@ function fallback_block(ctx) {
1210
1210
  };
1211
1211
  }
1212
1212
 
1213
- // (213:6) {#each results as result, index}
1213
+ // (231:6) {#each results as result, index}
1214
1214
  function create_each_block(ctx) {
1215
1215
  let li;
1216
1216
  let t;
@@ -1224,11 +1224,11 @@ function create_each_block(ctx) {
1224
1224
  const default_slot_or_fallback = default_slot || fallback_block(ctx);
1225
1225
 
1226
1226
  function click_handler_1() {
1227
- return /*click_handler_1*/ ctx[40](/*result*/ ctx[46], /*index*/ ctx[48]);
1227
+ return /*click_handler_1*/ ctx[40](/*result*/ ctx[47], /*index*/ ctx[49]);
1228
1228
  }
1229
1229
 
1230
1230
  function mouseenter_handler() {
1231
- return /*mouseenter_handler*/ ctx[41](/*result*/ ctx[46], /*index*/ ctx[48]);
1231
+ return /*mouseenter_handler*/ ctx[41](/*result*/ ctx[47], /*index*/ ctx[49]);
1232
1232
  }
1233
1233
 
1234
1234
  return {
@@ -1237,11 +1237,11 @@ function create_each_block(ctx) {
1237
1237
  if (default_slot_or_fallback) default_slot_or_fallback.c();
1238
1238
  t = space();
1239
1239
  attr(li, "role", "option");
1240
- attr(li, "id", li_id_value = "" + (/*id*/ ctx[2] + "-result-" + /*index*/ ctx[48]));
1241
- attr(li, "aria-selected", li_aria_selected_value = /*selectedIndex*/ ctx[8] === /*index*/ ctx[48]);
1240
+ attr(li, "id", li_id_value = "" + (/*id*/ ctx[2] + "-result-" + /*index*/ ctx[49]));
1241
+ attr(li, "aria-selected", li_aria_selected_value = /*selectedIndex*/ ctx[8] === /*index*/ ctx[49]);
1242
1242
  attr(li, "class", "svelte-9wywte");
1243
- toggle_class(li, "selected", /*selectedIndex*/ ctx[8] === /*index*/ ctx[48]);
1244
- toggle_class(li, "disabled", /*result*/ ctx[46].disabled);
1243
+ toggle_class(li, "selected", /*selectedIndex*/ ctx[8] === /*index*/ ctx[49]);
1244
+ toggle_class(li, "disabled", /*result*/ ctx[47].disabled);
1245
1245
  },
1246
1246
  m(target, anchor) {
1247
1247
  insert(target, li, anchor);
@@ -1284,20 +1284,20 @@ function create_each_block(ctx) {
1284
1284
  }
1285
1285
  }
1286
1286
 
1287
- if (!current || dirty[0] & /*id*/ 4 && li_id_value !== (li_id_value = "" + (/*id*/ ctx[2] + "-result-" + /*index*/ ctx[48]))) {
1287
+ if (!current || dirty[0] & /*id*/ 4 && li_id_value !== (li_id_value = "" + (/*id*/ ctx[2] + "-result-" + /*index*/ ctx[49]))) {
1288
1288
  attr(li, "id", li_id_value);
1289
1289
  }
1290
1290
 
1291
- if (!current || dirty[0] & /*selectedIndex*/ 256 && li_aria_selected_value !== (li_aria_selected_value = /*selectedIndex*/ ctx[8] === /*index*/ ctx[48])) {
1291
+ if (!current || dirty[0] & /*selectedIndex*/ 256 && li_aria_selected_value !== (li_aria_selected_value = /*selectedIndex*/ ctx[8] === /*index*/ ctx[49])) {
1292
1292
  attr(li, "aria-selected", li_aria_selected_value);
1293
1293
  }
1294
1294
 
1295
1295
  if (dirty[0] & /*selectedIndex*/ 256) {
1296
- toggle_class(li, "selected", /*selectedIndex*/ ctx[8] === /*index*/ ctx[48]);
1296
+ toggle_class(li, "selected", /*selectedIndex*/ ctx[8] === /*index*/ ctx[49]);
1297
1297
  }
1298
1298
 
1299
1299
  if (dirty[0] & /*results*/ 2) {
1300
- toggle_class(li, "disabled", /*result*/ ctx[46].disabled);
1300
+ toggle_class(li, "disabled", /*result*/ ctx[47].disabled);
1301
1301
  }
1302
1302
  },
1303
1303
  i(local) {
@@ -1318,7 +1318,7 @@ function create_each_block(ctx) {
1318
1318
  };
1319
1319
  }
1320
1320
 
1321
- // (236:4) {#if $$slots["no-results"] && !hideDropdown && value.length > 0 && results.length === 0}
1321
+ // (254:4) {#if $$slots["no-results"] && !hideDropdown && value.length > 0 && results.length === 0}
1322
1322
  function create_if_block(ctx) {
1323
1323
  let div;
1324
1324
  let current;
@@ -1649,7 +1649,7 @@ function instance($$self, $$props, $$invalidate) {
1649
1649
 
1650
1650
  afterUpdate(() => {
1651
1651
  if (prevResults !== resultsId && autoselect) {
1652
- $$invalidate(8, selectedIndex = 0);
1652
+ $$invalidate(8, selectedIndex = getNextNonDisabledIndex());
1653
1653
  }
1654
1654
 
1655
1655
  if (prevResults !== resultsId && !$$slots["no-results"]) {
@@ -1680,6 +1680,24 @@ function instance($$self, $$props, $$invalidate) {
1680
1680
  close();
1681
1681
  }
1682
1682
 
1683
+ /** @type {() => number} */
1684
+ function getNextNonDisabledIndex() {
1685
+ let index = 0;
1686
+ let disabled = results[index]?.disabled ?? false;
1687
+
1688
+ while (disabled) {
1689
+ if (index === results.length) {
1690
+ index = 0;
1691
+ } else {
1692
+ index += 1;
1693
+ }
1694
+
1695
+ disabled = results[index]?.disabled ?? false;
1696
+ }
1697
+
1698
+ return index;
1699
+ }
1700
+
1683
1701
  /** @type {(direction: -1 | 1) => void} */
1684
1702
  function change(direction) {
1685
1703
  let index = direction === 1 && selectedIndex === results.length - 1
@@ -1922,6 +1940,4 @@ class Typeahead extends SvelteComponent {
1922
1940
  }
1923
1941
  }
1924
1942
 
1925
- var Typeahead$1 = Typeahead;
1926
-
1927
- export { Typeahead$1 as default };
1943
+ export { Typeahead as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-typeahead",
3
- "version": "4.3.0",
3
+ "version": "4.3.1",
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",
@@ -57,7 +57,7 @@
57
57
 
58
58
  afterUpdate(() => {
59
59
  if (prevResults !== resultsId && autoselect) {
60
- selectedIndex = 0;
60
+ selectedIndex = getNextNonDisabledIndex();
61
61
  }
62
62
 
63
63
  if (prevResults !== resultsId && !$$slots["no-results"]) {
@@ -92,6 +92,24 @@
92
92
  close();
93
93
  }
94
94
 
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;
111
+ }
112
+
95
113
  /** @type {(direction: -1 | 1) => void} */
96
114
  function change(direction) {
97
115
  let index =