-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMultipleChoiceButtons.jsx
More file actions
96 lines (85 loc) · 3.77 KB
/
MultipleChoiceButtons.jsx
File metadata and controls
96 lines (85 loc) · 3.77 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
import { useState } from "react";
import { useRef, useEffect } from "react";
import FilterEnableCheckbox from "./FilterEnableCheckbox";
import Tooltip from "./ToolTip";
export default function MultipleChoiceButtons(props) {
const [filterEnabled, setFilterEnabled] = useState(props.filterEnable);
const [selectedItems, setSelectedItems] = useState(props.initialValues || []);
const checkboxRef = useRef(null);
useEffect(() => {
setFilterEnabled(props.filterEnable);
},[props.filterEnable])
const handleClick = (index) => {
setSelectedItems((prev) => {
if (!Array.isArray(prev) || index < 0 || index >= prev.length) {
console.warn("Invalid selectedItems or index:", prev, index);
return prev;
}
const updated = [...prev];
updated[index] = !updated[index];
props.HandleFilterChange(["buttongroup", "period", index, updated[index]]);
return updated;
});
};
const getButtonClasses = (index) => {
if (!selectedItems || !Array.isArray(selectedItems)) {
return "default-button-class"; // Fallback class if selectedItems is invalid
}
if (index < 0 || index >= selectedItems.length) {
return "default-button-class"; // Fallback class for invalid index
}
const baseClasses = `flex-auto py-1 px-4 inline-flex items-center text-sm
font-medium focus:z-10 border border-gray-200 shadow-2xs hover:bg-[#8785ac]
focus:outline-hidden sm:pl-7 pl-4 text-center`;
const activeClass = selectedItems[index] ? "bg-violet-500" : "bg-transparent";
const roundedClasses =
index === 0
? "rounded-l-lg"
: index === props.items.length - 1
? "rounded-r-lg"
: "border-l-0 ";
const mobileClasses = "w-full sm:w-auto"; // Adjust for mobile
return `${baseClasses} ${activeClass} ${roundedClasses} ${mobileClasses}`;
};
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="my-1">
<div className="flex sm:inline-flex rounded-lg shadow-2xs
w-full items-center font-medium text-white bg-[#aba8e0]">
{props.items.map((item, index) => (
<button
key={index}
type="button"
className={getButtonClasses(index)}
onClick={() => handleClick(index)}
>
{item}
</button>
))}
</div>
</div>
</div>
</div>
);
}