@jack-henry/jh-elements 2.0.0-beta.11 → 2.0.0-beta.13

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.
@@ -0,0 +1,92 @@
1
+ /**
2
+ * SPDX-FileCopyrightText: 2026 Jack Henry
3
+ *
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ // TODO for tagged multi-select we will need to filter out already selected items from the list of options.
8
+
9
+ // returns a filtered list of terms that include the search term in the specified key (default is 'label')
10
+ export const JhFilter = {
11
+ filterList(items, searchTerm, key = 'label') {
12
+ if (!items || items.length === 0) {
13
+ return [];
14
+ }
15
+ if (!searchTerm) {
16
+ return items;
17
+ }
18
+ const lowerSearchTerm = searchTerm.toLowerCase();
19
+ return items.filter((item) =>
20
+ String(item[key] || '')
21
+ .toLowerCase()
22
+ .includes(lowerSearchTerm),
23
+ );
24
+ },
25
+
26
+ // returns the first term that includes the search term in the specified key (default is 'label'), or null if not found.
27
+ // Skip disabled items in the inline suggestions as they are not selectable.
28
+ filterInline(items, searchTerm, key = 'label') {
29
+ if (!searchTerm || !items || items.length === 0) {
30
+ return null;
31
+ }
32
+ const lowerSearchTerm = searchTerm.toLowerCase();
33
+ return (
34
+ items.find(
35
+ (item) =>
36
+ !item.disabled &&
37
+ String(item[key] || '')
38
+ .toLowerCase()
39
+ .startsWith(lowerSearchTerm),
40
+ ) || null
41
+ );
42
+ },
43
+ //returns the index of the first term that starts with the buffer, or -1 if not found
44
+ // exclude disabled items from the search as they are not selectable.
45
+ // when it is a multi-select, return selected items as well, so users can deselect easily.
46
+
47
+ jumpAhead(items, buffer, activeIndex, key = 'label') {
48
+ if (!buffer || !items || items.length === 0) {
49
+ return -1;
50
+ }
51
+ // more than one character that are same.
52
+ const allCharsSame = (str) => {
53
+ if (str.length <= 1) return false;
54
+ return str.split('').every((char) => char === str[0]);
55
+ };
56
+
57
+ const safeActiveIndex = activeIndex ?? -1;
58
+ const lowerBuffer = buffer.toLowerCase();
59
+ const char = lowerBuffer[0];
60
+ const isCycling = allCharsSame(lowerBuffer);
61
+
62
+ if (isCycling) {
63
+ // Find the next item that starts with the character after the active index
64
+ const nextIndex = items.findIndex(
65
+ (item, index) =>
66
+ index > safeActiveIndex &&
67
+ !item.disabled &&
68
+ String(item[key] || '')
69
+ .toLowerCase()
70
+ .startsWith(char),
71
+ );
72
+ if (nextIndex !== -1) {
73
+ return nextIndex;
74
+ }
75
+ // If not found, wrap around and search from the beginning up to the active index
76
+ return items.findIndex(
77
+ (item) =>
78
+ !item.disabled &&
79
+ String(item[key] || '')
80
+ .toLowerCase()
81
+ .startsWith(char),
82
+ );
83
+ }
84
+ return items.findIndex(
85
+ (item) =>
86
+ !item.disabled &&
87
+ String(item[key] || '')
88
+ .toLowerCase()
89
+ .startsWith(lowerBuffer),
90
+ );
91
+ },
92
+ };