-
Notifications
You must be signed in to change notification settings - Fork 72
/
example.js
135 lines (109 loc) · 2.85 KB
/
example.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
123
124
125
126
127
128
129
130
131
132
133
134
135
import data from './sample-data';
import React from 'react';
import { render } from 'react-dom';
import SelectableGroup from './selectable-group';
import createSelectable from './createSelectable';
const isNodeInRoot = (node, root) => {
while (node) {
if (node === root) {
return true;
}
node = node.parentNode;
}
return false;
};
class App extends React.Component {
constructor (props) {
super(props);
this.state = {
selectedItems: [],
tolerance: 0,
distance: 0,
}
this.handleSelection = this.handleSelection.bind(this);
this.clearItems = this.clearItems.bind(this);
this.handleToleranceChange = this.handleToleranceChange.bind(this);
}
componentDidMount () {
document.addEventListener('click', this.clearItems);
}
componentWillUnmount () {
document.removeEventListener('click', this.clearItems);
}
handleSelection (keys) {
this.setState({
selectedItems: keys
});
}
clearItems (e) {
if(!isNodeInRoot(e.target, this.refs.selectable)) {
this.setState({
selectedItems: []
});
}
}
handleToleranceChange (e) {
this.setState({
tolerance: e.target.value
});
}
render () {
return (
<div>
<h1>React Selectable Demo</h1>
<div className="sidebar">
<div className="info">
<strong>Tolerance</strong>: <span>{this.state.tolerance}</span><br/>
<em>The number of pixels that must be in the bounding box in order for an item to be selected.</em>
<p><input type="range" min="0" max="50" step="1" onChange={this.handleToleranceChange} value={this.state.tolerance} /></p>
{this.state.selectedItems.length > 0 &&
<h3>You have selected the following items:</h3>
}
{this.state.selectedItems.length === 0 &&
<p>Please select some items from the right by clicking and dragging a box around them.</p>
}
<ul>
{this.state.selectedItems.map(function (key,i) {
return <li key={i}>{this.props.items[key].title}</li>
}.bind(this))}
</ul>
</div>
</div>
<SelectableGroup
className="main"
ref="selectable"
onSelection={this.handleSelection}
tolerance={this.state.tolerance}
globalMouse={this.state.isGlobal}
distance={this.state.distance}>
{this.props.items.map((item, i) => {
const selected = this.state.selectedItems.indexOf(i) > -1;
return (
<SelectableItem
selectableKey={i}
key={i}
title={item.title}
year={item.year}
selected={selected} />
);
})}
</SelectableGroup>
</div>
);
}
}
const Item = ({
selected,
title,
year
}) => {
const classes = selected ? 'item selected' : 'item';
return (
<div className={classes}>
<h2>{title}</h2>
<small>{year}</small>
</div>
)
};
const SelectableItem = createSelectable(Item);
render(<App items={data} />, document.getElementById('app'));