@itrocks/autocomplete 0.1.4 → 0.1.5

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.d.ts CHANGED
@@ -24,6 +24,8 @@ export declare class AutoComplete {
24
24
  onInput(event: Event): void;
25
25
  onInputValueChange(): void;
26
26
  onKeyDown(event: KeyboardEvent): void;
27
+ onTouchStart(event: Event): void;
28
+ openSuggestions(event: Event): boolean;
27
29
  select(): void;
28
30
  suggest(value?: string): void;
29
31
  }
package/autocomplete.js CHANGED
@@ -15,6 +15,7 @@ export class AutoComplete {
15
15
  input.addEventListener('blur', event => this.onBlur(event));
16
16
  input.addEventListener('keydown', event => this.onKeyDown(event));
17
17
  input.addEventListener('input', event => this.onInput(event));
18
+ input.addEventListener('touchstart', event => this.onTouchStart(event));
18
19
  }
19
20
  autoComplete() {
20
21
  if (DEBUG)
@@ -122,22 +123,11 @@ export class AutoComplete {
122
123
  keyDown(event) {
123
124
  if (DEBUG)
124
125
  console.log('keyDown()');
125
- const suggestions = this.suggestions;
126
- if (!suggestions.length) {
127
- this.fetch();
128
- }
129
- if (!suggestions.isVisible()) {
130
- if (suggestions.length > 1) {
131
- event.preventDefault();
132
- suggestions.show();
133
- }
126
+ if (this.openSuggestions(event))
134
127
  return;
135
- }
136
- event.preventDefault();
137
- if (suggestions.isLastSelected()) {
128
+ if (this.suggestions.isLastSelected())
138
129
  return;
139
- }
140
- this.suggest(suggestions.selectNext()?.caption);
130
+ this.suggest(this.suggestions.selectNext()?.caption);
141
131
  }
142
132
  keyEnter(event) {
143
133
  if (DEBUG)
@@ -223,6 +213,23 @@ export class AutoComplete {
223
213
  return this.keyEnter(event);
224
214
  }
225
215
  }
216
+ onTouchStart(event) {
217
+ this.openSuggestions(event);
218
+ }
219
+ openSuggestions(event) {
220
+ const suggestions = this.suggestions;
221
+ if (!suggestions.length) {
222
+ this.fetch();
223
+ }
224
+ if (suggestions.isVisible()) {
225
+ return false;
226
+ }
227
+ if ((suggestions.length > 1) || (!this.input.value.length && suggestions.length)) {
228
+ event.preventDefault();
229
+ suggestions.show();
230
+ }
231
+ return true;
232
+ }
226
233
  select() {
227
234
  if (DEBUG)
228
235
  console.log('select()');
@@ -322,6 +329,7 @@ class Suggestions {
322
329
  console.log('removeList()');
323
330
  if (!this.list)
324
331
  return;
332
+ this.length = 0;
325
333
  this.list.remove();
326
334
  this.list = undefined;
327
335
  }
package/package.json CHANGED
@@ -40,5 +40,5 @@
40
40
  },
41
41
  "type": "module",
42
42
  "types": "./autocomplete.d.ts",
43
- "version": "0.1.4"
43
+ "version": "0.1.5"
44
44
  }