126 lines
3.4 KiB
TypeScript
Raw Normal View History

2020-08-05 08:14:22 +03:00
import React, { useState } from "react";
import { Task } from "../../types/public-types";
import { BarProgressHandle } from "./bar-progress-handle";
import { BarDateHandle } from "./bar-date-handle";
import { BarDisplay } from "./bar-display";
import { BarTask } from "../../types/bar-task";
import { BarAction } from "../Gantt/gantt-content";
2020-07-22 20:50:43 +03:00
import {
progressWithByParams,
getProgressPoint,
2020-08-05 08:14:22 +03:00
} from "../../helpers/bar-helper";
import styles from "./bar.module.css";
2020-07-22 20:50:43 +03:00
export type BarProps = {
task: BarTask;
arrowIndent: number;
onDoubleClick?: (task: Task) => any;
isProgressChangeable: boolean;
isDateChangeable: boolean;
handleMouseEvents: (
event:
| React.MouseEvent<SVGPolygonElement, MouseEvent>
| React.MouseEvent<SVGGElement, MouseEvent>
| React.MouseEvent<SVGRectElement, MouseEvent>,
2020-07-30 22:58:23 +03:00
eventType: BarAction,
2020-07-22 20:50:43 +03:00
task: BarTask
) => void;
handleButtonSVGEvents: (
event: React.KeyboardEvent<SVGGElement>,
task: BarTask
) => void;
};
export const Bar: React.FC<BarProps> = ({
task,
arrowIndent,
onDoubleClick,
isProgressChangeable,
isDateChangeable,
handleMouseEvents,
handleButtonSVGEvents,
}) => {
const [isSelected, setIsSelected] = useState(false);
const progressWidth = progressWithByParams(task.x1, task.x2, task.progress);
const progressPoint = getProgressPoint(
progressWidth + task.x1,
task.y,
task.height
);
return (
<g
2020-08-05 08:14:22 +03:00
className={styles.barWrapper}
2020-07-22 20:50:43 +03:00
onDoubleClick={() => {
!!onDoubleClick && onDoubleClick(task);
}}
tabIndex={0}
onKeyDown={e => {
handleButtonSVGEvents(e, task);
}}
onMouseEnter={e => {
2020-08-05 08:14:22 +03:00
handleMouseEvents(e, "mouseenter", task);
2020-07-22 20:50:43 +03:00
}}
onMouseLeave={e => {
2020-08-05 08:14:22 +03:00
handleMouseEvents(e, "mouseleave", task);
2020-07-22 20:50:43 +03:00
}}
onFocus={() => setIsSelected(true)}
onBlur={() => setIsSelected(false)}
>
<BarDisplay
x={task.x1}
y={task.y}
width={task.x2 - task.x1}
height={task.height}
progressWidth={progressWidth}
barCornerRadius={task.barCornerRadius}
text={task.name}
hasChild={task.barChildren.length > 0}
arrowIndent={arrowIndent}
styles={task.styles}
isSelected={isSelected}
onMouseDown={e => {
2020-08-05 08:14:22 +03:00
isDateChangeable && handleMouseEvents(e, "move", task);
2020-07-22 20:50:43 +03:00
}}
/>
<g className="handleGroup">
{isDateChangeable && (
2020-08-05 08:14:22 +03:00
<g>
{/* left */}
2020-07-22 20:50:43 +03:00
<BarDateHandle
x={task.x1 + 1}
y={task.y + 1}
width={task.handleWidth}
height={task.height - 2}
barCornerRadius={task.barCornerRadius}
onMouseDown={e => {
2020-08-05 08:14:22 +03:00
handleMouseEvents(e, "start", task);
2020-07-22 20:50:43 +03:00
}}
/>
2020-08-05 08:14:22 +03:00
{/* right */}
2020-07-22 20:50:43 +03:00
<BarDateHandle
x={task.x2 - task.handleWidth - 1}
y={task.y + 1}
width={task.handleWidth}
height={task.height - 2}
barCornerRadius={task.barCornerRadius}
onMouseDown={e => {
2020-08-05 08:14:22 +03:00
handleMouseEvents(e, "end", task);
2020-07-22 20:50:43 +03:00
}}
/>
2020-08-05 08:14:22 +03:00
</g>
2020-07-22 20:50:43 +03:00
)}
{isProgressChangeable && (
<BarProgressHandle
progressPoint={progressPoint}
onMouseDown={e => {
2020-08-05 08:14:22 +03:00
handleMouseEvents(e, "progress", task);
2020-07-22 20:50:43 +03:00
}}
/>
)}
</g>
</g>
);
};