Skip to content

Commit 1b1f16c

Browse files
committed
Polish examples
1 parent 78de6a0 commit 1b1f16c

File tree

13 files changed

+172
-137
lines changed

13 files changed

+172
-137
lines changed
File renamed without changes.

example/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react'
2+
import { render } from 'react-dom'
3+
4+
import App from './src/App'
5+
render(
6+
<App />,
7+
document.getElementById('root')
8+
)

example/npm-debug.log

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
0 info it worked if it ends with ok
2+
1 verbose cli [ '/usr/bin/nodejs', '/usr/bin/npm', 'run', 'lint' ]
3+
2 info using npm@2.15.1
4+
3 info using node@v4.4.3
5+
4 verbose stack Error: missing script: lint
6+
4 verbose stack at run (/usr/lib/node_modules/npm/lib/run-script.js:142:19)
7+
4 verbose stack at /usr/lib/node_modules/npm/lib/run-script.js:58:5
8+
4 verbose stack at /usr/lib/node_modules/npm/node_modules/read-package-json/read-json.js:345:5
9+
4 verbose stack at checkBinReferences_ (/usr/lib/node_modules/npm/node_modules/read-package-json/read-json.js:309:45)
10+
4 verbose stack at final (/usr/lib/node_modules/npm/node_modules/read-package-json/read-json.js:343:3)
11+
4 verbose stack at then (/usr/lib/node_modules/npm/node_modules/read-package-json/read-json.js:113:5)
12+
4 verbose stack at ReadFileContext.<anonymous> (/usr/lib/node_modules/npm/node_modules/read-package-json/read-json.js:284:20)
13+
4 verbose stack at ReadFileContext.callback (/usr/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:78:16)
14+
4 verbose stack at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:303:13)
15+
5 verbose cwd /home/zyzo/Projects/react-dnd-mouse-backend/example
16+
6 error Linux 4.2.0-35-generic
17+
7 error argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "lint"
18+
8 error node v4.4.3
19+
9 error npm v2.15.1
20+
10 error missing script: lint
21+
11 error If you need help, you may report this error at:
22+
11 error <https://github.com/npm/npm/issues>
23+
12 verbose exit [ 1, true ]

example/src/App.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react'
2+
import { DragDropContext } from 'react-dnd'
3+
import MouseBackend from 'react-dnd-mouse-backend'
4+
import CSV from './CSV'
5+
import NormalDiv from './NormalDiv'
6+
7+
const App = React.createClass({
8+
render() {
9+
return (
10+
<div>
11+
<h3>CSV Elements</h3>
12+
<CSV/>
13+
<h3>Normal Div</h3>
14+
<NormalDiv/>
15+
</div>
16+
)
17+
}
18+
})
19+
20+
export default DragDropContext(MouseBackend)(App)

example/src/CSV/Source.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import React, { Component, PropTypes } from 'react'
1+
import React, { PropTypes } from 'react'
22
import ItemTypes from '../ItemTypes'
33
import { DragSource } from 'react-dnd'
4-
import { connect} from 'react-redux'
5-
const style = {
6-
position: 'absolute',
7-
border: '1px dashed gray',
8-
backgroundColor: 'white',
9-
padding: '0.5rem 1rem',
10-
cursor: 'move',
11-
}
4+
5+
const rand0To255 = () => Math.floor(Math.random() * 256)
6+
const randomColor = () =>
7+
`rgb(${rand0To255()}, ${rand0To255()}, ${rand0To255()})`
128

