@modernman00/shared-js-lib 1.2.61 โ†’ 1.2.63

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.
@@ -1,3 +1,3 @@
1
1
  <?php
2
- define('APP_VERSION', 'v1.2.61');
2
+ define('APP_VERSION', 'v1.2.63');
3
3
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modernman00/shared-js-lib",
3
- "version": "1.2.61",
3
+ "version": "1.2.63",
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",
@@ -41,3 +41,15 @@
41
41
  - ๐Ÿงน Auto-commit before release _(by modernman00)_
42
42
  - ๐Ÿงน Auto-commit before release _(by modernman00)_
43
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)_
50
+
51
+ ## v1.2.63 โ€” 2026-01-27 10:04
52
+ ### Changelog for v1.2.63
53
+
54
+ - ๐Ÿ”– Bump version to v1.2.63 _(by modernman00)_
55
+ - ๐Ÿงน 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
+