Skip to content

Commit eb03cce

Browse files
author
Andrew Russell
committed
fix: children elements in nested lists drag parent elements.
1 parent 45dcfcb commit eb03cce

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

src/.stories/Storybook.scss

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@
112112
}
113113
}
114114

115+
// Nested
116+
.category {
117+
height: auto;
118+
margin-bottom: 20px;
119+
120+
.categoryHeader {
121+
display: flex;
122+
flex-flow: row nowrap;
123+
align-items: center;
124+
padding: 10px;
125+
border-bottom: 1px solid #EFEFEF;
126+
}
127+
128+
.categoryList {
129+
height: auto;
130+
}
131+
}
132+
115133
// Divider
116134
.divider {
117135
padding: 10px 20px;

src/.stories/index.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,26 @@ const Item = SortableElement((props) => {
2727
<div className={props.className} style={{
2828
height: props.height
2929
}}>
30-
{props.shouldUseDragHandle && <Handle/>}
30+
{props.shouldUseDragHandle && <Handle/>}
3131
<div className={style.wrapper}>
3232
<span>Item</span> {props.value}
3333
</div>
3434
</div>
3535
)
3636
});
3737

38+
const Category = SortableElement((props) => {
39+
return (
40+
<div className={style.category}>
41+
<div className={style.categoryHeader}>
42+
<Handle/>
43+
<span>Category {props.value}</span>
44+
</div>
45+
<ListWrapper component={SortableList} className={style.categoryList} items={getItems(5, 59)} shouldUseDragHandle={true} helperClass={style.stylizedHelper} />
46+
</div>
47+
)
48+
});
49+
3850
class ListWrapper extends Component {
3951
constructor({items}) {
4052
super();
@@ -239,6 +251,20 @@ const ShrinkingSortableList = SortableContainer(({className, isSorting, items, i
239251
);
240252
});
241253

254+
const NestedSortableList = SortableContainer(({className, items, isSorting, sortableHandlers}) => {
255+
return (
256+
<div className={className} {...sortableHandlers}>
257+
{items.map((value, index) =>
258+
<Category
259+
key={`category-${value}`}
260+
index={index}
261+
value={value}
262+
/>
263+
)}
264+
</div>
265+
);
266+
});
267+
242268
storiesOf('Basic Configuration', module)
243269
.add('Basic usage', () => {
244270
return (
@@ -283,6 +309,13 @@ storiesOf('Basic Configuration', module)
283309
</div>
284310
);
285311
})
312+
.add('Nested Lists', () => {
313+
return (
314+
<div className={style.root}>
315+
<ListWrapper component={NestedSortableList} items={range(4)} shouldUseDragHandle={true} helperClass={style.stylizedHelper} />
316+
</div>
317+
);
318+
})
286319

287320
storiesOf('Advanced', module)
288321
.add('Press delay (200ms)', () => {

src/SortableContainer/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
116116

117117
const node = closest(e.target, (el) => el.sortableInfo != null);
118118

119-
if (node && node.sortableInfo && !this.state.sorting) {
119+
if (node && node.sortableInfo && this.nodeIsChild(node) && !this.state.sorting) {
120120
const {useDragHandle} = this.props;
121121
const {index, collection} = node.sortableInfo;
122122

@@ -143,6 +143,10 @@ export default function sortableContainer(WrappedComponent, config = {withRef: f
143143
}
144144
};
145145

146+
nodeIsChild = node => {
147+
return node.sortableInfo.manager == this.manager;
148+
};
149+
146150
handleMove = (e) => {
147151
const {distance} = this.props;
148152

src/SortableElement/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export default function sortableElement (WrappedComponent, config = {withRef: fa
5858
setDraggable(collection, index){
5959
let node = this.node = findDOMNode(this);
6060

61-
node.sortableInfo = {index, collection};
61+
node.sortableInfo = {
62+
index, collection,
63+
manager: this.context.manager
64+
};
6265

6366
this.ref = {node};
6467
this.context.manager.add(collection, this.ref);

0 commit comments

Comments
 (0)