Skip to content

Commit b4d6628

Browse files
committed
Fix issue where a React element wouldn't scroll by ref
1 parent 57dc64c commit b4d6628

File tree

4 files changed

+1498
-1670
lines changed

4 files changed

+1498
-1670
lines changed

package.json

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,33 @@
3535
"react": "^15.0.0 || ^16.0.0"
3636
},
3737
"devDependencies": {
38-
"@babel/core": "7.1.2",
39-
"@babel/plugin-proposal-class-properties": "7.1.0",
40-
"@babel/preset-env": "7.1.0",
38+
"@babel/core": "7.2.2",
39+
"@babel/plugin-proposal-class-properties": "7.2.3",
40+
"@babel/preset-env": "7.2.3",
4141
"@babel/preset-react": "7.0.0",
42-
"@storybook/react": "4.0.2",
43-
"all-contributors-cli": "5.4.1",
42+
"@storybook/react": "4.1.6",
43+
"all-contributors-cli": "5.7.0",
4444
"babel-core": "7.0.0-bridge.0",
4545
"babel-jest": "23.6.0",
46-
"babel-loader": "8.0.4",
46+
"babel-loader": "8.0.5",
4747
"copy-webpack-plugin": "4.6.0",
4848
"enzyme": "3.7.0",
4949
"enzyme-adapter-react-16": "1.6.0",
50-
"eslint": "5.8.0",
50+
"eslint": "5.12.0",
5151
"eslint-config-airbnb": "17.1.0",
52-
"eslint-config-prettier": "3.1.0",
52+
"eslint-config-prettier": "3.3.0",
5353
"eslint-plugin-import": "2.14.0",
5454
"eslint-plugin-jsx-a11y": "6.1.2",
55-
"eslint-plugin-react": "7.11.1",
56-
"husky": "1.1.3",
55+
"eslint-plugin-react": "7.12.3",
56+
"husky": "1.3.1",
5757
"jest": "23.6.0",
58-
"lint-staged": "8.0.4",
59-
"prettier": "1.14.3",
60-
"react": "16.6.0",
61-
"react-dom": "16.6.0",
62-
"react-testing-library": "5.2.3",
63-
"webpack": "4.24.0",
64-
"webpack-cli": "3.1.2"
58+
"lint-staged": "8.1.0",
59+
"prettier": "1.15.3",
60+
"react": "16.7.0",
61+
"react-dom": "16.7.0",
62+
"react-testing-library": "5.4.4",
63+
"webpack": "4.28.4",
64+
"webpack-cli": "3.2.1"
6565
},
6666
"jest": {
6767
"setupFiles": [

src/ScrollTo.jsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { Component, createContext } from "react";
2+
import { findDOMNode } from "react-dom";
23
import { relative } from "./utilities/relative";
34

45
export const ScrollToContext = createContext("scrollToContext");
@@ -59,6 +60,12 @@ class ScrollTo extends Component {
5960
const top = ScrollTo._parseLocation(options.y, node, true);
6061
const left = ScrollTo._parseLocation(options.x, node, false);
6162

63+
const rNode = findDOMNode(node);
64+
65+
if (rNode) {
66+
node = rNode;
67+
}
68+
6269
if (node.scrollTo) {
6370
node.scrollTo({
6471
top,

stories/ScrollByRef.jsx

Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
2-
import { ScrollStory } from "./";
3-
import { ScrollTo } from "../src/index";
42
import { Object } from "es6-shim";
3+
import { ScrollStory } from ".";
4+
import { ScrollTo } from "../src/index";
55

66
const refDOM = React.createRef();
77
const mockData = {
@@ -11,66 +11,62 @@ const mockData = {
1111
qty: [1, 200, 3.2, 4, 5, 6.55, 7, 8]
1212
};
1313

14-
ScrollStory.add("Scroll by Ref", () => {
15-
return (
16-
<div style={{ overflow: "auto", padding: 20 }}>
17-
<ScrollTo>
18-
{({ scrollTo }) => (
19-
<React.Fragment>
20-
<button
21-
type="button"
22-
onClick={() => scrollTo({ ref: refDOM, y: 700, smooth: true })}
23-
>
24-
Scroll Ref Down
25-
</button>
14+
ScrollStory.add("Scroll by Ref", () => (
15+
<div style={{ overflow: "auto", padding: 20 }}>
16+
<ScrollTo>
17+
{({ scrollTo }) => (
18+
<React.Fragment>
19+
<button
20+
type="button"
21+
onClick={() => scrollTo({ ref: refDOM, y: 700, smooth: true })}
22+
>
23+
Scroll Ref Down
24+
</button>
2625

27-
<button
28-
type="button"
29-
style={{ marginLeft: 5 }}
30-
onClick={() => scrollTo({ ref: refDOM, y: 0, smooth: true })}
31-
>
32-
Scroll Ref Up
33-
</button>
34-
</React.Fragment>
35-
)}
36-
</ScrollTo>
26+
<button
27+
type="button"
28+
style={{ marginLeft: 5 }}
29+
onClick={() => scrollTo({ ref: refDOM, y: 0, smooth: true })}
30+
>
31+
Scroll Ref Up
32+
</button>
33+
</React.Fragment>
34+
)}
35+
</ScrollTo>
3736

38-
<div
39-
ref={refDOM}
40-
style={{
41-
marginTop: 20,
42-
background: "#F1F1F1",
43-
height: 1000,
44-
maxHeight: 300,
45-
overflow: "auto",
46-
position: "relative"
47-
}}
48-
>
49-
<div style={{ height: 1000, padding: 20 }}>
50-
<table>
51-
<thead>
52-
<tr>
37+
<div
38+
ref={refDOM}
39+
style={{
40+
marginTop: 20,
41+
background: "#F1F1F1",
42+
height: 1000,
43+
maxHeight: 300,
44+
overflow: "auto",
45+
position: "relative"
46+
}}
47+
>
48+
<div style={{ height: 1000, padding: 20 }}>
49+
<table>
50+
<thead>
51+
<tr>
52+
{Object.keys(mockData).map(key => (
53+
<th key={key}>{key}</th>
54+
))}
55+
</tr>
56+
</thead>
57+
<tbody>
58+
{Array.from({ length: 40 }).map((_, i) => (
59+
<tr key={i}>
5360
{Object.keys(mockData).map(key => (
54-
<th key={key}>{key}</th>
61+
<td key={key + i}>
62+
{mockData[key][i % mockData[key].length]}
63+
</td>
5564
))}
5665
</tr>
57-
</thead>
58-
<tbody>
59-
{Array.from({ length: 40 }).map((_, i) => {
60-
return (
61-
<tr key={i}>
62-
{Object.keys(mockData).map(key => (
63-
<td key={key + i}>
64-
{mockData[key][i % mockData[key].length]}
65-
</td>
66-
))}
67-
</tr>
68-
);
69-
})}
70-
</tbody>
71-
</table>
72-
</div>
66+
))}
67+
</tbody>
68+
</table>
7369
</div>
7470
</div>
75-
);
76-
});
71+
</div>
72+
));

0 commit comments

Comments
 (0)