Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

Commit 3c857e1

Browse files
authored
Merge pull request #511 from mesosphere/danielmschmidt/fix-ref-for-dropdown
fix(Dropdown): ref was called without current
2 parents 23f72df + d9c7c1b commit 3c857e1

File tree

14 files changed

+245
-160
lines changed

14 files changed

+245
-160
lines changed

.travis.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ node_js:
66

77
jobs:
88
include:
9+
- stage: lint
10+
name: "Lint && Dist"
11+
script:
12+
- npm run lint && npm run dist
13+
- stage: test
14+
name: "Unit tests"
15+
script:
16+
- npm run test
917
# Define the release stage that runs semantic-release
1018
- stage: release
1119
node_js: lts/*
@@ -22,5 +30,5 @@ install:
2230
- ./install_react.sh
2331
env:
2432
matrix:
25-
- REACT_VERSION=15.6
2633
- REACT_VERSION=16.7
34+
- REACT_VERSION=16.8

docs/src/index.js

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,15 @@ class Docs extends Util.mixin(BindMixin) {
8282
};
8383
this.sectionScrollPositions = {};
8484
this.state = {
85-
activeSection: null,
86-
pageRef: null
85+
activeSection: null
8786
};
8887
this.viewportHeight = 0;
88+
89+
this.pageRef = React.createRef();
90+
this.nodeRefs.pageHeader = React.createRef();
91+
navigationItems.forEach(
92+
({ id }) => (this.nodeRefs.sectionRefs[id] = React.createRef())
93+
);
8994
}
9095

9196
componentDidMount() {
@@ -97,17 +102,16 @@ class Docs extends Util.mixin(BindMixin) {
97102
}
98103

99104
handleNavigationClick(id) {
100-
const { pageRef } = this.state;
101105
const sectionPosition = this.sectionScrollPositions[id];
102106

103-
if (pageRef && sectionPosition != null) {
104-
pageRef.scrollTop = sectionPosition;
107+
if (this.pageRef.current && sectionPosition != null) {
108+
this.pageRef.current.scrollTop = sectionPosition;
105109
}
106110
}
107111

108112
handlePageScroll() {
109113
let activeSection = null;
110-
const pageScrollTop = this.state.pageRef.scrollTop;
114+
const pageScrollTop = this.pageRef.current.scrollTop;
111115
const scrollThreshhold = pageScrollTop + this.viewportHeight / 2;
112116

113117
Object.keys(this.sectionScrollPositions).forEach(ref => {
@@ -122,18 +126,13 @@ class Docs extends Util.mixin(BindMixin) {
122126
}
123127
}
124128

125-
setPageRef(pageRef) {
126-
if (this.state.pageRef === null) {
127-
this.setState({ pageRef });
128-
}
129-
}
130-
131129
calculateNodePositions() {
132-
const pageHeaderHeight = this.nodeRefs.pageHeader.offsetHeight;
130+
const pageHeaderHeight = this.nodeRefs.pageHeader.current.offsetHeight;
133131
this.viewportHeight = DOMUtil.getViewportHeight();
134132

135133
Object.keys(this.nodeRefs.sectionRefs).forEach(ref => {
136-
const top = this.nodeRefs.sectionRefs[ref].offsetTop + pageHeaderHeight;
134+
const top =
135+
this.nodeRefs.sectionRefs[ref].current.offsetTop + pageHeaderHeight;
137136
this.sectionScrollPositions[ref] = top;
138137
});
139138
}
@@ -162,26 +161,20 @@ class Docs extends Util.mixin(BindMixin) {
162161

163162
render() {
164163
let docsContent = null;
165-
const { pageRef } = this.state;
166164

167165
// Delay the rendering of the components until we have a reference to the
168166
// scrolling container's DOM node.
169-
if (pageRef !== null) {
167+
if (this.pageRef !== null) {
170168
docsContent = navigationItems.map((navigationItem, index) => {
171169
const { id, passScrollContainer } = navigationItem;
172170
const props = {};
173171

174172
if (passScrollContainer) {
175-
props.scrollContainer = pageRef;
173+
props.scrollContainer = this.pageRef;
176174
}
177175

178176
return (
179-
<div
180-
key={index}
181-
ref={ref => {
182-
this.nodeRefs.sectionRefs[id] = ref;
183-
}}
184-
>
177+
<div key={index} ref={this.nodeRefs.sectionRefs[id]}>
185178
<navigationItem.component {...props} />
186179
</div>
187180
);
@@ -206,12 +199,12 @@ class Docs extends Util.mixin(BindMixin) {
206199
className="page flex-item-grow-1"
207200
id="page"
208201
onScroll={this.handlePageScroll}
209-
ref={ref => this.setPageRef(ref)}
202+
ref={this.pageRef}
210203
>
211204
<div
212205
id="page-header"
213206
className="page-header fill fill-light left"
214-
ref={ref => (this.nodeRefs.pageHeader = ref)}
207+
ref={this.nodeRefs.pageHeader}
215208
>
216209
<div id="page-header-inner" className="page-header-inner">
217210
<div className="container-fluid pod">

install_react.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/bash
22

3-
if [ "$REACT_VERSION" = "15.6" ]; then npm install [email protected] [email protected]; fi
43
if [ "$REACT_VERSION" = "16.7" ]; then npm install [email protected] [email protected]; fi
4+
if [ "$REACT_VERSION" = "16.8" ]; then npm install [email protected] [email protected]; fi

package-lock.json

Lines changed: 97 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)