Skip to content

Commit a37885f

Browse files
Merge pull request #122 from DiamondLightSource/80-update-widgets-to-use-mui-base
80 Update widgets to use MUI base
2 parents 9be55c2 + 639e18e commit a37885f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1959
-1051
lines changed

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export * from "./redux";
22
export * from "./misc";
33
export * from "./connection";
4-
export * from "./ui/components";
54
export * from "./ui/widgets";
65
export * from "./ui/hooks";
76
export * from "./types";

src/phoebusTheme.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { createTheme } from "@mui/material/styles";
2+
3+
export const phoebusTheme = createTheme({
4+
palette: {
5+
primary: {
6+
main: "#D2D2D2",
7+
// light: currently calculated automatically by MUI
8+
// dark: currently calculated automatically by MUI
9+
contrastText: "#000000"
10+
}
11+
},
12+
typography: {
13+
fontFamily: "Liberation Sans",
14+
fontSize: 14,
15+
button: {
16+
textTransform: "none"
17+
}
18+
}
19+
});

src/types/border.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ describe("Border", () => {
77
expect(border.css()).toEqual({
88
borderStyle: "solid",
99
borderWidth: "1px",
10-
borderColor: "rgba(255,0,0,255)"
10+
borderColor: "rgba(255,0,0,1)"
1111
});
1212
});
1313
it("sets border radius if defined", (): void => {
1414
const border = new Border(BorderStyle.Line, Color.RED, 1, 2);
1515
expect(border.css()).toEqual({
1616
borderStyle: "solid",
1717
borderWidth: "1px",
18-
borderColor: "rgba(255,0,0,255)",
18+
borderColor: "rgba(255,0,0,1)",
1919
borderRadius: "2px"
2020
});
2121
});

