@itrocks/autocomplete 0.1.3 → 0.1.4

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
@@ -1,30 +1,26 @@
1
- .combobox input[data-type=object] {
2
- display: block;
3
- }
4
- .combobox .suggestions {
1
+ .autocomplete + .suggestions {
5
2
  background: white;
6
3
  border: 1px solid #ddd;
7
4
  margin-top: -1px;
8
5
  padding: 0;
9
6
  position: absolute;
10
- top: 100%;
11
7
  z-index: 1;
12
8
  }
13
- .combobox .suggestions:empty {
9
+ .autocomplete + .suggestions:empty {
14
10
  height: 2em;
15
11
  width: 50px;
16
12
  }
17
- .combobox .suggestions > * {
13
+ .autocomplete + .suggestions > * {
18
14
  cursor: pointer;
19
15
  list-style: none;
20
16
  padding: 0.5em 1em;
21
17
  }
22
- .combobox .suggestions > *:hover {
18
+ .autocomplete + .suggestions > *:hover {
23
19
  background: #efe;
24
20
  }
25
- .combobox .suggestions > *.selected {
21
+ .autocomplete + .suggestions > *.selected {
26
22
  background: #edf;
27
23
  }
28
- .combobox .suggestions > *.selected:hover {
24
+ .autocomplete + .suggestions > *.selected:hover {
29
25
  background: #dce;
30
26
  }
package/autocomplete.d.ts CHANGED
@@ -15,6 +15,7 @@ export declare class AutoComplete {
15
15
  fetch(): void;
16
16
  initIdInput(): HTMLInputElement | undefined;
17
17
  initInput(input: HTMLInputElement): HTMLInputElement;
18
+ initParent(): void;
18
19
  keyDown(event: Event): void;
19
20
  keyEnter(event: Event): void;
20
21
  keyEscape(event: Event): void;
package/autocomplete.js CHANGED
@@ -11,6 +11,7 @@ export class AutoComplete {
11
11
  this.input = this.initInput(input);
12
12
  this.idInput = this.initIdInput();
13
13
  this.suggestions = new Suggestions(this);
14
+ this.initParent();
14
15
  input.addEventListener('blur', event => this.onBlur(event));
15
16
  input.addEventListener('keydown', event => this.onKeyDown(event));
16
17
  input.addEventListener('input', event => this.onInput(event));
@@ -112,6 +113,12 @@ export class AutoComplete {
112
113
  input.dataset.lastValue = input.value;
113
114
  return input;
114
115
  }
116
+ initParent() {
117
+ const parent = this.input.parentElement;
118
+ if (!parent)
119
+ return;
120
+ parent.style.position = 'relative';
121
+ }
115
122
  keyDown(event) {
116
123
  if (DEBUG)
117
124
  console.log('keyDown()');
@@ -217,6 +224,8 @@ export class AutoComplete {
217
224
  }
218
225
  }
219
226
  select() {
227
+ if (DEBUG)
228
+ console.log('select()');
220
229
  const suggestions = this.suggestions;
221
230
  const suggestion = suggestions.selected();
222
231
  if (!suggestion)
@@ -254,29 +263,22 @@ class Suggestions {
254
263
  }
255
264
  createList() {
256
265
  if (DEBUG)
257
- console.log('Suggestions.createList()');
266
+ console.log('createList()');
258
267
  const list = this.list = document.createElement('ul');
259
268
  list.classList.add('suggestions');
260
- let input = this.combo.input;
261
- const idInput = input.nextElementSibling;
262
- if ((idInput instanceof HTMLInputElement) && (idInput.type === 'hidden')) {
263
- input = idInput;
264
- }
265
- input.insertAdjacentElement('afterend', list);
266
- if (DEBUG)
267
- console.log('Suggestions.prepareClic');
269
+ this.combo.input.insertAdjacentElement('afterend', list);
268
270
  list.addEventListener('pointerdown', event => this.onPointerDown(event));
269
271
  return list;
270
272
  }
271
273
  first() {
272
274
  if (DEBUG)
273
- console.log('Suggestions.first()');
275
+ console.log('first()');
274
276
  const item = this.list?.firstElementChild ?? null;
275
277
  return item && { caption: item.innerText, id: +(item.dataset.id ?? 0) };
276
278
  }
277
279
  hide() {
278
280
  if (DEBUG)
279
- console.log('Suggestions.hide()');
281
+ console.log('hide()');
280
282
  const list = this.list;
281
283
  if (!list)
282
284
  return;
@@ -297,7 +299,7 @@ class Suggestions {
297
299
  }
298
300
  onPointerDown(event) {
299
301
  if (DEBUG)
300
- console.log('Suggestions.onPointerDown()', event.button);
302
+ console.log('onPointerDown()', event.button);
301
303
  if (event.button !== 0)
302
304
  return;
303
305
  if (!(event.target instanceof Element))
@@ -317,7 +319,7 @@ class Suggestions {
317
319
  }
318
320
  removeList() {
319
321
  if (DEBUG)
320
- console.log('Suggestions.removeList()');
322
+ console.log('removeList()');
321
323
  if (!this.list)
322
324
  return;
323
325
  this.list.remove();
@@ -326,7 +328,7 @@ class Suggestions {
326
328
  selected(item = null) {
327
329
  item ??= this.list?.querySelector('li.selected') ?? null;
328
330
  if (DEBUG)
329
- console.log('Suggestions.selected()', item && { caption: item.innerText, id: +(item.dataset.id ?? 0) });
331
+ console.log('selected()', item && { caption: item.innerText, id: +(item.dataset.id ?? 0) });
330
332
  return item && { caption: item.innerText, id: +(item.dataset.id ?? 0) };
331
333
  }
332
334
  selectFirst() {
@@ -369,11 +371,16 @@ class Suggestions {
369
371
  console.log('show()');
370
372
  if (this.list) {
371
373
  this.list.style.removeProperty('display');
374
+ if (!this.list.getAttribute('style')?.length) {
375
+ this.list.removeAttribute('style');
376
+ }
372
377
  return this.list;
373
378
  }
374
379
  return this.createList();
375
380
  }
376
381
  unselect(item = this.list?.querySelector('li.selected')) {
382
+ if (DEBUG)
383
+ console.log('unselect()');
377
384
  if (!item)
378
385
  return;
379
386
  const classList = item.classList;
package/package.json CHANGED
@@ -40,5 +40,5 @@
40
40
  },
41
41
  "type": "module",
42
42
  "types": "./autocomplete.d.ts",
43
- "version": "0.1.3"
43
+ "version": "0.1.4"
44
44
  }