Skip to content

Commit 24a9d97

Browse files
committed
90 replace label with mui typography (#93)
* Replaced div with MUI Typography * Added styling props to Typography * Updated the tests for label * Updated symbols test * Removed ThemeProvider and tidied styling parameters * Updated tokens and fixed rotation * Updated snapshots * Removed redundant css file * Moved static styling to styled API * Updated tests and snapshots * Set display to flex * Updated snapshots * Updated snapshot
1 parent dd0112f commit 24a9d97

File tree

6 files changed

+74
-79
lines changed

6 files changed

+74
-79
lines changed
Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`<Label /> > it handles transparent prop`] = `
4+
<DocumentFragment>
5+
<p
6+
class="MuiTypography-root MuiTypography-body1 MuiTypography-noWrap css-k3pvii-MuiTypography-root"
7+
>
8+
hello
9+
</p>
10+
</DocumentFragment>
11+
`;
12+
13+
exports[`<Label /> > it handles transparent prop 1`] = `
14+
<DocumentFragment>
15+
<p
16+
class="MuiTypography-root MuiTypography-body1 css-1fe740m-MuiTypography-root"
17+
>
18+
hello
19+
</p>
20+
</DocumentFragment>
21+
`;
22+
323
exports[`<Label /> > it matches the snapshot 1`] = `
424
<DocumentFragment>
5-
<div
6-
class="Label _Label_c35983"
7-
style="background-color: transparent; cursor: default; justify-content: flex-start; align-items: flex-start;"
25+
<p
26+
class="MuiTypography-root MuiTypography-body1 css-1fe740m-MuiTypography-root"
827
>
9-
<span>
10-
hello
11-
</span>
12-
</div>
28+
hello
29+
</p>
1330
</DocumentFragment>
1431
`;

src/ui/widgets/Label/label.module.css

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

src/ui/widgets/Label/label.test.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,9 @@ describe("<Label />", (): void => {
1414
});
1515

1616
test("it handles transparent prop", (): void => {
17-
render(<LabelComponent text="hello" transparent={true} />);
18-
const label = screen.getByText("hello");
19-
// The text is in the span, not the parent div.
20-
if (label.parentElement && "style" in label.parentElement) {
21-
expect(label.parentElement.style).toHaveProperty(
22-
"backgroundColor",
23-
"transparent"
24-
);
25-
}
26-
expect.assertions(1);
17+
const { asFragment } = render(
18+
<LabelComponent text="hello" transparent={true} />
19+
);
20+
expect(asFragment()).toMatchSnapshot();
2721
});
2822
});

src/ui/widgets/Label/label.tsx

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import React, { CSSProperties } from "react";
1+
import React from "react";
22

