@itrocks/autocomplete 0.0.1
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/LICENSE +14 -0
- package/README.md +9 -0
- package/autocomplete.d.ts +32 -0
- package/autocomplete.js +119 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
Copyright (C) 2024, Baptiste Pillot <baptiste@pillot.fr>
|
|
3
|
+
|
|
4
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
6
|
+
copyright notice and this permission notice appear in all copies.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
9
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
10
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
11
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
12
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
13
|
+
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
14
|
+
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
[](https://www.npmjs.org/package/@itrocks/autocomplete)
|
|
2
|
+
[](https://www.npmjs.org/package/@itrocks/autocomplete)
|
|
3
|
+
[](https://github.com/itrocks-ts/autocomplete)
|
|
4
|
+
[](https://github.com/itrocks-ts/autocomplete/issues)
|
|
5
|
+
[](https://25.re/ditr)
|
|
6
|
+
|
|
7
|
+
# autocomplete
|
|
8
|
+
|
|
9
|
+
Editable combobox component featuring smart autocomplete from a list of id-caption pairs for it.rocks.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
interface HTMLAutoCompleteInputElement extends HTMLInputElement {
|
|
2
|
+
lastValue: string;
|
|
3
|
+
selectedValue?: string;
|
|
4
|
+
}
|
|
5
|
+
export declare class AutoComplete {
|
|
6
|
+
idInput?: HTMLInputElement;
|
|
7
|
+
input: HTMLAutoCompleteInputElement;
|
|
8
|
+
suggestions: Suggestions;
|
|
9
|
+
constructor(input: HTMLInputElement);
|
|
10
|
+
autoIdInput(): void;
|
|
11
|
+
emptyClass(): void;
|
|
12
|
+
fetch(): void;
|
|
13
|
+
onBlur(_event: FocusEvent): void;
|
|
14
|
+
onKeyDown(event: KeyboardEvent): void;
|
|
15
|
+
onInput(_event: Event): void;
|
|
16
|
+
}
|
|
17
|
+
declare class Suggestions {
|
|
18
|
+
combo: AutoComplete;
|
|
19
|
+
list: HTMLUListElement;
|
|
20
|
+
constructor(combo: AutoComplete);
|
|
21
|
+
first(): {
|
|
22
|
+
caption: string;
|
|
23
|
+
id: number;
|
|
24
|
+
};
|
|
25
|
+
hide(): void;
|
|
26
|
+
show(): void;
|
|
27
|
+
update(suggestions: {
|
|
28
|
+
caption: string;
|
|
29
|
+
id: number;
|
|
30
|
+
}[]): void;
|
|
31
|
+
}
|
|
32
|
+
export {};
|
package/autocomplete.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
export class AutoComplete {
|
|
2
|
+
idInput;
|
|
3
|
+
input;
|
|
4
|
+
suggestions;
|
|
5
|
+
constructor(input) {
|
|
6
|
+
this.input = Object.assign(input, { lastValue: input.value });
|
|
7
|
+
this.suggestions = new Suggestions(this);
|
|
8
|
+
this.autoIdInput();
|
|
9
|
+
input.addEventListener('blur', event => this.onBlur(event));
|
|
10
|
+
input.addEventListener('keydown', event => this.onKeyDown(event));
|
|
11
|
+
input.addEventListener('input', event => this.onInput(event));
|
|
12
|
+
}
|
|
13
|
+
autoIdInput() {
|
|
14
|
+
const input = this.input;
|
|
15
|
+
const next = input.nextElementSibling;
|
|
16
|
+
const prev = input.previousElementSibling;
|
|
17
|
+
this.idInput
|
|
18
|
+
= ((next instanceof HTMLInputElement) && (next.type === 'hidden')) ? next
|
|
19
|
+
: ((prev instanceof HTMLInputElement) && (prev.type === 'hidden')) ? prev
|
|
20
|
+
: undefined;
|
|
21
|
+
}
|
|
22
|
+
emptyClass() {
|
|
23
|
+
const input = this.input;
|
|
24
|
+
const classList = input.classList;
|
|
25
|
+
if (!input.value.length) {
|
|
26
|
+
classList.add('empty');
|
|
27
|
+
delete input.selectedValue;
|
|
28
|
+
if (this.idInput)
|
|
29
|
+
this.idInput.value = '';
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (classList.contains('empty')) {
|
|
33
|
+
(classList.length > 1)
|
|
34
|
+
? classList.remove('empty')
|
|
35
|
+
: input.removeAttribute('class');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
fetch() {
|
|
39
|
+
const input = this.input;
|
|
40
|
+
const dataFetch = input.dataset.fetch ?? input.closest('[data-fetch]')?.dataset.fetch;
|
|
41
|
+
const requestInit = { headers: { Accept: 'application/json' } };
|
|
42
|
+
const summaryRoute = dataFetch + '?startsWith=' + input.value;
|
|
43
|
+
fetch(summaryRoute, requestInit).then(response => response.text()).then(json => {
|
|
44
|
+
const summary = JSON.parse(json);
|
|
45
|
+
const startsWith = input.value.toLowerCase();
|
|
46
|
+
const suggestions = summary.map(([id, caption]) => ({ caption, id: +id }))
|
|
47
|
+
.filter(item => item.caption.toLowerCase().startsWith(startsWith));
|
|
48
|
+
this.suggestions.update(suggestions);
|
|
49
|
+
const suggestion = this.suggestions.first();
|
|
50
|
+
if (!suggestion) {
|
|
51
|
+
delete input.selectedValue;
|
|
52
|
+
if (this.idInput)
|
|
53
|
+
this.idInput.value = '';
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const caption = suggestion.caption;
|
|
57
|
+
input.selectedValue = caption;
|
|
58
|
+
if (this.idInput)
|
|
59
|
+
this.idInput.value = '' + suggestion.id;
|
|
60
|
+
const position = input.value.length;
|
|
61
|
+
input.setRangeText(caption.slice(position));
|
|
62
|
+
input.setSelectionRange(position, input.value.length);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
onBlur(_event) {
|
|
66
|
+
const input = this.input;
|
|
67
|
+
if (!input.selectedValue)
|
|
68
|
+
return;
|
|
69
|
+
input.value = input.selectedValue;
|
|
70
|
+
}
|
|
71
|
+
onKeyDown(event) {
|
|
72
|
+
switch (event.key) {
|
|
73
|
+
case 'ArrowDown':
|
|
74
|
+
case 'Down':
|
|
75
|
+
return this.suggestions.show();
|
|
76
|
+
case 'ArrowUp':
|
|
77
|
+
case 'Up':
|
|
78
|
+
return this.suggestions.hide();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
onInput(_event) {
|
|
82
|
+
if (this.input.lastValue === this.input.value) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
this.fetch();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
class Suggestions {
|
|
89
|
+
combo;
|
|
90
|
+
list;
|
|
91
|
+
constructor(combo) {
|
|
92
|
+
this.combo = combo;
|
|
93
|
+
const list = this.list = document.createElement('ul');
|
|
94
|
+
list.classList.add('suggestions');
|
|
95
|
+
list.style.display = 'none';
|
|
96
|
+
combo.input.insertAdjacentElement('afterend', list);
|
|
97
|
+
}
|
|
98
|
+
first() {
|
|
99
|
+
const item = this.list.firstElementChild;
|
|
100
|
+
return { caption: item.innerText, id: +(item.dataset.id ?? 0) };
|
|
101
|
+
}
|
|
102
|
+
hide() {
|
|
103
|
+
this.list.style.display = 'none';
|
|
104
|
+
}
|
|
105
|
+
show() {
|
|
106
|
+
this.list.style.removeProperty('display');
|
|
107
|
+
}
|
|
108
|
+
update(suggestions) {
|
|
109
|
+
const list = this.list;
|
|
110
|
+
list.innerHTML = '';
|
|
111
|
+
for (const suggestion of suggestions) {
|
|
112
|
+
const item = document.createElement('li');
|
|
113
|
+
item.dataset.id = '' + suggestion.id;
|
|
114
|
+
item.innerText = suggestion.caption;
|
|
115
|
+
list.appendChild(item);
|
|
116
|
+
}
|
|
117
|
+
list.firstElementChild?.classList.add('selected');
|
|
118
|
+
}
|
|
119
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": {
|
|
3
|
+
"name": "Baptiste Pillot",
|
|
4
|
+
"email": "baptiste@pillot.fr"
|
|
5
|
+
},
|
|
6
|
+
"description": "Editable combobox component featuring smart autocomplete from a list of id-caption pairs for it.rocks",
|
|
7
|
+
"devDependencies": {
|
|
8
|
+
"@types/node": "^22.9",
|
|
9
|
+
"typescript": "5.6"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"LICENSE",
|
|
13
|
+
"README.md",
|
|
14
|
+
"*.d.ts",
|
|
15
|
+
"*.js"
|
|
16
|
+
],
|
|
17
|
+
"homepage": "https://it.rocks",
|
|
18
|
+
"keywords": [
|
|
19
|
+
"auto",
|
|
20
|
+
"autocomplete",
|
|
21
|
+
"box",
|
|
22
|
+
"combo",
|
|
23
|
+
"combobox",
|
|
24
|
+
"complete",
|
|
25
|
+
"front-end",
|
|
26
|
+
"input",
|
|
27
|
+
"it.rocks"
|
|
28
|
+
],
|
|
29
|
+
"license": "ISC",
|
|
30
|
+
"module": "./autocomplete.js",
|
|
31
|
+
"name": "@itrocks/autocomplete",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/itrocks-ts/autocomplete.git"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsc"
|
|
38
|
+
},
|
|
39
|
+
"type": "module",
|
|
40
|
+
"types": "./autocomplete.d.ts",
|
|
41
|
+
"version": "0.0.1"
|
|
42
|
+
}
|