Skip to content

Commit bdf52d3

Browse files
authored
Merge pull request #46 from rbenton/virtuoso-props
Add virtuosoProps property to ReactSortableTree
2 parents 39159de + 9af1fe6 commit bdf52d3

File tree

7 files changed

+854
-748
lines changed

7 files changed

+854
-748
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ yarn-error.log
2626

2727
.vscode
2828
.eslintcache
29+
.nvmrc

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ export default class Tree extends Component {
125125
| scaffoldBlockPxWidth | number | The width of the blocks containing the lines representing the structure of the tree. Defaults to `44`. |
126126
| nodeContentRenderer | any | Override the default component ([`NodeRendererDefault`](./src/node-renderer-default.tsx)) for rendering nodes (but keep the scaffolding generator). This is a last resort for customization - most custom styling should be able to be solved with `generateNodeProps`, a `theme` or CSS rules. If you must use it, is best to copy the component in `node-renderer-default.tsx` to use as a base, and customize as needed. |
127127
| placeholderRenderer | any | Override the default placeholder component ([`PlaceholderRendererDefault`](./src/placeholder-renderer-default.tsx)) which is displayed when the tree is empty. This is an advanced option, and in most cases should probably be solved with a `theme` or custom CSS instead. |
128+
| virtuosoProps | object | Properties to set directly on the underlying [Virtuoso](https://virtuoso.dev/virtuoso-api-reference/) component.
129+
| virtuosoRef | ref | A [Ref](https://react.dev/learn/manipulating-the-dom-with-refs) via which to access the underlying [Virtuoso](https://virtuoso.dev/virtuoso-api-reference/) component's methods.
130+
128131
## Data Helper Functions
129132

130133
Need a hand turning your flat data into nested tree data?

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"lodash.isequal": "^4.5.0",
7171
"react-dnd": "14.0.4",
7272
"react-dnd-html5-backend": "^14.1.0",
73-
"react-virtuoso": "^4.1.0"
73+
"react-virtuoso": "^4.3.1"
7474
},
7575
"devDependencies": {
7676
"@babel/core": "^7.21.0",

src/react-sortable-tree.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import withScrolling, {
99
import isEqual from 'lodash.isequal'
1010
import { DndContext, DndProvider } from 'react-dnd'
1111
import { HTML5Backend } from 'react-dnd-html5-backend'
12-
import { Virtuoso, VirtuosoHandle } from 'react-virtuoso'
12+
import { Virtuoso, VirtuosoHandle, VirtuosoProps } from 'react-virtuoso'
1313
import NodeRendererDefault from './node-renderer-default'
1414
import PlaceholderRendererDefault from './placeholder-renderer-default'
1515
import './react-sortable-tree.css'
@@ -186,6 +186,7 @@ class ReactSortableTree extends Component {
186186
super(props)
187187

188188
this.listRef = props.virtuosoRef || React.createRef()
189+
this.listProps = props.virtuosoProps || {}
189190

190191
const { dndType, nodeContentRenderer, treeNodeRenderer, slideRegionSize } =
191192
mergeTheme(props)
@@ -721,6 +722,7 @@ class ReactSortableTree extends Component {
721722
swapLength,
722723
})
723724
}
725+
{...this.listProps}
724726
/>
725727
)
726728
}
@@ -814,6 +816,10 @@ export type ReactSortableTreeProps = {
814816
// Class name for the container wrapping the tree
815817
className?: string
816818

819+
// Properties passed directly to the underlying Virtuoso component
820+
// See https://virtuoso.dev/virtuoso-api-reference/#virtuoso-properties
821+
virtuosoProps?: VirtuosoProps
822+
817823
// Ref for Virtuoso component
818824
// Use virtuosoRef when you wont to use virtuoso handler
819825
// (ex. scrollTo scrollToIndex)

stories/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import ThemesExample from './themes'
1818
import TouchSupportExample from './touch-support'
1919
import TreeDataIOExample from './tree-data-io'
2020
import TreeToTreeExample from './tree-to-tree'
21+
import VirtuosoPropsExample from './virtuoso-props'
2122

2223
storiesOf('Basics', module)
2324
.add('Minimal implementation', () => <BarebonesExample />)
@@ -29,6 +30,7 @@ storiesOf('Basics', module)
2930
.add('Themes', () => <ThemesExample />)
3031
.add('Callbacks', () => <CallbacksExample />)
3132
.add('Row direction support', () => <RowDirectionExample />)
33+
.add('Virtuoso properties', () => <VirtuosoPropsExample />)
3234

3335
storiesOf('Advanced', module)
3436
.add('Drag from external source', () => <ExternalNodeExample />)

stories/virtuoso-props.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React, { Component } from 'react'
2+
import SortableTree from '../src'
3+
// In your own app, you would need to use import styles once in the app
4+
// import 'react-sortable-tree/styles.css';
5+
6+
export default class App extends Component {
7+
constructor(props) {
8+
super(props)
9+
10+
this.virtuosoRef = React.createRef()
11+
12+
this.state = {
13+
isScrolling: false,
14+
scrollTop: 0,
15+
treeData: [
16+
{
17+
title: 'Chicken',
18+
expanded: true,
19+
children: [
20+
{ title: 'Egg' },
21+
{ title: 'Egg' },
22+
{ title: 'Egg' },
23+
{ title: 'Egg' },
24+
{ title: 'Egg' },
25+
{ title: 'Egg' },
26+
{ title: 'Egg' },
27+
],
28+
},
29+
],
30+
}
31+
}
32+
33+
isScrolling = scrollingState => {
34+
if (this.virtuosoRef?.current) {
35+
this.virtuosoRef?.current.getState(virtuosoState => {
36+
this.setState(prevState => ({
37+
...prevState,
38+
...{
39+
isScrolling: scrollingState,
40+
scrollTop: virtuosoState.scrollTop,
41+
},
42+
}))
43+
})
44+
}
45+
}
46+
47+
render() {
48+
return (
49+
<div>
50+
<div style={{ height: 300 }}>
51+
<SortableTree
52+
treeData={this.state.treeData}
53+
onChange={treeData => this.setState({ treeData })}
54+
virtuosoRef={this.virtuosoRef}
55+
virtuosoProps={{ isScrolling: this.isScrolling }}
56+
/>
57+
</div>
58+
<hr />
59+
Current scrollTop:{' '}
60+
{this.state.isScrolling ? '...' : this.state.scrollTop}
61+
</div>
62+
)
63+
}
64+
}

0 commit comments

Comments
 (0)