frontend/src/components/SchedulerRow.tsx

124 lines
3.6 KiB
TypeScript
Raw Normal View History

import React, { MouseEvent } from 'react';
import { Group } from '../types';
import styled from 'styled-components/macro';
import Popover from '@material-ui/core/Popover';
import Typography from '@material-ui/core/Typography';
import { makeStyles, createStyles, Theme } from '@material-ui/core/styles';
const useStyles = makeStyles((theme: Theme) =>
createStyles({
popover: {
pointerEvents: 'none',
},
paper: {
padding: theme.spacing(1),
},
}),
);
2020-08-12 04:13:14 +02:00
interface SchedulerEventProps {
eventIndex: number;
cellTop: number;
cellWidth: number;
cellHeight: number;
2020-08-12 04:13:14 +02:00
}
const SchedulerEvent = styled.div<SchedulerEventProps>`
position: absolute;
display: flex;
top: ${({ cellTop }) => cellTop}px;
left: ${({ cellWidth, eventIndex }) => cellWidth + 5 + cellWidth * eventIndex}px;
width: ${({ cellWidth }) => (cellWidth * 2.5) / 3}px;
height: ${({ cellHeight }) => (cellHeight * 2 * 3) / 4}px;
2020-09-11 18:56:14 +02:00
z-index: 2;
`;
const Classes = styled.div<SchedulerEventProps>`
2020-08-12 04:13:14 +02:00
z-index: 2;
2020-09-11 18:56:14 +02:00
border-radius: 10px;
padding-left: 5px;
2020-09-11 18:56:14 +02:00
background-color: rgb(100, 181, 246);
2020-08-12 04:13:14 +02:00
`;
2020-08-09 20:44:35 +02:00
interface SchedulerRowProps {
groups: Array<Group & { name: string }>;
2020-08-09 20:44:35 +02:00
indexRow: number;
cellTop: number;
cellWidth: number;
cellHeight: number;
2020-08-09 20:44:35 +02:00
}
export const SchedulerRow = ({ groups, indexRow, cellTop, cellWidth, cellHeight }: SchedulerRowProps) => {
const classes = useStyles();
const [anchorEl, setAnchorEl] = React.useState<HTMLDivElement | null>(null);
//looks weird
const handlePopoverOpen = (event: MouseEvent<HTMLDivElement, globalThis.MouseEvent>) => {
setAnchorEl(event.currentTarget);
};
const handlePopoverClose = () => {
setAnchorEl(null);
};
const open = Boolean(anchorEl);
2020-08-09 20:44:35 +02:00
return (
2020-08-12 04:13:14 +02:00
<>
2020-08-18 00:21:21 +02:00
{[...Array(5)].map((_, eventIndex) => (
2020-08-12 04:13:14 +02:00
<SchedulerEvent
eventIndex={eventIndex}
cellTop={cellTop}
cellWidth={cellWidth}
cellHeight={cellHeight}
2020-08-18 00:21:21 +02:00
key={eventIndex}
2020-08-09 21:41:52 +02:00
id={`eventRow${indexRow}eventCol${eventIndex}`}
2020-08-09 20:44:35 +02:00
>
2020-09-11 18:56:14 +02:00
{groups.map(
(group, index) =>
group.day === eventIndex && (
<>
<Classes
eventIndex={eventIndex}
cellTop={cellTop}
cellWidth={cellWidth}
cellHeight={cellHeight}
id={`eventRow${indexRow}eventCol${eventIndex}`}
key={index}
aria-owns={open ? 'mouse-over-popover' : undefined}
aria-haspopup="true"
onMouseEnter={handlePopoverOpen}
onMouseLeave={handlePopoverClose}
>
{groups[index].name}
</Classes>
<Popover
id="mouse-over-popover"
className={classes.popover}
classes={{
paper: classes.paper,
}}
open={open}
anchorEl={anchorEl}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'left',
}}
transformOrigin={{
vertical: 'top',
horizontal: 'left',
}}
onClose={handlePopoverClose}
disableRestoreFocus
>
<Typography>I use Popover.</Typography>
</Popover>
</>
2020-09-11 18:56:14 +02:00
),
)}
2020-08-12 04:13:14 +02:00
</SchedulerEvent>
2020-08-09 20:44:35 +02:00
))}
2020-08-12 04:13:14 +02:00
</>
2020-08-09 20:44:35 +02:00
);
};