Skip to content

Commit 79f826a

Browse files
committed
feat: add extra columns and configurable column widths with ISO 8601 date format
- Add ExtraColumn interface for custom columns with optional render functions - Add nameColumnWidth, fromColumnWidth, toColumnWidth props for configurable column widths - Add DateFormat type with 'locale' and 'iso8601' options for date formatting - Add extraColumns prop to Task interface for storing additional column data - Update TaskListHeader and TaskListTable components to support extra columns - Add date format selector in example with ISO 8601 (YYYY-MM-DD) support - Export new DateFormat type from main index BREAKING CHANGE: TaskListHeader and TaskListTable component props have been extended with new optional parameters
1 parent 939ca2a commit 79f826a

File tree

6 files changed

+60
-11
lines changed

6 files changed

+60
-11
lines changed

example/src/ExtraColumnsApp.tsx

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useState } from "react";
2-
import { ViewMode, Gantt } from "gantt-task-react";
2+
import { ViewMode, Gantt, DateFormat } from "gantt-task-react";
33
import { ViewSwitcher } from "./components/view-switcher";
44
import { initTasksWithExtraColumns, extraColumns } from "./extra-columns-helper";
55
import "gantt-task-react/dist/index.css";
@@ -8,6 +8,8 @@ import "gantt-task-react/dist/index.css";
88
const ExtraColumnsApp: React.FC = () => {
99
const [tasks, setTasks] = useState(initTasksWithExtraColumns());
1010
const [view, setView] = useState<ViewMode>(ViewMode.Day);
11+
const [dateFormat, setDateFormat] = useState<DateFormat>("locale");
12+
1113
let columnWidth = 65;
1214
if (view === ViewMode.Year) {
1315
columnWidth = 350;
@@ -22,11 +24,25 @@ const ExtraColumnsApp: React.FC = () => {
2224
<h2>Gantt Chart with Extra Columns Example</h2>
2325
<p>This example demonstrates how to add custom columns to the Gantt chart task list.</p>
2426

25-
<ViewSwitcher
26-
onViewModeChange={(viewMode) => setView(viewMode)}
27-
onViewListChange={() => {}}
28-
isChecked={true}
29-
/>
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>
3046

3147
<div style={{ marginTop: "20px" }}>
3248
<Gantt
@@ -36,6 +52,7 @@ const ExtraColumnsApp: React.FC = () => {
3652
nameColumnWidth="180px"
3753
fromColumnWidth="120px"
3854
toColumnWidth="120px"
55+
dateFormat={dateFormat}
3956
onDateChange={(task, _children) => {
4057
console.log("On date change Id:" + task.id);
4158
setTasks(tasks);
@@ -69,6 +86,8 @@ const ExtraColumnsApp: React.FC = () => {
6986
<li><strong>Assignee Column:</strong> Displays who is responsible for each task</li>
7087
<li><strong>Priority Column:</strong> Shows task priority with colored indicators</li>
7188
<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>
7291
</ul>
7392

7493
<h4>How to Use:</h4>
@@ -77,6 +96,8 @@ const ExtraColumnsApp: React.FC = () => {
7796
<li>Add <code>extraColumns</code> data to your task objects</li>
7897
<li>Pass the columns configuration to the <code>Gantt</code> component</li>
7998
<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>
80101
</ol>
81102
</div>
82103
</div>

src/components/gantt/gantt.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const Gantt: React.FunctionComponent<GanttProps> = ({
6262
nameColumnWidth,
6363
fromColumnWidth,
6464
toColumnWidth,
65+
dateFormat = "locale",
6566
onDateChange,
6667
onProgressChange,
6768
onDoubleClick,
@@ -454,6 +455,7 @@ export const Gantt: React.FunctionComponent<GanttProps> = ({
454455
nameColumnWidth,
455456
fromColumnWidth,
456457
toColumnWidth,
458+
dateFormat,
457459
TaskListHeader,
458460
TaskListTable,
459461
};

src/components/task-list/task-list-table.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useMemo } from "react";
22
import styles from "./task-list-table.module.css";
3-
import { Task, ExtraColumn } from "../../types/public-types";
3+
import { Task, ExtraColumn, DateFormat } from "../../types/public-types";
44

55
const localeDateStringCache = {};
66
const toLocaleDateStringFactory =
@@ -14,6 +14,11 @@ const toLocaleDateStringFactory =
1414
}
1515
return lds;
1616
};
17+
18+
const toISODateString = (date: Date): string => {
19+
return date.toISOString().split("T")[0]; // Returns yyyy-MM-dd format
20+
};
21+
1722
const dateTimeOptions: Intl.DateTimeFormatOptions = {
1823
weekday: "short",
1924
year: "numeric",
@@ -35,6 +40,7 @@ export const TaskListTableDefault: React.FC<{
3540
nameColumnWidth?: string;
3641
fromColumnWidth?: string;
3742
toColumnWidth?: string;
43+
dateFormat?: DateFormat;
3844
}> = ({
3945
rowHeight,
4046
rowWidth,
@@ -47,12 +53,20 @@ export const TaskListTableDefault: React.FC<{
4753
nameColumnWidth,
4854
fromColumnWidth,
4955
toColumnWidth,
56+
dateFormat = "locale",
5057
}) => {
5158
const toLocaleDateString = useMemo(
5259
() => toLocaleDateStringFactory(locale),
5360
[locale]
5461
);
5562

63+
const formatDate = useMemo(() => {
64+
if (dateFormat === "iso8601") {
65+
return toISODateString;
66+
}
67+
return (date: Date) => toLocaleDateString(date, dateTimeOptions);
68+
}, [dateFormat, toLocaleDateString]);
69+
5670
return (
5771
<div
5872
className={styles.taskListWrapper}
@@ -104,7 +118,7 @@ export const TaskListTableDefault: React.FC<{
104118
maxWidth: fromColumnWidth || rowWidth,
105119
}}
106120
>
107-
&nbsp;{toLocaleDateString(t.start, dateTimeOptions)}
121+
&nbsp;{formatDate(t.start)}
108122
</div>
109123
<div
110124
className={styles.taskListCell}
@@ -113,7 +127,7 @@ export const TaskListTableDefault: React.FC<{
113127
maxWidth: toColumnWidth || rowWidth,
114128
}}
115129
>
116-
&nbsp;{toLocaleDateString(t.end, dateTimeOptions)}
130+
&nbsp;{formatDate(t.end)}
117131
</div>
118132
{/* Render extra column values */}
119133
{extraColumns.map((column) => (
@@ -126,7 +140,7 @@ export const TaskListTableDefault: React.FC<{
126140
}}
127141
title={column.render ? undefined : String(t.extraColumns?.[column.key] || "")}
128142
>
129-
&nbsp;{column.render
143+
{column.render
130144
? column.render(t)
131145
: t.extraColumns?.[column.key] || ""
132146
}

src/components/task-list/task-list.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useEffect, useRef } from "react";
22
import { BarTask } from "../../types/bar-task";
3-
import { Task, ExtraColumn } from "../../types/public-types";
3+
import { Task, ExtraColumn, DateFormat } from "../../types/public-types";
44

55
export type TaskListProps = {
66
headerHeight: number;
@@ -21,6 +21,7 @@ export type TaskListProps = {
2121
nameColumnWidth?: string;
2222
fromColumnWidth?: string;
2323
toColumnWidth?: string;
24+
dateFormat?: DateFormat;
2425
TaskListHeader: React.FC<{
2526
headerHeight: number;
2627
rowWidth: string;
@@ -45,6 +46,7 @@ export type TaskListProps = {
4546
nameColumnWidth?: string;
4647
fromColumnWidth?: string;
4748
toColumnWidth?: string;
49+
dateFormat?: DateFormat;
4850
}>;
4951
};
5052

@@ -67,6 +69,7 @@ export const TaskList: React.FC<TaskListProps> = ({
6769
nameColumnWidth,
6870
fromColumnWidth,
6971
toColumnWidth,
72+
dateFormat,
7073
TaskListHeader,
7174
TaskListTable,
7275
}) => {
@@ -102,6 +105,7 @@ export const TaskList: React.FC<TaskListProps> = ({
102105
nameColumnWidth,
103106
fromColumnWidth,
104107
toColumnWidth,
108+
dateFormat,
105109
};
106110

107111
return (

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export type {
44
GanttProps,
55
Task,
66
ExtraColumn,
7+
DateFormat,
78
StylingOption,
89
DisplayOption,
910
EventOption,

src/types/public-types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export enum ViewMode {
1111
}
1212
export type TaskType = "task" | "milestone" | "project";
1313

14+
export type DateFormat = "locale" | "iso8601";
15+
1416
export interface ExtraColumn {
1517
key: string;
1618
title: string;
@@ -157,6 +159,7 @@ export interface StylingOption {
157159
nameColumnWidth?: string;
158160
fromColumnWidth?: string;
159161
toColumnWidth?: string;
162+
dateFormat?: DateFormat;
160163
}>;
161164
}
162165

@@ -178,4 +181,8 @@ export interface GanttProps extends EventOption, DisplayOption, StylingOption {
178181
* Width of the To column
179182
*/
180183
toColumnWidth?: string;
184+
/**
185+
* Date format for start and end dates. "locale" uses locale formatting, "iso8601" uses yyyy-MM-dd format
186+
*/
187+
dateFormat?: DateFormat;
181188
}

0 commit comments

Comments
 (0)