@itrocks/autocomplete 0.1.9 → 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 +2 -1
- package/autocomplete.js +25 -9
- 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>);
|
|
@@ -34,7 +35,7 @@ export declare class AutoComplete {
|
|
|
34
35
|
onInputValueChange(): void;
|
|
35
36
|
onKeyDown(event: KeyboardEvent): void;
|
|
36
37
|
onTouchStart(event: Event): void;
|
|
37
|
-
openSuggestions(event: Event): boolean;
|
|
38
|
+
openSuggestions(event: Event, force?: boolean): boolean;
|
|
38
39
|
select(): void;
|
|
39
40
|
suggest(value?: string): void;
|
|
40
41
|
updateSuggestions(items: Item[], startsWith?: string): void;
|
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;
|
|
@@ -166,7 +171,7 @@ export class AutoComplete {
|
|
|
166
171
|
keyDown(event) {
|
|
167
172
|
if (DEBUG)
|
|
168
173
|
console.log('keyDown()');
|
|
169
|
-
if (this.openSuggestions(event))
|
|
174
|
+
if (this.openSuggestions(event, true))
|
|
170
175
|
return;
|
|
171
176
|
if (this.suggestions.isLastSelected())
|
|
172
177
|
return;
|
|
@@ -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
|
{
|
|
@@ -277,7 +292,7 @@ export class AutoComplete {
|
|
|
277
292
|
onTouchStart(event) {
|
|
278
293
|
this.openSuggestions(event);
|
|
279
294
|
}
|
|
280
|
-
openSuggestions(event) {
|
|
295
|
+
openSuggestions(event, force = false) {
|
|
281
296
|
if (DEBUG)
|
|
282
297
|
console.log('openSuggestions()');
|
|
283
298
|
const suggestions = this.suggestions;
|
|
@@ -291,7 +306,7 @@ export class AutoComplete {
|
|
|
291
306
|
console.log('OS: isVisible is true => return');
|
|
292
307
|
return false;
|
|
293
308
|
}
|
|
294
|
-
if ((suggestions.length > 1) || (!this.input.value.length && suggestions.length)) {
|
|
309
|
+
if (force || (suggestions.length > 1) || (!this.input.value.length && suggestions.length)) {
|
|
295
310
|
if (DEBUG)
|
|
296
311
|
console.log('OS: has items => show');
|
|
297
312
|
event.preventDefault();
|
|
@@ -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