Skip to content

Commit cb03918

Browse files
committed
chore: fix incomplete getstarted docs
1 parent e4f0477 commit cb03918

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

packages/docs/docs/getstarted.mdx

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,78 @@ is generic and can contain any kind of data such as objects or strings. The envi
160160
implement the `getItemTitle` method, which derives the title of the item given a [TreeItem](/docs/api/interfaces/TreeItem)
161161
instance. In our example, where `data` is just a string that contains the title, we can use the trivial
162162
implementation of `getItemTitle={item => item.data}`.
163+
164+
We then create a new `StaticTreeDataProvider` instance that uses this data structure as data source. See the
165+
[public interface](/docs/api/classes/StaticTreeDataProvider) of the `StaticTreeDataProvider` to get details on its constructor and abilities.
166+
167+
```typescript jsx
168+
const dataProvider = new StaticTreeDataProvider(items, (item, newName) => ({ ...item, data: newName }));
169+
```
170+
171+
The first constructor parameter is a record specifying available items. The second is an optional callback with which
172+
you can customize how renaming an item works. It is called when the user renames an item (by using the F2 hotkey),
173+
and how the item data payload is changed by that rename operation. This is necessary since react-complex-tree does not
174+
by itself know which part of the item payload contains the name. This parameter has the type `(item: TreeItem<T>, newName: string) => TreeItem<T>`.
175+
176+
:::tip Handling drag events
177+
178+
To handle drag events, call `dataProvider.onDidChangeTreeData` like that:
179+
180+
```typescript
181+
const listener = (changedItemIds: (string | number)[]) => {
182+
const changedItems = changedItemIds.map(dataProvider.getTreeItem)
183+
console.log("Changed items:", changedItems)
184+
}
185+
dataProvider.onDidChangeTreeData(listener)
186+
```
187+
188+
You also need to set the property `canDragAndDrop` and either `canDropOnFolder` or `canReorderItems` on the tree environment.
189+
190+
:::
191+
192+
Finally, you can mount a `Tree` component within an `UncontrolledTreeEnvironment` that uses your dataProvider
193+
as data source.
194+
195+
```jsx live
196+
function App() {
197+
const items = {
198+
root: {
199+
index: 'root',
200+
isFolder: true,
201+
children: ['child1', 'child2'],
202+
data: 'Root item',
203+
},
204+
child1: {
205+
index: 'child1',
206+
children: [],
207+
data: 'Child item 1',
208+
},
209+
child2: {
210+
index: 'child2',
211+
isFolder: true,
212+
children: ['child3'],
213+
data: 'Child item 2',
214+
},
215+
child3: {
216+
index: 'child3',
217+
children: [],
218+
data: 'Child item 3',
219+
},
220+
};
221+
222+
const dataProvider = new StaticTreeDataProvider(items, (item, newName) => ({ ...item, data: newName }));
223+
224+
return (
225+
<UncontrolledTreeEnvironment
226+
dataProvider={dataProvider}
227+
getItemTitle={item => item.data}
228+
viewState={{}}
229+
canDragAndDrop={true}
230+
canDropOnFolder={true}
231+
canReorderItems={true}
232+
>
233+
<Tree treeId="tree-2" rootItem="root" treeLabel="Tree Example" />
234+
</UncontrolledTreeEnvironment>
235+
);
236+
}
237+
```

0 commit comments

Comments
 (0)