@itrocks/autocomplete 0.0.2 → 0.0.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
@@ -9,6 +9,10 @@
9
9
  padding: 0;
10
10
  position: absolute;
11
11
  }
12
+ .combobox .suggestions:empty {
13
+ height: 2em;
14
+ width: 50px;
15
+ }
12
16
  .combobox .suggestions > * {
13
17
  cursor: pointer;
14
18
  list-style: none;
package/autocomplete.d.ts CHANGED
@@ -26,6 +26,7 @@ export declare class AutoComplete {
26
26
  }
27
27
  declare class Suggestions {
28
28
  combo: AutoComplete;
29
+ length: number;
29
30
  list?: HTMLUListElement;
30
31
  constructor(combo: AutoComplete);
31
32
  createList(): HTMLUListElement;
package/autocomplete.js CHANGED
@@ -83,12 +83,15 @@ export class AutoComplete {
83
83
  }
84
84
  keyDown(event) {
85
85
  const suggestions = this.suggestions;
86
- if (suggestions.isLastSelected()) {
86
+ if (!suggestions.isVisible()) {
87
+ if (suggestions.length > 1) {
88
+ event.preventDefault();
89
+ suggestions.show();
90
+ }
87
91
  return;
88
92
  }
89
93
  event.preventDefault();
90
- if (!suggestions.isVisible()) {
91
- suggestions.show();
94
+ if (suggestions.isLastSelected()) {
92
95
  return;
93
96
  }
94
97
  this.suggest(suggestions.selectNext()?.caption);
@@ -177,6 +180,7 @@ export class AutoComplete {
177
180
  }
178
181
  class Suggestions {
179
182
  combo;
183
+ length = 0;
180
184
  list;
181
185
  constructor(combo) {
182
186
  this.combo = combo;
@@ -256,13 +260,11 @@ class Suggestions {
256
260
  return this.createList();
257
261
  }
258
262
  update(suggestions) {
259
- if (!suggestions.length) {
260
- return this.hide();
261
- }
262
263
  let hasSelected = false;
263
- const list = this.show();
264
+ const list = this.list ?? this.createList();
264
265
  const selected = list.querySelector('li.selected')?.innerText;
265
266
  list.innerHTML = '';
267
+ this.length = suggestions.length;
266
268
  for (const suggestion of suggestions) {
267
269
  const item = document.createElement('li');
268
270
  item.dataset.id = '' + suggestion.id;
@@ -276,5 +278,13 @@ class Suggestions {
276
278
  if (!hasSelected) {
277
279
  list.firstElementChild?.classList.add('selected');
278
280
  }
281
+ if (this.length > 1) {
282
+ if (!this.isVisible())
283
+ this.show();
284
+ }
285
+ else {
286
+ if (this.isVisible())
287
+ this.hide();
288
+ }
279
289
  }
280
290
  }
package/package.json CHANGED
@@ -40,5 +40,5 @@
40
40
  },
41
41
  "type": "module",
42
42
  "types": "./autocomplete.d.ts",
43
- "version": "0.0.2"
43
+ "version": "0.0.3"
44
44
  }