Skip to content

Commit 53e8230

Browse files
bvaughnSTRML
authored andcommitted
Added support for custom dragging and dragged class names (#178)
Added :defaultClassName, :defaultClassNameDragging, and :defaultClassNameDragged properties to Draggable. Updated docs and tests
1 parent ac26943 commit 53e8230

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ bounds: {left: number, top: number, right: number, bottom: number} | string,
166166
// Example: '.body'
167167
cancel: string,
168168

169+
// Class names for draggable UI.
170+
// Default to 'react-draggable', 'react-draggable-dragging', and 'react-draggable-dragged'
171+
defaultClassName: string,
172+
defaultClassNameDragging: string,
173+
defaultClassNameDragged: string,
174+
169175
// Specifies the `x` and `y` that the dragged item should start at.
170176
// This is generally not necessary to use (you can use absolute or relative
171177
// positioning of the child directly), but can be helpful for uniformity in

lib/Draggable.es6

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ export default class Draggable extends React.Component {
8787
PropTypes.oneOf([false])
8888
]),
8989

90+
defaultClassName: PropTypes.string,
91+
defaultClassNameDragging: PropTypes.string,
92+
defaultClassNameDragged: PropTypes.string,
93+
9094
/**
9195
* `defaultPosition` specifies the x and y that the dragged item should start at
9296
*
@@ -146,6 +150,9 @@ export default class Draggable extends React.Component {
146150
...DraggableCore.defaultProps,
147151
axis: 'both',
148152
bounds: false,
153+
defaultClassName: 'react-draggable',
154+
defaultClassNameDragging: 'react-draggable-dragging',
155+
defaultClassNameDragged: 'react-draggable-dragged',
149156
defaultPosition: {x: 0, y: 0},
150157
position: null
151158
};
@@ -319,10 +326,16 @@ export default class Draggable extends React.Component {
319326
style = createCSSTransform(transformOpts);
320327
}
321328

329+
const {
330+
defaultClassName,
331+
defaultClassNameDragging,
332+
defaultClassNameDragged
333+
} = this.props;
334+
322335
// Mark with class while dragging
323-
const className = classNames((this.props.children.props.className || ''), 'react-draggable', {
324-
'react-draggable-dragging': this.state.dragging,
325-
'react-draggable-dragged': this.state.dragged
336+
const className = classNames((this.props.children.props.className || ''), defaultClassName, {
337+
[defaultClassNameDragging]: this.state.dragging,
338+
[defaultClassNameDragged]: this.state.dragged
326339
});
327340

328341
// Reuse the child provided

specs/draggable.spec.jsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ describe('react-draggable', function () {
5959
assert(node.getAttribute('class') === 'foo react-draggable');
6060
});
6161

62+
it('should set the appropriate custom className when dragging or dragged', function () {
63+
drag = TestUtils.renderIntoDocument(
64+
<Draggable
65+
defaultClassName='foo'
66+
defaultClassNameDragging='bar'
67+
defaultClassNameDragged='baz'
68+
>
69+
<div/>
70+
</Draggable>
71+
);
72+
var node = ReactDOM.findDOMNode(drag);
73+
assert(node.getAttribute('class').indexOf('foo') >= 0);
74+
TestUtils.Simulate.mouseDown(node);
75+
assert(node.getAttribute('class').indexOf('bar') >= 0);
76+
TestUtils.Simulate.mouseUp(node);
77+
assert(node.getAttribute('class').indexOf('baz') >= 0);
78+
});
79+
6280
// NOTE: this runs a shallow renderer, which DOES NOT actually render <DraggableCore>
6381
it('should pass handle on to <DraggableCore>', function () {
6482
drag = <Draggable handle=".foo"><div /></Draggable>;

0 commit comments

Comments
 (0)