Skip to content

Commit 5d08c62

Browse files
author
Dang Hai An
committed
example: add MultiDropTargets examples
1 parent e909c80 commit 5d08c62

File tree

10 files changed

+251
-11
lines changed

10 files changed

+251
-11
lines changed

example/src/App.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { DragDropContext } from 'react-dnd'
33
import MouseBackend from 'react-dnd-mouse-backend'
44
import CSV from './CSV'
55
import NormalDiv from './NormalDiv'
6+
import MultipleTargets from './MultipleTargets'
67

78
const App = React.createClass({
89
render() {
910
return (
10-
<div>
11-
<h3>CSV Elements</h3>
11+
<div style={{ display: 'flex', height: '50vh', justifyContent: 'space-between' }}>
1212
<CSV/>
13-
<h3>Normal Div</h3>
1413
<NormalDiv/>
14+
<MultipleTargets />
1515
</div>
1616
)
1717
}

example/src/CSV/Target.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const styles = {
88
width: 300,
99
height: 300,
1010
border: '1px solid black',
11-
position: 'relative'
11+
position: 'relative',
12+
flex: 1
1213
}
1314

1415
const boxTarget = {
@@ -74,6 +75,7 @@ Target.propTypes = {
7475
connectDropTarget: PropTypes.func.isRequired
7576
}
7677

77-
export default DropTarget(ItemTypes.CSV, boxTarget, connect => ({
78-
connectDropTarget: connect.dropTarget()
78+
export default DropTarget(ItemTypes.CSV, boxTarget, (connect, monitor) => ({
79+
connectDropTarget: connect.dropTarget(),
80+
isOver: monitor.isOver(),
7981
}))(Target)

example/src/CSV/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const DragAroundCSV = React.createClass({
2020
const { hideSourceOnDrag } = this.state
2121

2222
return (
23-
<div>
23+
<div><div style={{ display: 'flex', flexDirection: 'column', flex: 1 }}>
24+
<h3>CSV Elements</h3>
2425
<Target hideSourceOnDrag={hideSourceOnDrag} />
2526
<p>
2627
<label>
@@ -30,7 +31,7 @@ const DragAroundCSV = React.createClass({
3031
<small>Hide source while dragging</small>
3132
</label>
3233
</p>
33-
</div>
34+
</div></div>
3435
)
3536
}
3637
})

example/src/MultipleTargets/Source.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React, { PropTypes } from 'react'
2+
import ItemTypes from '../ItemTypes'
3+
import { DragSource } from 'react-dnd'
4+
5+
const style = {
6+
position: 'absolute',
7+
border: '1px dashed gray',
8+
backgroundColor: 'white',
9+
padding: '4px',
10+
cursor: 'move',
11+
zIndex: 1,
12+
cursor: 'pointer',
13+
userSelect: 'none'
14+
}
15+
16+
const boxSource = {
17+
beginDrag(props) {
18+
const { id, left, top } = props
19+
return { id, left, top }
20+
}
21+
}
22+
23+
const Source = React.createClass({
24+
propTypes: {
25+
connectDragSource: PropTypes.func.isRequired,
26+
isDragging: PropTypes.bool.isRequired,
27+
id: PropTypes.any.isRequired,
28+
left: PropTypes.number.isRequired,
29+
top: PropTypes.number.isRequired,
30+
hideSourceOnDrag: PropTypes.bool.isRequired,
31+
children: PropTypes.node
32+
},
33+
render() {
34+
const {
35+
hideSourceOnDrag, left, top, connectDragSource, isDragging, children
36+
} = this.props
37+
if (isDragging && hideSourceOnDrag) {
38+
return null
39+
}
40+
41+
return connectDragSource(
42+
<div style={{ ...style, left, top }}>
43+
{children}
44+
</div>
45+
)
46+
}
47+
})
48+
49+
export default DragSource(ItemTypes.BOX, boxSource, (connect, monitor) => ({
50+
connectDragSource: connect.dragSource(),
51+
isDragging: monitor.isDragging()
52+
}))(Source)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from 'react'
2+
import Source from './Source'
3+
4+
const Sources = React.createClass({
5+
6+
render() {
7+
const { hideSourceOnDrag, boxes } = this.props
8+
9+
return (
10+
<div>
11+
{Object.keys(boxes).map(key => {
12+
const { left, top, title } = boxes[key]
13+
return (
14+
<Source key={key}
15+
id={key}
16+
left={left}
17+
top={top}
18+
hideSourceOnDrag={hideSourceOnDrag}>
19+
{title}
20+
</Source>
21+
)
22+
})}
23+
</div>
24+
)
25+
}
26+
})
27+
28+
export default Sources

example/src/MultipleTargets/Target.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React, { PropTypes } from 'react'
2+
import ItemTypes from '../ItemTypes'
3+
import { DropTarget } from 'react-dnd'
4+
5+
const styles = {
6+
position: 'absolute',
7+
width: 80,
8+
height: 80,
9+
border: '1px solid #ccc',
10+
flex: 1
11+
}
12+
13+
const boxTarget = {
14+
drop(props, monitor, component) {
15+
const item = monitor.getItem()
16+
const delta = monitor.getDifferenceFromInitialOffset()
17+
const left = Math.round(item.left + delta.x)
18+
const top = Math.round(item.top + delta.y)
19+
20+
component.moveBox(item.id, left, top)
21+
}
22+
}
23+
24+
const Target = React.createClass({
25+
moveBox(id, left, top) {
26+
if (this.props.isOccupied()) {
27+
alert('This box is occupied !')
28+
return
29+
}
30+
this.props.moveBox(id, left, top, this.props.id)
31+
},
32+
render() {
33+
const { connectDropTarget, left, top } = this.props
34+
35+
return connectDropTarget(
36+
<div style={{ ...styles, left, top }}>
37+
38+
</div>
39+
)
40+
}
41+
})
42+
43+
Target.propTypes = {
44+
connectDropTarget: PropTypes.func.isRequired
45+
}
46+
47+
export default DropTarget(ItemTypes.BOX, boxTarget, connect => ({
48+
connectDropTarget: connect.dropTarget()
49+
}))(Target)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react'
2+
import Target from './Target'
3+
4+
const Targets = React.createClass({
5+
6+
render() {
7+
const { moveBox, isOccupied, } = this.props
8+
const targets = [{
9+
left: 10, top: 10, id: '1'
10+
}, {
11+
left: 10, top: 205, id: '2'
12+
}, {
13+
left: 205, top: 205, id: '3'
14+
}, {
15+
left: 205, top: 10, id: '4'
16+
}]
17+
18+
return (
19+
<div>
20+
{targets.map(target => {
21+
return (
22+
<Target
23+
moveBox={moveBox} key={target.id} left={target.left} top={target.top}
24+
id={target.id}
25+
isOccupied={() => isOccupied(target.id)}
26+
/>
27+
)
28+
})}
29+
</div>
30+
)
31+
}
32+
})
33+
34+
export default Targets

example/src/MultipleTargets/index.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* eslint-disable max-len */
2+
import React from 'react'
3+
import update from 'react/lib/update'
4+
5+
import Sources from './Sources'
6+
import Targets from './Targets'
7+
8+
const DragAroundNaive = React.createClass({
9+
getInitialState() {
10+
return {
11+
boxes: {
12+
'a': { top: 50, left: 140, title: '0' },
13+
'b': { top: 180, left: 20, title: '0' },
14+
'c': { top: 90, left: 40, title: '0' },
15+
'd': { top: 230, left: 160, title: '0' },
16+
'e': { top: 140, left: 150, title: '0' }
17+
},
18+
hideSourceOnDrag: true
19+
}
20+
},
21+
22+
moveBox(id, left, top, title) {
23+
this.setState(update(this.state, {
24+
boxes: {
25+
[id]: {
26+
$merge: {
27+
left: left,
28+
top: top,
29+
title: title
30+
}
31+
}
32+
}
33+
}))
34+
},
35+
36+
isOccupied(id) {
37+
return Object.values(this.state.boxes).filter(box => box.title === id).length >= 1
38+
},
39+
40+
handleHideSourceClick() {
41+
this.setState({
42+
hideSourceOnDrag: !this.state.hideSourceOnDrag
43+
})
44+
},
45+
46+
render() {
47+
const { hideSourceOnDrag, boxes } = this.state
48+
49+
return (
50+
<div><div style={{
51+
display: 'flex', flexDirection: 'column', flex: 1,
52+
}}>
53+
<h3>Multiple Drop Targets</h3>
54+
<div style={{ position: 'relative', width: '300px',
55+
height: '300px', border: '1px solid black' }}>
56+
<Sources hideSourceOnDrag={hideSourceOnDrag} boxes={boxes} />
57+
<Targets moveBox={this.moveBox} isOccupied={this.isOccupied} />
58+
</div>
59+
<p>
60+
<label>
61+
<input type="checkbox"
62+
checked={hideSourceOnDrag}
63+
onChange={this.handleHideSourceClick} />
64+
<small>Hide source while dragging</small>
65+
</label>
66+
</p>
67+
</div></div>
68+
)
69+
}
70+
})
71+
72+
export default DragAroundNaive

example/src/NormalDiv/Target.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const styles = {
88
width: 300,
99
height: 300,
1010
border: '1px solid black',
11-
position: 'relative'
11+
position: 'relative',
12+
flex: 1
1213
}
1314

1415
const boxTarget = {

example/src/NormalDiv/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ const DragAroundNaive = React.createClass({
1919
const { hideSourceOnDrag } = this.state
2020

2121
return (
22-
<div>
22+
<div><div style={{ display: 'flex', flexDirection: 'column', flex: 1 }}>
23+
<h3>Normal Div</h3>
2324
<Target hideSourceOnDrag={hideSourceOnDrag} />
2425
<p>
2526
<label>
@@ -29,7 +30,7 @@ const DragAroundNaive = React.createClass({
2930
<small>Hide source while dragging</small>
3031
</label>
3132
</p>
32-
</div>
33+
</div></div>
3334
)
3435
}
3536
})

0 commit comments

Comments
 (0)