Skip to content

Commit 538f987

Browse files
author
Dang Hai An
committed
doc: add with drag preview example
1 parent 8d031d4 commit 538f987

File tree

7 files changed

+197
-40
lines changed

7 files changed

+197
-40
lines changed

example/src/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import MouseBackend from 'react-dnd-mouse-backend'
44
import CSV from './CSV'
55
import NormalDiv from './NormalDiv'
66
import MultipleTargets from './MultipleTargets'
7+
import WithDragPreview from './WithDragPreview'
78

89
const App = React.createClass({
910
render() {
@@ -12,6 +13,7 @@ const App = React.createClass({
1213
<CSV/>
1314
<NormalDiv/>
1415
<MultipleTargets />
16+
<WithDragPreview />
1517
</div>
1618
)
1719
}

example/src/MultipleTargets/index.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ const DragAroundNaive = React.createClass({
1414
'c': { top: 90, left: 40, title: '0' },
1515
'd': { top: 230, left: 160, title: '0' },
1616
'e': { top: 140, left: 150, title: '0' }
17-
},
18-
hideSourceOnDrag: true
17+
}
1918
}
2019
},
2120

@@ -37,14 +36,8 @@ const DragAroundNaive = React.createClass({
3736
return Object.values(this.state.boxes).filter(box => box.title === id).length >= 1
3837
},
3938

40-
handleHideSourceClick() {
41-
this.setState({
42-
hideSourceOnDrag: !this.state.hideSourceOnDrag
43-
})
44-
},
45-
4639
render() {
47-
const { hideSourceOnDrag, boxes } = this.state
40+
const { boxes } = this.state
4841

4942
return (
5043
<div><div style={{
@@ -53,17 +46,9 @@ const DragAroundNaive = React.createClass({
5346
<h3>Multiple Drop Targets</h3>
5447
<div style={{ position: 'relative', width: '300px',
5548
height: '300px', border: '1px solid black' }}>
56-
<Sources hideSourceOnDrag={hideSourceOnDrag} boxes={boxes} />
49+
<Sources hideSourceOnDrag={true} boxes={boxes} />
5750
<Targets moveBox={this.moveBox} isOccupied={this.isOccupied} />
5851
</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>
6752
</div></div>
6853
)
6954
}

example/src/NormalDiv/index.js

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,12 @@ import React from 'react'
33
import Target from './Target'
44

55
const DragAroundNaive = React.createClass({
6-
getInitialState() {
7-
return {
8-
hideSourceOnDrag: true
9-
}
10-
},
11-
12-
handleHideSourceClick() {
13-
this.setState({
14-
hideSourceOnDrag: !this.state.hideSourceOnDrag
15-
})
16-
},
17-
186
render() {
19-
const { hideSourceOnDrag } = this.state
207

218
return (
229
<div><div style={{ display: 'flex', flexDirection: 'column', flex: 1 }}>
2310
<h3>Normal Div</h3>
24-
<Target hideSourceOnDrag={hideSourceOnDrag} />
25-
<p>
26-
<label>
27-
<input type="checkbox"
28-
checked={hideSourceOnDrag}
29-
onChange={this.handleHideSourceClick} />
30-
<small>Hide source while dragging</small>
31-
</label>
32-
</p>
11+
<Target hideSourceOnDrag={true} />
3312
</div></div>
3413
)
3514
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React from 'react'
2+
import { DragLayer } from 'react-dnd'
3+
4+
const defaultStyle = (item, currentOffset) => (
5+
{
6+
left: currentOffset.x,
7+
top: currentOffset.y,
8+
position: 'fixed'
9+
}
10+
)
11+
12+
const DragPreview = React.createClass({
13+
render() {
14+
const {
15+
isDragging,
16+
currentOffset,
17+
item
18+
} = this.props
19+
return !isDragging || !currentOffset || !item.withDragPreview ?
20+
null
21+
:
22+
<svg style={defaultStyle(item, currentOffset)}>
23+
<circle cx={14} cy={14} r={12} fill={item.color} stroke="black"
24+
strokeWidth={2}/>
25+
</svg>
26+
27+
}
28+
})
29+
30+
export default DragLayer(monitor => ({
31+
item: monitor.getItem(),
32+
itemType: monitor.getItemType(),
33+
currentOffset: monitor.getSourceClientOffset(),
34+
isDragging: monitor.isDragging()
35+
}))(DragPreview)

example/src/WithDragPreview/Source.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React, { PropTypes } from 'react'
2+
import ItemTypes from '../ItemTypes'
3+
import { DragSource } from 'react-dnd'
4+
5+
const rand0To255 = () => Math.floor(Math.random() * 256)
6+
const randomColor = () =>
7+
`rgb(${rand0To255()}, ${rand0To255()}, ${rand0To255()})`
8+
9+
const boxSource = {
10+
beginDrag(props, monitor, component) {
11+
const { id, left, top } = props
12+
return { id, left, top, color: component.state.color, withDragPreview: true }
13+
}
14+
}
15+
16+
const Source = React.createClass({
17+
getInitialState() {
18+
return {
19+
color: randomColor()
20+
}
21+
},
22+
propTypes: {
23+
connectDragSource: PropTypes.func.isRequired,
24+
isDragging: PropTypes.bool.isRequired,
25+
id: PropTypes.any.isRequired,
26+
left: PropTypes.number.isRequired,
27+
top: PropTypes.number.isRequired,
28+
hideSourceOnDrag: PropTypes.bool.isRequired,
29+
children: PropTypes.node
30+
},
31+
32+
render() {
33+
const {
34+
hideSourceOnDrag, left, top, connectDragSource, isDragging
35+
} = this.props
36+
if (isDragging && hideSourceOnDrag) {
37+
return null
38+
}
39+
40+
return connectDragSource(
41+
<circle cx={left} cy={top} r={12} fill={this.state.color} stroke="black"
42+
strokeWidth={2}/>
43+
)
44+
}
45+
})
46+
47+
const connect = (connect, monitor) => (
48+
{
49+
connectDragSource: connect.dragSource(),
50+
isDragging: monitor.isDragging()
51+
}
52+
)
53+
54+
export default DragSource(ItemTypes.CSV, boxSource, connect)(Source)

example/src/WithDragPreview/Target.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import React, { PropTypes } from 'react'
2+
import update from 'react/lib/update'
3+
import ItemTypes from '../ItemTypes'
4+
import Source from './Source'
5+
import { DropTarget } from 'react-dnd'
6+
7+
const styles = {
8+
width: 300,
9+
height: 300,
10+
border: '1px solid black',
11+
position: 'relative',
12+
flex: 1
13+
}
14+
15+
const boxTarget = {
16+
drop(props, monitor, component) {
17+
const item = monitor.getItem()
18+
const delta = monitor.getDifferenceFromInitialOffset()
19+
const left = Math.round(item.left + delta.x)
20+
const top = Math.round(item.top + delta.y)
21+
22+
component.moveBox(item.id, left, top)
23+
}
24+
}
25+
26+
const Target = React.createClass({
27+
getInitialState() {
28+
return {
29+
circles: {
30+
'a': { top: 20, left: 80 },
31+
'b': { top: 180, left: 20 },
32+
'c': { top: 130, left: 250 }
33+
}
34+
}
35+
},
36+
37+
moveBox(id, left, top) {
38+
this.setState(update(this.state, {
39+
circles: {
40+
[id]: {
41+
$merge: {
42+
left: left,
43+
top: top
44+
}
45+
}
46+
}
47+
}))
48+
},
49+
50+
render() {
51+
const { hideSourceOnDrag, connectDropTarget } = this.props
52+
const { circles } = this.state
53+
54+
return connectDropTarget(
55+
<div style={styles}>
56+
<svg style={{width:'100%', height:'100%'}}>
57+
{Object.keys(circles).map(key => {
58+
const { left, top } = circles[key]
59+
return (
60+
<Source key={key}
61+
id={key}
62+
left={left}
63+
top={top}
64+
hideSourceOnDrag={hideSourceOnDrag} />
65+
)
66+
})}
67+
</svg>
68+
</div>
69+
)
70+
}
71+
})
72+
73+
Target.propTypes = {
74+
hideSourceOnDrag: PropTypes.bool.isRequired,
75+
connectDropTarget: PropTypes.func.isRequired
76+
}
77+
78+
export default DropTarget(ItemTypes.CSV, boxTarget, (connect, monitor) => ({
79+
connectDropTarget: connect.dropTarget(),
80+
isOver: monitor.isOver(),
81+
}))(Target)

example/src/WithDragPreview/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* eslint-disable max-len */
2+
import React from 'react'
3+
4+
import DragPreview from './DragPreview'
5+
import Target from './Target'
6+
7+
const DragAroundCSV = React.createClass({
8+
9+
render() {
10+
11+
return (
12+
<div><div style={{ display: 'flex', flexDirection: 'column', flex: 1 }}>
13+
<h3>With Drag Preview</h3>
14+
<Target hideSourceOnDrag={true} />
15+
<DragPreview />
16+
</div></div>
17+
)
18+
}
19+
})
20+
21+
export default DragAroundCSV

0 commit comments

Comments
 (0)