/
Item.js
87 lines (69 loc) · 1.81 KB
/
Item.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
'use strict';
import React, { PropTypes } from 'react';
import ItemTypes from './ItemTypes';
import { DragSource } from 'dnd-core';
class ItemDragSource extends DragSource {
constructor(component) {
this.component = component;
}
beginDrag() {
return {
name: this.component.props.name
};
}
endDrag(context) {
const what = context.getItem();
const where = context.getDropResult();
if (where) {
alert('You dropped ' + what.name + ' into ' + where.name + '!');
}
}
}
const style = {
border: '1px dashed gray',
backgroundColor: 'white',
padding: '0.5rem',
margin: '0.5rem',
maxWidth: 80
};
const Item = React.createClass({
propTypes: {
name: PropTypes.string.isRequired
},
getInitialState() {
return this.getDragState();
},
getDragState() {
return {
isDraggingItem: this.sourceHandle && this.props.manager.getContext().isDragging(this.sourceHandle)
};
},
componentWillMount() {
this.sourceHandle = this.props.manager.getRegistry().addSource(ItemTypes.ITEM, new ItemDragSource(this));
},
componentDidMount() {
this.props.manager.getContext().addChangeListener(this.handleDragContextChange);
},
componentWillUnmount() {
this.props.manager.getRegistry().removeSource(this.sourceHandle);
this.props.manager.getContext().removeChangeListener(this.handleDragContextChange);
},
handleDragContextChange() {
this.setState(this.getDragState());
},
render() {
const { name } = this.props;
const { isDraggingItem } = this.state;
const opacity = isDraggingItem ? 0.4 : 1;
return (
<div {...this.props.manager.getBackend().getDraggableProps(this.sourceHandle)}
style={{
...style,
opacity
}}>
{name}
</div>
);
}
});
export default Item;