Skip to content

Commit bb712e2

Browse files
author
Dang Hai An
committed
feat: add NestedTargets example
1 parent 927e659 commit bb712e2

File tree

14 files changed

+244
-16
lines changed

14 files changed

+244
-16
lines changed

example/src/App.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import CSV from './CSV'
55
import NormalDiv from './NormalDiv'
66
import MultipleTargets from './MultipleTargets'
77
import WithDragPreview from './WithDragPreview'
8+
import NestedTargets from './NestedTargets'
89

910
const App = React.createClass({
1011
render() {
1112
return (
12-
<div style={{ display: 'flex', height: '50vh', justifyContent: 'space-between' }}>
13-
<CSV/>
14-
<NormalDiv/>
15-
<MultipleTargets />
16-
<WithDragPreview />
13+
<div style={{
14+
display: 'flex', height: '50vh', justifyContent: 'space-between',
15+
flexWrap: 'wrap',
16+
}}>
17+
<NestedTargets />
1718
</div>
1819
)
1920
}

example/src/CSV/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const DragAroundCSV = React.createClass({
2020
const { hideSourceOnDrag } = this.state
2121

2222
return (
23-
<div><div style={{ display: 'flex', flexDirection: 'column', flex: 1 }}>
23+
<div style={{ flex: 1 }}><div style={{ display: 'flex', flexDirection: 'column', }}>
2424
<h3>CSV Elements</h3>
2525
<Target hideSourceOnDrag={hideSourceOnDrag} />
2626
<p>

example/src/MultipleTargets/Target.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ const boxTarget = {
2424
const Target = React.createClass({
2525
moveBox(id, left, top) {
2626
if (this.props.isOccupied()) {
27-
alert('This box is occupied !')
27+
setTimeout(() => {
28+
alert('This box is occupied !')
29+
}, 0)
2830
return
2931
}
3032
this.props.moveBox(id, left, top, this.props.id)
@@ -44,6 +46,6 @@ Target.propTypes = {
4446
connectDropTarget: PropTypes.func.isRequired
4547
}
4648

47-
export default DropTarget(ItemTypes.BOX, boxTarget, connect => ({
49+
export default DropTarget(ItemTypes.BOX, boxTarget, (connect) => ({
4850
connectDropTarget: connect.dropTarget()
4951
}))(Target)

example/src/MultipleTargets/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ const DragAroundNaive = React.createClass({
4040
const { boxes } = this.state
4141

4242
return (
43-
<div><div style={{
44-
display: 'flex', flexDirection: 'column', flex: 1,
43+
<div style={{ flex: 1 }}><div style={{
44+
display: 'flex', flexDirection: 'column',
4545
}}>
4646
<h3>Multiple Drop Targets</h3>
4747
<div style={{ position: 'relative', width: '300px',

example/src/NestedTargets/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)

example/src/NestedTargets/Sources.js

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/NestedTargets/Target.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
border: '1px solid #ccc',
8+
flex: 1
9+
}
10+
11+
const boxTarget = {
12+
drop(props, monitor, component) {
13+
console.log(monitor.didDrop())
14+
if (monitor.didDrop()) return
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+
component.moveBox(item.id, left, top)
20+
}
21+
}
22+
23+
const Target = React.createClass({
24+
moveBox(id, left, top) {
25+
if (this.props.isOccupied()) {
26+
alert('This box is occupied !')
27+
return
28+
}
29+
this.props.moveBox(id, left, top, this.props.id)
30+
},
31+
render() {
32+
const { connectDropTarget, left, top, id, size } = this.props
33+
34+
return connectDropTarget(
35+
<div style={{ ...styles, left, top, width: size, height: size }}>
36+
{id}
37+
</div>
38+
)
39+
}
40+
})
41+
42+
Target.propTypes = {
43+
connectDropTarget: PropTypes.func.isRequired
44+
}
45+
46+
export default DropTarget(ItemTypes.BOX, boxTarget, (connect) => ({
47+
connectDropTarget: connect.dropTarget()
48+
}))(Target)

example/src/NestedTargets/Targets.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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: 60, top: 60, size: 180, id: '2'
10+
}, {
11+
left: 30, top: 30, size: 240, id: '1'
12+
}, {
13+
left: 90, top: 90, size: 120, id: '3'
14+
}, {
15+
left: 120, top: 120, size: 60, id: '4'
16+
}, {
17+
left: 170, top: 40, size: 80, id: '5'
18+
},]
19+
20+
return (
21+
<div>
22+
{targets.map(target => {
23+
return (
24+
<Target
25+
moveBox={moveBox} key={target.id}
26+
{...target}
27+
isOccupied={() => isOccupied(target.id)}
28+
/>
29+
)
30+
})}
31+
</div>
32+
)
33+
}
34+
})
35+
36+
export default Targets

example/src/NestedTargets/index.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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: 10, title: '0' },
13+
'b': { top: 180, left: 10, title: '0' },
14+
'c': { top: 90, left: 10, title: '0' },
15+
'd': { top: 230, left: 10, title: '0' },
16+
'e': { top: 140, left: 10, title: '0' }
17+
}
18+
}
19+
},
20+
21+
moveBox(id, left, top, title) {
22+
this.setState(update(this.state, {
23+
boxes: {
24+
[id]: {
25+
$merge: {
26+
left: left,
27+
top: top,
28+
title: title
29+
}
30+
}
31+
}
32+
}))
33+
},
34+
35+
isOccupied(id) {
36+
return Object.values(this.state.boxes).filter(box => box.title === id).length >= 1
37+
},
38+
39+
render() {
40+
const { boxes } = this.state
41+
42+
return (
43+
<div style={{ flex: 1 }}><div style={{
44+
display: 'flex', flexDirection: 'column',
45+
}}>
46+
<h3>Nested Drop Targets</h3>
47+
<div style={{ position: 'relative', width: '400px', height: '300px', display: 'flex' }}>
48+
<div style={{ width: '100px' }}>
49+
<Sources hideSourceOnDrag={true} boxes={boxes} />
50+
</div>
51+
<div style={{ position: 'relative', width: '300px',
52+
height: '300px', border: '1px solid black' }}>
53+
<Targets moveBox={this.moveBox} isOccupied={this.isOccupied} />
54+
</div>
55+
</div>
56+
</div></div>
57+
)
58+
}
59+
})
60+
61+
export default DragAroundNaive

example/src/NormalDiv/Target.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,6 @@ Target.propTypes = {
7575
connectDropTarget: PropTypes.func.isRequired
7676
}
7777

78-
export default DropTarget(ItemTypes.BOX, boxTarget, connect => ({
78+
export default DropTarget(ItemTypes.BOX, boxTarget, (connect) => ({
7979
connectDropTarget: connect.dropTarget()
8080
}))(Target)

0 commit comments

Comments
 (0)