@itrocks/autocomplete 0.0.6 → 0.0.7
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/autocomplete.css +1 -0
- package/autocomplete.d.ts +1 -1
- package/autocomplete.js +12 -7
- package/package.json +1 -1
package/autocomplete.css
CHANGED
package/autocomplete.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ export declare class AutoComplete {
|
|
|
19
19
|
keyEscape(event: KeyboardEvent): void;
|
|
20
20
|
keyUp(event: KeyboardEvent): void;
|
|
21
21
|
onBlur(_event: FocusEvent): void;
|
|
22
|
-
onInput(
|
|
22
|
+
onInput(event: Event): void;
|
|
23
23
|
onInputValueChange(): void;
|
|
24
24
|
onKeyDown(event: KeyboardEvent): void;
|
|
25
25
|
suggest(value?: string): void;
|
package/autocomplete.js
CHANGED
|
@@ -55,12 +55,13 @@ export class AutoComplete {
|
|
|
55
55
|
const dataFetch = input.dataset.fetch ?? input.closest('[data-fetch]')?.dataset.fetch;
|
|
56
56
|
const lastKey = this.lastKey;
|
|
57
57
|
const requestInit = { headers: { Accept: 'application/json' } };
|
|
58
|
-
const summaryRoute = dataFetch + '?startsWith=' + input.value;
|
|
58
|
+
const summaryRoute = dataFetch + (input.value ? ('?startsWith=' + input.value) : '');
|
|
59
59
|
fetch(summaryRoute, requestInit).then(response => response.text()).then(json => {
|
|
60
|
-
const summary = JSON.parse(json);
|
|
60
|
+
const summary = JSON.parse(json).map(([id, caption]) => ({ caption, id: +id }));
|
|
61
61
|
const startsWith = input.value.toLowerCase();
|
|
62
|
-
const suggestions =
|
|
63
|
-
.filter(item => item.caption.toLowerCase().startsWith(startsWith))
|
|
62
|
+
const suggestions = startsWith.length
|
|
63
|
+
? summary.filter(item => item.caption.toLowerCase().startsWith(startsWith))
|
|
64
|
+
: summary;
|
|
64
65
|
this.suggestions.update(suggestions);
|
|
65
66
|
if (!['Backspace', 'Delete'].includes(lastKey)) {
|
|
66
67
|
this.autoComplete();
|
|
@@ -83,6 +84,9 @@ export class AutoComplete {
|
|
|
83
84
|
}
|
|
84
85
|
keyDown(event) {
|
|
85
86
|
const suggestions = this.suggestions;
|
|
87
|
+
if (!suggestions.length) {
|
|
88
|
+
this.fetch();
|
|
89
|
+
}
|
|
86
90
|
if (!suggestions.isVisible()) {
|
|
87
91
|
if (suggestions.length > 1) {
|
|
88
92
|
event.preventDefault();
|
|
@@ -140,10 +144,11 @@ export class AutoComplete {
|
|
|
140
144
|
onBlur(_event) {
|
|
141
145
|
setTimeout(() => this.suggestions.removeList());
|
|
142
146
|
}
|
|
143
|
-
onInput(
|
|
144
|
-
if (
|
|
147
|
+
onInput(event) {
|
|
148
|
+
if (document.activeElement !== event.target)
|
|
149
|
+
return;
|
|
150
|
+
if (this.input.dataset.lastValue === this.input.value)
|
|
145
151
|
return;
|
|
146
|
-
}
|
|
147
152
|
this.fetch();
|
|
148
153
|
}
|
|
149
154
|
onInputValueChange() {
|
package/package.json
CHANGED