139
const boxSource = {
1410
beginDrag(props) {
@@ -17,33 +13,37 @@ const boxSource = {
1713
}
1814
}
1915

20-
class Source extends Component {
16+
const Source = React.createClass({
17+
propTypes: {
18+
connectDragSource: PropTypes.func.isRequired,
19+
isDragging: PropTypes.bool.isRequired,
20+
id: PropTypes.any.isRequired,
21+
left: PropTypes.number.isRequired,
22+
top: PropTypes.number.isRequired,
23+
hideSourceOnDrag: PropTypes.bool.isRequired,
24+
children: PropTypes.node
25+
},
2126

2227
render() {
2328
const {
24-
hideSourceOnDrag, left, top, connectDragSource, isDragging, children
29+
hideSourceOnDrag, left, top, connectDragSource, isDragging
2530
} = this.props
2631
if (isDragging && hideSourceOnDrag) {
2732
return null
2833
}
2934

3035
return connectDragSource(
31-
<circle cx={left} cy={top} r={12} fill="black"/>
36+
<circle cx={left} cy={top} r={12} fill={randomColor()} stroke="black"
37+
strokeWidth={2}/>
3238
)
3339
}
34-
}
40+
})
3541

36-
Source.propTypes = {
37-
connectDragSource: PropTypes.func.isRequired,
38-
isDragging: PropTypes.bool.isRequired,
39-
id: PropTypes.any.isRequired,
40-
left: PropTypes.number.isRequired,
41-
top: PropTypes.number.isRequired,
42-
hideSourceOnDrag: PropTypes.bool.isRequired,
43-
children: PropTypes.node
44-
}
42+
const connect = (connect, monitor) => (
43+
{
44+
connectDragSource: connect.dragSource(),
45+
isDragging: monitor.isDragging()
46+
}
47+
)
4548

46-
export default DragSource(ItemTypes.CSV, boxSource, (connect, monitor) => ({
47-
connectDragSource: connect.dragSource(),
48-
isDragging: monitor.isDragging()
49-
}))(connect()(Source))
49+
export default DragSource(ItemTypes.CSV, boxSource, connect)(Source)

example/src/CSV/Target.js

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import React, { Component, PropTypes } from 'react'
1+
import React, { PropTypes } from 'react'
22
import update from 'react/lib/update'
33
import ItemTypes from '../ItemTypes'
44
import Source from './Source'
5-
import { DropTarget, DragDropContext } from 'react-dnd'
6-
import MouseBackend from 'react-dnd-mouse-backend'
7-
import Touch from 'react-dnd-touch-backend'
8-
import { connect} from 'react-redux'
5+
import { DropTarget } from 'react-dnd'
6+
97
const styles = {
108
width: 300,
119
height: 300,
@@ -24,17 +22,16 @@ const boxTarget = {
2422
}
2523
}
2624

27-
class Container extends Component {
28-
29-
constructor(props) {
30-
super(props)
31-
this.state = {
32-
boxes: {
33-
'a': { top: 20, left: 80, title: 'Drag me around' },
34-
'b': { top: 180, left: 20, title: 'Drag me too' }
25+
const Target = React.createClass({
26+
getInitialState() {
27+
return {
28+
circles: {
29+
'a': { top: 20, left: 80 },
30+
'b': { top: 180, left: 20 },
31+
'c': { top: 130, left: 250 }
3532
}
3633
}
37-
}
34+
},
3835

3936
moveBox(id, left, top) {
4037
this.setState(update(this.state, {
@@ -47,44 +44,36 @@ class Container extends Component {
4744
}
4845
}
4946
}))
50-
}
47+
},
5148

5249
render() {
5350
const { hideSourceOnDrag, connectDropTarget } = this.props
54-
const { boxes} = this.state
51+
const { circles } = this.state
5552

5653
return connectDropTarget(
5754
<div style={styles}>
58-
<div style={{height: '50%'}}>
59-
<svg><Source left={12} top={13}>adz</Source></svg>
60-
</div>
61-
<div style={{height: '50%'}}>
6255
<svg style={{width:'100%', height:'100%'}}>
63-
{Object.keys(boxes).map(key => {
64-
const { left, top, title } = boxes[key]
56+
{Object.keys(circles).map(key => {
57+
const { left, top } = circles[key]
6558
return (
6659
<Source key={key}
6760
id={key}
6861
left={left}
6962
top={top}
70-
hideSourceOnDrag={hideSourceOnDrag}>
71-
{title}
72-
</Source>
63+
hideSourceOnDrag={hideSourceOnDrag} />
7364
)
7465
})}
7566
</svg>
76-
</div>
7767
</div>
7868
)
7969
}
80-
}
70+
})
8171

82-
Container.propTypes = {
72+
Target.propTypes = {
8373
hideSourceOnDrag: PropTypes.bool.isRequired,
8474
connectDropTarget: PropTypes.func.isRequired
8575
}
8676

87-
export default connect()(DragDropContext(MouseBackend)(
88-
DropTarget(ItemTypes.CSV, boxTarget, connect => ({
77+
export default DropTarget(ItemTypes.CSV, boxTarget, connect => ({
8978
connectDropTarget: connect.dropTarget()
90-
}))(Container)))
79+
}))(Target)

example/src/CSV/index.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
11
/* eslint-disable max-len */
2-
import React, { Component } from 'react'
2+
import React from 'react'
33

4-
import Container from './Target'
5-
import Source from './Source'
4+
import Target from './Target'
65

7-
export default class DragAroundCSV extends Component {
8-
constructor(props) {
9-
super(props)
10-
this.handleHideSourceClick = this.handleHideSourceClick.bind(this)
11-
this.state = {
6+
const DragAroundCSV = React.createClass({
7+
getInitialState() {
8+
return {
129
hideSourceOnDrag: true
1310
}
14-
}
11+
},
1512

1613
handleHideSourceClick() {
1714
this.setState({
1815
hideSourceOnDrag: !this.state.hideSourceOnDrag
1916
})
20-
}
17+
},
2118

2219
render() {
2320
const { hideSourceOnDrag } = this.state
2421

2522
return (
2623
<div>
27-
<Container hideSourceOnDrag={hideSourceOnDrag} />
24+
<Target hideSourceOnDrag={hideSourceOnDrag} />
2825
<p>
2926
<label>
3027
<input type="checkbox"
3128
checked={hideSourceOnDrag}
3229
onChange={this.handleHideSourceClick} />
33-
<small>Hide the source item while dragging</small>
30+
<small>Hide source while dragging</small>
3431
</label>
3532
</p>
3633
</div>
3734
)
3835
}
39-
}
36+
})
37+
38+
export default DragAroundCSV

example/src/Naive/Box.js renamed to example/src/NormalDiv/Source.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component, PropTypes } from 'react'
1+
import React, { PropTypes } from 'react'
22
import ItemTypes from '../ItemTypes'
33
import { DragSource } from 'react-dnd'
44

@@ -17,8 +17,16 @@ const boxSource = {
1717
}
1818
}
1919

20-
class Box extends Component {
21-
20+
const Source = React.createClass({
21+
propTypes: {
22+
connectDragSource: PropTypes.func.isRequired,
23+
isDragging: PropTypes.bool.isRequired,
24+
id: PropTypes.any.isRequired,
25+
left: PropTypes.number.isRequired,
26+
top: PropTypes.number.isRequired,
27+
hideSourceOnDrag: PropTypes.bool.isRequired,
28+
children: PropTypes.node
29+
},
2230
render() {
2331
const {
2432
hideSourceOnDrag, left, top, connectDragSource, isDragging, children
@@ -33,19 +41,9 @@ class Box extends Component {
3341
</div>
3442
)
3543
}
36-
}
37-
38-
Box.propTypes = {
39-
connectDragSource: PropTypes.func.isRequired,
40-
isDragging: PropTypes.bool.isRequired,
41-
id: PropTypes.any.isRequired,
42-
left: PropTypes.number.isRequired,
43-
top: PropTypes.number.isRequired,
44-
hideSourceOnDrag: PropTypes.bool.isRequired,
45-
children: PropTypes.node
46-
}
44+
})
4745

4846
export default DragSource(ItemTypes.BOX, boxSource, (connect, monitor) => ({
4947
connectDragSource: connect.dragSource(),
5048
isDragging: monitor.isDragging()
51-
}))(Box)
49+
}))(Source)

0 commit comments

Comments
 (0)