You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+32-18Lines changed: 32 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -2,39 +2,54 @@
2
2
3
3
A small package to provide an autocomplete list as a user is typing.
4
4
5
-
### Links
5
+
### Contents
6
6
7
+
-[Features](#features)
8
+
-[Usage](#usage)
9
+
-[Strong Types](#strong-types)
7
10
-[Options](#options)
8
11
-[Option Methods](#option-methods)
9
12
-[Change log](./CHANGELOG.md)
10
13
-[License (MIT)](./LICENSE)
11
14
15
+
### Features
16
+
17
+
- Supports different sources; whether `string`, `object`, `array` or callback
18
+
- Allows specification of the source type with strong types
19
+
- Complete control over construction, with custom renderers
20
+
- Option to automatically focus on the first element when the menu opens
21
+
- Configurable delay between keystrokes and source calls
22
+
- Supports a minimum term, before querying the source
23
+
- Custom mapping of object properties to the generic type
24
+
- Flexible positioning of the autocomplete box
25
+
- Ability to append the menu to a specific element
26
+
- Event callbacks for menu interactions and lifecycle events
27
+
12
28
## Usage
13
29
14
30
### Basic usage
15
31
16
-
To add autocomplete to an input with the class "`autocomplete`", use the sample code below - which will query the specified source URL and expect a JSON response.
17
-
18
-
Without specifying a type, autocomplete defaults to the generic type:
19
-
`{ label: string; value: string }`
32
+
> ℹ️ Without specifying a type, autocomplete defaults to the generic type:
33
+
> `{ label: string; value: string }`
20
34
21
35
```TS
22
36
// Using the default options (source is always required)
23
37
newAutocomplete(
24
38
'.autocomplete',
25
39
{
40
+
// Query this source & expect a JSON response
26
41
source: './relative-folder/query.html'
27
42
}
28
43
)
29
44
// Don't forget to start it
30
45
.start();
31
46
```
32
47
33
-
<details><summary>
34
-
<h3>Custom source type</h3>
35
-
</summary>
48
+
### Strong Types
36
49
37
-
You can also specify the source type for some strong types!
50
+
As the package is written in TypeScript, you can specify the `source`'s type.
51
+
52
+
With the type specified you can write your TypeScript with strong type definitions!
38
53
39
54
In this example I've provided an array of inputs (that will always be returned) - this is also strongly typed!
40
55
@@ -44,7 +59,7 @@ type MyType = {
44
59
myCustomValue:string;
45
60
}
46
61
47
-
//Using the custom MyType (which is now tightly-bound)
62
+
//Specifying MyType means "source" is now tightly-bound
48
63
newAutocomplete<MyType>(
49
64
'.autocomplete',
50
65
{
@@ -69,7 +84,9 @@ new Autocomplete<MyType>(
69
84
70
85
li.dataset.value=item.myCustomName;
71
86
72
-
//li.innerText = item.detail; <-- This isn't in `MyType`
87
+
// li.innerText = item.detail;
88
+
// The above would now throw:
89
+
// "Property 'detail' does not exist on type 'MyType'"
73
90
74
91
li.innerText=item.myCustomValue;
75
92
@@ -81,8 +98,6 @@ new Autocomplete<MyType>(
81
98
.start();
82
99
```
83
100
84
-
</details>
85
-
86
101
## Options
87
102
88
103
### source: `SourceTypes<T>`
@@ -165,11 +180,12 @@ Called as soon as an item is focused, but before changing its state
165
180
**Call:**
166
181
167
182
```TS
183
+
// T is your generic type, if specified
168
184
onItemFocus: (
169
185
ev: Event,
170
186
data: {
171
187
ul: HTMLUListElement
172
-
item: <T>,// Your generic type, if specified
188
+
item: <T>,
173
189
input: HTMLInputElement
174
190
}
175
191
) => {}
@@ -181,11 +197,12 @@ Called as soon as an item is selecetd, but before changing any state
181
197
**Call:**
182
198
183
199
```TS
200
+
// T is your generic type, if specified
184
201
onItemSelect: (
185
202
ev: Event,
186
203
data: {
187
204
ul: HTMLUListElement
188
-
item: <T>,// Your generic type, if specified
205
+
item: <T>,
189
206
input: HTMLInputElement
190
207
}
191
208
) => {}
@@ -216,7 +233,6 @@ Called before processing the search
216
233
**Call:**
217
234
218
235
```TS
219
-
// T is your generic type, if specified
220
236
onSearch: (ev:Event, data: { term:string }) => {}
221
237
```
222
238
@@ -226,7 +242,6 @@ A method called after events have been added
226
242
**Call:**
227
243
228
244
```TS
229
-
// T is your generic type, if specified
230
245
onStart: () => {}
231
246
```
232
247
@@ -236,6 +251,5 @@ A method called after events have been removed
0 commit comments