Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/download template redesign #342

Open
wants to merge 8 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions src/webapp/components/collapsible-section/Section.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Icon, IconButton, makeStyles, Paper } from "@material-ui/core";
import React from "react";

type iconPosition = "left" | "right";
type arrowStyle = "upDown" | "rightLeft";

export interface SectionProps {
isOpen?: boolean;
setOpen?: (open: boolean) => void;
children: React.ReactNode;
title: React.ReactNode;
collapsible?: boolean;
elevation?: number;
iconPos?: iconPosition;
classProps?: {
section?: string;
header?: string;
content?: string;
};
arrowStyle?: arrowStyle;
}

export const Section = ({
Copy link

@tokland tokland Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you preferred to define children explicitly in the props instead of using React.FC. We typically use FC, BUT since it adds always children (that's why some people don't like FC), we lose some control, so let's go with this. Eventually, we can decide on creating our own "FCWithProps" or whatever, but no need to to it here.

children,
title,
collapsible,
isOpen = true,
setOpen = () => {},
elevation = 1,
iconPos = "right",
classProps = {},
arrowStyle = "upDown",
Copy link

@tokland tokland Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About the expand/collapse styles, I checked what DHIS2 does, and... it depends:

orgUnits tree (icon: left the name): right/down
maps (icon: top-right): up/down

https://play.im.dhis2.org/stable-2-40-6/dhis-web-maintenance/index.html#/list/organisationUnitSection/organisationUnit

https://play.im.dhis2.org/stable-2-40-6/dhis-web-maps/#/GlCLRPPLsWF

I have no strong opinion on this, so PM to decide.

}: SectionProps) => {
Copy link

@tokland tokland Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not wrong at all, but typically we prefer to write props: SectionProps here and unpack in the first line of the component, so the signature stays single-line and more readable (subjective!).

const classes = useStyles();
const leftIcon = iconPos === "left" ? classes.leftIcon : null;
const toggle = () => setOpen(!isOpen);

return (
<Paper elevation={elevation} className={`${classes.paper} ${classProps.section}`}>
<div
className={`${classes.header} ${leftIcon} ${classProps.header} ${isOpen ? "" : classes.noBorder}`}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am not mistaken, when leftIcon is null, it will appear "null" in the class list. There are packages for that (classnames), but you can also do this: _.compact([name1, name2, name3]).join(" ")

onClick={toggle}
>
{collapsible && leftIcon && <CollapsibleToggle isOpen={isOpen} arrowStyle={arrowStyle} />}
{title}
{collapsible && !leftIcon && <CollapsibleToggle isOpen={isOpen} arrowStyle={arrowStyle} />}
</div>
<div className={`${classProps.content}`} style={{ display: isOpen ? "" : "none" }}>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever possible, we strive for immutable props (memoized components will benefit from that).

{children}
</div>
</Paper>
);
};

export interface CollapsibleToggleProps {
isOpen: boolean;
arrowStyle: "upDown" | "rightLeft";
}

export const CollapsibleToggle = ({ isOpen, arrowStyle }: CollapsibleToggleProps) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not used externally, right? we can remove the export.

const classes = useStyles();
const [open, close] = arrowStyle === "upDown" ? ["up", "down"] : ["right", "left"];

return (
<IconButton disableTouchRipple={true} style={isOpen ? {} : { left: 0 }} className={classes.button}>
<Icon>{isOpen ? `keyboard_arrow_${open}` : `keyboard_arrow_${close}`}</Icon>
</IconButton>
);
};

const useStyles = makeStyles({
header: {
margin: 0,
padding: "1em",
paddingLeft: 0,
display: "flex",
justifyContent: "space-between",
alignItems: "center",
borderBottom: "solid 1px #e8edf2",
cursor: "pointer",
},
noBorder: { border: "none" },
leftIcon: {
justifyContent: "normal",
gap: 10,
},
paper: { padding: "1em", paddingTop: "0.5em", marginBottom: "1em" },
button: { padding: 0 },
});
Loading