Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,10 @@ export class Rnd extends React.PureComponent<Props, State> {
}

componentDidMount() {
this.updateOffsetFromParent();
const { left, top } = this.offsetFromParent;
const { x, y } = this.getDraggablePosition();
this.draggable.setState({
x: x - left,
y: y - top,
x,
y,
});
// HACK: Apply position adjustment
this.forceUpdate();
Expand Down Expand Up @@ -363,23 +361,51 @@ export class Rnd extends React.PureComponent<Props, State> {
if (!this.props.onDrag) return;
const { left, top } = this.offsetFromParent;
if (!this.props.dragAxis || this.props.dragAxis === "both") {
return this.props.onDrag(e, { ...data, x: data.x - left, y: data.y - top });
return this.props.onDrag(e, {
...data,
x: data.x - left,
y: data.y - top,
});
} else if (this.props.dragAxis === "x") {
return this.props.onDrag(e, { ...data, x: data.x + left, y: this.originalPosition.y + top, deltaY: 0 });
return this.props.onDrag(e, {
...data,
x: data.x + left,
y: this.originalPosition.y + top,
deltaY: 0,
});
} else if (this.props.dragAxis === "y") {
return this.props.onDrag(e, { ...data, x: this.originalPosition.x + left, y: data.y + top, deltaX: 0 });
return this.props.onDrag(e, {
...data,
x: this.originalPosition.x + left,
y: data.y + top,
deltaX: 0,
});
}
}

onDragStop(e: RndDragEvent, data: DraggableData) {
if (!this.props.onDragStop) return;
const { left, top } = this.offsetFromParent;
if (!this.props.dragAxis || this.props.dragAxis === "both") {
return this.props.onDragStop(e, { ...data, x: data.x + left, y: data.y + top });
return this.props.onDragStop(e, {
...data,
x: data.x + left,
y: data.y + top,
});
} else if (this.props.dragAxis === "x") {
return this.props.onDragStop(e, { ...data, x: data.x + left, y: this.originalPosition.y + top, deltaY: 0 });
return this.props.onDragStop(e, {
...data,
x: data.x + left,
y: this.originalPosition.y + top,
deltaY: 0,
});
} else if (this.props.dragAxis === "y") {
return this.props.onDragStop(e, { ...data, x: this.originalPosition.x + left, y: data.y + top, deltaX: 0 });
return this.props.onDragStop(e, {
...data,
x: this.originalPosition.x + left,
y: data.y + top,
deltaX: 0,
});
}
}

Expand Down Expand Up @@ -636,6 +662,8 @@ export class Rnd extends React.PureComponent<Props, State> {
const pos = this.state.resizing ? undefined : draggablePosition;
const dragAxisOrUndefined = this.state.resizing ? "both" : dragAxis;

console.log(defaultValue, pos);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dan-cooke Please remove this line.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed


return (
<Draggable
ref={this.refDraggable}
Expand Down