79 lines
2.2 KiB
TypeScript
Raw Normal View History

2020-08-25 20:58:36 +03:00
import React, { useRef, useEffect, SyntheticEvent } from "react";
import { GridProps, Grid } from "../grid/grid";
import { CalendarProps, Calendar } from "../calendar/calendar";
import { TaskGanttContentProps, TaskGanttContent } from "./task-gantt-content";
import styles from "./gantt.module.css";
export type TaskGanttProps = {
gridProps: GridProps;
calendarProps: CalendarProps;
barProps: TaskGanttContentProps;
ganttHeight: number;
scrollY: number;
2020-08-25 20:58:36 +03:00
scrollX: number;
onScroll: (event: SyntheticEvent<HTMLDivElement>) => void;
};
export const TaskGantt: React.FC<TaskGanttProps> = ({
gridProps,
calendarProps,
barProps,
ganttHeight,
scrollY,
2020-08-25 20:58:36 +03:00
scrollX,
onScroll,
}) => {
const ganttSVGRef = useRef<SVGSVGElement>(null);
const horizontalContainerRef = useRef<HTMLDivElement>(null);
2020-08-25 20:58:36 +03:00
const verticalContainerRef = useRef<HTMLDivElement>(null);
const newBarProps = { ...barProps, svg: ganttSVGRef };
useEffect(() => {
if (horizontalContainerRef.current) {
horizontalContainerRef.current.scrollTop = scrollY;
}
}, [scrollY]);
2020-08-25 20:58:36 +03:00
useEffect(() => {
if (verticalContainerRef.current) {
verticalContainerRef.current.scrollLeft = scrollX;
}
}, [scrollX]);
return (
2020-08-25 20:58:36 +03:00
<div
className={styles.ganttVerticalContainer}
ref={verticalContainerRef}
onScroll={onScroll}
>
<svg
xmlns="http://www.w3.org/2000/svg"
width={gridProps.gridWidth}
height={calendarProps.headerHeight}
fontFamily={barProps.fontFamily}
>
<Calendar {...calendarProps} />
</svg>
<div
ref={horizontalContainerRef}
className={styles.horizontalContainer}
style={
ganttHeight
? { height: ganttHeight, width: gridProps.gridWidth }
: { width: gridProps.gridWidth }
}
>
<svg
xmlns="http://www.w3.org/2000/svg"
width={gridProps.gridWidth}
height={barProps.rowHeight * barProps.tasks.length}
fontFamily={barProps.fontFamily}
ref={ganttSVGRef}
>
<Grid {...gridProps} />
<TaskGanttContent {...newBarProps} />
</svg>
</div>
</div>
);
};