Skip to content

Commit 18734fe

Browse files
committed
resize listener
1 parent 5198a07 commit 18734fe

7 files changed

Lines changed: 66 additions & 49 deletions

File tree

‎packages/patternfly-4/react-docs/gatsby-node.js‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const navHelpers = require('./src/helpers/navHelpers');
2-
const path = require('path');
1+
const navHelpers = require("./src/helpers/navHelpers");
2+
const path = require("path");
33

44
// Add map PR-related environment variables to gatsby nodes
55
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }) => {
@@ -8,17 +8,17 @@ exports.sourceNodes = ({ actions, createNodeId, createContentDigest }) => {
88
// Docs https://www.gatsbyjs.org/docs/actions/#createNode
99
actions.createNode({
1010
name: 'PR_INFO',
11-
num: num || '',
12-
url: url || '',
11+
num: num ? num : '',
12+
url: url ? url : '',
1313
id: createNodeId(`PR_INFO`),
1414
parent: null,
1515
children: [],
1616
internal: {
1717
contentDigest: createContentDigest({ a: 'PR_INFO' }),
18-
type: `EnvVars`
19-
}
18+
type: `EnvVars`,
19+
},
2020
});
21-
}
21+
};
2222

2323
// Create pages for markdown files
2424
exports.createPages = ({ graphql, actions }) => {
@@ -35,6 +35,7 @@ exports.createPages = ({ graphql, actions }) => {
3535
}
3636
}
3737
}
38+
}
3839
`);
3940

4041
return mdx.then(({ data }) => {
@@ -54,7 +55,7 @@ exports.createPages = ({ graphql, actions }) => {
5455
component: path.resolve('./src/templates/mdxFullscreenTemplate.js'),
5556
context: {
5657
title: node.frontmatter.title,
57-
fileAbsolutePath: node.fileAbsolutePath // Helps us get the markdown
58+
fileAbsolutePath: node.fileAbsolutePath, // Helps us get the markdown
5859
}
5960
});
6061
} else {
@@ -73,8 +74,9 @@ exports.createPages = ({ graphql, actions }) => {
7374
});
7475
}
7576
});
76-
})
77-
}
77+
});
78+
};
79+
7880

7981
exports.onCreateWebpackConfig = ({ stage, actions }) => {
8082
actions.setWebpackConfig({
@@ -98,6 +100,6 @@ exports.onCreateWebpackConfig = ({ stage, actions }) => {
98100
// Hack to work downstream in https://github.com/patternfly/patternfly-org
99101
'@content': path.resolve(__dirname, 'src/components/componentDocs'),
100102
}
101-
}
102-
});
103-
}
103+
},
104+
})
105+
};

‎packages/patternfly-4/react-virtualized-extension/package.json‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
"homepage": "https://github.com/patternfly/patternfly-react/tree/master/packages/patternfly-4/",
2929
"dependencies": {
3030
"@patternfly/patternfly": "2.6.5",
31-
"@patternfly/react-core": "^3.16.10",
32-
"@patternfly/react-icons": "^3.8.1",
33-
"@patternfly/react-styles": "^3.2.0",
31+
"@patternfly/react-core": "^3.18.1",
32+
"@patternfly/react-icons": "^3.9.2",
33+
"@patternfly/react-styles": "^3.2.2",
3434
"exenv": "^1.2.2"
3535
},
3636
"peerDependencies": {
37-
"@patternfly/react-table": "^2.5.11",
37+
"@patternfly/react-table": "^2.6.1",
3838
"lodash-es": "4.x",
3939
"prop-types": "^15.6.1",
4040
"react": "^16.4.0",

‎packages/patternfly-4/react-virtualized-extension/src/components/Virtualized/Body.js‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,22 @@ class Body extends React.Component {
106106
registerContainer = () => {
107107
setTimeout(() => {
108108
const element = this.props.container();
109-
element && element.addEventListener('scroll', this.onScroll);
110-
this._detectElementResize = createDetectElementResize();
111-
this._detectElementResize.addResizeListener(element, this.onResize);
112-
this.setContainerOffset();
109+
if (element) {
110+
element && element.addEventListener('scroll', this.onScroll);
111+
this._detectElementResize = createDetectElementResize();
112+
this._detectElementResize.addResizeListener(element, this.onResize);
113+
this.setContainerOffset();
114+
}
113115
}, 0);
114116
};
115117

118+
unregisterContainer = () => {
119+
const element = this.props.container();
120+
if (element && element.__resizeListeners__) {
121+
element.removeEventListener('scroll', this.onScroll);
122+
}
123+
};
124+
116125
setContainerOffset = () => {
117126
const element = this.props.container && this.props.container();
118127
if (element) {
@@ -147,7 +156,10 @@ class Body extends React.Component {
147156

148157
componentWillUnmount() {
149158
if (this.tbodyRef && this.tbodyRef.__resizeListeners__) {
150-
this.tbodyRef && this._detectElementResize.removeResizeListener(this.tbodyRef, this.onResize);
159+
this._detectElementResize.removeResizeListener(this.tbodyRef, this.onResize);
160+
}
161+
if (this.props.container) {
162+
this.unregisterContainer();
151163
}
152164
clearTimeout(this.timeoutId);
153165
}

‎packages/patternfly-4/react-virtualized-extension/src/components/Virtualized/Virtualized.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class DynamicHeightExample extends React.Component {
244244
constructor(props) {
245245
super(props);
246246
const rows = [];
247-
for (let i = 0; i < 100; i++) {
247+
for (let i = 0; i < 100000; i++) {
248248
const cells = [];
249249
const num = Math.floor(Math.random() * Math.floor(9)) + 1;
250250
for (let j = 0; j < 5; j++) {

‎packages/patternfly-4/react-virtualized-extension/src/components/Virtualized/WindowScroller.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class WindowScrollerExample extends React.Component {
3333
this.container = element;
3434
};
3535
const rows = [];
36-
for (let i = 0; i < 100; i++) {
36+
for (let i = 0; i < 100000; i++) {
3737
rows.push({
3838
id: UUID(),
3939
cells: [`one-${i}`, `two-${i}`, `three-${i}`, `four-${i}`, `five-${i}`]

‎packages/patternfly-4/react-virtualized-extension/src/components/Virtualized/utils/calculateAverageHeight.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
* - Use arrow-rowindex based measured amounts for simplicity
99
* - prevent divide by zero exception
1010
* */
11-
const calculateAverageHeight = (measuredRows) => {
11+
const calculateAverageHeight = measuredRows => {
1212
const measuredAmounts = Object.keys(measuredRows).map(key => measuredRows[key]);
1313
const amountOfMeasuredRows = measuredAmounts.length;
1414
// prevent divide by zero exception
15-
return Math.max(measuredAmounts.reduce((a, b) => a + b / amountOfMeasuredRows, 0), 1);
15+
return Math.max(measuredAmounts.reduce((a, b) => a + b, 0) / amountOfMeasuredRows, 1);
1616
};
1717

1818
export default calculateAverageHeight;

‎packages/patternfly-4/react-virtualized-extension/src/components/Virtualized/utils/calculateRows.js‎

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,47 @@
55
* Forked from version 8.17.0; includes the following modifications:
66
* 1) Calculate actual row heights in determining startIndex. This allows dynamic row heights.
77
* */
8-
import calculateAverageHeight from './calculateAverageHeight';
8+
// import calculateAverageHeight from './calculateAverageHeight';
99

1010
const calculateRows = ({ measuredRows, height, rowKey, rows, scrollTop = 0 }) => {
11-
// used exact measured row heights for determining `startIndex` for smooth scroll
12-
// average heights are not accurate when there is lots of variation in row heights
11+
// default overscan to 10 for now, could be accepted as a prop in the future
12+
const overscan = 20;
13+
14+
// averageHeight of measuredRows can still be used to closely approximate amount of rows to render
15+
const measuredAmounts = Object.keys(measuredRows).map(key => measuredRows[key]);
16+
const amountOfMeasuredRows = measuredAmounts.length;
17+
const totalMeasuredHeight = measuredAmounts.reduce((a, b) => a + b, 0);
18+
19+
// if we have no rows, use a small row height so we get a good sample in future loops
20+
const averageHeight = amountOfMeasuredRows > 0 ? totalMeasuredHeight / amountOfMeasuredRows : 50;
21+
1322
let startIndex = 0;
1423
let startHeight = 0;
1524
let accruedHeight = 0;
25+
let i = 0;
1626

17-
//default overscan to 10 for now, could be accepted as a prop in the future
18-
const overscan = 10;
19-
20-
for (let i = 0; i < Object.keys(measuredRows).length; i++) {
27+
while (accruedHeight < scrollTop) {
2128
// measuredRows use aria-rowindex as identifiers which is 1 based
22-
accruedHeight += measuredRows[i + 1];
23-
24-
if (scrollTop < accruedHeight) {
29+
if (measuredRows.hasOwnProperty(i + 1)) {
30+
accruedHeight += measuredRows[i + 1];
31+
} else {
32+
accruedHeight += averageHeight;
33+
}
34+
if (scrollTop <= accruedHeight) {
2535
startIndex = i;
2636
break;
27-
}
28-
else if(i + overscan > Object.keys(measuredRows).length){
29-
// stop accruing after we reach i + overscan
37+
} else if (i + overscan > rows.length) {
38+
// stop accruing after we reach i + overscan (the end)
3039
startHeight = accruedHeight;
3140
startIndex = i;
3241
break;
3342
}
34-
else {
35-
// accrue and continue
36-
startHeight = accruedHeight;
37-
}
43+
// accrue and continue
44+
startHeight = accruedHeight;
45+
i += 1;
3846
}
3947

40-
// averageHeight of measuredRows can still be used to closely approximate amount of rows to render
41-
// if this causes issues w/ row visibility, exact heights can still be used
42-
const averageHeight = calculateAverageHeight(measuredRows);
4348
const amountOfRowsToRender = Math.ceil(height / averageHeight) + overscan;
44-
45-
// const zeroedIndex = startIndex;
4649
const rowsToRender = rows.slice(startIndex, Math.max(startIndex + amountOfRowsToRender, 0));
4750

4851
if (process.env.NODE_ENV !== 'production' && typeof window !== 'undefined' && window.LOG_VIRTUALIZED) {
@@ -90,7 +93,7 @@ const calculateRows = ({ measuredRows, height, rowKey, rows, scrollTop = 0 }) =>
9093

9194
return {
9295
amountOfRowsToRender,
93-
startIndex: startIndex,
96+
startIndex,
9497
showExtraRow: !(startIndex % 2),
9598
startHeight,
9699
endHeight

0 commit comments

Comments
 (0)