You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
BREAKING CHANGE: `dndWrapExternalSource` api no longer exists.
You can achieve the same functionality and more with react-dnd
APIs, as demonstrated in the storybook example.
Copy file name to clipboardExpand all lines: README.md
-82
Original file line number
Diff line number
Diff line change
@@ -83,88 +83,6 @@ Notable among the available functions:
83
83
Documentation for each method is only available in the code at this time. You can also refer to the tests for simple usage examples.
84
84
If your hobbies happen to include writing documentation, by all means submit a pull request. It would really help out.
85
85
86
-
## Adding External Nodes
87
-
88
-
### How to wrap your own component as an external node
89
-
90
-
To use your own components as external nodes, you can call `dndWrapExternalSource`, exported from [`drag-and-drop-utils.js`](https://github.com/fritz-c/react-sortable-tree/blob/master/src/utils/drag-and-drop-utils.js), like in this example below, as long as you also pass the exact same [react-dnd type](http://react-dnd.github.io/react-dnd/docs-overview.html) as set for your tree component, so your custom components can become valid react-dnd [DragSources](http://react-dnd.github.io/react-dnd/docs-drag-source.html), that can be dropped in to add nodes to your own tree component.
__NOTE:__ You need to implement a `dropCancelled` method and an `addNewItem` method, passed as props to your external node component from your parent component, so that your tree-component can effectively respond to your external node. Check out the external node demo for an example implementation. A simple example below:
106
-
107
-
```jsx
108
-
importReact, { Component } from'react'
109
-
import {
110
-
SortableTreeWithoutDndContextasSortableTree,
111
-
insertNode,
112
-
} from'react-sortable-tree'
113
-
import YourExternalNodeComponent, {
114
-
externalNodeType,
115
-
} from'./YourExternalNodeComponent.js'
116
-
117
-
classAppextendsComponent {
118
-
constructor(props) {
119
-
super(props)
120
-
121
-
this.state= {
122
-
treeData: [
123
-
{ title:'node1' },
124
-
{ title:'node2' },
125
-
],
126
-
}
127
-
}
128
-
129
-
render() {
130
-
return (
131
-
<div>
132
-
<div style={{ height:200 }}>
133
-
<SortableTree
134
-
treeData={this.state.treeData}
135
-
onChange={treeData=>this.setState({ treeData })}
136
-
dndType={externalNodeType}
137
-
/>
138
-
</div>
139
-
140
-
<YourExternalNodeComponent
141
-
node={{ title:'I am an external node' }}
142
-
addNewItem={(newItem) => {
143
-
const { treeData } =insertNode({
144
-
treeData:this.state.treeData,
145
-
newNode:newItem.node,
146
-
depth:newItem.depth,
147
-
minimumTreeIndex:newItem.minimumTreeIndex,
148
-
expandParent:true,
149
-
getNodeKey: ({ treeIndex }) => treeIndex,
150
-
});
151
-
this.setState({ treeData });
152
-
}}
153
-
// Update the tree appearance post-drag
154
-
dropCancelled={() =>this.setState(state=> ({
155
-
treeData:state.treeData.concat(),
156
-
}))}
157
-
/>
158
-
</div>
159
-
)
160
-
}
161
-
}
162
-
```
163
-
164
-
165
-
In addition, the external node wrapper assumes you are using the tree component as `SortableTreeWithoutDndContext`
0 commit comments