Skip to content

Commit

Permalink
Merge pull request #869 from CodeForAfrica/fix/pesayetu-treeview
Browse files Browse the repository at this point in the history
@/PesaYetu Fix TreeView Component
  • Loading branch information
m453h authored Sep 3, 2024
2 parents 13b879a + b77ce9f commit 7ff05ef
Show file tree
Hide file tree
Showing 5 changed files with 419 additions and 58 deletions.
2 changes: 1 addition & 1 deletion apps/pesayetu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
"@emotion/styled": "^11.13.0",
"@hurumap/core": "workspace:*",
"@hurumap/next": "workspace:*",
"@mui/lab": "5.0.0-alpha.155",
"@mui/material": "^5.16.6",
"@mui/styles": "^5.16.6",
"@mui/utils": "^5.16.6",
"@mui/x-tree-view": "^7.15.0",
"@reactour/tour": "^3.7.0",
"aws-sdk": "^2.1666.0",
"clsx": "^2.1.1",
Expand Down
30 changes: 19 additions & 11 deletions apps/pesayetu/src/components/HURUmap/TreeView/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import TreeItem from "@mui/lab/TreeItem";
import MuiTreeView from "@mui/lab/TreeView";
import { Typography } from "@mui/material";
import { SimpleTreeView, TreeItem } from "@mui/x-tree-view";
import clsx from "clsx";
import PropTypes from "prop-types";
import React, { useState } from "react";
Expand All @@ -12,13 +11,15 @@ import slugify from "@/pesayetu/utils/slugify";

function TreeView({ items, onLabelClick, ...props }) {
const classes = useStyles(props);

const [expanded, setExpanded] = useState();

const handleLabelClick = (e) => {
e.preventDefault();
const { id, expand } = e.target.dataset;

if (expand) {
setExpanded(id);
setExpanded([id]);
}
if (onLabelClick) {
onLabelClick(id);
Expand All @@ -30,23 +31,27 @@ function TreeView({ items, onLabelClick, ...props }) {
}
return (
<div className={classes.root}>
<MuiTreeView expanded={[expanded]}>
<SimpleTreeView expandedItems={expanded}>
{items.map((item) => {
const itemId = slugify(item.title);

return (
<TreeItem
itemId={itemId}
key={itemId}
nodeId={itemId}
label={
<>
<Typography data-id={itemId} data-expand variant="caption">
<Typography
data-id={itemId}
data-expand
variant="caption"
onClick={handleLabelClick}
>
{item.title}
</Typography>
<CheckIcon className={classes.icon} />
</>
}
onLabelClick={handleLabelClick}
classes={{
root: classes.tree,
expanded: classes.expanded,
Expand All @@ -58,14 +63,17 @@ function TreeView({ items, onLabelClick, ...props }) {

return (
<TreeItem
itemId={childId}
key={childId}
nodeId={childId}
label={
<Typography data-id={childId} variant="caption">
<Typography
data-id={childId}
variant="caption"
onClick={handleLabelClick}
>
{child.title}
</Typography>
}
onLabelClick={handleLabelClick}
classes={{
label: clsx(classes.label, classes.childLabel),
}}
Expand All @@ -75,7 +83,7 @@ function TreeView({ items, onLabelClick, ...props }) {
</TreeItem>
);
})}
</MuiTreeView>
</SimpleTreeView>
</div>
);
}
Expand Down
30 changes: 17 additions & 13 deletions apps/pesayetu/src/components/HURUmap/TreeView/useStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,26 @@ const useStyles = makeStyles(({ typography, palette }) => ({
root: {
textAlign: "right",
background: palette.background.paper,
"& .MuiTreeItem-root.Mui-selected > .MuiTreeItem-content .MuiTreeItem-label":
{
backgroundColor: "unset",
},
"& .MuiTreeItem-content.Mui-selected": {
backgroundColor: "unset",
},
"& .MuiTreeItem-iconContainer": {
width: 0,
},
"& .MuiTreeItem-iconContainer svg": {
display: "none",
},
"& .MuiTreeItem-content": {
padding: 0,
borderRadius: 0,
"&.Mui-expanded": {
borderRightColor: palette.primary.main,
borderRightWidth: typography.pxToRem(2),
borderRightStyle: "Solid",
backgroundColor: palette.background.default,
borderBottom: `1px solid ${palette.grey.main}`,
},
},
},
label: {
marginRight: typography.pxToRem(20),
Expand All @@ -29,18 +42,9 @@ const useStyles = makeStyles(({ typography, palette }) => ({
fill: palette.grey.main,
width: typography.pxToRem(19),
},
tree: {},
expanded: {
"& .MuiCollapse-root": {
marginLeft: 0,
borderTop: `1px solid ${palette.grey.main}`,
borderBottom: `1px solid ${palette.grey.main}`,
},
"&> .MuiTreeItem-content": {
borderRightColor: palette.primary.main,
borderRightWidth: typography.pxToRem(2),
borderRightStyle: "Solid",
backgroundColor: palette.background.default,
},
"& $icon": {
fill: "#666666",
Expand Down
7 changes: 2 additions & 5 deletions packages/hurumap-next/src/Map/LazyMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@ const LazyMap = React.forwardRef(function LazyMap(props, ref) {
[preferredChildren, isPinOrCompare],
);

const { choropleth, legend } = generateChoropleth(
choroplethProps,
locations,
mapType,
);
const { choropleth, legend } =
generateChoropleth(choroplethProps, locations, mapType) || {};

useEffect(() => {
let selectedBound =
Expand Down
Loading

0 comments on commit 7ff05ef

Please sign in to comment.