Skip to content

Commit bb020a4

Browse files
committed
Wire up & test disabled prop.
1 parent 1b8ea93 commit bb020a4

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

lib/Draggable.es6

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,6 @@ export default class Draggable extends DraggableCore {
6464
PropTypes.oneOf(['parent', false])
6565
]),
6666

67-
/**
68-
* `disabled`, if true, stops the <Draggable> from dragging. It will stay where it is.
69-
*
70-
* Example:
71-
*
72-
* ```jsx
73-
* let App = React.createClass({
74-
* render: function () {
75-
* return (
76-
* <Draggable disabled={true}>
77-
* <div>I can't be dragged</div>
78-
* </Draggable>
79-
* );
80-
* }
81-
* });
82-
* ```
83-
*/
84-
disabled: PropTypes.bool,
85-
8667
/**
8768
* `grid` specifies the x and y that dragging should snap to.
8869
*
@@ -154,7 +135,6 @@ export default class Draggable extends DraggableCore {
154135
static defaultProps = assign({}, DraggableCore.defaultProps, {
155136
axis: 'both',
156137
bounds: false,
157-
disabled: false,
158138
grid: null,
159139
start: {x: 0, y: 0},
160140
zIndex: NaN

lib/DraggableCore.es6

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,26 @@ export default class DraggableCore extends React.Component {
3434
static displayName = 'DraggableCore';
3535

3636
static propTypes = {
37+
/**
38+
* `disabled`, if true, stops the <Draggable> from dragging. All handlers,
39+
* with the exception of `onMouseDown`, will not fire.
40+
*
41+
* Example:
42+
*
43+
* ```jsx
44+
* let App = React.createClass({
45+
* render: function () {
46+
* return (
47+
* <Draggable disabled={true}>
48+
* <div>I can't be dragged</div>
49+
* </Draggable>
50+
* );
51+
* }
52+
* });
53+
* ```
54+
*/
55+
disabled: PropTypes.bool,
56+
3757
/**
3858
* By default, we add 'user-select:none' attributes to the document body
3959
* to prevent ugly text selection during drag. If this is causing problems
@@ -162,9 +182,10 @@ export default class DraggableCore extends React.Component {
162182
};
163183

164184
static defaultProps = {
165-
handle: null,
166185
cancel: null,
186+
disabled: false,
167187
enableUserSelectHack: true,
188+
handle: null,
168189
transform: null,
169190
onStart: function(){},
170191
onDrag: function(){},
@@ -193,7 +214,8 @@ export default class DraggableCore extends React.Component {
193214
this.props.onMouseDown(e);
194215

195216
// Short circuit if handle or cancel prop was provided and selector doesn't match.
196-
if ((this.props.handle && !matchesSelector(e.target, this.props.handle)) ||
217+
if (this.props.disabled ||
218+
(this.props.handle && !matchesSelector(e.target, this.props.handle)) ||
197219
(this.props.cancel && matchesSelector(e.target, this.props.cancel))) {
198220
return;
199221
}

specs/draggable.spec.jsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,18 @@ describe('react-draggable', function () {
151151
expect(called).toEqual(true);
152152
});
153153

154+
it('should not call onStart when dragging begins and disabled', function () {
155+
var called = false;
156+
drag = TestUtils.renderIntoDocument(
157+
<Draggable onStart={function () { called = true; }} disabled={true}>
158+
<div/>
159+
</Draggable>
160+
);
161+
162+
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(drag));
163+
expect(called).toEqual(false);
164+
});
165+
154166
it('should render with style translate() for DOM nodes', function () {
155167
var dragged = false;
156168
drag = TestUtils.renderIntoDocument(

0 commit comments

Comments
 (0)