-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCollapsibleCheckboxes.jsx
More file actions
237 lines (217 loc) · 8.66 KB
/
CollapsibleCheckboxes.jsx
File metadata and controls
237 lines (217 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import React, { useState, useRef } from "react";
import FilterEnableCheckbox from "./FilterEnableCheckbox";
import Tooltip from "./ToolTip";
/**
* Used by the SidebarView for filtering things with checkboxes.
* @param {*} props
* @returns
*/
const CollapsibleCheckboxes = (props) => {
const [expanded, setExpanded] = useState([]);
const [filterEnabled, setFilterEnabled] = useState(props.filterEnable);
const [checkedSubItems, setCheckedSubItems] = useState({});
const [initalLoad, setInitalLoad] = useState(true);
const strokeWidth = 5;
let paramFieldType = "checkboxhierarchy";
const checkboxRef = useRef(null);
const rows = props.fields;
const toggleExpand = (id, subItems) => {
let entry = rows.find(item => item.id === id);
if (!entry?.subItems || (entry?.subItems.length == 1 && !entry?.subItems[0])) {
props.HandleFilterChange([paramFieldType, props.filterName,
entry?.label
]);
}else if (entry && entry?.label && entry?.subItems) {
subItems?.map((_, index) => {
if ((checkedSubItems[`${id}-${index}`] && expanded[id]) || (!expanded[id])) {
props.HandleFilterChange([paramFieldType, props.filterName,
entry?.label + "/" + entry?.subItems[index]
]);
}
setSubCheckbox(id, index);
});
}
setExpanded((prev) => ({
...prev,
[id]: !prev[id],
}));
};
const setSubCheckbox = (mainId, index) => {
const key = `${mainId}-${index}`;
setCheckedSubItems((prev) => ({
...prev,
[key]: ((expanded[mainId] && prev[key]) || !expanded[mainId]) ? !prev[key]:false,
}));
};
const toggleSubCheckbox = (mainId, index) => {
const key = `${mainId}-${index}`;
props.HandleFilterChange([paramFieldType, props.filterName,
rows.find(item => item.id === mainId)?.label + "/" + rows.find(item => item.id === mainId)?.subItems[index]
]);
setCheckedSubItems((prev) => ({
...prev,
[key]: !prev[key],
}));
};
React.useEffect(() => {
if (rows && rows.length > 0 && initalLoad) {
let tempExpanded = {};
let tempChecked = {};
rows.forEach(r => r.subItems.forEach((_, index) => {
tempChecked[`${r.id}-${index}`] = false;
}));
props?.initialValues?.forEach(i => {
let mainRow = rows.find((item) => item.label === i.label);
if (mainRow) {
tempExpanded[mainRow.id] = true;
i.subItems.forEach(s => {
let subItemIndex = mainRow.subItems.findIndex(item => item === s);
if (subItemIndex !== -1) {
tempChecked[`${mainRow.id}-${subItemIndex}`] = true;
}
});
}
});
setCheckedSubItems(tempChecked);
setInitalLoad(false);
setExpanded(tempExpanded);
}
}, [rows, initalLoad, props?.initialValues]);
return (
<div className="m-2">
<div className="mb-2 text-white flex items-center justify-between">
<div className="flex-none items-center">
<h3>{String(props.filterName).charAt(0).toUpperCase() + String(props.filterName).slice(1)}</h3>
</div>
<div className="flex-auto pl-3 pt-2">
<Tooltip
text={props.description}
position={"right"}
/>
</div>
<FilterEnableCheckbox
ref={checkboxRef}
initialValue={filterEnabled}
onToggle={() => { setFilterEnabled(!filterEnabled); props.HandleFilterEnable([props.filterName, !filterEnabled]); }}
/>
</div>
<div className={`${filterEnabled ? "opacity-100" : "opacity-50"}`} onClick={() => {
if (!filterEnabled && checkboxRef.current) {
checkboxRef.current.click();
}
}}>
<div className="rounded-lg shadow-2xs w-full text-white bg-[#aba8e0] border border-gray-200 p-4">
{rows.map((row) => (
<div key={row.id} className="relative pl-4 ml-2">
<div className="flex items-center gap-2 mb-1 relative">
<input
type="checkbox"
id={`checkbox-${row?.id}`}
checked={expanded[row?.id] || false}
onChange={() => toggleExpand(row?.id, row?.subItems)}
className="accent-violet-500 z-10"
/>
<label htmlFor={`checkbox-${row.id}`} className="cursor-pointer font-semibold">
{row.label}
</label>
</div>
<svg
width="40"
height={`${expanded[row.id] ? (((!row?.subItems || row?.subItems?.length > 1) ? row.subItems.length : 0)) * 24 + 34 : 30}`}
viewBox={`0 0 40 ${expanded[row.id] ? (((!row?.subItems || row?.subItems?.length > 1) ? row.subItems.length : 0)) * 24 + 34 : 30}`}
preserveAspectRatio="none"
className="absolute left-[-25px] top-[-5px]"
>
{/*big horizontal line */}
<path
d={`M20 0 V${((!row?.subItems || row?.subItems?.length > 1) ? row.subItems.length : 0) * 24 + 33}`}
stroke="white"
strokeWidth={strokeWidth}
fill="none"
className=""
/>
{/*top vertical line */}
{row.id === 1 && (
<path
d={`M20 2 H33`}
stroke="white"
strokeWidth={strokeWidth}
fill="none"
className=""
/>
)}
{/*bottom vertical line */}
{row.id === rows.length && (
<path
d={`M20 ${(expanded[row.id] ? row.subItems.length : 1) * 34 - 8} H33`}
stroke="white"
strokeWidth={strokeWidth}
fill="none"
className=""
/>
)}
</svg>
{expanded[row.id] && row.subItems.length > 1 && (
<div className="mt-2 relative ">
{/*vertical line */}
<svg
width="40"
height={`${(row.subItems.length) * 24}`}
viewBox={`0 0 40 ${(row.subItems.length) * 24}`}
preserveAspectRatio="none"
className="absolute left-[-14px] top-[-10px] transition-all duration-300 ease-in-out stroke-animate"
>
<path
d={`M20 0 V${(row.subItems.length) * 32}`}
stroke="white"
strokeWidth={strokeWidth}
fill="none"
className="transition-all duration-300 ease-in-out stroke-animate"
/>
</svg>
{row.subItems.map((subItem, index) => {
const checkboxId = `sub-checkbox-${row.id}-${index}`;
const key = `${row.id}-${index}`;
return (
<div key={checkboxId} className="relative pl-6 flex items-center">
{/* SVG line only if the checkbox is checked */}
{checkedSubItems[key] && (
<svg
width="40"
height="40"
viewBox="0 0 40 50"
preserveAspectRatio="none"
className="absolute left-[-14px] top-1 transition-all duration-300 ease-in-out stroke-animate"
>
<path
d="M20 10 H33"
stroke="white"
strokeWidth={strokeWidth}
fill="none"
className="transition-all duration-300 ease-in-out stroke-animate"
/>
</svg>
)}
<input
type="checkbox"
id={checkboxId}
className="accent-violet-500"
checked={!!checkedSubItems[key]}
onChange={() => toggleSubCheckbox(row.id, index)}
/>
<label htmlFor={checkboxId} className="cursor-pointer ml-2 truncate max-w-xs">
{subItem}
</label>
</div>
);
})}
</div>
)}
</div>
))}
</div>
</div>
</div>
);
};
export default CollapsibleCheckboxes;