|
| 1 | +import React, { useState } from "react"; |
| 2 | +import { ViewMode, Gantt, DateFormat } from "gantt-task-react"; |
| 3 | +import { ViewSwitcher } from "./components/view-switcher"; |
| 4 | +import { initTasksWithExtraColumns, extraColumns } from "./extra-columns-helper"; |
| 5 | +import "gantt-task-react/dist/index.css"; |
| 6 | + |
| 7 | +//Init |
| 8 | +const ExtraColumnsApp: React.FC = () => { |
| 9 | + const [tasks, setTasks] = useState(initTasksWithExtraColumns()); |
| 10 | + const [view, setView] = useState<ViewMode>(ViewMode.Day); |
| 11 | + const [dateFormat, setDateFormat] = useState<DateFormat>("locale"); |
| 12 | + |
| 13 | + let columnWidth = 65; |
| 14 | + if (view === ViewMode.Year) { |
| 15 | + columnWidth = 350; |
| 16 | + } else if (view === ViewMode.Month) { |
| 17 | + columnWidth = 300; |
| 18 | + } else if (view === ViewMode.Week) { |
| 19 | + columnWidth = 250; |
| 20 | + } |
| 21 | + |
| 22 | + return ( |
| 23 | + <div className="Wrapper"> |
| 24 | + <h2>Gantt Chart with Extra Columns Example</h2> |
| 25 | + <p>This example demonstrates how to add custom columns to the Gantt chart task list.</p> |
| 26 | + |
| 27 | + <div style={{ display: "flex", gap: "20px", alignItems: "center", marginBottom: "10px" }}> |
| 28 | + <ViewSwitcher |
| 29 | + onViewModeChange={(viewMode) => setView(viewMode)} |
| 30 | + onViewListChange={() => {}} |
| 31 | + isChecked={true} |
| 32 | + /> |
| 33 | + |
| 34 | + <div> |
| 35 | + <label style={{ marginRight: "10px" }}>Date Format:</label> |
| 36 | + <select |
| 37 | + value={dateFormat} |
| 38 | + onChange={(e) => setDateFormat(e.target.value as DateFormat)} |
| 39 | + style={{ padding: "5px", borderRadius: "4px", border: "1px solid #ccc" }} |
| 40 | + > |
| 41 | + <option value="locale">Locale Format (e.g., Fri, June 15, 2025)</option> |
| 42 | + <option value="iso8601">ISO 8601 Format (YYYY-MM-DD)</option> |
| 43 | + </select> |
| 44 | + </div> |
| 45 | + </div> |
| 46 | + |
| 47 | + <div style={{ marginTop: "20px" }}> |
| 48 | + <Gantt |
| 49 | + tasks={tasks} |
| 50 | + viewMode={view} |
| 51 | + extraColumns={extraColumns} |
| 52 | + nameColumnWidth="180px" |
| 53 | + fromColumnWidth="120px" |
| 54 | + toColumnWidth="120px" |
| 55 | + dateFormat={dateFormat} |
| 56 | + onDateChange={(task, _children) => { |
| 57 | + console.log("On date change Id:" + task.id); |
| 58 | + setTasks(tasks); |
| 59 | + }} |
| 60 | + onDelete={(task) => { |
| 61 | + const conf = window.confirm("Are you sure about " + task.name + " ?"); |
| 62 | + if (conf) { |
| 63 | + setTasks(tasks.filter((t) => t.id !== task.id)); |
| 64 | + } |
| 65 | + return conf; |
| 66 | + }} |
| 67 | + onProgressChange={(task, _children) => { |
| 68 | + console.log("On progress change Id:" + task.id); |
| 69 | + setTasks(tasks); |
| 70 | + }} |
| 71 | + onDoubleClick={(task) => { |
| 72 | + console.log("On Double Click event Id:" + task.id); |
| 73 | + }} |
| 74 | + onClick={(task) => { |
| 75 | + console.log("On Click event Id:" + task.id); |
| 76 | + }} |
| 77 | + columnWidth={columnWidth} |
| 78 | + listCellWidth="180px" |
| 79 | + /> |
| 80 | + </div> |
| 81 | + |
| 82 | + <div style={{ marginTop: "20px", padding: "15px", backgroundColor: "#f8f9fa", borderRadius: "5px" }}> |
| 83 | + <h3>Features Demonstrated:</h3> |
| 84 | + <ul> |
| 85 | + <li><strong>Status Column:</strong> Shows task status with colored badges</li> |
| 86 | + <li><strong>Assignee Column:</strong> Displays who is responsible for each task</li> |
| 87 | + <li><strong>Priority Column:</strong> Shows task priority with colored indicators</li> |
| 88 | + <li><strong>Budget Column:</strong> Displays formatted budget amounts</li> |
| 89 | + <li><strong>Custom Column Widths:</strong> Name, From, and To columns have custom widths</li> |
| 90 | + <li><strong>Date Format Options:</strong> Toggle between locale and ISO (YYYY-MM-DD) date formats</li> |
| 91 | + </ul> |
| 92 | + |
| 93 | + <h4>How to Use:</h4> |
| 94 | + <ol> |
| 95 | + <li>Define your extra columns configuration with <code>ExtraColumn[]</code></li> |
| 96 | + <li>Add <code>extraColumns</code> data to your task objects</li> |
| 97 | + <li>Pass the columns configuration to the <code>Gantt</code> component</li> |
| 98 | + <li>Optionally use custom render functions for complex column content</li> |
| 99 | + <li>Set custom widths using <code>nameColumnWidth</code>, <code>fromColumnWidth</code>, <code>toColumnWidth</code></li> |
| 100 | + <li>Choose date format with <code>dateFormat</code> prop: "locale" or "iso"</li> |
| 101 | + </ol> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + ); |
| 105 | +}; |
| 106 | + |
| 107 | +export default ExtraColumnsApp; |
0 commit comments