Skip to content

Commit 478b92c

Browse files
committed
Replaced base react button group with MUI ToggleButtonGroup
1 parent 6e57a14 commit 478b92c

File tree

1 file changed

+53
-54
lines changed

1 file changed

+53
-54
lines changed

src/ui/widgets/ChoiceButton/choiceButton.tsx

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { CSSProperties, useEffect, useState } from "react";
1+
import React, { useEffect, useState } from "react";
22

33
import { Widget } from "../widget";
44
import { PVComponent, PVWidgetPropType } from "../widgetProps";
@@ -13,10 +13,12 @@ import {
1313
StringPropOpt
1414
} from "../propTypes";
1515
import { DType } from "../../../types/dtypes";
16-
import classes from "./choiceButton.module.css";
17-
import { Color } from "../../../types/color";
16+
// import classes from "./choiceButton.module.css";
1817
import { writePv } from "../../hooks/useSubscription";
1918
import { Font } from "../../../types/font";
19+
import { ToggleButton, ToggleButtonGroup, ThemeProvider } from "@mui/material";
20+
import { defaultColours } from "../../../colourscheme";
21+
import { Color } from "../../../types";
2022

2123
const ChoiceButtonProps = {
2224
pvName: StringPropOpt,
@@ -50,8 +52,6 @@ export const ChoiceButtonComponent = (
5052
pvName,
5153
items = ["Item 1", "Item 2"],
5254
horizontal = true,
53-
backgroundColor = Color.fromRgba(210, 210, 210),
54-
foregroundColor = Color.BLACK,
5555
selectedColor = Color.fromRgba(200, 200, 200),
5656
font = new Font(14)
5757
} = props;
@@ -74,61 +74,60 @@ export const ChoiceButtonComponent = (
7474
// Number of buttons to create
7575
const numButtons = options.length || 1;
7676
// Determine width and height of buttons if horizontal or vertically placed
77-
const buttonHeight = horizontal ? height : height / numButtons - 4;
78-
const buttonWidth = horizontal ? width / numButtons - 4 : width;
77+
const buttonHeight = horizontal ? height : height / numButtons;
78+
const buttonWidth = horizontal ? width / numButtons : width;
7979

80-
const style: CSSProperties = {
81-
height: buttonHeight,
82-
width: buttonWidth,
83-
textAlignLast: "center",
84-
cursor: enabled ? "default" : "not-allowed",
85-
color: foregroundColor?.toString(),
86-
...font.css()
87-
};
88-
89-
function handleClick(index: number) {
80+
const handleChange = (
81+
event: React.MouseEvent<HTMLElement>,
82+
newSelect: number
83+
) => {
9084
// Write to PV
9185
if (pvName) {
92-
writePv(pvName, new DType({ doubleValue: index }));
86+
writePv(pvName, new DType({ doubleValue: newSelect }));
9387
}
94-
}
95-
96-
// Iterate over items to create buttons
97-
const elements: Array<JSX.Element> = [];
98-
options.forEach((item: string | null | undefined, idx: number) => {
99-
if (typeof item === "string") {
100-
elements.push(
101-
<button
102-
className={classes.ChoiceButton}
103-
disabled={enabled ? false : true}
104-
onClick={() => handleClick(idx)}
105-
style={{
106-
...style,
107-
backgroundColor:
108-
selected === idx
109-
? selectedColor.toString()
110-
: backgroundColor.toString(),
111-
boxShadow:
112-
selected === idx
113-
? `inset 0px ${Math.round(height / 6)}px ${Math.round(
114-
height / 4
115-
)}px 0px rgba(0,0,0,0.3)`
116-
: "none"
117-
}}
118-
key={item}
119-
>
120-
{item}
121-
</button>
122-
);
123-
}
124-
});
88+
setSelected(newSelect);
89+
};
12590

12691
return (
127-
<div
128-
style={{ display: "flex", flexDirection: horizontal ? "row" : "column" }}
129-
>
130-
{elements}
131-
</div>
92+
<ThemeProvider theme={defaultColours}>
93+
<ToggleButtonGroup
94+
exclusive
95+
fullWidth={true}
96+
disabled={enabled ? false : true}
97+
value={selected}
98+
onChange={handleChange}
99+
orientation={horizontal ? "horizontal" : "vertical"}
100+
sx={{
101+
height: height,
102+
width: width
103+
}}
104+
>
105+
{options
106+
.filter(item => typeof item === "string")
107+
.map((item, idx) => (
108+
<ToggleButton
109+
key={item}
110+
value={idx}
111+
sx={{
112+
minWidth: buttonWidth,
113+
minHeight: buttonHeight,
114+
fontFamily: font.css(),
115+
color:
116+
props.foregroundColor?.toString() ??
117+
defaultColours.palette.primary.contrastText,
118+
backgroundColor:
119+
props.backgroundColor?.toString() ??
120+
defaultColours.palette.primary.main,
121+
"&.Mui-selected, &.Mui-selected:hover, &:hover": {
122+
backgroundColor: selectedColor.toString()
123+
}
124+
}}
125+
>
126+
{item}
127+
</ToggleButton>
128+
))}
129+
</ToggleButtonGroup>
130+
</ThemeProvider>
132131
);
133132
};
134133

0 commit comments

Comments
 (0)