-
Notifications
You must be signed in to change notification settings - Fork 908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Moving off of react-virtualized to react-window #467
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { AutoSizer, List } from 'react-virtualized'; | ||
import AutoSizer from 'react-virtualized-auto-sizer'; | ||
import { VariableSizeList } from 'react-window'; | ||
import isEqual from 'lodash.isequal'; | ||
import withScrolling, { | ||
createScrollingComponent, | ||
createVerticalStrength, | ||
createHorizontalStrength, | ||
} from 'frontend-collective-react-dnd-scrollzone'; | ||
|
@@ -98,11 +98,10 @@ class ReactSortableTree extends Component { | |
|
||
// Prepare scroll-on-drag options for this list | ||
if (isVirtualized) { | ||
this.scrollZoneVirtualList = (createScrollingComponent || withScrolling)( | ||
List | ||
); | ||
this.scrollZoneVirtualList = withScrolling(VariableSizeList); | ||
this.vStrength = createVerticalStrength(slideRegionSize); | ||
this.hStrength = createHorizontalStrength(slideRegionSize); | ||
this.variableSizeListRef = React.createRef(); | ||
} | ||
|
||
this.state = { | ||
|
@@ -209,6 +208,25 @@ class ReactSortableTree extends Component { | |
}); | ||
} | ||
} | ||
|
||
// if there is search, scroll to component | ||
if ( | ||
this.state.searchFocusTreeIndex !== null && | ||
this.variableSizeListRef.current | ||
) { | ||
this.variableSizeListRef.current.scrollToItem( | ||
this.state.searchFocusTreeIndex, | ||
'start' | ||
); | ||
} | ||
} | ||
|
||
shouldComponentUpdate(nextProps, nextState) { | ||
if (nextState.draggedNode !== this.state.draggedNode) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
componentWillUnmount() { | ||
|
@@ -585,7 +603,7 @@ class ReactSortableTree extends Component { | |
return ( | ||
<TreeNodeRenderer | ||
style={style} | ||
key={nodeKey} | ||
key="something" | ||
listIndex={listIndex} | ||
getPrevRow={getPrevRow} | ||
lowerSiblingCounts={lowerSiblingCounts} | ||
|
@@ -622,7 +640,6 @@ class ReactSortableTree extends Component { | |
} = mergeTheme(this.props); | ||
const { | ||
searchMatches, | ||
searchFocusTreeIndex, | ||
draggedNode, | ||
draggedDepth, | ||
draggedMinimumTreeIndex, | ||
|
@@ -664,12 +681,6 @@ class ReactSortableTree extends Component { | |
matchKeys[path[path.length - 1]] = i; | ||
}); | ||
|
||
// Seek to the focused search result if there is one specified | ||
const scrollToInfo = | ||
searchFocusTreeIndex !== null | ||
? { scrollToIndex: searchFocusTreeIndex } | ||
: {}; | ||
|
||
let containerStyle = style; | ||
let list; | ||
if (rows.length < 1) { | ||
|
@@ -684,40 +695,43 @@ class ReactSortableTree extends Component { | |
containerStyle = { height: '100%', ...containerStyle }; | ||
|
||
const ScrollZoneVirtualList = this.scrollZoneVirtualList; | ||
// Render list with react-virtualized | ||
|
||
// Render list with react-window | ||
list = ( | ||
<AutoSizer> | ||
{({ height, width }) => ( | ||
<ScrollZoneVirtualList | ||
{...scrollToInfo} | ||
// ref={this.variableSizeListRef} | ||
// to be fixed in the new version of react-dnd-scrollzone | ||
direction={rowDirection} | ||
dragDropManager={dragDropManager} | ||
verticalStrength={this.vStrength} | ||
horizontalStrength={this.hStrength} | ||
speed={30} | ||
scrollToAlignment="start" | ||
className="rst__virtualScrollOverride" | ||
width={width} | ||
onScroll={({ scrollTop }) => { | ||
this.scrollTop = scrollTop; | ||
onScroll={({ scrollOffset }) => { | ||
this.scrollTop = scrollOffset; | ||
}} | ||
height={height} | ||
style={innerStyle} | ||
rowCount={rows.length} | ||
estimatedRowSize={ | ||
itemCount={rows.length} | ||
estimatedItemSize={ | ||
typeof rowHeight !== 'function' ? rowHeight : undefined | ||
} | ||
rowHeight={ | ||
typeof rowHeight !== 'function' | ||
? rowHeight | ||
: ({ index }) => | ||
rowHeight({ | ||
index, | ||
treeIndex: index, | ||
node: rows[index].node, | ||
path: rows[index].path, | ||
}) | ||
itemSize={({ index }) => | ||
typeof rowHeight === 'function' | ||
? rowHeight({ | ||
index, | ||
treeIndex: index, | ||
node: rows[index].node, | ||
path: rows[index].path, | ||
}) | ||
: rowHeight | ||
} | ||
rowRenderer={({ index, style: rowStyle }) => | ||
{...reactVirtualizedListProps} | ||
> | ||
{({ index, style: rowStyle }) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the issue. It creates new function every render and so every row is mounted and unmounted. You should extract it to extra function and give here only reference to that function so it remains always same. E.g.
Should be good to refactor this so there is not much extra computations... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But I would also consider it bug in react-dnd, but it is a question if it gets addressed anytime soon ... |
||
this.renderRow(rows[index], { | ||
listIndex: index, | ||
style: rowStyle, | ||
|
@@ -728,13 +742,12 @@ class ReactSortableTree extends Component { | |
swapLength, | ||
}) | ||
} | ||
{...reactVirtualizedListProps} | ||
/> | ||
</ScrollZoneVirtualList> | ||
)} | ||
</AutoSizer> | ||
); | ||
} else { | ||
// Render list without react-virtualized | ||
// Render list without react-window | ||
list = rows.map((row, index) => | ||
this.renderRow(row, { | ||
listIndex: index, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also add
itemKey={(index) => getNodeKey(rows[index])}