Skip to content

Commit 63b1b37

Browse files
committed
All tests passing, add typechecks, merge svg tests
1 parent 3eecf34 commit 63b1b37

File tree

8 files changed

+180
-120
lines changed

8 files changed

+180
-120
lines changed

.babelrc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
{
2-
stage: 0
2+
"stage": 0,
3+
"env": {
4+
"local": {
5+
"plugins": [
6+
"typecheck"
7+
]
8+
}
9+
}
310
}

lib/Draggable.es6

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React from 'react';
1+
import {default as React, PropTypes} from 'react';
2+
import ReactDOM from 'react-dom';
23
import classNames from 'classnames';
34
import assign from 'object-assign';
4-
import {autobind} from './utils/shims';
5-
import {createUIEvent, createCSSTransform} from './utils/domFns';
5+
import {createUIEvent, createTransform} from './utils/domFns';
66
import {canDragX, canDragY, getBoundPosition, snapToGrid} from './utils/positionFns';
77
import DraggableCore from './DraggableCore';
88
import log from './utils/log';
@@ -13,6 +13,8 @@ import log from './utils/log';
1313

1414
export default class Draggable extends DraggableCore {
1515

16+
static displayName = 'Draggable';
17+
1618
static propTypes = assign({}, DraggableCore.propTypes, {
1719
/**
1820
* `axis` determines which axis the draggable can move.
@@ -23,7 +25,7 @@ export default class Draggable extends DraggableCore {
2325
*
2426
* Defaults to 'both'.
2527
*/
26-
axis: React.PropTypes.oneOf(['both', 'x', 'y']),
28+
axis: PropTypes.oneOf(['both', 'x', 'y']),
2729

2830
/**
2931
* `bounds` determines the range of movement available to the element.
@@ -51,14 +53,14 @@ export default class Draggable extends DraggableCore {
5153
* });
5254
* ```
5355
*/
54-
bounds: React.PropTypes.oneOfType([
55-
React.PropTypes.shape({
56-
left: React.PropTypes.Number,
57-
right: React.PropTypes.Number,
58-
top: React.PropTypes.Number,
59-
bottom: React.PropTypes.Number
56+
bounds: PropTypes.oneOfType([
57+
PropTypes.shape({
58+
left: PropTypes.Number,
59+
right: PropTypes.Number,
60+
top: PropTypes.Number,
61+
bottom: PropTypes.Number
6062
}),
61-
React.PropTypes.oneOf(['parent', false])
63+
PropTypes.oneOf(['parent', false])
6264
]),
6365

6466
/**
@@ -78,7 +80,7 @@ export default class Draggable extends DraggableCore {
7880
* });
7981
* ```
8082
*/
81-
disabled: React.PropTypes.bool,
83+
disabled: PropTypes.bool,
8284

8385
/**
8486
* `grid` specifies the x and y that dragging should snap to.
@@ -97,7 +99,7 @@ export default class Draggable extends DraggableCore {
9799
* });
98100
* ```
99101
*/
100-
grid: React.PropTypes.arrayOf(React.PropTypes.number),
102+
grid: PropTypes.arrayOf(PropTypes.number),
101103

102104
/**
103105
* `start` specifies the x and y that the dragged item should start at
@@ -116,9 +118,9 @@ export default class Draggable extends DraggableCore {
116118
* });
117119
* ```
118120
*/
119-
start: React.PropTypes.shape({
120-
x: React.PropTypes.number,
121-
y: React.PropTypes.number
121+
start: PropTypes.shape({
122+
x: PropTypes.number,
123+
y: PropTypes.number
122124
}),
123125

124126
/**
@@ -138,8 +140,8 @@ export default class Draggable extends DraggableCore {
138140
* });
139141
* ```
140142
*/
141-
zIndex: React.PropTypes.number
142-
})
143+
zIndex: PropTypes.number
144+
});
143145

144146
static defaultProps = assign({}, DraggableCore.defaultProps, {
145147
axis: 'both',
@@ -148,22 +150,27 @@ export default class Draggable extends DraggableCore {
148150
grid: null,
149151
start: {x: 0, y: 0},
150152
zIndex: NaN
151-
})
153+
});
152154

153-
constructor(props) {
154-
super(props);
155-
this.state = {
156-
// Whether or not we are currently dragging.
157-
dragging: false,
155+
state = {
156+
// Whether or not we are currently dragging.
157+
dragging: false,
158158

159-
// Current transform x and y.
160-
clientX: props.start.x, clientY: props.start.y
161-
};
162-
log('Draggable: initializing state: %j, and props: %j', this.state, props);
163-
autobind(this);
159+
// Current transform x and y.
160+
clientX: this.props.start.x, clientY: this.props.start.y,
161+
162+
// Can only determine if SVG after mounting
163+
isElementSVG: false
164+
};
165+
166+
componentDidMount() {
167+
// Check to see if the element passed is an instanceof SVGElement
168+
if(ReactDOM.findDOMNode(this) instanceof SVGElement) {
169+
this.setState({ isElementSVG: true });
170+
}
164171
}
165172

166-
onDragStart(e, coreEvent) {
173+
onDragStart = (e, coreEvent) => {
167174
log('Draggable: onDragStart: %j', coreEvent.position);
168175

169176
// Short-circuit if user's callback killed it.
@@ -174,9 +181,9 @@ export default class Draggable extends DraggableCore {
174181
this.setState({
175182
dragging: true
176183
});
177-
}
184+
};
178185

179-
onDrag(e, coreEvent) {
186+
onDrag = (e, coreEvent) => {
180187
if (!this.state.dragging) return false;
181188
log('Draggable: onDrag: %j', coreEvent.position);
182189

@@ -204,9 +211,9 @@ export default class Draggable extends DraggableCore {
204211
}
205212

206213
this.setState(newState);
207-
}
214+
};
208215

209-
onDragStop(e, coreEvent) {
216+
onDragStop = (e, coreEvent) => {
210217
if (!this.state.dragging) return false;
211218

212219
// Short-circuit if user's callback killed it.
@@ -218,15 +225,15 @@ export default class Draggable extends DraggableCore {
218225
this.setState({
219226
dragging: false
220227
});
221-
}
228+
};
222229

223230
render() {
224-
231+
let style, svgTransform = null;
225232
// Add a CSS transform to move the element around. This allows us to move the element around
226233
// without worrying about whether or not it is relatively or absolutely positioned.
227234
// If the item you are dragging already has a transform set, wrap it in a <span> so <Draggable>
228235
// has a clean slate.
229-
let style = createCSSTransform({
236+
style = createTransform({
230237
// Set left if horizontal drag is enabled
231238
x: canDragX(this) ?
232239
this.state.clientX :
@@ -236,7 +243,13 @@ export default class Draggable extends DraggableCore {
236243
y: canDragY(this) ?
237244
this.state.clientY :
238245
this.props.start.y
239-
});
246+
}, this.state.isElementSVG);
247+
248+
// If this element was SVG, we use the `transform` attribute.
249+
if (this.state.isElementSVG) {
250+
svgTransform = style;
251+
style = {};
252+
}
240253

241254
// zIndex option
242255
if (this.state.dragging && !isNaN(this.props.zIndex)) {
@@ -252,7 +265,7 @@ export default class Draggable extends DraggableCore {
252265
// Reuse the child provided
253266
// This makes it flexible to use whatever element is wanted (div, ul, etc)
254267
return (
255-
<DraggableCore {...this.props} style={style} className={className}
268+
<DraggableCore {...this.props} style={style} className={className} transform={svgTransform}
256269
onStart={this.onDragStart} onDrag={this.onDrag} onStop={this.onDragStop}>
257270
{React.Children.only(this.props.children)}
258271
</DraggableCore>

0 commit comments

Comments
 (0)