Skip to content

Commit 560c0f2

Browse files
authored
Merge pull request #626 from bokuweb/fix-#622
Fix #622
2 parents 117e62c + a8728c9 commit 560c0f2

File tree

6 files changed

+2777
-1379
lines changed

6 files changed

+2777
-1379
lines changed

package.json

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@
4040
},
4141
"homepage": "https://github.com/bokuweb/react-rnd",
4242
"devDependencies": {
43-
"@babel/cli": "7.7.5",
44-
"@babel/core": "7.7.5",
45-
"@babel/plugin-proposal-class-properties": "7.7.4",
46-
"@babel/plugin-transform-modules-commonjs": "7.7.5",
47-
"@babel/preset-react": "7.7.4",
48-
"@babel/preset-typescript": "7.7.4",
49-
"@babel/traverse": "7.7.4",
50-
"@babel/types": "7.7.4",
51-
"@emotion/core": "10.0.14",
52-
"@storybook/addon-actions": "5.1.3",
53-
"@storybook/addon-info": "5.1.3",
54-
"@storybook/addon-links": "5.1.3",
55-
"@storybook/addon-options": "5.1.3",
56-
"@storybook/react": "5.1.3",
43+
"@babel/cli": "7.8.0",
44+
"@babel/core": "7.8.0",
45+
"@babel/plugin-proposal-class-properties": "7.8.0",
46+
"@babel/plugin-transform-modules-commonjs": "7.8.0",
47+
"@babel/preset-react": "7.8.0",
48+
"@babel/preset-typescript": "7.8.0",
49+
"@babel/traverse": "7.8.0",
50+
"@babel/types": "7.8.0",
51+
"@emotion/core": "10.0.27",
52+
"@storybook/addon-actions": "5.3.1",
53+
"@storybook/addon-info": "5.3.1",
54+
"@storybook/addon-links": "5.3.1",
55+
"@storybook/addon-options": "5.3.1",
56+
"@storybook/react": "5.3.1",
5757
"@types/enzyme": "3.1.16",
5858
"@types/enzyme-adapter-react-16": "1.0.5",
5959
"@types/node": "^12.0.10",
@@ -67,7 +67,7 @@
6767
"babel-eslint": "10.0.3",
6868
"babel-loader": "8.0.6",
6969
"cpy-cli": "2.0.0",
70-
"cross-env": "5.2.0",
70+
"cross-env": "6.0.3",
7171
"enzyme": "3.8.0",
7272
"enzyme-adapter-react-16": "1.9.1",
7373
"gh-pages": "2.0.1",
@@ -86,10 +86,10 @@
8686
"rollup-plugin-typescript2": "0.22.0",
8787
"rollup-watch": "4.3.1",
8888
"sinon": "7.3.2",
89-
"tslint": "5.18.0",
89+
"tslint": "5.20.1",
9090
"tslint-eslint-rules": "5.4.0",
91-
"tslint-plugin-prettier": "2.0.1",
92-
"tslint-react": "4.0.0",
91+
"tslint-plugin-prettier": "2.1.0",
92+
"tslint-react": "4.1.0",
9393
"typescript": "3.7.4"
9494
},
9595
"files": [

src/index.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ test("should call onDrag when dragging", async t => {
146146
.simulate("mousedown", { clientX: 0, clientY: 0 });
147147
mouseMove(200, 220);
148148
t.is(onDrag.callCount, 1);
149-
t.is(onDrag.firstCall.args[1].x, 600);
150-
t.is(onDrag.firstCall.args[1].y, 620);
149+
t.is(onDrag.firstCall.args[1].x, 500);
150+
t.is(onDrag.firstCall.args[1].y, 520);
151151
t.not((rnd.getDOMNode().getAttribute("style") || "").indexOf("transform: translate(400px, 420px)"), -1);
152152
});
153153

@@ -161,8 +161,8 @@ test("should call onDragStop when drag stop", async t => {
161161
mouseMove(200, 220);
162162
mouseUp(100, 120);
163163
t.is(onDragStop.callCount, 1);
164-
t.is(onDragStop.firstCall.args[1].x, -100);
165-
t.is(onDragStop.firstCall.args[1].y, -100);
164+
t.is(onDragStop.firstCall.args[1].x, 200);
165+
t.is(onDragStop.firstCall.args[1].y, 220);
166166
});
167167

168168
test("should dragging disabled when axis equals none", async t => {

src/index.tsx

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ export class Rnd extends React.PureComponent<Props, State> {
198198
draggable!: $TODO; // Draggable;
199199
resizing = false;
200200
resizingPosition = { x: 0, y: 0 };
201+
offsetFromParent = { left: 0, top: 0 };
201202

202203
constructor(props: Props) {
203204
super(props);
@@ -226,7 +227,8 @@ export class Rnd extends React.PureComponent<Props, State> {
226227
}
227228

228229
componentDidMount() {
229-
const { left, top } = this.getOffsetFromParent();
230+
this.updateOffsetFromParent();
231+
const { left, top } = this.offsetFromParent;
230232
const { x, y } = this.getDraggablePosition();
231233
this.draggable.setState({
232234
x: x - left,
@@ -329,7 +331,8 @@ export class Rnd extends React.PureComponent<Props, State> {
329331
const left = (boundaryLeft - parentLeft) / scale;
330332
const top = boundaryTop - parentTop;
331333
if (!this.resizable) return;
332-
const offset = this.getOffsetFromParent();
334+
this.updateOffsetFromParent();
335+
const offset = this.offsetFromParent;
333336
this.setState({
334337
bounds: {
335338
top: top - offset.top,
@@ -342,14 +345,14 @@ export class Rnd extends React.PureComponent<Props, State> {
342345

343346
onDrag(e: RndDragEvent, data: DraggableData) {
344347
if (this.props.onDrag) {
345-
const offset = this.getOffsetFromParent();
348+
const offset = this.offsetFromParent;
346349
this.props.onDrag(e, { ...data, x: data.x - offset.left, y: data.y - offset.top });
347350
}
348351
}
349352

350353
onDragStop(e: RndDragEvent, data: DraggableData) {
351354
if (this.props.onDragStop) {
352-
const { left, top } = this.getOffsetFromParent();
355+
const { left, top } = this.offsetFromParent;
353356
return this.props.onDragStop(e, { ...data, x: data.x + left, y: data.y + top });
354357
}
355358
}
@@ -363,7 +366,7 @@ export class Rnd extends React.PureComponent<Props, State> {
363366
this.resizing = true;
364367

365368
const scale = this.props.scale as number;
366-
const offset = this.getOffsetFromParent();
369+
const offset = this.offsetFromParent;
367370
const pos = this.getDraggablePosition();
368371
this.resizingPosition = { x: pos.x + offset.left, y: pos.y + offset.top };
369372
this.setState({
@@ -458,27 +461,22 @@ export class Rnd extends React.PureComponent<Props, State> {
458461
elementRef: HTMLDivElement,
459462
delta: { height: number; width: number },
460463
) {
461-
let x;
462-
let y;
463-
const offset = this.getOffsetFromParent();
464464
if (/left/i.test(direction)) {
465-
x = this.state.original.x - delta.width;
465+
const x = this.state.original.x - delta.width;
466466
// INFO: Apply x position by resize to draggable.
467467
this.draggable.setState({ x });
468-
x += offset.left;
469468
}
470469
if (/top/i.test(direction)) {
471-
y = this.state.original.y - delta.height;
470+
const y = this.state.original.y - delta.height;
472471
// INFO: Apply x position by resize to draggable.
473472
this.draggable.setState({ y });
474-
y += offset.top;
475-
}
476-
if (typeof x === "undefined") {
477-
x = this.getDraggablePosition().x + offset.left;
478-
}
479-
if (typeof y === "undefined") {
480-
y = this.getDraggablePosition().y + offset.top;
481473
}
474+
475+
this.updateOffsetFromParent();
476+
const offset = this.offsetFromParent;
477+
const x = this.getDraggablePosition().x + offset.left;
478+
const y = this.getDraggablePosition().y + offset.top;
479+
482480
this.resizingPosition = { x, y };
483481
if (!this.props.onResize) return;
484482
this.props.onResize(e, direction, elementRef, delta, {
@@ -510,7 +508,7 @@ export class Rnd extends React.PureComponent<Props, State> {
510508
this.draggable.setState(position);
511509
}
512510

513-
getOffsetFromParent(): { top: number; left: number } {
511+
updateOffsetFromParent() {
514512
const scale = this.props.scale as number;
515513
const parent = this.getParent();
516514
const self = this.getSelfElement();
@@ -525,7 +523,7 @@ export class Rnd extends React.PureComponent<Props, State> {
525523
const parentTop = parentRect.top;
526524
const selfRect = self.getBoundingClientRect();
527525
const position = this.getDraggablePosition();
528-
return {
526+
this.offsetFromParent = {
529527
left: selfRect.left - parentLeft - position.x * scale,
530528
top: selfRect.top - parentTop - position.y * scale,
531529
};
@@ -581,7 +579,7 @@ export class Rnd extends React.PureComponent<Props, State> {
581579
...cursorStyle,
582580
...style,
583581
};
584-
const { left, top } = this.getOffsetFromParent();
582+
const { left, top } = this.offsetFromParent;
585583
let draggablePosition;
586584
if (position) {
587585
draggablePosition = {

stories/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import SandboxBodySizeToMaxWidth from "./sandbox/bodysize-to-maxwidth";
4141
import SandboxLockAspectRatioWithBounds from "./sandbox/lock-aspect-ratio-with-bounds";
4242

4343
import LockAspectRatioBasic from "./lock-aspect-ratio/basic";
44+
import Issue622 from "./sandbox/issue-#622";
4445

4546
storiesOf("bare", module).add("bare", () => <Bare />);
4647

@@ -84,6 +85,7 @@ storiesOf("grid", module)
8485

8586
storiesOf("sandbox", module)
8687
.add("body size apply to maxwidth", () => <SandboxBodySizeToMaxWidth />)
87-
.add("lock aspect ratio with bounds", () => <SandboxLockAspectRatioWithBounds />);
88+
.add("lock aspect ratio with bounds", () => <SandboxLockAspectRatioWithBounds />)
89+
.add("issue622", () => <Issue622 />);
8890

8991
storiesOf("ratio", module).add("lock aspect ratio", () => <LockAspectRatioBasic />);

stories/sandbox/issue-#622.tsx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import React from "react";
2+
import { Rnd } from "../../src";
3+
4+
const style = {
5+
display: "flex",
6+
alignItems: "center",
7+
justifyContent: "center",
8+
borderLeft: "solid 5px #ddd",
9+
borderRight: "solid 5px #ddd",
10+
borderTop: "solid 1px #ddd",
11+
borderBottom: "solid 1px #ddd",
12+
background: "#f0f0f0",
13+
height: "100%",
14+
};
15+
16+
export default class App extends React.Component {
17+
state = {
18+
width: 200,
19+
height: 52,
20+
x: 100,
21+
y: 0,
22+
};
23+
24+
render() {
25+
return (
26+
<div
27+
style={{
28+
width: "100%",
29+
height: "50px",
30+
border: "1px solid pink",
31+
position: "relative",
32+
}}
33+
>
34+
<Rnd
35+
style={style}
36+
bounds="parent"
37+
enableResizing={{
38+
top: false,
39+
right: true,
40+
bottom: false,
41+
left: true,
42+
topRight: false,
43+
bottomRight: false,
44+
bottomLeft: false,
45+
topLeft: false,
46+
}}
47+
size={{ width: this.state.width, height: this.state.height }}
48+
position={{ x: this.state.x, y: this.state.y }}
49+
onDragStop={(e, d) => {
50+
this.setState({ x: d.x, y: d.y });
51+
}}
52+
onResizeStop={(e, direction, ref, delta, position) => {
53+
this.setState({
54+
width: ref.style.width,
55+
height: ref.style.height,
56+
...position,
57+
});
58+
}}
59+
>
60+
Rnd
61+
</Rnd>
62+
</div>
63+
);
64+
}
65+
}

0 commit comments

Comments
 (0)