Skip to content

Commit

Permalink
Merge pull request clauderic#34 from TalShavit/master
Browse files Browse the repository at this point in the history
Fix changing disable property while receiving props
  • Loading branch information
Claudéric Demers authored Jul 27, 2016
2 parents f32d810 + e32f743 commit db3c6c2
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/SortableElement/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,50 @@ export default function SortableElement (WrappedComponent, config = {withRef: fa
collection: 0
};
componentDidMount() {

let {collection, disabled, index} = this.props;

if (!disabled) {
let node = this.node = findDOMNode(this);

node.sortableInfo = {index, collection};

this.ref = {node};
this.context.manager.add(collection, this.ref);
this.setDraggable(collection, index);
}
}
componentWillReceiveProps(nextProps) {
const {index} = this.props;
if (index !== nextProps.index && this.node) {
this.node.sortableInfo.index = nextProps.index;
}
if (this.props.disabled !== nextProps.disabled)
{
let {collection, disabled, index} = nextProps;
if (disabled) {
this.removeDraggable(collection);
}
else {
this.setDraggable(collection, index);
}
}
}

componentWillUnmount() {
let {collection, disabled} = this.props;

if (!disabled) this.context.manager.remove(collection, this.ref);
if (!disabled) this.removeDraggable(collection);
}


setDraggable(collection, index){
let node = this.node = findDOMNode(this);

node.sortableInfo = {index, collection};

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

removeDraggable(collection) {
this.context.manager.remove(collection, this.ref);
}

getWrappedInstance() {
invariant(config.withRef, 'To access the wrapped instance, you need to pass in {withRef: true} as the second argument of the SortableElement() call');
return this.refs.wrappedInstance;
Expand Down

0 comments on commit db3c6c2

Please sign in to comment.