@itrocks/autocomplete 0.1.1 → 0.1.3

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
@@ -7,6 +7,7 @@
7
7
  margin-top: -1px;
8
8
  padding: 0;
9
9
  position: absolute;
10
+ top: 100%;
10
11
  z-index: 1;
11
12
  }
12
13
  .combobox .suggestions:empty {
package/autocomplete.d.ts CHANGED
@@ -23,6 +23,7 @@ export declare class AutoComplete {
23
23
  onInput(event: Event): void;
24
24
  onInputValueChange(): void;
25
25
  onKeyDown(event: KeyboardEvent): void;
26
+ select(): void;
26
27
  suggest(value?: string): void;
27
28
  }
28
29
  declare class Suggestions {
@@ -36,7 +37,7 @@ declare class Suggestions {
36
37
  isFirstSelected(): boolean | null | undefined;
37
38
  isLastSelected(): boolean | null | undefined;
38
39
  isVisible(): boolean | undefined;
39
- onClick(event: MouseEvent): void;
40
+ onPointerDown(event: MouseEvent): void;
40
41
  removeList(): void;
41
42
  selected(item?: HTMLLIElement | null): Item | null;
42
43
  selectFirst(): void;
package/autocomplete.js CHANGED
@@ -136,20 +136,10 @@ export class AutoComplete {
136
136
  if (DEBUG)
137
137
  console.log('keyEnter()');
138
138
  const suggestions = this.suggestions;
139
- if (!suggestions.isVisible()) {
139
+ if (!suggestions.isVisible())
140
140
  return;
141
- }
142
141
  event.preventDefault();
143
- const suggestion = suggestions.selected();
144
- if (!suggestion) {
145
- return;
146
- }
147
- if (DEBUG)
148
- console.log(' input =', suggestion.caption);
149
- this.input.value = suggestion.caption;
150
- this.onInputValueChange();
151
- this.autoIdInputValue();
152
- suggestions.hide();
142
+ this.select();
153
143
  }
154
144
  keyEscape(event) {
155
145
  if (DEBUG)
@@ -226,6 +216,18 @@ export class AutoComplete {
226
216
  return this.keyEnter(event);
227
217
  }
228
218
  }
219
+ select() {
220
+ const suggestions = this.suggestions;
221
+ const suggestion = suggestions.selected();
222
+ if (!suggestion)
223
+ return;
224
+ if (DEBUG)
225
+ console.log(' input =', suggestion.caption);
226
+ this.input.value = suggestion.caption;
227
+ this.onInputValueChange();
228
+ this.autoIdInputValue();
229
+ suggestions.hide();
230
+ }
229
231
  suggest(value) {
230
232
  if (DEBUG)
231
233
  console.log('suggest()', value);
@@ -263,7 +265,7 @@ class Suggestions {
263
265
  input.insertAdjacentElement('afterend', list);
264
266
  if (DEBUG)
265
267
  console.log('Suggestions.prepareClic');
266
- list.addEventListener('click', event => this.onClick(event));
268
+ list.addEventListener('pointerdown', event => this.onPointerDown(event));
267
269
  return list;
268
270
  }
269
271
  first() {
@@ -293,9 +295,9 @@ class Suggestions {
293
295
  isVisible() {
294
296
  return this.list && (this.list.style.display !== 'none');
295
297
  }
296
- onClick(event) {
298
+ onPointerDown(event) {
297
299
  if (DEBUG)
298
- console.log('Suggestions.onClick()', event.button);
300
+ console.log('Suggestions.onPointerDown()', event.button);
299
301
  if (event.button !== 0)
300
302
  return;
301
303
  if (!(event.target instanceof Element))
@@ -306,24 +308,20 @@ class Suggestions {
306
308
  console.log(' item', item, 'list', list);
307
309
  if (!item || !list)
308
310
  return;
311
+ if (DEBUG)
312
+ console.log(' select', item);
309
313
  this.unselect();
310
314
  item.classList.add('selected');
311
- if (DEBUG)
312
- console.log(' selected', item);
313
- this.combo.keyEnter(event);
315
+ this.combo.select();
316
+ event.preventDefault();
314
317
  }
315
318
  removeList() {
316
319
  if (DEBUG)
317
320
  console.log('Suggestions.removeList()');
318
- setTimeout(() => this.hide(), 100);
319
- setTimeout(() => {
320
- if (DEBUG)
321
- console.log(' list.remove()');
322
- if (!this.list || (this.list.style.display !== 'none'))
323
- return;
324
- this.list.remove();
325
- this.list = undefined;
326
- }, 200);
321
+ if (!this.list)
322
+ return;
323
+ this.list.remove();
324
+ this.list = undefined;
327
325
  }
328
326
  selected(item = null) {
329
327
  item ??= this.list?.querySelector('li.selected') ?? null;
package/package.json CHANGED
@@ -40,5 +40,5 @@
40
40
  },
41
41
  "type": "module",
42
42
  "types": "./autocomplete.d.ts",
43
- "version": "0.1.1"
43
+ "version": "0.1.3"
44
44
  }