Skip to content

Commit cbca55b

Browse files
committed
refactor: Move dnd utils to manager that can access tree settings directly
1 parent afe5c0b commit cbca55b

File tree

5 files changed

+318
-299
lines changed

5 files changed

+318
-299
lines changed

src/node-renderer-default.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,8 @@ class NodeRendererDefault extends Component {
3838
className,
3939
style,
4040
didDrop,
41-
/* eslint-disable no-unused-vars */
42-
isOver: _isOver, // Not needed, but preserved for other renderers
43-
parentNode: _parentNode, // Needed for drag-and-drop utils
44-
endDrag: _endDrag, // Needed for drag-and-drop utils
45-
startDrag: _startDrag, // Needed for drag-and-drop utils
46-
treeId: _treeId, // Needed for drag-and-drop utils
47-
/* eslint-enable no-unused-vars */
41+
isOver, // Not needed, but preserved for other renderers
42+
parentNode, // Needed for dndManager
4843
...otherProps
4944
} = this.props;
5045
const nodeTitle = title || node.title;
@@ -223,10 +218,7 @@ NodeRendererDefault.propTypes = {
223218
// Drag source
224219
connectDragPreview: PropTypes.func.isRequired,
225220
connectDragSource: PropTypes.func.isRequired,
226-
parentNode: PropTypes.shape({}), // Needed for drag-and-drop utils
227-
startDrag: PropTypes.func.isRequired, // Needed for drag-and-drop utils
228-
endDrag: PropTypes.func.isRequired, // Needed for drag-and-drop utils
229-
treeId: PropTypes.string.isRequired, // Needed for drag-and-drop utils
221+
parentNode: PropTypes.shape({}), // Needed for dndManager
230222
isDragging: PropTypes.bool.isRequired,
231223
didDrop: PropTypes.bool.isRequired,
232224
draggedNode: PropTypes.shape({}),

src/react-sortable-tree.js

+10-26
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@ import {
3232
defaultGetNodeKey,
3333
defaultSearchMethod,
3434
} from './utils/default-handlers';
35-
import {
36-
dndWrapRoot,
37-
dndWrapSource,
38-
dndWrapTarget,
39-
dndWrapPlaceholder,
40-
} from './utils/drag-and-drop-utils';
35+
import DndManager from './utils/dnd-manager';
4136
import styles from './react-sortable-tree.scss';
4237

4338
let treeIdCounter = 1;
@@ -54,16 +49,17 @@ class ReactSortableTree extends Component {
5449
treeData,
5550
} = props;
5651

52+
this.dndManager = new DndManager(this);
53+
5754
// Wrapping classes for use with react-dnd
5855
this.treeId = `rst__${treeIdCounter}`;
5956
treeIdCounter += 1;
6057
this.dndType = dndType || this.treeId;
61-
this.nodeContentRenderer = dndWrapSource(nodeContentRenderer, this.dndType);
62-
this.treePlaceholderRenderer = dndWrapPlaceholder(
63-
TreePlaceholder,
64-
this.dndType
58+
this.nodeContentRenderer = this.dndManager.wrapSource(nodeContentRenderer);
59+
this.treePlaceholderRenderer = this.dndManager.wrapPlaceholder(
60+
TreePlaceholder
6561
);
66-
this.treeNodeRenderer = dndWrapTarget(TreeNode, this.dndType);
62+
this.treeNodeRenderer = this.dndManager.wrapTarget(TreeNode);
6763

6864
// Prepare scroll-on-drag options for this list
6965
if (isVirtualized) {
@@ -401,10 +397,7 @@ class ReactSortableTree extends Component {
401397
) {
402398
const {
403399
canDrag,
404-
canDrop,
405400
generateNodeProps,
406-
getNodeKey,
407-
maxDepth,
408401
scaffoldBlockPxWidth,
409402
searchFocusOffset,
410403
} = this.props;
@@ -434,7 +427,6 @@ class ReactSortableTree extends Component {
434427
scaffoldBlockPxWidth,
435428
node,
436429
path,
437-
treeId: this.treeId,
438430
};
439431

440432
return (
@@ -443,24 +435,16 @@ class ReactSortableTree extends Component {
443435
key={nodeKey}
444436
listIndex={listIndex}
445437
getPrevRow={getPrevRow}
446-
treeData={this.state.draggingTreeData || this.state.treeData}
447-
getNodeKey={getNodeKey}
448-
customCanDrop={canDrop}
449438
lowerSiblingCounts={lowerSiblingCounts}
450439
swapFrom={this.state.swapFrom}
451440
swapLength={this.state.swapLength}
452441
swapDepth={this.state.swapDepth}
453-
maxDepth={maxDepth}
454-
dragHover={this.dragHover}
455-
drop={this.drop}
456442
{...sharedProps}
457443
>
458444
<NodeContentRenderer
459445
parentNode={parentNode}
460446
isSearchMatch={isSearchMatch}
461447
isSearchFocus={isSearchFocus}
462-
startDrag={this.startDrag}
463-
endDrag={this.endDrag}
464448
canDrag={rowCanDrag}
465449
toggleChildrenVisibility={this.toggleChildrenVisibility}
466450
{...sharedProps}
@@ -603,7 +587,7 @@ ReactSortableTree.propTypes = {
603587
scaffoldBlockPxWidth: PropTypes.number,
604588

605589
// Maximum depth nodes can be inserted at. Defaults to infinite.
606-
maxDepth: PropTypes.number,
590+
maxDepth: PropTypes.number, // eslint-disable-line react/no-unused-prop-types
607591

608592
// The method used to search nodes.
609593
// Defaults to a function that uses the `searchQuery` string to search for nodes with
@@ -658,7 +642,7 @@ ReactSortableTree.propTypes = {
658642
canDrag: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]),
659643

660644
// Determine whether a node can be dropped based on its path and parents'.
661-
canDrop: PropTypes.func,
645+
canDrop: PropTypes.func, // eslint-disable-line react/no-unused-prop-types
662646

663647
// Called after children nodes collapsed or expanded.
664648
onVisibilityToggle: PropTypes.func,
@@ -700,4 +684,4 @@ ReactSortableTree.contextTypes = {
700684
// see: https://github.com/gaearon/react-dnd/issues/186
701685
export { ReactSortableTree as SortableTreeWithoutDndContext };
702686

703-
export default dndWrapRoot(ReactSortableTree);
687+
export default DndManager.wrapRoot(ReactSortableTree);

src/tree-node.js

+6-22
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,9 @@ class TreeNode extends Component {
1717
draggedNode,
1818
canDrop,
1919
treeIndex,
20-
customCanDrop, // Delete from otherProps
21-
dragHover, // Delete from otherProps
22-
getNodeKey, // Delete from otherProps
2320
getPrevRow, // Delete from otherProps
24-
maxDepth, // Delete from otherProps
2521
node, // Delete from otherProps
2622
path, // Delete from otherProps
27-
treeData, // Delete from otherProps
28-
treeId, // Delete from otherProps
29-
drop, // Delete from otherProps
3023
...otherProps
3124
} = this.props;
3225

@@ -147,17 +140,10 @@ TreeNode.defaultProps = {
147140
swapLength: null,
148141
canDrop: false,
149142
draggedNode: null,
150-
customCanDrop: null,
151-
maxDepth: null,
152-
treeData: null,
153143
};
154144

155145
TreeNode.propTypes = {
156146
treeIndex: PropTypes.number.isRequired,
157-
node: PropTypes.shape({}).isRequired,
158-
path: PropTypes.arrayOf(
159-
PropTypes.oneOfType([PropTypes.string, PropTypes.number])
160-
).isRequired,
161147
swapFrom: PropTypes.number,
162148
swapDepth: PropTypes.number,
163149
swapLength: PropTypes.number,
@@ -173,14 +159,12 @@ TreeNode.propTypes = {
173159
canDrop: PropTypes.bool,
174160
draggedNode: PropTypes.shape({}),
175161

176-
customCanDrop: PropTypes.func, // used in drag-and-drop-utils
177-
dragHover: PropTypes.func.isRequired, // used in drag-and-drop-utils
178-
getNodeKey: PropTypes.func.isRequired, // used in drag-and-drop-utils
179-
getPrevRow: PropTypes.func.isRequired, // used in drag-and-drop-utils
180-
drop: PropTypes.func.isRequired, // used in drag-and-drop-utils
181-
maxDepth: PropTypes.number, // used in drag-and-drop-utils
182-
treeData: PropTypes.arrayOf(PropTypes.object), // used in drag-and-drop-utils
183-
treeId: PropTypes.string.isRequired, // used in drag-and-drop-utils
162+
// used in dndManager
163+
getPrevRow: PropTypes.func.isRequired,
164+
node: PropTypes.shape({}).isRequired,
165+
path: PropTypes.arrayOf(
166+
PropTypes.oneOfType([PropTypes.string, PropTypes.number])
167+
).isRequired,
184168
};
185169

186170
export default TreeNode;

0 commit comments

Comments
 (0)