Skip to content
This repository was archived by the owner on Mar 13, 2023. It is now read-only.

Commit 50fe741

Browse files
committed
add returnToDefaultPosition
1 parent 648f7fb commit 50fe741

6 files changed

Lines changed: 32 additions & 6 deletions

File tree

build/cjs/Draggable.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ var Draggable = /*#__PURE__*/function (_React$Component) {
203203
y = _this$props$position.y;
204204
newState.x = x;
205205
newState.y = y;
206-
} else {
206+
} else if (_this.props.returnToDefaultPosition) {
207207
var _this$props$defaultPo = _this.props.defaultPosition,
208208
_x = _this$props$defaultPo.x,
209209
_y = _this$props$defaultPo.y;
@@ -265,6 +265,7 @@ var Draggable = /*#__PURE__*/function (_React$Component) {
265265
var _this$props = this.props,
266266
axis = _this$props.axis,
267267
bounds = _this$props.bounds,
268+
returnToDefaultPosition = _this$props.returnToDefaultPosition,
268269
children = _this$props.children,
269270
defaultPosition = _this$props.defaultPosition,
270271
defaultClassName = _this$props.defaultClassName,
@@ -273,7 +274,7 @@ var Draggable = /*#__PURE__*/function (_React$Component) {
273274
position = _this$props.position,
274275
positionOffset = _this$props.positionOffset,
275276
scale = _this$props.scale,
276-
draggableCoreProps = _objectWithoutProperties(_this$props, ["axis", "bounds", "children", "defaultPosition", "defaultClassName", "defaultClassNameDragging", "defaultClassNameDragged", "position", "positionOffset", "scale"]);
277+
draggableCoreProps = _objectWithoutProperties(_this$props, ["axis", "bounds", "returnToDefaultPosition", "children", "defaultPosition", "defaultClassName", "defaultClassNameDragging", "defaultClassNameDragged", "position", "positionOffset", "scale"]);
277278

278279
var style = {};
279280
var svgTransform = null; // If this is controlled, we don't want to move it - unless it's dragging.
@@ -369,6 +370,11 @@ _defineProperty(Draggable, "propTypes", _objectSpread({}, _DraggableCore.default
369370
top: _propTypes.default.number,
370371
bottom: _propTypes.default.number
371372
}), _propTypes.default.string, _propTypes.default.oneOf([false])]),
373+
374+
/**
375+
* `returnToDefaultPosition` Determines if element returns to start position when drag ends.
376+
*/
377+
returnToDefaultPosition: _propTypes.default.bool,
372378
defaultClassName: _propTypes.default.string,
373379
defaultClassNameDragging: _propTypes.default.string,
374380
defaultClassNameDragged: _propTypes.default.string,
@@ -435,6 +441,8 @@ _defineProperty(Draggable, "propTypes", _objectSpread({}, _DraggableCore.default
435441
_defineProperty(Draggable, "defaultProps", _objectSpread({}, _DraggableCore.default.defaultProps, {
436442
axis: 'both',
437443
bounds: false,
444+
returnToDefaultPosition: false,
445+
dragContainer: null,
438446
defaultClassName: 'react-draggable',
439447
defaultClassNameDragging: 'react-draggable-dragging',
440448
defaultClassNameDragged: 'react-draggable-dragged',

build/module/Draggable.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import log from './utils/log';
3030
...$Exact<DraggableCoreProps>,
3131
axis: 'both' | 'x' | 'y' | 'none',
3232
bounds: DraggableBounds | string | false,
33+
returnToDefaultPosition: boolean,
3334
defaultClassName: string,
3435
defaultClassNameDragging: string,
3536
defaultClassNameDragged: string,
@@ -156,7 +157,7 @@ class Draggable extends React.Component
156157
} = this.props.position;
157158
newState.x = x;
158159
newState.y = y;
159-
} else {
160+
} else if (this.props.returnToDefaultPosition) {
160161
const {
161162
x,
162163
y
@@ -212,6 +213,7 @@ class Draggable extends React.Component
212213
const {
213214
axis,
214215
bounds,
216+
returnToDefaultPosition,
215217
children,
216218
defaultPosition,
217219
defaultClassName,
@@ -319,6 +321,11 @@ _defineProperty(Draggable, "propTypes", { // Accepts all props <DraggableCore> a
319321
top: PropTypes.number,
320322
bottom: PropTypes.number
321323
}), PropTypes.string, PropTypes.oneOf([false])]),
324+
325+
/**
326+
* `returnToDefaultPosition` Determines if element returns to start position when drag ends.
327+
*/
328+
returnToDefaultPosition: PropTypes.bool,
322329
defaultClassName: PropTypes.string,
323330
defaultClassNameDragging: PropTypes.string,
324331
defaultClassNameDragged: PropTypes.string,
@@ -385,6 +392,8 @@ _defineProperty(Draggable, "propTypes", { // Accepts all props <DraggableCore> a
385392
_defineProperty(Draggable, "defaultProps", { ...DraggableCore.defaultProps,
386393
axis: 'both',
387394
bounds: false,
395+
returnToDefaultPosition: false,
396+
dragContainer: null,
388397
defaultClassName: 'react-draggable',
389398
defaultClassNameDragging: 'react-draggable-dragging',
390399
defaultClassNameDragged: 'react-draggable-dragged',

build/web/react-draggable.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/web/react-draggable.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Draggable.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type DraggableProps = {
2525
...$Exact<DraggableCoreProps>,
2626
axis: 'both' | 'x' | 'y' | 'none',
2727
bounds: DraggableBounds | string | false,
28+
returnToDefaultPosition: boolean,
2829
defaultClassName: string,
2930
defaultClassNameDragging: string,
3031
defaultClassNameDragged: string,
@@ -98,6 +99,11 @@ class Draggable extends React.Component<DraggableProps, DraggableState> {
9899
PropTypes.oneOf([false])
99100
]),
100101

102+
/**
103+
* `returnToDefaultPosition` Determines if element returns to start position when drag ends.
104+
*/
105+
returnToDefaultPosition: PropTypes.bool,
106+
101107
defaultClassName: PropTypes.string,
102108
defaultClassNameDragging: PropTypes.string,
103109
defaultClassNameDragged: PropTypes.string,
@@ -165,6 +171,7 @@ class Draggable extends React.Component<DraggableProps, DraggableState> {
165171
...DraggableCore.defaultProps,
166172
axis: 'both',
167173
bounds: false,
174+
returnToDefaultPosition: false,
168175
defaultClassName: 'react-draggable',
169176
defaultClassNameDragging: 'react-draggable-dragging',
170177
defaultClassNameDragged: 'react-draggable-dragged',
@@ -313,7 +320,7 @@ class Draggable extends React.Component<DraggableProps, DraggableState> {
313320
const {x, y} = this.props.position;
314321
newState.x = x;
315322
newState.y = y;
316-
} else {
323+
} else if (this.props.returnToDefaultPosition) {
317324
const {x, y} = this.props.defaultPosition;
318325
newState.x = x;
319326
newState.y = y;
@@ -326,6 +333,7 @@ class Draggable extends React.Component<DraggableProps, DraggableState> {
326333
const {
327334
axis,
328335
bounds,
336+
returnToDefaultPosition,
329337
children,
330338
defaultPosition,
331339
defaultClassName,

specs/draggable.spec.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ describe('react-draggable', function () {
4343
assert(drag.props.handle === null);
4444
assert(drag.props.cancel === null);
4545
assert(drag.props.bounds == false);
46+
assert(drag.props.returnToDefaultPosition == false);
4647
assert(typeof drag.props.onStart === 'function');
4748
assert(typeof drag.props.onDrag === 'function');
4849
assert(typeof drag.props.onStop === 'function');

0 commit comments

Comments
 (0)