svelte-typeahead 4.1.0 → 4.2.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.
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.2.0](https://github.com/metonym/svelte-typeahead/releases/tag/v4.2.0) - 2021-10-26
9
+
10
+ **Features**
11
+
12
+ - use TypeScript generics so that the item type can be inferred from `data` value
13
+
8
14
  ## [4.1.0](https://github.com/metonym/svelte-typeahead/releases/tag/v4.1.0) - 2021-10-03
9
15
 
10
16
  **Features**
package/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  This component uses the lightweight [fuzzy](https://github.com/mattyork/fuzzy) library for client-side, fuzzy search and follows [WAI-ARIA guidelines](https://www.w3.org/TR/wai-aria-practices/examples/combobox/aria1.1pattern/listbox-combo.html).
10
10
 
11
- Try it in the [Svelte REPL](https://svelte.dev/repl/a1b828d80de24f7e995b2365782c8d04?version=3.24.1).
11
+ Try it in the [Svelte REPL](https://svelte.dev/repl/a1b828d80de24f7e995b2365782c8d04).
12
12
 
13
13
  ---
14
14
 
@@ -130,6 +130,8 @@ Example for disabling and filtering items by their title length:
130
130
 
131
131
  ```svelte
132
132
  <script>
133
+ import Typeahead from "svelte-typeahead";
134
+
133
135
  const disable = (item) => item.state.length > 4;
134
136
  const filter = (item) => item.state.length > 8;
135
137
  </script>
@@ -140,18 +142,13 @@ Example for disabling and filtering items by their title length:
140
142
  Example for disabling items after selecting them:
141
143
 
142
144
  ```svelte
143
- <script>
144
- function handleSelect(e) {
145
- let i = e.detail.originalIndex;
146
- data[i].selected = true;
147
- }
148
- </script>
149
-
150
145
  <Typeahead
151
146
  {data}
152
147
  extract={(item) => item.state}
153
148
  disable={(item) => item.selected}
154
- on:select={handleSelect}
149
+ on:select={({ detail }) => {
150
+ data[detail.originalIndex].selected = true;
151
+ }}
155
152
  />
156
153
  ```
157
154
 
@@ -188,6 +185,8 @@ Set `focusAfterSelect` to `true` to re-focus the search input after selecting a
188
185
 
189
186
  ```svelte
190
187
  <script>
188
+ import Typeahead from "svelte-typeahead";
189
+
191
190
  let events = [];
192
191
 
193
192
  const update = (event, detail) => (events = [...events, { event, detail }]);
package/lib/index.js CHANGED
@@ -703,7 +703,7 @@
703
703
 
704
704
  var fuzzy = fuzzy$1.exports;
705
705
 
706
- /* node_modules/svelte-search/src/Search.svelte generated by Svelte v3.43.1 */
706
+ /* node_modules/svelte-search/src/Search.svelte generated by Svelte v3.44.0 */
707
707
 
708
708
  function add_css$1(target) {
709
709
  append_styles(target, "svelte-5m0wg6", ".hide-label.svelte-5m0wg6{position:absolute;height:1px;width:1px;overflow:hidden;clip:rect(1px 1px 1px 1px);clip:rect(1px, 1px, 1px, 1px);white-space:nowrap}");
@@ -1040,7 +1040,7 @@
1040
1040
 
1041
1041
  var Search$1 = Search;
1042
1042
 
1043
- /* src/Typeahead.svelte generated by Svelte v3.43.1 */
1043
+ /* src/Typeahead.svelte generated by Svelte v3.44.0 */
1044
1044
 
1045
1045
  function add_css(target) {
1046
1046
  append_styles(target, "svelte-j5n77y", "[data-svelte-typeahead].svelte-j5n77y{position:relative;background-color:#fff}ul.svelte-j5n77y{position:absolute;top:100%;left:0;width:100%;padding:0;list-style:none;background-color:inherit;box-shadow:0 4px 8px rgba(0, 0, 0, 0.1)}li.svelte-j5n77y,.no-results.svelte-j5n77y{padding:0.25rem 1rem}li.svelte-j5n77y{cursor:pointer}li.svelte-j5n77y:not(:last-of-type){border-bottom:1px solid #e0e0e0}li.svelte-j5n77y:hover{background-color:#e5e5e5}.selected.svelte-j5n77y{background-color:#e5e5e5}.selected.svelte-j5n77y:hover{background-color:#cacaca}.disabled.svelte-j5n77y{opacity:0.4;cursor:not-allowed}[data-svelte-search] label{margin-bottom:0.25rem;display:inline-flex;font-size:0.875rem}[data-svelte-search] input{width:100%;padding:0.5rem 0.75rem;background:none;font-size:1rem;border:0;border-radius:0;border:1px solid #e5e5e5}[data-svelte-search] input:focus{outline-color:#0f62fe;outline-offset:2px;outline-width:1px}");
@@ -1075,7 +1075,7 @@
1075
1075
  value: /*value*/ ctx[0]
1076
1076
  });
1077
1077
 
1078
- // (184:4) {#if !hideDropdown && results.length > 0}
1078
+ // (180:4) {#if !hideDropdown && results.length > 0}
1079
1079
  function create_if_block_1(ctx) {
1080
1080
  let each_1_anchor;
1081
1081
  let current;
@@ -1159,7 +1159,7 @@
1159
1159
  };
1160
1160
  }
1161
1161
 
1162
- // (199:43)
1162
+ // (195:43)
1163
1163
  function fallback_block(ctx) {
1164
1164
  let html_tag;
1165
1165
  let raw_value = /*result*/ ctx[39].string + "";
@@ -1185,7 +1185,7 @@
1185
1185
  };
1186
1186
  }
1187
1187
 
1188
- // (185:6) {#each results as result, i}
1188
+ // (181:6) {#each results as result, i}
1189
1189
  function create_each_block(ctx) {
1190
1190
  let li;
1191
1191
  let t;
@@ -1285,7 +1285,7 @@
1285
1285
  };
1286
1286
  }
1287
1287
 
1288
- // (205:4) {#if $$slots["no-results"] && !hideDropdown && value.length > 0 && results.length === 0}
1288
+ // (201:4) {#if $$slots["no-results"] && !hideDropdown && value.length > 0 && results.length === 0}
1289
1289
  function create_if_block(ctx) {
1290
1290
  let div;
1291
1291
  let current;
package/lib/index.mjs CHANGED
@@ -697,7 +697,7 @@ fuzzy.filter = function(pattern, arr, opts) {
697
697
 
698
698
  var fuzzy = fuzzy$1.exports;
699
699
 
700
- /* node_modules/svelte-search/src/Search.svelte generated by Svelte v3.43.1 */
700
+ /* node_modules/svelte-search/src/Search.svelte generated by Svelte v3.44.0 */
701
701
 
702
702
  function add_css$1(target) {
703
703
  append_styles(target, "svelte-5m0wg6", ".hide-label.svelte-5m0wg6{position:absolute;height:1px;width:1px;overflow:hidden;clip:rect(1px 1px 1px 1px);clip:rect(1px, 1px, 1px, 1px);white-space:nowrap}");
@@ -1034,7 +1034,7 @@ class Search extends SvelteComponent {
1034
1034
 
1035
1035
  var Search$1 = Search;
1036
1036
 
1037
- /* src/Typeahead.svelte generated by Svelte v3.43.1 */
1037
+ /* src/Typeahead.svelte generated by Svelte v3.44.0 */
1038
1038
 
1039
1039
  function add_css(target) {
1040
1040
  append_styles(target, "svelte-j5n77y", "[data-svelte-typeahead].svelte-j5n77y{position:relative;background-color:#fff}ul.svelte-j5n77y{position:absolute;top:100%;left:0;width:100%;padding:0;list-style:none;background-color:inherit;box-shadow:0 4px 8px rgba(0, 0, 0, 0.1)}li.svelte-j5n77y,.no-results.svelte-j5n77y{padding:0.25rem 1rem}li.svelte-j5n77y{cursor:pointer}li.svelte-j5n77y:not(:last-of-type){border-bottom:1px solid #e0e0e0}li.svelte-j5n77y:hover{background-color:#e5e5e5}.selected.svelte-j5n77y{background-color:#e5e5e5}.selected.svelte-j5n77y:hover{background-color:#cacaca}.disabled.svelte-j5n77y{opacity:0.4;cursor:not-allowed}[data-svelte-search] label{margin-bottom:0.25rem;display:inline-flex;font-size:0.875rem}[data-svelte-search] input{width:100%;padding:0.5rem 0.75rem;background:none;font-size:1rem;border:0;border-radius:0;border:1px solid #e5e5e5}[data-svelte-search] input:focus{outline-color:#0f62fe;outline-offset:2px;outline-width:1px}");
@@ -1069,7 +1069,7 @@ const get_default_slot_context = ctx => ({
1069
1069
  value: /*value*/ ctx[0]
1070
1070
  });
1071
1071
 
1072
- // (184:4) {#if !hideDropdown && results.length > 0}
1072
+ // (180:4) {#if !hideDropdown && results.length > 0}
1073
1073
  function create_if_block_1(ctx) {
1074
1074
  let each_1_anchor;
1075
1075
  let current;
@@ -1153,7 +1153,7 @@ function create_if_block_1(ctx) {
1153
1153
  };
1154
1154
  }
1155
1155
 
1156
- // (199:43)
1156
+ // (195:43)
1157
1157
  function fallback_block(ctx) {
1158
1158
  let html_tag;
1159
1159
  let raw_value = /*result*/ ctx[39].string + "";
@@ -1179,7 +1179,7 @@ function fallback_block(ctx) {
1179
1179
  };
1180
1180
  }
1181
1181
 
1182
- // (185:6) {#each results as result, i}
1182
+ // (181:6) {#each results as result, i}
1183
1183
  function create_each_block(ctx) {
1184
1184
  let li;
1185
1185
  let t;
@@ -1279,7 +1279,7 @@ function create_each_block(ctx) {
1279
1279
  };
1280
1280
  }
1281
1281
 
1282
- // (205:4) {#if $$slots["no-results"] && !hideDropdown && value.length > 0 && results.length === 0}
1282
+ // (201:4) {#if $$slots["no-results"] && !hideDropdown && value.length > 0 && results.length === 0}
1283
1283
  function create_if_block(ctx) {
1284
1284
  let div;
1285
1285
  let current;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-typeahead",
3
- "version": "4.1.0",
3
+ "version": "4.2.0",
4
4
  "license": "MIT",
5
5
  "description": "Accessible, fuzzy search typeahead component",
6
6
  "author": "Eric Liu (https://github.com/metonym)",
@@ -21,10 +21,10 @@
21
21
  "svelte-search": "^1.1.0"
22
22
  },
23
23
  "devDependencies": {
24
- "@rollup/plugin-commonjs": "^21.0.0",
25
- "svelte": "^3.43.1",
24
+ "@rollup/plugin-commonjs": "^21.0.1",
25
+ "svelte": "^3.44.0",
26
26
  "svelte-check": "^2.2.6",
27
- "svelte-readme": "^3.3.1"
27
+ "svelte-readme": "^3.4.1"
28
28
  },
29
29
  "repository": {
30
30
  "type": "git",
@@ -1,25 +1,21 @@
1
1
  <script>
2
2
  /**
3
- * @typedef {string | number | Record<string, any>} Item
4
- * @typedef {{ original: Item; index: number; score: number; string: string; }} FuzzyResult
5
- * @event {{ searched: string; selected: Item; selectedIndex: number; original: Item; originalIndex: number; }} select
6
- * @event {any} clear
7
- * @slot {{ result: FuzzyResult; index: number }}
3
+ * @template TItem = string | number | Record<string, any>
8
4
  */
9
5
 
10
6
  export let id = "typeahead-" + Math.random().toString(36);
11
7
  export let value = "";
12
8
 
13
- /** @type {Item[]} */
9
+ /** @type {TItem[]} */
14
10
  export let data = [];
15
11
 
16
- /** @type {(item: Item) => Item} */
12
+ /** @type {(item: TItem) => any} */
17
13
  export let extract = (item) => item;
18
14
 
19
- /** @type {(item: Item) => Item} */
15
+ /** @type {(item: TItem) => boolean} */
20
16
  export let disable = (item) => false;
21
17
 
22
- /** @type {(item: Item) => Item} */
18
+ /** @type {(item: TItem) => boolean} */
23
19
  export let filter = (item) => false;
24
20
 
25
21
  /** Set to `false` to prevent the first result from being selected */
@@ -31,7 +27,7 @@
31
27
  */
32
28
  export let inputAfterSelect = "update";
33
29
 
34
- /** @type {FuzzyResult[]} */
30
+ /** @type {{ original: TItem; index: number; score: number; string: string; }[]} */
35
31
  export let results = [];
36
32
 
37
33
  /** Set to `true` to re-focus the input after selecting a result */
@@ -2,16 +2,7 @@
2
2
  import { SvelteComponentTyped } from "svelte";
3
3
  import { SearchProps } from "svelte-search/types/Search";
4
4
 
5
- export type Item = string | number | Record<string, any>;
6
-
7
- export interface FuzzyResult {
8
- original: Item;
9
- index: number;
10
- score: number;
11
- string: string;
12
- }
13
-
14
- export interface TypeaheadProps extends SearchProps {
5
+ export interface TypeaheadProps<TItem> extends SearchProps {
15
6
  /**
16
7
  * @default "typeahead-" + Math.random().toString(36)
17
8
  */
@@ -25,22 +16,22 @@ export interface TypeaheadProps extends SearchProps {
25
16
  /**
26
17
  * @default []
27
18
  */
28
- data?: Item[];
19
+ data?: TItem[];
29
20
 
30
21
  /**
31
22
  * @default (item) => item
32
23
  */
33
- extract?: (item: Item) => Item;
24
+ extract?: (item: TItem) => any;
34
25
 
35
26
  /**
36
27
  * @default (item) => false
37
28
  */
38
- disable?: (item: Item) => boolean;
29
+ disable?: (item: TItem) => boolean;
39
30
 
40
31
  /**
41
32
  * @default (item) => false
42
33
  */
43
- filter?: (item: Item) => boolean;
34
+ filter?: (item: TItem) => boolean;
44
35
 
45
36
  /**
46
37
  * Set to `false` to prevent the first result from being selected
@@ -57,7 +48,12 @@ export interface TypeaheadProps extends SearchProps {
57
48
  /**
58
49
  * @default []
59
50
  */
60
- results?: FuzzyResult[];
51
+ results?: {
52
+ original: TItem;
53
+ index: number;
54
+ score: number;
55
+ string: string;
56
+ }[];
61
57
 
62
58
  /**
63
59
  * Set to `true` to re-focus the input after selecting a result
@@ -72,14 +68,16 @@ export interface TypeaheadProps extends SearchProps {
72
68
  limit?: number;
73
69
  }
74
70
 
75
- export default class Typeahead extends SvelteComponentTyped<
76
- TypeaheadProps,
71
+ export default class Typeahead<
72
+ TItem = string | number | Record<string, any>
73
+ > extends SvelteComponentTyped<
74
+ TypeaheadProps<TItem>,
77
75
  {
78
76
  select: CustomEvent<{
79
77
  searched: string;
80
- selected: Item;
78
+ selected: TItem;
81
79
  selectedIndex: number;
82
- original: Item;
80
+ original: TItem;
83
81
  originalIndex: number;
84
82
  }>;
85
83
  type: CustomEvent<string>;
@@ -92,7 +90,12 @@ export default class Typeahead extends SvelteComponentTyped<
92
90
  },
93
91
  {
94
92
  default: {
95
- result: FuzzyResult;
93
+ result: {
94
+ original: TItem;
95
+ index: number;
96
+ score: number;
97
+ string: string;
98
+ };
96
99
  index: number;
97
100
  value: string;
98
101
  };