diff --git a/src/ui/widgets/ChoiceButton/__snapshots__/choiceButton.test.tsx.snap b/src/ui/widgets/ChoiceButton/__snapshots__/choiceButton.test.tsx.snap new file mode 100644 index 00000000..978f3403 --- /dev/null +++ b/src/ui/widgets/ChoiceButton/__snapshots__/choiceButton.test.tsx.snap @@ -0,0 +1,79 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[` > it renders ChoiceButton with default props 1`] = ` + +
+ + +
+
+`; + +exports[` > pass props to widget 1`] = ` + +
+ + + + +
+
+`; diff --git a/src/ui/widgets/ChoiceButton/choiceButton.module.css b/src/ui/widgets/ChoiceButton/choiceButton.module.css deleted file mode 100644 index 8d166cf4..00000000 --- a/src/ui/widgets/ChoiceButton/choiceButton.module.css +++ /dev/null @@ -1,16 +0,0 @@ -.ChoiceButton { - align-items: center; - justify-content: center; - text-overflow: ellipsis; - border-radius: 3px; - margin-right: 1px; - margin-bottom: 1px; - border: 1px solid gray; - word-wrap: break-word; - overflow: hidden; - display: inline-block; -} - -.ChoiceButton:hover { - background-image: linear-gradient(rgb(255 255 255 0.4) 0 0); -} \ No newline at end of file diff --git a/src/ui/widgets/ChoiceButton/choiceButton.test.tsx b/src/ui/widgets/ChoiceButton/choiceButton.test.tsx index ed3e634c..745a67a5 100644 --- a/src/ui/widgets/ChoiceButton/choiceButton.test.tsx +++ b/src/ui/widgets/ChoiceButton/choiceButton.test.tsx @@ -13,15 +13,14 @@ describe("", (): void => { const choiceButtonProps = { value: null }; - const { getAllByRole } = render(ChoiceButtonRenderer(choiceButtonProps)); + const { getAllByRole, asFragment } = render( + ChoiceButtonRenderer(choiceButtonProps) + ); const buttons = getAllByRole("button") as Array; expect(buttons[0].textContent).toEqual("Item 1"); expect(buttons[1].textContent).toEqual("Item 2"); - expect(buttons[0].style.height).toEqual("43px"); - expect(buttons[1].style.width).toEqual("46px"); - expect(buttons[0].style.backgroundColor).toEqual("rgb(210, 210, 210)"); - expect(buttons[1].style.fontSize).toEqual("0.875rem"); + expect(asFragment()).toMatchSnapshot(); }); test("pass props to widget", (): void => { @@ -36,19 +35,15 @@ describe("", (): void => { itemsFromPv: false, enabled: false }; - const { getAllByRole } = render(ChoiceButtonRenderer(choiceButtonProps)); + const { getAllByRole, asFragment } = render( + ChoiceButtonRenderer(choiceButtonProps) + ); const buttons = getAllByRole("button") as Array; expect(buttons.length).toEqual(4); - // First button is selected therefore different color and box shadow - expect(buttons[0].style.boxShadow).toEqual( - "inset 0px 23px 35px 0px rgba(0,0,0,0.3)" - ); - expect(buttons[0].style.backgroundColor).toEqual("rgb(10, 60, 40)"); expect(buttons[2].textContent).toEqual("Setting"); - expect(buttons[3].style.cursor).toEqual("not-allowed"); - expect(buttons[3].style.height).toEqual("31px"); - expect(buttons[3].style.backgroundColor).toEqual("rgb(20, 20, 200)"); + expect(buttons[3]).toHaveProperty("disabled", true); + expect(asFragment()).toMatchSnapshot(); }); test("pass props to widget, using itemsFromPv", (): void => { diff --git a/src/ui/widgets/ChoiceButton/choiceButton.tsx b/src/ui/widgets/ChoiceButton/choiceButton.tsx index ebbc0400..5b61941b 100644 --- a/src/ui/widgets/ChoiceButton/choiceButton.tsx +++ b/src/ui/widgets/ChoiceButton/choiceButton.tsx @@ -1,4 +1,4 @@ -import React, { CSSProperties, useEffect, useState } from "react"; +import React, { useEffect, useState } from "react"; import { Widget } from "../widget"; import { PVComponent, PVWidgetPropType } from "../widgetProps"; @@ -13,10 +13,14 @@ import { StringPropOpt } from "../propTypes"; import { DType } from "../../../types/dtypes"; -import classes from "./choiceButton.module.css"; -import { Color } from "../../../types/color"; import { writePv } from "../../hooks/useSubscription"; -import { Font } from "../../../types/font"; +import { + ToggleButton as MuiToggleButton, + styled, + ToggleButtonGroup +} from "@mui/material"; +import { diamondTheme } from "../../../diamondTheme"; +import { Color } from "../../../types"; const ChoiceButtonProps = { pvName: StringPropOpt, @@ -38,6 +42,28 @@ export type ChoiceButtonComponentProps = InferWidgetProps< > & PVComponent; +const ToggleButton = styled(MuiToggleButton)({ + "&.MuiToggleButton-root": { + textTransform: "none", + overflow: "hidden", + display: "block", + padding: 0, + lineHeight: 1, + alignItems: "center", + justifyContent: "center", + textOverflow: "ellipsis", + wordWrap: "break-word" + }, + "&.Mui-disabled": { + cursor: "not-allowed", + pointerEvents: "all !important" + }, + "&.MuiToggleButtonGroup-grouped": { + border: "1px solid !important", + borderColor: "#000000" + } +}); + export const ChoiceButtonComponent = ( props: ChoiceButtonComponentProps ): JSX.Element => { @@ -50,11 +76,11 @@ export const ChoiceButtonComponent = ( pvName, items = ["Item 1", "Item 2"], horizontal = true, - backgroundColor = Color.fromRgba(210, 210, 210), - foregroundColor = Color.BLACK, - selectedColor = Color.fromRgba(200, 200, 200), - font = new Font(14) + foregroundColor = diamondTheme.palette.primary.contrastText, + backgroundColor = diamondTheme.palette.primary.main, + selectedColor = Color.fromRgba(200, 200, 200) } = props; + const font = props.font?.css() ?? diamondTheme.typography; const [selected, setSelected] = useState(value?.getDoubleValue()); // Use items from file, unless itemsFRomPv set @@ -74,61 +100,63 @@ export const ChoiceButtonComponent = ( // Number of buttons to create const numButtons = options.length || 1; // Determine width and height of buttons if horizontal or vertically placed - const buttonHeight = horizontal ? height : height / numButtons - 4; - const buttonWidth = horizontal ? width / numButtons - 4 : width; - - const style: CSSProperties = { - height: buttonHeight, - width: buttonWidth, - textAlignLast: "center", - cursor: enabled ? "default" : "not-allowed", - color: foregroundColor?.toString(), - ...font.css() - }; + const buttonHeight = horizontal ? height : height / numButtons; + const buttonWidth = horizontal ? width / numButtons : width; - function handleClick(index: number) { + const handleChange = ( + event: React.MouseEvent, + newSelect: number + ) => { // Write to PV if (pvName) { - writePv(pvName, new DType({ doubleValue: index })); + writePv(pvName, new DType({ doubleValue: newSelect })); } - } - - // Iterate over items to create buttons - const elements: Array = []; - options.forEach((item: string | null | undefined, idx: number) => { - if (typeof item === "string") { - elements.push( - - ); - } - }); + setSelected(newSelect); + }; return ( -
- {elements} -
+ {options + .filter(item => typeof item === "string") + .map((item, idx) => ( + + {item} + + ))} + ); };