Skip to content

Commit 6986a23

Browse files
committed
feat(tree-to-tree): Enable tree-to-tree drag-and-drop
Also makes implementation of external nodes more lightweight BREAKING CHANGE: `dropCancelled` and `addNewItem` callbacks on external nodes wrapped with `dndWrapExternalSource` are now ignored. Drag-and-drop now works without them.
1 parent 7bfe115 commit 6986a23

10 files changed

+821
-139
lines changed

examples/storybooks/__snapshots__/storyshots.test.js.snap

+420
Large diffs are not rendered by default.

examples/storybooks/external-node.js

+2-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { DragDropContext } from 'react-dnd';
33
import HTML5Backend from 'react-dnd-html5-backend';
44
import {
55
SortableTreeWithoutDndContext as SortableTree,
6-
insertNode,
76
dndWrapExternalSource,
87
} from '../../src';
98

@@ -47,22 +46,9 @@ class App extends Component {
4746
</div>
4847
<YourExternalNodeComponent
4948
node={{ title: 'External node' }}
50-
addNewItem={newItem => {
51-
const { treeData } = insertNode({
52-
treeData: this.state.treeData,
53-
newNode: newItem.node,
54-
depth: newItem.depth,
55-
minimumTreeIndex: newItem.minimumTreeIndex,
56-
expandParent: true,
57-
getNodeKey: ({ treeIndex }) => treeIndex,
58-
});
59-
this.setState({ treeData });
60-
}}
49+
addNewItem={() => {}}
6150
// Update the tree appearance post-drag
62-
dropCancelled={() =>
63-
this.setState(state => ({
64-
treeData: state.treeData.concat(),
65-
}))}
51+
dropCancelled={() => {}}
6652
/>
6753
↑ drag this
6854
</div>

examples/storybooks/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import BarebonesExample from './barebones';
77
import AddRemoveExample from './add-remove';
88
import ExternalNodeExample from './external-node';
99
import TouchSupportExample from './touch-support';
10+
import TreeToTreeExample from './tree-to-tree';
1011

1112
const wrapWithSource = (node, src) =>
1213
<div>
@@ -36,4 +37,7 @@ storiesOf('Advanced', module)
3637
)
3738
.add('Touch support (Experimental)', () =>
3839
wrapWithSource(<TouchSupportExample />, 'touch-support.js')
40+
)
41+
.add('Tree-to-tree dragging', () =>
42+
wrapWithSource(<TreeToTreeExample />, 'tree-to-tree.js')
3943
);

examples/storybooks/tree-to-tree.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import React, { Component } from 'react';
2+
// import { DragDropContext } from 'react-dnd';
3+
// import HTML5Backend from 'react-dnd-html5-backend';
4+
import SortableTree from '../../src';
5+
// SortableTreeWithoutDndContext as SortableTree,
6+
// insertNode,
7+
// dndWrapExternalSource,
8+
9+
const externalNodeType = 'yourNodeType';
10+
11+
class App extends Component {
12+
constructor(props) {
13+
super(props);
14+
15+
this.state = {
16+
treeData1: [
17+
{ title: 'node1', children: [{ title: 'Child node' }] },
18+
{ title: 'node2' },
19+
],
20+
treeData2: [{ title: 'node3' }, { title: 'node4' }],
21+
};
22+
}
23+
24+
render() {
25+
return (
26+
<div>
27+
<div
28+
style={{
29+
height: 350,
30+
width: 350,
31+
float: 'left',
32+
border: 'solid black 1px',
33+
}}
34+
>
35+
<SortableTree
36+
treeData={this.state.treeData1}
37+
onChange={treeData1 => this.setState({ treeData1 })}
38+
dndType={externalNodeType}
39+
/>
40+
</div>
41+
42+
<div
43+
style={{
44+
height: 350,
45+
width: 350,
46+
float: 'left',
47+
border: 'solid black 1px',
48+
}}
49+
>
50+
<SortableTree
51+
treeData={this.state.treeData2}
52+
onChange={treeData2 => this.setState({ treeData2 })}
53+
dndType={externalNodeType}
54+
/>
55+
</div>
56+
57+
<div style={{ clear: 'both' }} />
58+
</div>
59+
);
60+
}
61+
}
62+
63+
export default App;

0 commit comments

Comments
 (0)