Set initial scroll for gantt chart

This commit is contained in:
Sakura Knoll
2022-07-23 01:12:51 +09:00 Unverified
parent d290355a69
commit 87ed39d669
2 changed files with 32 additions and 5 deletions
+18 -3
View File
@@ -8,7 +8,7 @@ import {
format,
} from 'date-fns'
import { times } from 'ramda'
import React, { useMemo, MouseEventHandler } from 'react'
import React, { useMemo, MouseEventHandler, useEffect } from 'react'
export interface GanttChartItem {
id: string
@@ -23,6 +23,7 @@ interface GanttChartProps {
endDate: string
today: Date | null
items: GanttChartItem[]
scrollTo?: (to: number) => void
}
const widthOfDay = 20
@@ -35,6 +36,7 @@ const GanttChart = ({
endDate: endDateString,
today,
items,
scrollTo,
}: GanttChartProps) => {
const startDate = new Date(startDateString)
const endDate = new Date(endDateString)
@@ -57,6 +59,19 @@ const GanttChart = ({
}, 1)
}, [items])
useEffect(() => {
if (today == null || scrollTo == null) {
return
}
const target =
differenceInCalendarDays(today, firstDateToRender) * widthOfDay -
widthOfWeek -
widthOfDay / 2
scrollTo(target > 0 ? target : 0)
}, [firstDateToRender, scrollTo, today])
return (
<Box sx={{ width: weeksToRender * widthOfWeek + 70 }}>
<WeekLabelRow
@@ -171,8 +186,8 @@ const GanttChart = ({
borderWidth: 1,
borderStyle: 'solid',
backgroundColor: 'background',
width: length,
left: offset,
width: length + 'px',
left: offset + 'px',
top: (item.row - 1) * (itemHeight + 10) + 30,
overflow: 'hidden',
textOverflow: 'ellipsis',