-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
122 lines (109 loc) · 3.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import React, { Component } from 'react'
import moment from 'moment'
import Timeline from 'react-calendar-timeline'
import generateFakeData from '../generate-fake-data'
var keys = {
groupIdKey: 'id',
groupTitleKey: 'title',
groupRightTitleKey: 'rightTitle',
groupLabelKey: 'label',
itemIdKey: 'id',
itemTitleKey: 'title',
itemDivTitleKey: 'title',
itemGroupKey: 'group',
itemTimeStartKey: 'start',
itemTimeEndKey: 'end'
}
export default class App extends Component {
constructor(props) {
super(props)
const { groups, items } = generateFakeData()
const defaultTimeStart = moment()
.startOf('day')
.toDate()
const defaultTimeEnd = moment()
.startOf('day')
.add(1, 'day')
.toDate()
// convert every 2 groups out of 3 to nodes, leaving the first as the root
const newGroups = groups.map(group => {
const isRoot = (parseInt(group.id) - 1) % 3 === 0
const parent = isRoot
? null
: Math.floor((parseInt(group.id) - 1) / 3) * 3 + 1
const height = isRoot ? 15 : null;
return Object.assign({}, group, {
root: isRoot,
parent: parent,
height: height
})
})
const newItems = items.filter(item => {
const group = newGroups.find(group => group.id === item.group);
return !group.root;
});
this.state = {
groups: newGroups,
items: newItems,
defaultTimeStart,
defaultTimeEnd,
openGroups: {}
}
}
toggleGroup = id => {
const { openGroups } = this.state
this.setState({
openGroups: {
...openGroups,
[id]: !openGroups[id]
}
})
}
render() {
const {
groups,
items,
defaultTimeStart,
defaultTimeEnd,
openGroups
} = this.state
// hide (filter) the groups that are closed, for the rest, patch their "title" and add some callbacks or padding
const newGroups = groups
.filter(g => g.root || openGroups[g.parent])
.map(group => {
return Object.assign({}, group, {
title: group.root ? (
<div
onClick={() => this.toggleGroup(parseInt(group.id))}
style={{ cursor: 'pointer' }}
>
{openGroups[parseInt(group.id)] ? '[-]' : '[+]'} {group.title}
</div>
) : (
<div style={{ paddingLeft: 20 }}>{group.title}</div>
)
})
})
return (
<Timeline
groups={newGroups}
items={items}
keys={keys}
sidebarWidth={150}
sidebarContent={<div>Above The Left</div>}
rightSidebarWidth={150}
rightSidebarContent={<div>Above The Right</div>}
canMove={true}
canResize="right"
canSelect
itemsSorted
itemTouchSendsClick={false}
stackItems
itemHeightRatio={0.75}
defaultTimeStart={defaultTimeStart}
defaultTimeEnd={defaultTimeEnd}
horizontalLineClassNamesForGroup={(group) => group.root ? ["row-root"] : []}
/>
)
}
}