Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
"react-dnd-html5-backend": "^7.0.1",
"react-lifecycles-compat": "^3.0.4",
"react-sortable-tree": "^2.6.0",
"react-virtualized": "^9.19.1"
"react-virtualized": "^9.19.1",
"react-virtualized-auto-sizer": "^1.0.2",
"react-window": "^1.7.1"
},
"peerDependencies": {
"react": "^16.3.0",
Expand Down
81 changes: 47 additions & 34 deletions src/react-sortable-tree.js
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';
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -585,7 +603,7 @@ class ReactSortableTree extends Component {
return (
<TreeNodeRenderer
style={style}
key={nodeKey}
key="something"
listIndex={listIndex}
getPrevRow={getPrevRow}
lowerSiblingCounts={lowerSiblingCounts}
Expand Down Expand Up @@ -622,7 +640,6 @@ class ReactSortableTree extends Component {
} = mergeTheme(this.props);
const {
searchMatches,
searchFocusTreeIndex,
draggedNode,
draggedDepth,
draggedMinimumTreeIndex,
Expand Down Expand Up @@ -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) {
Expand All @@ -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}
Copy link
Collaborator

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])}

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 }) =>
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

this._render = this._render .bind(this); // in constructor

...

_render({ index, style: rowStyle }) {
    const {
      getNodeKey,
    } = mergeTheme(this.props);
    const {
      searchMatches,
      draggedNode,
      draggedDepth,
      draggedMinimumTreeIndex,
      instanceProps,
    } = this.state;

    const treeData = this.state.draggingTreeData || instanceProps.treeData;

    let rows;
    let swapFrom = null;
    let swapLength = null;
    if (draggedNode && draggedMinimumTreeIndex !== null) {
      const addedResult = memoizedInsertNode({
        treeData,
        newNode: draggedNode,
        depth: draggedDepth,
        minimumTreeIndex: draggedMinimumTreeIndex,
        expandParent: true,
        getNodeKey,
      });

      const swapTo = draggedMinimumTreeIndex;
      swapFrom = addedResult.treeIndex;
      swapLength = 1 + memoizedGetDescendantCount({ node: draggedNode });
      rows = slideRows(
        this.getRows(addedResult.treeData),
        swapFrom,
        swapTo,
        swapLength
      );
    } else {
      rows = this.getRows(treeData);
    }
    const matchKeys = {};
    searchMatches.forEach(({ path }, i) => {
      matchKeys[path[path.length - 1]] = i;
    });
    return this.renderRow(rows[index], {
      listIndex: index,
      style: rowStyle,
      getPrevRow: () => rows[index - 1] || null,
      matchKeys,
      swapFrom,
      swapDepth: draggedDepth,
      swapLength,
    })
  }

...

{this._render} // here

Should be good to refactor this so there is not much extra computations...

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 ...
https://github.com/react-dnd/react-dnd/search?q=Expected+to+find+a+valid+target&type=Issues

this.renderRow(rows[index], {
listIndex: index,
style: rowStyle,
Expand All @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5780,6 +5780,19 @@ frontend-collective-react-dnd-scrollzone@^1.0.1:
react-display-name "^0.2.0"
react-dom "^16.3.0"

frontend-collective-react-dnd-scrollzone@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/frontend-collective-react-dnd-scrollzone/-/frontend-collective-react-dnd-scrollzone-1.0.2.tgz#cf5ed6165335f7d26504a40126f8e972ee644698"
integrity sha512-me/D9PZJq9j/sjEjs/OPmm6V6nbaHbhgeQiwrWu0t35lhwAOKWc+QBzzKKcZQeboYTkgE8UvCD9el+5ANp+g5Q==
dependencies:
hoist-non-react-statics "^3.1.0"
lodash.throttle "^4.0.1"
prop-types "^15.5.9"
raf "^3.2.0"
react "^16.3.0"
react-display-name "^0.2.0"
react-dom "^16.3.0"

fs-access@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/fs-access/-/fs-access-1.0.1.tgz#d6a87f262271cefebec30c553407fb995da8777a"
Expand Down Expand Up @@ -8333,6 +8346,11 @@ mem@^1.1.0:
dependencies:
mimic-fn "^1.0.0"

"memoize-one@>=3.1.1 <6":
version "5.0.0"
resolved "https://registry.yarnpkg.com/memoize-one/-/memoize-one-5.0.0.tgz#d55007dffefb8de7546659a1722a5d42e128286e"
integrity sha512-7g0+ejkOaI9w5x6LvQwmj68kUj6rxROywPSCqmclG/HBacmFnZqhVscQ8kovkn9FBCNJmOz6SY42+jnvZzDWdw==

memory-fs@^0.4.0, memory-fs@~0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/memory-fs/-/memory-fs-0.4.1.tgz#3a9a20b8462523e447cfbc7e8bb80ed667bfc552"
Expand Down Expand Up @@ -10906,6 +10924,11 @@ react-treebeard@^3.1.0:
shallowequal "^1.1.0"
velocity-react "^1.4.1"

react-virtualized-auto-sizer@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/react-virtualized-auto-sizer/-/react-virtualized-auto-sizer-1.0.2.tgz#a61dd4f756458bbf63bd895a92379f9b70f803bd"
integrity sha512-MYXhTY1BZpdJFjUovvYHVBmkq79szK/k7V3MO+36gJkWGkrXKtyr4vCPtpphaTLRAdDNoYEYFZWE8LjN+PIHNg==

react-virtualized@^9.13.0, react-virtualized@^9.19.1:
version "9.21.0"
resolved "https://registry.yarnpkg.com/react-virtualized/-/react-virtualized-9.21.0.tgz#8267c40ffb48db35b242a36dea85edcf280a6506"
Expand All @@ -10918,6 +10941,14 @@ react-virtualized@^9.13.0, react-virtualized@^9.19.1:
prop-types "^15.6.0"
react-lifecycles-compat "^3.0.4"

react-window@^1.7.1:
version "1.7.1"
resolved "https://registry.yarnpkg.com/react-window/-/react-window-1.7.1.tgz#c1db640415b97b85bc0a1c66eb82dadabca39b86"
integrity sha512-y4/Qc98agCtHulpeI5b6K2Hh8J7TeZIfvccBVesfqOFx4CS+TSUpnJl1/ipeXzhfvzPwvVEmaU/VosQ6O5VhTg==
dependencies:
"@babel/runtime" "^7.0.0"
memoize-one ">=3.1.1 <6"

react@^16.3.0:
version "16.6.3"
resolved "https://registry.yarnpkg.com/react/-/react-16.6.3.tgz#25d77c91911d6bbdd23db41e70fb094cc1e0871c"
Expand Down