From fea778c8e89db2a4e1a35e563b65634f8146e7e4 Mon Sep 17 00:00:00 2001 From: Samuel Reed Date: Mon, 26 Aug 2019 13:38:47 -0500 Subject: [PATCH] fix(react): React 16.3+ compatibility using `getDerivedPropsFromState(props, state)` --- lib/Draggable.js | 79 +++++++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/lib/Draggable.js b/lib/Draggable.js index 96156fd7..f8d7e79e 100644 --- a/lib/Draggable.js +++ b/lib/Draggable.js @@ -17,7 +17,8 @@ type DraggableState = { dragged: boolean, x: number, y: number, slackX: number, slackY: number, - isElementSVG: boolean + isElementSVG: boolean, + prevPropsPosition: ?ControlPosition, }; export type DraggableProps = { @@ -172,6 +173,26 @@ export default class Draggable extends React.Component { - let style = {}, svgTransform = null; + const { + axis, + bounds, + children, + defaultPosition, + defaultClassName, + defaultClassNameDragging, + defaultClassNameDragged, + position, + positionOffset, + ...draggableCoreProps + } = this.props; + + let style = {}; + let svgTransform = null; // If this is controlled, we don't want to move it - unless it's dragging. - const controlled = Boolean(this.props.position); + const controlled = Boolean(position); const draggable = !controlled || this.state.dragging; - const position = this.props.position || this.props.defaultPosition; + const validPosition = position || defaultPosition; const transformOpts = { // Set left if horizontal drag is enabled x: canDragX(this) && draggable ? this.state.x : - position.x, + validPosition.x, // Set top if vertical drag is enabled y: canDragY(this) && draggable ? this.state.y : - position.y + validPosition.y }; // If this element was SVG, we use the `transform` attribute. if (this.state.isElementSVG) { - svgTransform = createSVGTransform(transformOpts, this.props.positionOffset); + svgTransform = createSVGTransform(transformOpts, positionOffset); } else { // Add a CSS transform to move the element around. This allows us to move the element around // without worrying about whether or not it is relatively or absolutely positioned. // If the item you are dragging already has a transform set, wrap it in a so // has a clean slate. - style = createCSSTransform(transformOpts, this.props.positionOffset); + style = createCSSTransform(transformOpts, positionOffset); } - const { - defaultClassName, - defaultClassNameDragging, - defaultClassNameDragged - } = this.props; - - const children = React.Children.only(this.props.children); - // Mark with class while dragging const className = classNames((children.props.className || ''), defaultClassName, { [defaultClassNameDragging]: this.state.dragging, @@ -355,8 +372,8 @@ export default class Draggable extends React.Component - {React.cloneElement(children, { + + {React.cloneElement(React.Children.only(children), { className: className, style: {...children.props.style, ...style}, transform: svgTransform