Skip to content

Commit 50024c6

Browse files
authored
Merge pull request nosferatu500#10 from harrylepotter-win/master
added rowHeight prop, fixed issue with canDrop not returning correct nextParent
2 parents 6cf2290 + 61ea0bc commit 50024c6

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

src/react-sortable-tree.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const mergeTheme = (props) => {
5050
placeholderRenderer: PlaceholderRendererDefault,
5151
scaffoldBlockPxWidth: 44,
5252
slideRegionSize: 100,
53+
rowHeight: 62,
5354
treeNodeRenderer: TreeNode,
5455
}
5556
for (const propKey of Object.keys(overridableDefaults)) {
@@ -563,6 +564,7 @@ class ReactSortableTree extends Component {
563564
scaffoldBlockPxWidth,
564565
searchFocusOffset,
565566
rowDirection,
567+
rowHeight,
566568
} = mergeTheme(this.props)
567569
const TreeNodeRenderer = this.treeNodeRenderer
568570
const NodeContentRenderer = this.nodeContentRenderer
@@ -597,6 +599,7 @@ class ReactSortableTree extends Component {
597599
return (
598600
<TreeNodeRenderer
599601
style={style}
602+
rowHeight={rowHeight}
600603
key={nodeKey}
601604
listIndex={listIndex}
602605
getPrevRow={getPrevRow}
@@ -862,6 +865,10 @@ export type ReactSortableTreeProps = {
862865
placeholderRenderer: any
863866
}
864867

868+
// Sets the height of a given tree row item in pixels. Can either be a number
869+
// or a function to calculate dynamically
870+
rowHeight?: number | ((treeIndex: number, node: any, path: any[]) => number)
871+
865872
// Determine the unique key used to identify each node and
866873
// generate the `path` array passed in callbacks.
867874
// By default, returns the index in the tree (omitting hidden nodes).

src/tree-node.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface TreeRendererProps {
2222
scaffoldBlockPxWidth: number
2323
lowerSiblingCounts: number[]
2424
rowDirection?: 'ltr' | 'rtl' | string | undefined
25+
rowHeight: number | ((treeIndex: number, node: any, path: any[]) => number)
2526

2627
listIndex: number
2728
children: JSX.Element[]
@@ -63,6 +64,7 @@ const TreeNode: React.FC<TreeRendererProps> = (props) => {
6364
draggedNode,
6465
canDrop,
6566
treeIndex,
67+
rowHeight,
6668
treeId: _treeId, // Delete from otherProps
6769
getPrevRow: _getPrevRow, // Delete from otherProps
6870
node: _node, // Delete from otherProps
@@ -183,9 +185,14 @@ const TreeNode: React.FC<TreeRendererProps> = (props) => {
183185
? { right: scaffoldBlockPxWidth * scaffoldBlockCount }
184186
: { left: scaffoldBlockPxWidth * scaffoldBlockCount }
185187

188+
let calculatedRowHeight = rowHeight
189+
if (typeof rowHeight === 'function') {
190+
calculatedRowHeight = rowHeight(treeIndex, _node, _path)
191+
}
186192
return connectDropTarget(
187193
<div
188194
{...otherProps}
195+
style={{ height: `${calculatedRowHeight}px` }}
189196
className={classnames('rst__node', rowDirectionClass ?? '')}>
190197
{scaffold}
191198

src/utils/dnd-manager.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,23 +183,15 @@ const canDrop = (
183183

184184
if (typeof treeRefcanDrop === 'function') {
185185
const { node } = monitor.getItem()
186-
const addedResult = memoizedInsertNode({
187-
treeData: draggingTreeData || treeReftreeData,
188-
newNode: node,
189-
depth: targetDepth,
190-
getNodeKey,
191-
minimumTreeIndex: dropTargetProps.listIndex,
192-
expandParent: true,
193-
})
194186

195187
return treeRefcanDrop({
196188
node,
197189
prevPath: monitor.getItem().path,
198190
prevParent: monitor.getItem().parentNode,
199191
prevTreeIndex: monitor.getItem().treeIndex, // Equals -1 when dragged from external tree
200-
nextPath: addedResult.path,
201-
nextParent: addedResult.parentNode,
202-
nextTreeIndex: addedResult.treeIndex,
192+
nextPath: dropTargetProps.children.props.path,
193+
nextParent: dropTargetProps.children.props.parentNode,
194+
nextTreeIndex: dropTargetProps.children.props.treeIndex,
203195
})
204196
}
205197

0 commit comments

Comments
 (0)