@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 CHANGED
@@ -8,6 +8,7 @@
8
8
  margin-top: -1px;
9
9
  padding: 0;
10
10
  position: absolute;
11
+ z-index: 1;
11
12
  }
12
13
  .combobox .suggestions:empty {
13
14
  height: 2em;
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(_event: Event): void;
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 = summary.map(([id, caption]) => ({ caption, id: +id }))
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(_event) {
144
- if (this.input.dataset.lastValue === this.input.value) {
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
@@ -40,5 +40,5 @@
40
40
  },
41
41
  "type": "module",
42
42
  "types": "./autocomplete.d.ts",
43
- "version": "0.0.6"
43
+ "version": "0.0.7"
44
44
  }