React Gantt - XState 튜토리얼
이 튜토리얼은 Vite를 사용한 React TypeScript 애플리케이션을 만들고, DHTMLX React Gantt 컴포넌트를 통합하며, XState로 상태를 관리하는 방법을 안내합니다.
필수 지식
- React, TypeScript, Vite, 및 XState에 대한 기본 지식
- 권장: 데이터 바인딩 모드와 이 튜토리얼이 기반으로 하는
data.save콜백을 이해하기 위해 Basics을 읽는 것이 좋습니다.
빠른 설정 - 프로젝트 생성
시작하기 전에 Node.js를 설치하세요.
Vite React + TypeScript 프로젝트를 생성합니다:
npm create vite@latest react-gantt-xstate-demo -- --template react-ts
cd react-gantt-xstate-demo
이제 필요한 의존 성을 설치합니다.
- For npm:
npm install xstate @xstate/react @mui/material @mui/icons-material @emotion/react @emotion/styled
- For yarn:
yarn add xstate @xstate/react @mui/material @mui/icons-material @emotion/react @emotion/styled
그 다음 React Gantt 패키지를 설치해야 합니다.
Installing React Gantt
React Gantt를 React Gantt 설치 가이드에 따라 설치합니다.
이번 튜토리얼에서는 평가 패키지를 사용합니다:
npm install @dhtmlx/trial-react-gantt
또는
yarn add @dhtmlx/trial-react-gantt
Professional 패키지를 이미 사용 중이라면 명령과 임포트에서 @dhtmlx/trial-react-gantt를 @dhx/react-gantt로 교체하세요.
Dev 서버를 이제 시작할 수 있습니다:
npm run dev
이제 React 프로젝트가 http://localhost:5173에서 실행 중이어야 합니다.
노트
Gantt가 body의 전체 공간을 차지하도록 하려면, src 폴더에 있는 App.css의 기본 스타일을 제거하고 아래 스타일을 추가해야 합니다:
#root {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
}
예제 데이터 및 구성 설정 설정
Gantt 차트용 초기 데이터를 담은 src/seed/Seed.ts 파일을 생성합니다:
import type { SerializedTask, Link, GanttConfig } from '@dhtmlx/trial-react-gantt';
export type ZoomLevel = 'day' | 'month' | 'year';
export const defaultZoomLevels: NonNullable<GanttConfig['zoom']> = {
current: 'day',
levels: [
{ name: 'day', scale_height: 27, min_column_width: 80, scales: [{ unit: 'day', step: 1, format: '%d %M' }] },
{ name: 'month', scale_height: 50, min_column_width: 120, scales: [{ unit: 'month', format: '%F, %Y' }, { unit: 'week', format: 'Week #%W' }] },
{ name: 'year', scale_height: 50, min_column_width: 30, scales: [{ unit: 'year', step: 1, format: '%Y' }] },
],
};
export const seedTasks: SerializedTask[] = [
{ id: 1, text: 'Office itinerancy', type: 'project', start_date: new Date(2025, 3, 2).toISOString(), duration: 17, progress: 0.4, parent: 0, open: true },
{ id: 2, text: 'Office facing', type: 'project', start_date: new Date(2025, 3, 2).toISOString(), duration: 8, progress: 0.6, parent: 1, open: true },
{ id: 3, text: 'Furniture installation', type: 'project', start_date: new Date(2025, 3, 11).toISOString(), duration: 8, progress: 0.6, parent: 1, open: true },
{ id: 4, text: 'The employee relocation', type: 'project', start_date: new Date(2025, 3, 13).toISOString(), duration: 5, progress: 0.5, parent: 1, priority: 3, open: true },
{ id: 5, text: 'Interior office', type: 'task', start_date: new Date(2025, 3, 3).toISOString(), duration: 7, progress: 0.6, parent: 2, priority: 1 },
{ id: 6, text: 'Air conditioners check', type: 'task', start_date: new Date(2025, 3, 3).toISOString(), duration: 7, progress: 0.6, parent: 2, priority: 2 },
{ id: 7, text: 'Workplaces preparation', type: 'task', start_date: new Date(2025, 3, 12).toISOString(), duration: 8, progress: 0.6, parent: 3 },
{ id: 8, text: 'Preparing workplaces', type: 'task', start_date: new Date(2025, 3, 14).toISOString(), duration: 5, progress: 0.5, parent: 4, priority: 1 },
{ id: 9, text: 'Workplaces importation', type: 'task', start_date: new Date(2025, 3, 21).toISOString(), duration: 4, progress: 0.5, parent: 4 },
{ id: 10, text: 'Workplaces exportation', type: 'task', start_date: new Date(2025, 3, 27).toISOString(), duration: 3, progress: 0.5, parent: 4, priority: 2 }
];
export const seedLinks: Link[] = [
{ id: 2, source: 2, target: 3, type: '0' },
{ id: 3, source: 3, target: 4, type: '0' },
{ id: 7, source: 8, target: 9, type: '0' }
];