Skip to content

Commit 1043b76

Browse files
author
Claudéric Demers
authored
Merge pull request clauderic#127 from DeadHeadRussell/master
fix: children elements in nested lists drag parent elements.
2 parents 2ada018 + d87a456 commit 1043b76

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed

src/.stories/Storybook.scss

+18
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@
112112
}
113113
}
114114

115+
// Nested
116+
.category {
117+
height: auto;
118+
119+
.categoryHeader {
120+
display: flex;
121+
flex-flow: row nowrap;
122+
align-items: center;
123+
padding: 10px 14px;
124+
background: #F9F9F9;
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

+40-7
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,28 @@ function getItems(count, height) {
2323
const Handle = SortableHandle(() => <div className={style.handle}></div>);
2424

2525
const Item = SortableElement((props) => {
26-
return (
27-
<div className={props.className} style={{
28-
height: props.height
29-
}}>
26+
return (
27+
<div className={props.className} style={{
28+
height: props.height
29+
}}>
3030
{props.shouldUseDragHandle && <Handle/>}
3131
<div className={style.wrapper}>
32-
<span>Item</span> {props.value}
32+
<span>Item</span> {props.value}
33+
</div>
34+
</div>
35+
)
36+
});
37+
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>
3344
</div>
34-
</div>
35-
)
45+
<ListWrapper component={SortableList} className={style.categoryList} items={getItems(3, 59)} shouldUseDragHandle={true} helperClass={style.stylizedHelper} />
46+
</div>
47+
)
3648
});
3749

3850
class ListWrapper extends Component {
@@ -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

+5-1
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

+5-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ 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,
63+
collection,
64+
manager: this.context.manager
65+
};
6266

6367
this.ref = {node};
6468
this.context.manager.add(collection, this.ref);

0 commit comments

Comments
 (0)