-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathexample-texturalList.jsx
More file actions
114 lines (92 loc) · 3.11 KB
/
example-texturalList.jsx
File metadata and controls
114 lines (92 loc) · 3.11 KB
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
import * as React from 'react';
import d3 from 'd3';
import { TexturalList } from '../../src/main';
export default class TexturalListExample extends React.Component {
constructor (props) {
super(props);
this.state = {
data: []
};
// Some of the smaller options we will
// send to the List component
// `keyAccessor` is required
this.listOptions = {
keyAccessor: d => d.properties.cartodb_id,
selected: '1604'
};
// formatters just for this example
this.itemDateFormatter = d3.time.format('%B %e, %Y');
this.selectedDateFormatter = d3.time.format('%x');
// Creating a selected entry
// In a real scenario, you could pass
// this in as a prop
// or modify this example
// with another list
// and use setState to change the selected diarist
this.selectedDiarist = {
name: 'Medorem Crawford'
};
// callbacks for our List item
this.renderItem = this.renderItem.bind(this);
this.onStoryScroll = this.onStoryScroll.bind(this);
this.onItemClicked = this.onItemClicked.bind(this);
}
componentWillMount() {
this.dataLoader();
}
// load data for list
// data must be an Array of Objects
dataLoader() {
d3.json('data/diary-entries.json', (err, rsp) => {
if (err) return console.error('TexturalList data loading error!');
const data = rsp.features.filter((d) => {return d.properties.name === this.selectedDiarist.name;});
// let's prep our data
data.forEach((d) => {
d.properties.date = new Date(d.properties.date);
});
data.sort((a,b) => d3.descending(a.date, b.date));
this.selectedDiarist.entryLength = data.length;
this.selectedDiarist.begins = data[0].properties.date;
this.setState({data: data});
});
}
// Required by list
// Must return back valid React DOM
renderItem(index) {
let {data} = this.state;
const props = data[index].properties;
return (
<div>
<h3>{this.itemDateFormatter(props.date)} <span>{props.cartodb_id}</span></h3>
<p>{props.entry}</p>
</div>
);
}
// Callback for when a list item becomes
// the top most item in the list viewport.
onStoryScroll(k) {
console.log('Scroll change: ', k);
}
// Callback for a click on a list item
onItemClicked(item) {
console.log('Scroll item clicked: ', item);
}
render () {
// Example only
const beginDate = this.selectedDiarist.begins ? this.selectedDateFormatter(this.selectedDiarist.begins) : '';
return (
<div className='textural-list-example'>
<div className='textural-list-selecteditem'>
<h3>{this.selectedDiarist.name}</h3>
<p className='meta'>
<span className='meta--item'>Diary begins {beginDate}</span>
<span className='meta--item'>({this.selectedDiarist.entryLength}) entries</span>
</p>
</div>
<div className='list'>
<TexturalList items={this.state.data} renderItem={this.renderItem} onItemClicked={this.onItemClicked} onStoryScroll={this.onStoryScroll} {...this.listOptions} />
</div>
</div>
);
}
}