-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathExample.js
90 lines (81 loc) · 1.92 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
import React from 'react';
import PropTypes from 'prop-types';
import Selection from 'react-ds';
export default class Example extends React.PureComponent {
constructor() {
super();
this.state = {
ref: null,
elRefs: [],
selectedElements: [], // track the elements that are selected
};
}
handleSelection = (indexes) => { // eslint-disable-line no-undef
this.setState({
selectedElements: indexes,
});
};
getStyle = (index) => { // eslint-disable-line no-undef
if (this.state.selectedElements.indexOf(index) > -1) {
// Selected state
return {
background: '#2185d0',
borderColor: '#2185d0',
color: 'white',
};
}
return {};
};
addElementRef = (ref) => { // eslint-disable-line no-undef
const elRefs = this.state.elRefs;
elRefs.push(ref);
this.setState({
elRefs,
});
};
renderSelection() {
if (!this.state.ref || !this.state.elRefs) {
return null;
}
return (
<Selection
target={ this.state.ref}
elements={ this.state.elRefs }
onSelectionChange={ this.handleSelection }
style={ this.props.style }
ignoreTargets={ this.props.ignoreTargets }
/>
);
}
render() {
const selectableElements = [
'one',
'another',
'hey there',
'item',
'two',
'three',
'something longer?',
'last'
];
return (
<div ref={ (ref) => { this.setState({ ref }); } } className='item-container'>
{ selectableElements.map((el, index) => (
<div
key={ el }
ref={ this.addElementRef }
style={ this.getStyle(index) }
className={ `item item-${el}` }
>
{ el }
</div>
)) }
{ this.renderSelection() }
</div>
);
}
}
Example.PropTypes = {
style: PropTypes.object,
ignoreTargets: PropTypes.array,
};