Skip to content

Commit 7aa2d34

Browse files
committed
Fix changing disable property while receiving props
1 parent 8ff6601 commit 7aa2d34

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/SortableElement/index.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,50 @@ export default function SortableElement (WrappedComponent, config = {withRef: fa
1919
collection: 0
2020
};
2121
componentDidMount() {
22+
2223
let {collection, disabled, index} = this.props;
2324

2425
if (!disabled) {
25-
let node = this.node = findDOMNode(this);
26-
27-
node.sortableInfo = {index, collection};
28-
29-
this.ref = {node};
30-
this.context.manager.add(collection, this.ref);
26+
this.setDraggable(collection, index);
3127
}
3228
}
3329
componentWillReceiveProps(nextProps) {
3430
const {index} = this.props;
3531
if (index !== nextProps.index && this.node) {
3632
this.node.sortableInfo.index = nextProps.index;
3733
}
34+
if (this.props.disabled !== nextProps.disabled)
35+
{
36+
let {collection, disabled, index} = nextProps;
37+
if (disabled) {
38+
this.removeDraggable(collection);
39+
}
40+
else {
41+
this.setDraggable(collection, index);
42+
}
43+
}
3844
}
45+
3946
componentWillUnmount() {
4047
let {collection, disabled} = this.props;
4148

42-
if (!disabled) this.context.manager.remove(collection, this.ref);
49+
if (!disabled) this.removeDraggable(collection);
50+
}
51+
52+
53+
setDraggable(collection, index){
54+
let node = this.node = findDOMNode(this);
55+
56+
node.sortableInfo = {index, collection};
57+
58+
this.ref = {node};
59+
this.context.manager.add(collection, this.ref);
4360
}
61+
62+
removeDraggable(collection) {
63+
this.context.manager.remove(collection, this.ref);
64+
}
65+
4466
getWrappedInstance() {
4567
invariant(config.withRef, 'To access the wrapped instance, you need to pass in {withRef: true} as the second argument of the SortableElement() call');
4668
return this.refs.wrappedInstance;

0 commit comments

Comments
 (0)