src/types/color.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class Color {
2121
public static CHANGING = new Color("var(--changing)");
2222
public static UNDEFINED = new Color("var(--undefined)");
2323

24-
public static fromRgba(r: number, g: number, b: number, a = 255): Color {
24+
public static fromRgba(r: number, g: number, b: number, a = 1): Color {
2525
if (r < 0 || r > 255) {
2626
throw new Error(`r value ${r} out of range`);
2727
} else if (g < 0 || g > 255) {

src/ui/components/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/ui/components/input/input.tsx

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/ui/widgets/ActionButton/__snapshots__/actionButton.test.tsx.snap

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,34 @@
22

33
exports[`<ActionButton /> > it matches the snapshot 1`] = `
44
<button
5-
className="_actionbutton_b413d3"
5+
className="MuiButtonBase-root MuiButton-root MuiButton-contained MuiButton-containedPrimary MuiButton-sizeMedium MuiButton-containedSizeMedium MuiButton-colorPrimary MuiButton-fullWidth MuiButton-root MuiButton-contained MuiButton-containedPrimary MuiButton-sizeMedium MuiButton-containedSizeMedium MuiButton-colorPrimary MuiButton-fullWidth css-fcc9ak-MuiButtonBase-root-MuiButton-root"
6+
disabled={false}
7+
onBlur={[Function]}
68
onClick={[MockFunction spy]}
7-
style={
8-
{
9-
"backgroundColor": undefined,
10-
"color": undefined,
11-
"cursor": undefined,
12-
"visibility": undefined,
13-
"whiteSpace": "normal",
14-
}
15-
}
9+
onContextMenu={[Function]}
10+
onDragLeave={[Function]}
11+
onFocus={[Function]}
12+
onKeyDown={[Function]}
13+
onKeyUp={[Function]}
14+
onMouseDown={[Function]}
15+
onMouseLeave={[Function]}
16+
onMouseUp={[Function]}
17+
onTouchEnd={[Function]}
18+
onTouchMove={[Function]}
19+
onTouchStart={[Function]}
20+
tabIndex={0}
21+
type="button"
1622
>
1723
<span
1824
style={
1925
{
20-
"whiteSpace": "normal",
26+
"alignItems": "center",
27+
"display": "flex",
28+
"height": "100%",
29+
"justifyContent": "center",
30+
"whiteSpace": "pre-wrap",
31+
"width": "100%",
32+
"wordBreak": "break-word",
2133
}
2234
}
2335
>

src/ui/widgets/ActionButton/actionButton.test.tsx

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,85 @@
11
import React from "react";
22
import { ActionButtonComponent } from "./actionButton";
3-
import { create } from "react-test-renderer";
3+
import renderer from "react-test-renderer";
44
import { fireEvent, render, waitFor } from "@testing-library/react";
55
import { vi } from "vitest";
6+
import { contextRender } from "../../../testResources";
7+
import { ThemeProvider } from "@mui/material";
8+
import { phoebusTheme } from "../../../phoebusTheme";
9+
import { Color } from "../../../types";
610

11+
// Pass the theme in with the component to access default values
712
const mock = vi.fn();
8-
const actionButton = <ActionButtonComponent text={"hello"} onClick={mock} />;
13+
const actionButton = (props: any) => (
14+
<ThemeProvider theme={phoebusTheme}>
15+
<ActionButtonComponent text={"hello"} onClick={mock} {...props} />
16+
</ThemeProvider>
17+
);
918

1019
describe("<ActionButton />", (): void => {
1120
test("it matches the snapshot", (): void => {
12-
const snapshot = create(actionButton);
21+
const snapshot = renderer.create(actionButton({}));
1322
expect(snapshot.toJSON()).toMatchSnapshot();
1423
});
1524

16-
test("it renders a button", (): void => {
17-
const { getByRole } = render(actionButton);
18-
const button = getByRole("button") as HTMLButtonElement;
25+
test("it renders a button with default style from theme", (): void => {
26+
const { getByRole } = contextRender(actionButton({}));
27+
const button = getByRole("button");
28+
expect(button).toHaveStyle({
29+
"background-color": "rgb(210, 210, 210)",
30+
color: "rgb(0, 0, 0)",
31+
width: "100px",
32+
height: "30px"
33+
});
34+
// For some reason, background colour doesn't like to be passed as an object so pass as string
1935
expect(button.textContent).toEqual("hello");
2036
});
2137

38+
test("it renders a button with style from props", (): void => {
39+
const { getByRole } = render(
40+
actionButton({
41+
foregroundColor: Color.fromRgba(155, 160, 209),
42+
backgroundColor: Color.fromRgba(10, 200, 1),
43+
height: 40,
44+
width: 60
45+
})
46+
);
47+
const button = getByRole("button");
48+
expect(button).toHaveStyle({
49+
"background-color": "rgb(10, 200, 1)",
50+
color: "rgb(155, 160, 209)",
51+
width: "60px",
52+
height: "40px"
53+
});
54+
// For some reason, background colour doesn't like to be passed as an object so pass as string
55+
});
56+
57+
test("button variation changes if transparent", (): void => {
58+
const { getByRole } = render(actionButton({ transparent: true }));
59+
const button = getByRole("button");
60+
expect(button).toHaveStyle("background-color: rgba(0, 0, 0, 0)");
61+
});
62+
63+
test("button is rotated correctly when rotationStep is set", async () => {
64+
const { getByRole } = render(
65+
actionButton({ rotationStep: 1, height: 40, width: 60 })
66+
);
67+
const button = await getByRole("button");
68+
expect(button).toHaveStyle({
69+
transform: "rotate(-90deg) translateY(10px) translateX(10px)",
70+
height: "60px",
71+
width: "40px"
72+
});
73+
});
74+
75+
test("whether button is disabled", async () => {
76+
const { getByRole } = render(actionButton({ enabled: false }));
77+
const button = await getByRole("button");
78+
expect(button).toBeDisabled();
79+
});
80+
2281
test("function called on click", async (): Promise<void> => {
23-
const { getByRole } = render(actionButton);
82+
const { getByRole } = render(actionButton({}));
2483
const button = getByRole("button") as HTMLButtonElement;
2584
fireEvent.click(button);
2685
await waitFor(() => expect(mock).toHaveBeenCalled());

0 commit comments

Comments
 (0)