Skip to content

Commit b1827c1

Browse files
committed
feat: change behavior for dropping at bottom of open folders [#148]
1 parent f0bdcae commit b1827c1

File tree

4 files changed

+78
-8
lines changed

4 files changed

+78
-8
lines changed

next-release-notes.md

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
<!--
21
### Breaking Changes
3-
4-
### Features
5-
6-
### Bug Fixes and Improvements
7-
8-
### Other Changes
9-
-->
2+
- Changed the behavior for dropping an item at the bottom of an open folder has changed, and will now drop
3+
into the open folder at its top, instead of the parent folder below the open folder. See discussion at #148 for details.
4+
You can opt out of this behavior by setting the `canDropBelowOpenFolders` prop on the tree environment (#148).

packages/core/src/controlledEnvironment/useOnDragOverTreeHandler.ts

+13
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export const useOnDragOverTreeHandler = (
9595
items,
9696
canReorderItems,
9797
trees,
98+
canDropBelowOpenFolders,
9899
} = useTreeEnvironment();
99100
const getParentOfLinearItem = useGetGetParentOfLinearItem();
100101
const isDescendant = useIsDescendant();
@@ -188,6 +189,18 @@ export const useOnDragOverTreeHandler = (
188189
return;
189190
}
190191

192+
const nextItem = linearItems[treeId][linearIndex + 1];
193+
const redirectToFirstChild =
194+
!canDropBelowOpenFolders &&
195+
nextItem &&
196+
targetItem.depth === nextItem.depth - 1 &&
197+
offset === 'bottom';
198+
if (redirectToFirstChild) {
199+
targetItem = nextItem;
200+
linearIndex += 1;
201+
offset = 'top';
202+
}
203+
191204
const { depth } = targetItem;
192205
const targetItemData = items[targetItem.item];
193206

packages/core/src/stories/DndLimitations.stories.tsx renamed to packages/core/src/stories/DndConfigurability.stories.tsx

+51
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ export default {
99
title: 'Core/Drag-and-Drop Configurability',
1010
} as Meta;
1111

12+
export const Default = () => (
13+
<UncontrolledTreeEnvironment<string>
14+
canDragAndDrop
15+
canDropOnFolder
16+
canReorderItems
17+
dataProvider={
18+
new StaticTreeDataProvider(longTree.items, (item, data) => ({
19+
...item,
20+
data,
21+
}))
22+
}
23+
getItemTitle={item => item.data}
24+
viewState={{
25+
'tree-1': {
26+
expandedItems: ['Fruit'],
27+
},
28+
}}
29+
>
30+
<Tree treeId="tree-1" rootItem="root" treeLabel="Tree Example" />
31+
</UncontrolledTreeEnvironment>
32+
);
33+
1234
export const NoDragAndDrop = () => (
1335
<UncontrolledTreeEnvironment<string>
1436
dataProvider={new StaticTreeDataProvider(longTree.items)}
@@ -179,3 +201,32 @@ export const AllowDroppingOnlyOnItemsStartingWithA = () => (
179201
<Tree treeId="tree-1" rootItem="root" treeLabel="Tree Example" />
180202
</UncontrolledTreeEnvironment>
181203
);
204+
205+
export const CanDropBelowOpenFolders = () => (
206+
<UncontrolledTreeEnvironment<string>
207+
canDragAndDrop
208+
canDropOnFolder
209+
canReorderItems
210+
canDropBelowOpenFolders
211+
dataProvider={
212+
new StaticTreeDataProvider(longTree.items, (item, data) => ({
213+
...item,
214+
data,
215+
}))
216+
}
217+
getItemTitle={item => item.data}
218+
viewState={{
219+
'tree-1': {
220+
expandedItems: ['Fruit'],
221+
},
222+
}}
223+
>
224+
<p>
225+
In this sample, canDropBelowOpenFolders is enabled. Try dropping Orange on
226+
the bottom part of the Fruit folder. It will be dropped in the parent
227+
folder, below the open Fruit folder. If you disable this option, it
228+
dropping there will drop at the top of the contents of the Fruit folder.
229+
</p>
230+
<Tree treeId="tree-1" rootItem="root" treeLabel="Tree Example" />
231+
</UncontrolledTreeEnvironment>
232+
);

packages/core/src/types.ts

+11
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ export interface TreeCapabilities<T = any, C extends string = never> {
176176
itemTitle: string
177177
) => boolean;
178178
showLiveDescription?: boolean;
179+
180+
/**
181+
* See Issue #148 or the sample at
182+
* https://rct.lukasbach.com/storybook/?path=/story/core-basic-examples--single-tree?path=/story/core-drag-and-drop-configurability--can-drop-below-open-folders
183+
* for details.
184+
*
185+
* If enabled, dropping at the bottom of an open folder will drop the items
186+
* in the parent folder below the hovered item instead of inside the folder
187+
* at the top.
188+
*/
189+
canDropBelowOpenFolders?: boolean;
179190
}
180191

181192
export type IndividualTreeViewState<C extends string = never> = {

0 commit comments

Comments
 (0)