Skip to content

Commit c9091f3

Browse files
committed
feat: Make the DnD library work as part of the component. Expose it to make it importable
1 parent 66ae127 commit c9091f3

File tree

7 files changed

+326
-78
lines changed

7 files changed

+326
-78
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"lodash": "^4.17.4",
4444
"prop-types": "^15.6.0",
4545
"react-redux": "^5.0.3",
46-
"react-smooth-dnd": "0.4.4",
46+
"smooth-dnd": "0.6.2",
4747
"redux": "^3.6.0",
4848
"redux-actions": "^1.2.2",
4949
"redux-logger": "^3.0.6",
@@ -91,14 +91,14 @@
9191
"jest-cli": "^21.0.1",
9292
"jsdom": "^9.12.0",
9393
"mocha": "^3.4.2",
94-
"node-sass": "^4.5.3",
94+
"node-sass": "4.9.3",
9595
"prettier": "1.10.2",
9696
"pretty-quick": "^1.4.1",
9797
"react": "^16.2.0",
9898
"react-addons-test-utils": "^15.5.1",
9999
"react-dom": "^16.2.0",
100100
"react-test-renderer": "^15.4.2",
101-
"sass-loader": "^6.0.6",
101+
"sass-loader": "7.1.0",
102102
"semantic-release": "^6.3.6",
103103
"style-loader": "^0.18.2",
104104
"webpack": "^3.0.0"

src/components/BoardContainer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {BoardDiv} from '../styles/Base'
66
import {bindActionCreators} from 'redux'
77
import {connect} from 'react-redux'
88
import Lane from './Lane'
9-
import {Container, Draggable} from 'react-smooth-dnd'
10-
9+
import Container from '../dnd/Container'
10+
import Draggable from '../dnd/Draggable'
1111
import * as boardActions from '../actions/BoardActions'
1212
import * as laneActions from '../actions/LaneActions'
1313

src/components/Lane.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import PropTypes from 'prop-types'
33
import {bindActionCreators} from 'redux'
44
import {connect} from 'react-redux'
55
import isEqual from 'lodash/isEqual'
6-
import {Container, Draggable} from 'react-smooth-dnd'
6+
import Container from '../dnd/Container'
7+
import Draggable from '../dnd/Draggable'
78
import uuidv1 from 'uuid/v1'
89

910
import Loader from './Loader'

src/dnd/Container.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import React, { Component } from 'react';
2+
import ReactDOM from 'react-dom'
3+
import PropTypes from 'prop-types';
4+
import container from 'smooth-dnd';
5+
import { dropHandlers } from 'smooth-dnd';
6+
import Draggable from './Draggable';
7+
8+
container.dropHandler = dropHandlers.reactDropHandler().handler;
9+
container.wrapChild = p => p; // dont wrap children they will already be wrapped
10+
11+
class Container extends Component {
12+
constructor(props) {
13+
super(props);
14+
this.getContainerOptions = this.getContainerOptions.bind(this);
15+
this.setRef = this.setRef.bind(this);
16+
this.prevContainer = null;
17+
}
18+
19+
componentDidMount() {
20+
this.containerDiv = this.containerDiv || ReactDOM.findDOMNode(this);
21+
this.prevContainer = this.containerDiv;
22+
this.container = container(this.containerDiv, this.getContainerOptions(this.props));
23+
}
24+
25+
componentWillUnmount() {
26+
this.container.dispose();
27+
this.container = null;
28+
}
29+
30+
componentDidUpdate() {
31+
this.containerDiv = this.containerDiv || ReactDOM.findDOMNode(this);
32+
if (this.containerDiv) {
33+
if (this.prevContainer && this.prevContainer !== this.containerDiv) {
34+
this.container.dispose();
35+
this.container = container(this.containerDiv, this.getContainerOptions(this.props));
36+
this.prevContainer = this.containerDiv;
37+
}
38+
}
39+
}
40+
41+
render() {
42+
if (this.props.render) {
43+
return this.props.render(this.setRef);
44+
} else {
45+
return (
46+
<div style={this.props.style} ref={this.setRef}>
47+
{this.props.children}
48+
</div>
49+
);
50+
}
51+
}
52+
53+
setRef(element) {
54+
this.containerDiv = element;
55+
}
56+
57+
getContainerOptions(props) {
58+
return Object.assign({}, props);
59+
}
60+
}
61+
62+
Container.propTypes = {
63+
behaviour: PropTypes.oneOf(["move", "copy", "drag-zone"]),
64+
groupName: PropTypes.string,
65+
orientation: PropTypes.oneOf(["horizontal", "vertical"]),
66+
style: PropTypes.object,
67+
dragHandleSelector: PropTypes.string,
68+
nonDragAreaSelector: PropTypes.string,
69+
dragBeginDelay: PropTypes.number,
70+
animationDuration: PropTypes.number,
71+
autoScrollEnabled: PropTypes.string,
72+
lockAxis: PropTypes.string,
73+
dragClass: PropTypes.string,
74+
dropClass: PropTypes.string,
75+
onDragStart: PropTypes.func,
76+
onDragEnd: PropTypes.func,
77+
onDrop: PropTypes.func,
78+
getChildPayload: PropTypes.func,
79+
shouldAnimateDrop: PropTypes.func,
80+
shouldAcceptDrop: PropTypes.func,
81+
onDragEnter: PropTypes.func,
82+
onDragLeave: PropTypes.func,
83+
render: PropTypes.func
84+
};
85+
86+
Container.defaultProps = {
87+
behaviour: 'move',
88+
orientation: 'vertical'
89+
};
90+
91+
export default Container;

src/dnd/Draggable.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
3+
import { constants } from 'smooth-dnd';
4+
const {
5+
wrapperClass
6+
} = constants;
7+
8+
class Draggable extends Component {
9+
render() {
10+
if (this.props.render) {
11+
return React.cloneElement(this.props.render(), { className: wrapperClass });
12+
}
13+
14+
const clsName = `${this.props.className ? (this.props.className + ' ') : ''}`
15+
return (
16+
<div {...this.props} className={`${clsName}${wrapperClass}`} >
17+
{this.props.children}
18+
</div>
19+
);
20+
}
21+
}
22+
23+
Draggable.propTypes = {
24+
render: PropTypes.func
25+
};
26+
27+
export default Draggable;

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import Board from './components/Board'
22
import Tag from './components/Tag'
33
import Lane from './components/Lane'
44
import Card from './components/Card'
5+
import Draggable from './dnd/Draggable'
6+
import Container from './dnd/Container'
57

68
export {Board, Tag, Lane, Card}
9+
export {Container, Draggable}
710
export default Board

0 commit comments

Comments
 (0)