Skip to content

Commit 8a56c6a

Browse files
committed
Add ViewMode Year
1 parent babd3e0 commit 8a56c6a

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

example/src/App.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ const App = () => {
1010
const [tasks, setTasks] = React.useState<Task[]>(initTasks());
1111
const [isChecked, setIsChecked] = React.useState(true);
1212
let columnWidth = 65;
13-
if (view === ViewMode.Month) {
13+
if (view === ViewMode.Year) {
14+
columnWidth = 350;
15+
} else if (view === ViewMode.Month) {
1416
columnWidth = 300;
1517
} else if (view === ViewMode.Week) {
1618
columnWidth = 250;

example/src/components/view-switcher.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ export const ViewSwitcher: React.SFC<ViewSwitcherProps> = ({
4646
>
4747
Month
4848
</button>
49-
49+
<button
50+
className="Button"
51+
onClick={() => onViewModeChange(ViewMode.Year)}
52+
>
53+
Year
54+
</button>
5055
<div className="Switch">
5156
<label className="Switch_Toggle">
5257
<input

src/components/calendar/calendar.tsx

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,50 @@ export const Calendar: React.FC<CalendarProps> = ({
3232
fontFamily,
3333
fontSize,
3434
}) => {
35+
const getCalendarValuesForYear = () => {
36+
const topValues: ReactChild[] = [];
37+
const bottomValues: ReactChild[] = [];
38+
const topDefaultHeight = headerHeight * 0.5;
39+
for (let i = 0; i < dateSetup.dates.length; i++) {
40+
const date = dateSetup.dates[i];
41+
const bottomValue = date.getFullYear();
42+
bottomValues.push(
43+
<text
44+
key={date.getFullYear()}
45+
y={headerHeight * 0.8}
46+
x={columnWidth * i + columnWidth * 0.5}
47+
className={styles.calendarBottomText}
48+
>
49+
{bottomValue}
50+
</text>
51+
);
52+
if (
53+
i === 0 ||
54+
date.getFullYear() !== dateSetup.dates[i - 1].getFullYear()
55+
) {
56+
const topValue = date.getFullYear().toString();
57+
let xText: number;
58+
if (rtl) {
59+
xText = (6 + i + date.getFullYear() + 1) * columnWidth;
60+
} else {
61+
xText = (6 + i - date.getFullYear()) * columnWidth;
62+
}
63+
topValues.push(
64+
<TopPartOfCalendar
65+
key={topValue}
66+
value={topValue}
67+
x1Line={columnWidth * i}
68+
y1Line={0}
69+
y2Line={headerHeight}
70+
xText={xText}
71+
yText={topDefaultHeight * 0.9}
72+
/>
73+
);
74+
}
75+
}
76+
return [topValues, bottomValues];
77+
};
78+
3579
const getCalendarValuesForMonth = () => {
3680
const topValues: ReactChild[] = [];
3781
const bottomValues: ReactChild[] = [];
@@ -268,10 +312,13 @@ export const Calendar: React.FC<CalendarProps> = ({
268312
let topValues: ReactChild[] = [];
269313
let bottomValues: ReactChild[] = [];
270314
switch (dateSetup.viewMode) {
271-
case ViewMode.Month:
272-
[topValues, bottomValues] = getCalendarValuesForMonth();
315+
case ViewMode.Year:
316+
[topValues, bottomValues] = getCalendarValuesForYear();
273317
break;
274-
case ViewMode.Week:
318+
case ViewMode.Month:
319+
[topValues, bottomValues] = getCalendarValuesForMonth();
320+
break;
321+
case ViewMode.Week:
275322
[topValues, bottomValues] = getCalendarValuesForWeek();
276323
break;
277324
case ViewMode.Day:

src/helpers/date-helper.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ export const ganttDateRange = (tasks: Task[], viewMode: ViewMode) => {
8181
}
8282
}
8383
switch (viewMode) {
84+
case ViewMode.Year:
85+
newStartDate = addToDate(newStartDate, -1, "year");
86+
newStartDate = startOfDate(newStartDate, "year");
87+
newEndDate = addToDate(newEndDate, 1, "year");
88+
newEndDate = startOfDate(newEndDate, "year");
89+
break;
8490
case ViewMode.Month:
8591
newStartDate = addToDate(newStartDate, -1, "month");
8692
newStartDate = startOfDate(newStartDate, "month");
@@ -130,6 +136,9 @@ export const seedDates = (
130136
const dates: Date[] = [currentDate];
131137
while (currentDate < endDate) {
132138
switch (viewMode) {
139+
case ViewMode.Year:
140+
currentDate = addToDate(currentDate, 1, "year");
141+
break;
133142
case ViewMode.Month:
134143
currentDate = addToDate(currentDate, 1, "month");
135144
break;

src/types/public-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export enum ViewMode {
66
/** ISO-8601 week */
77
Week = "Week",
88
Month = "Month",
9+
Year = "Year"
910
}
1011
export type TaskType = "task" | "milestone" | "project";
1112
export interface Task {

0 commit comments

Comments
 (0)