-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSummaryCard.tsx
165 lines (154 loc) · 4.23 KB
/
SummaryCard.tsx
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import {
Box,
Card,
CardActions,
CardContent,
CardHeader,
IconButton,
Popover,
Stack,
cardHeaderClasses
} from "@mui/material";
import React, { useState } from "react";
import { Infographic } from "../../CardContent";
import LabelChipGroup from "../../LabelSelector/LabelChipGroup/LabelChipGroup";
import type { LabelChipGroupProps } from "../../LabelSelector/LabelChipGroup/LabelChipGroup.types";
import { MoreVert } from "@mui/icons-material";
import NoWrapTypography from "../../NoWrapTypography/NoWrapTypography";
import { SummaryCardProps } from "./SummaryCard.types";
// TODO: add tests in browser once we are done with the migration to cypress. The old tests live in a txt file in this folder until then.
function SummaryCard({
content = null,
height = 600,
labels = [],
media = "",
version,
moreOptionsPopover = null,
onClickLabel = () => {},
moreCardActions = null,
subtitle = "subtitle",
title = "title",
width = 368,
moreOptionsRef
}: SummaryCardProps) {
// more options popover anchor state
const [moreOptionsAnchorEl, setMoreOptionsAnchorEl] =
useState<HTMLElement | null>(null);
// header content width
const headerContentWidth = width - 60;
// handle the click of the more options button by setting the more options anchor element
const handleMoreOptionsClick = (event: React.MouseEvent<HTMLElement>) => {
setMoreOptionsAnchorEl(event.currentTarget);
};
// handle the close of the more options popover by setting the more options anchor element to null
const handleMoreOptionsClose = () => {
setMoreOptionsAnchorEl(null);
};
// determine if the more options popover is open
const isMoreOptionsOpen = Boolean(moreOptionsAnchorEl);
// convert the labels to chips
const labelChips: LabelChipGroupProps["chips"] = labels.map(label => {
return {
clickable: true,
color: label.color,
label: label.name,
onClick: () => handleLabelClick(label),
size: "small"
};
});
// handle label click by calling the onClickLabel prop
const handleLabelClick = (label: {
_id: string;
color: string;
description?: string;
name: string;
}) => {
onClickLabel(label);
};
// render the summary card
return (
<Stack id="SummaryCard">
<Card sx={{ height, width }}>
<CardHeader
sx={{
[` .${cardHeaderClasses.content}`]: {
overflowX: "hidden"
},
height: 50
}}
action={
moreOptionsPopover ? (
<IconButton
ref={moreOptionsRef}
aria-label="settings"
onClick={handleMoreOptionsClick}
>
<MoreVert />
</IconButton>
) : null
}
disableTypography
title={
<NoWrapTypography
sx={{
fontSize: 20,
fontWeight: 500
}}
>
{title}
</NoWrapTypography>
}
subheader={
<NoWrapTypography
sx={{
fontSize: 14,
fontWeight: 400
}}
>
{subtitle}
</NoWrapTypography>
}
/>
<Box
ml={2}
pr={2}
sx={{
height: 24,
maxWidth: headerContentWidth,
overflowX: "hidden"
}}
>
<LabelChipGroup chips={labelChips} />
</Box>
<Infographic media={media} version={version} />
<CardContent
sx={{
height: height - 386,
overflowY: "hidden",
px: 0,
py: 1
}}
>
{content}
</CardContent>
<CardActions disableSpacing sx={{ padding: 0 }}>
{moreCardActions}
</CardActions>
</Card>
<Popover
open={isMoreOptionsOpen}
anchorEl={moreOptionsAnchorEl}
onClose={handleMoreOptionsClose}
onClick={handleMoreOptionsClose}
anchorOrigin={{
horizontal: "left",
vertical: "bottom"
}}
>
{moreOptionsPopover}
</Popover>
</Stack>
);
}
// export the summary card
export default SummaryCard;