@itrocks/autocomplete 0.1.10 → 0.1.11
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 +1 -0
- package/autocomplete.js +22 -6
- package/package.json +1 -1
package/autocomplete.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export declare class AutoComplete {
|
|
|
13
13
|
lastItems: Item[];
|
|
14
14
|
lastKey: string;
|
|
15
15
|
lastStart: string;
|
|
16
|
+
lastValue: string;
|
|
16
17
|
options: AutoCompleteOptions;
|
|
17
18
|
suggestions: Suggestions;
|
|
18
19
|
constructor(input: HTMLInputElement, options?: Partial<AutoCompleteOptions>);
|
package/autocomplete.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const DEBUG =
|
|
1
|
+
const DEBUG = true;
|
|
2
2
|
const defaultOptions = {
|
|
3
3
|
allowNew: false,
|
|
4
4
|
fetchAttribute: 'data-fetch'
|
|
@@ -10,6 +10,7 @@ export class AutoComplete {
|
|
|
10
10
|
lastItems = [];
|
|
11
11
|
lastKey = '';
|
|
12
12
|
lastStart = '';
|
|
13
|
+
lastValue = '';
|
|
13
14
|
options;
|
|
14
15
|
suggestions;
|
|
15
16
|
constructor(input, options = {}) {
|
|
@@ -149,9 +150,13 @@ export class AutoComplete {
|
|
|
149
150
|
const input = this.input;
|
|
150
151
|
const next = input.nextElementSibling;
|
|
151
152
|
const prev = input.previousElementSibling;
|
|
152
|
-
|
|
153
|
+
const idInput = ((next instanceof HTMLInputElement) && (next.type === 'hidden')) ? next
|
|
153
154
|
: ((prev instanceof HTMLInputElement) && (prev.type === 'hidden')) ? prev
|
|
154
155
|
: undefined;
|
|
156
|
+
if (!idInput)
|
|
157
|
+
return undefined;
|
|
158
|
+
idInput.dataset.lastValue = idInput.value;
|
|
159
|
+
return idInput;
|
|
155
160
|
}
|
|
156
161
|
initInput(input) {
|
|
157
162
|
input.dataset.lastValue = input.value;
|
|
@@ -185,9 +190,19 @@ export class AutoComplete {
|
|
|
185
190
|
keyEscape(event) {
|
|
186
191
|
if (DEBUG)
|
|
187
192
|
console.log('keyEscape');
|
|
193
|
+
const input = this.input;
|
|
188
194
|
const suggestions = this.suggestions;
|
|
189
|
-
if ((
|
|
190
|
-
|
|
195
|
+
if ((input.value === '') && input.dataset.lastValue) {
|
|
196
|
+
input.value = input.dataset.lastValue;
|
|
197
|
+
const idInput = this.idInput;
|
|
198
|
+
if (idInput?.dataset.lastValue) {
|
|
199
|
+
idInput.value = idInput.dataset.lastValue;
|
|
200
|
+
}
|
|
201
|
+
input.setSelectionRange(0, input.value.length);
|
|
202
|
+
this.onInputValueChange();
|
|
203
|
+
if (!suggestions.isVisible()) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
191
206
|
}
|
|
192
207
|
event.preventDefault();
|
|
193
208
|
if (suggestions.isVisible()) {
|
|
@@ -239,14 +254,14 @@ export class AutoComplete {
|
|
|
239
254
|
console.log('onInput()');
|
|
240
255
|
if (document.activeElement !== event.target)
|
|
241
256
|
return;
|
|
242
|
-
if (this.
|
|
257
|
+
if (this.lastValue === this.input.value)
|
|
243
258
|
return;
|
|
244
259
|
this.fetch(this.lastKey);
|
|
245
260
|
}
|
|
246
261
|
onInputValueChange() {
|
|
247
262
|
if (DEBUG)
|
|
248
263
|
console.log('onInputValueChange()');
|
|
249
|
-
this.
|
|
264
|
+
this.lastValue = this.input.value;
|
|
250
265
|
this.input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
251
266
|
if (document.activeElement !== this.input) {
|
|
252
267
|
{
|
|
@@ -500,6 +515,7 @@ class Suggestions {
|
|
|
500
515
|
if (!this.list.getAttribute('style')?.length) {
|
|
501
516
|
this.list.removeAttribute('style');
|
|
502
517
|
}
|
|
518
|
+
this.list.querySelector('li.selected')?.scrollIntoView({ block: 'nearest' });
|
|
503
519
|
return this.list;
|
|
504
520
|
}
|
|
505
521
|
return this.createList();
|
package/package.json
CHANGED