-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathButtonGroupField.jsx
More file actions
61 lines (55 loc) · 2.06 KB
/
ButtonGroupField.jsx
File metadata and controls
61 lines (55 loc) · 2.06 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
import { useEffect, useState } from "react";
import ToolTip from './ToolTip';
/**
* The ButtonGroupField is used as a selector for the period.
* Used by the SidebarView.
* @param {*} props
* @returns
*/
export default function ButtonGroupField(props) {
const [activeIndex, setActiveIndex] = useState(0);
useEffect(() => {
const normalizedInitialValue = props.initialValue.toLowerCase();
const normalizedItems = props.items.map(item => item.toLowerCase());
setActiveIndex(normalizedItems.findIndex(item => item === normalizedInitialValue));
}, [props.initialValue, props.items]);
const handleClick = (index) => {
setActiveIndex(index);
props.HandleFilterChange(["buttongroup","eligibility",props.items[index]]);
};
const getButtonClasses = (index) => {
const baseClasses = `flex-auto py-1 px-4 inline-flex items-center gap-x-2 text-sm
font-medium focus:z-10 border border-gray-200 shadow-2xs hover:bg-[#8785ac]
focus:outline-hidden disabled:opacity-50 disabled:pointer-events-none`;
const activeClass = activeIndex === index ? "bg-violet-500" : "bg-transparent";
const roundedClasses =
index === 0
? "rounded-l-lg"
: index === props.items.length - 1
? "rounded-r-lg"
: "border-l-0";
return `${baseClasses} ${activeClass} ${roundedClasses}`;
};
return (
<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}
<ToolTip
text={`select this option to filter courses using the ${item} filtering option`}
position={index == props.items.length - 1 ? "left" : "right"}
hiddenIcon={true}
/>
</button>
))}
</div>
</div>
);
}