3-
import classes from "./label.module.css";
4-
import { Widget, commonCss } from "../widget";
3+
import { Widget } from "../widget";
54
import { WidgetPropType } from "../widgetProps";
65
import { registerWidget } from "../register";
76
import {
@@ -15,6 +14,8 @@ import {
1514
FloatPropOpt,
1615
MacrosPropOpt
1716
} from "../propTypes";
17+
import { Typography as MuiTypography, styled } from "@mui/material";
18+
import { diamondTheme } from "../../../diamondTheme";
1819

1920
const LabelProps = {
2021
macros: MacrosPropOpt,
@@ -28,7 +29,7 @@ const LabelProps = {
2829
foregroundColor: ColorPropOpt,
2930
backgroundColor: ColorPropOpt,
3031
border: BorderPropOpt,
31-
rotationAngle: FloatPropOpt,
32+
rotationStep: FloatPropOpt,
3233
wrapWords: BoolPropOpt
3334
};
3435

@@ -37,65 +38,66 @@ const LabelWidgetProps = {
3738
...WidgetPropType
3839
};
3940

41+
const Typography = styled(MuiTypography)({
42+
width: "100%",
43+
height: "100%",
44+
display: "flex",
45+
overflow: "hidden",
46+
padding: 0
47+
});
48+
4049
export const LabelComponent = (
4150
props: InferWidgetProps<typeof LabelProps>
4251
): JSX.Element => {
4352
// Default labels to transparent.
44-
const editedProps = {
45-
...props,
46-
transparent: props.transparent ?? true
47-
};
48-
const style: CSSProperties = commonCss(editedProps);
4953
const {
54+
transparent = true,
55+
foregroundColor = diamondTheme.palette.primary.contrastText,
5056
textAlign = "left",
5157
textAlignV = "top",
5258
text = "",
53-
rotationAngle,
54-
wrapWords
59+
rotationStep = 0,
60+
wrapWords = true
5561
} = props;
56-
const className = props.className ?? `Label ${classes.Label}`;
62+
const backgroundColor = transparent
63+
? "transparent"
64+
: (props.backgroundColor?.toString() ?? diamondTheme.palette.primary.main);
65+
const font = props.font?.css() ?? diamondTheme.typography;
66+
5767
// Since display is "flex", use "flex-start" and "flex-end" to align
5868
// the content.
5969
let alignment = "center";
6070
if (textAlign === "left") {
6171
alignment = "flex-start";
62-
if (wrapWords) {
63-
style["textAlign"] = "left";
64-
}
6572
} else if (textAlign === "right") {
6673
alignment = "flex-end";
67-
if (wrapWords) {
68-
style["textAlign"] = "right";
69-
}
70-
} else {
71-
if (wrapWords) {
72-
style["textAlign"] = "center";
73-
}
7474
}
75-
style["justifyContent"] = alignment;
76-
style["cursor"] = "default";
75+
7776
let alignmentV = "center";
7877
if (textAlignV === "top") {
7978
alignmentV = "flex-start";
8079
} else if (textAlignV === "bottom") {
8180
alignmentV = "flex-end";
8281
}
83-
style["alignItems"] = alignmentV;
84-
let transform = undefined;
85-
if (rotationAngle) {
86-
transform = `rotate(${rotationAngle}deg)`;
87-
}
88-
89-
if (wrapWords) {
90-
style["wordBreak"] = "break-word";
91-
style["whiteSpace"] = "break-spaces";
92-
}
9382

9483
// Simple component to display text - defaults to black text and dark grey background
9584
return (
96-
<div className={className} style={style}>
97-
<span style={{ transform }}> {text} </span>
98-
</div>
85+
<Typography
86+
noWrap={!wrapWords}
87+
sx={{
88+
justifyContent: alignment,
89+
alignItems: alignmentV,
90+
textAlign: textAlign,
91+
wordBreak: wrapWords ? "break-word" : null,
92+
whiteSpace: wrapWords ? "break-spaces" : null,
93+
color: foregroundColor.toString(),
94+
backgroundColor: backgroundColor,
95+
fontFamily: font,
96+
transform: `rotate(${rotationStep * -90}deg)`.toString()
97+
}}
98+
>
99+
{text}
100+
</Typography>
99101
);
100102
};
101103

src/ui/widgets/Readback/readback.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const ReadbackProps = {
3434
foregroundColor: ColorPropOpt,
3535
backgroundColor: ColorPropOpt,
3636
border: BorderPropOpt,
37-
rotationAngle: FloatPropOpt,
37+
rotationStep: FloatPropOpt,
3838
visible: BoolPropOpt,
3939
wrapWords: BoolPropOpt
4040
};
@@ -60,7 +60,7 @@ export const ReadbackComponent = (
6060
textAlignV = "top",
6161
showUnits = false,
6262
precisionFromPv = false,
63-
rotationAngle,
63+
rotationStep,
6464
visible,
6565
wrapWords = false
6666
} = props;
@@ -140,7 +140,7 @@ export const ReadbackComponent = (
140140
foregroundColor={foregroundColor}
141141
backgroundColor={backgroundColor}
142142
border={border}
143-
rotationAngle={rotationAngle}
143+
rotationStep={rotationStep}
144144
visible={visible}
145145
wrapWords={wrapWords}
146146
></LabelComponent>

src/ui/widgets/Symbol/__snapshots__/symbol.test.tsx.snap

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,11 @@ exports[`<Symbol /> from .opi file > matches snapshot 1`] = `
8282
<div
8383
style="padding: 5%;"
8484
>
85-
<div
86-
class="Label _Label_c35983"
87-
style="background-color: transparent; cursor: default; justify-content: flex-start; align-items: flex-start;"
85+
<p
86+
class="MuiTypography-root MuiTypography-body1 css-1fe740m-MuiTypography-root"
8887
>
89-
<span>
90-
Fake value
91-
</span>
92-
</div>
88+
Fake value
89+
</p>
9390
</div>
9491
</div>
9592
</DocumentFragment>

0 commit comments

Comments
 (0)