@modernman00/shared-js-lib 1.2.60 → 1.2.62
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/config/version.php +1 -1
- package/package.json +2 -1
- package/release-history.md +16 -0
- package/theAutoComplete.js +93 -0
package/config/version.php
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@modernman00/shared-js-lib",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.62",
|
|
4
4
|
"description": "Reusable JS utilities for numerous js problems",
|
|
5
5
|
"homepage": "https://github.com/modernman00/shared-js-lib#readme",
|
|
6
6
|
"keywords": [
|
|
@@ -44,6 +44,7 @@
|
|
|
44
44
|
"Utility.js",
|
|
45
45
|
"UtilityHtml.js",
|
|
46
46
|
"axiosWrapper.js",
|
|
47
|
+
"theAutoComplete.js",
|
|
47
48
|
"general.js",
|
|
48
49
|
"config/",
|
|
49
50
|
"README.md",
|
package/release-history.md
CHANGED
|
@@ -31,3 +31,19 @@
|
|
|
31
31
|
- 🧹 Auto-commit before release _(by modernman00)_
|
|
32
32
|
- 🧹 Auto-commit before release _(by modernman00)_
|
|
33
33
|
- 🧹 Auto-commit before release _(by modernman00)_
|
|
34
|
+
|
|
35
|
+
## v1.2.61 — 2026-01-27 09:25
|
|
36
|
+
### Changelog for v1.2.61
|
|
37
|
+
|
|
38
|
+
- 🔖 Bump version to v1.2.61 _(by modernman00)_
|
|
39
|
+
- 🧹 Auto-commit before release _(by modernman00)_
|
|
40
|
+
- 🧹 Auto-commit before release _(by modernman00)_
|
|
41
|
+
- 🧹 Auto-commit before release _(by modernman00)_
|
|
42
|
+
- 🧹 Auto-commit before release _(by modernman00)_
|
|
43
|
+
- 🧹 Auto-commit before release _(by modernman00)_
|
|
44
|
+
|
|
45
|
+
## v1.2.62 — 2026-01-27 09:57
|
|
46
|
+
### Changelog for v1.2.62
|
|
47
|
+
|
|
48
|
+
- 🔖 Bump version to v1.2.62 _(by modernman00)_
|
|
49
|
+
- 🧹 Auto-commit before release _(by modernman00)_
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { id } from './UtilityHtml.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param {string} inputId
|
|
6
|
+
* @param {Array} arr
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Sets up an autocomplete functionality for an input element.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} inputId - The ID of the input element to attach the autocomplete to.
|
|
13
|
+
* @param {Array<string>} arr - An array of strings containing the autocomplete suggestions.
|
|
14
|
+
*
|
|
15
|
+
* The function converts all strings in the array to lowercase for case-insensitive matching.
|
|
16
|
+
* It initializes the autocomplete with the given input element and array of suggestions,
|
|
17
|
+
* providing real-time suggestions based on user input. On selecting a suggestion,
|
|
18
|
+
* it populates the input element with the selected suggestion. very good
|
|
19
|
+
*/
|
|
20
|
+
export const autocomplete = (inputId, arr) => {
|
|
21
|
+
const whatToBuyInput = id(inputId); // Get the text input
|
|
22
|
+
if (whatToBuyInput) {
|
|
23
|
+
// Create a <ul> for autocomplete suggestions
|
|
24
|
+
const suggestionList = document.createElement("ul");
|
|
25
|
+
suggestionList.classList.add("autocomplete-suggestions");
|
|
26
|
+
suggestionList.id = "suggestions"; // For accessibility
|
|
27
|
+
whatToBuyInput.parentElement.appendChild(suggestionList); // Append to input's parent
|
|
28
|
+
|
|
29
|
+
// Function to show matching suggestions based on user input
|
|
30
|
+
const showSuggestions = (inputValue) => {
|
|
31
|
+
suggestionList.innerHTML = ""; // Clear previous suggestions
|
|
32
|
+
if (!inputValue) return; // Exit if input is empty
|
|
33
|
+
|
|
34
|
+
// Filter items that match the input (case-insensitive), limit to 8
|
|
35
|
+
const matches = arr
|
|
36
|
+
.filter(item => item.toLowerCase().includes(inputValue.toLowerCase()))
|
|
37
|
+
.slice(0, 8);
|
|
38
|
+
|
|
39
|
+
// Create <li> for each match
|
|
40
|
+
matches.forEach((item, index) => {
|
|
41
|
+
const li = document.createElement("li");
|
|
42
|
+
li.textContent = item;
|
|
43
|
+
li.setAttribute("tabindex", "0"); // Make focusable for keyboard
|
|
44
|
+
li.setAttribute("data-index", index); // Store index for navigation
|
|
45
|
+
li.addEventListener("click", () => {
|
|
46
|
+
whatToBuyInput.value = item; // Set input value on click
|
|
47
|
+
suggestionList.innerHTML = ""; // Clear suggestions
|
|
48
|
+
});
|
|
49
|
+
li.addEventListener("keypress", (e) => {
|
|
50
|
+
if (e.key === "Enter") {
|
|
51
|
+
whatToBuyInput.value = item; // Set input value on Enter
|
|
52
|
+
suggestionList.innerHTML = ""; // Clear suggestions
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
suggestionList.appendChild(li);
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// Show suggestions as user types
|
|
60
|
+
whatToBuyInput.addEventListener("input", (e) => {
|
|
61
|
+
showSuggestions(e.target.value);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Clear suggestions on blur (with delay for click to register)
|
|
65
|
+
whatToBuyInput.addEventListener("blur", () => {
|
|
66
|
+
setTimeout(() => suggestionList.innerHTML = "", 200);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Handle keyboard navigation for suggestions
|
|
70
|
+
whatToBuyInput.addEventListener("keydown", (e) => {
|
|
71
|
+
const suggestions = suggestionList.querySelectorAll("li");
|
|
72
|
+
if (!suggestions.length) return; // Exit if no suggestions
|
|
73
|
+
|
|
74
|
+
let focusedIndex = Array.from(suggestions).findIndex(li => li === document.activeElement);
|
|
75
|
+
if (e.key === "ArrowDown") {
|
|
76
|
+
e.preventDefault(); // Prevent cursor movement
|
|
77
|
+
focusedIndex = (focusedIndex + 1) % suggestions.length; // Loop to start
|
|
78
|
+
suggestions[focusedIndex].focus();
|
|
79
|
+
} else if (e.key === "ArrowUp") {
|
|
80
|
+
e.preventDefault();
|
|
81
|
+
focusedIndex = (focusedIndex - 1 + suggestions.length) % suggestions.length; // Loop to end
|
|
82
|
+
suggestions[focusedIndex].focus();
|
|
83
|
+
} else if (e.key === "Enter" && focusedIndex >= 0) {
|
|
84
|
+
e.preventDefault();
|
|
85
|
+
whatToBuyInput.value = suggestions[focusedIndex].textContent; // Select item
|
|
86
|
+
suggestionList.innerHTML = ""; // Clear suggestions
|
|
87
|
+
} else if (e.key === "Escape") {
|
|
88
|
+
suggestionList.innerHTML = ""; // Clear suggestions
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|