Skip to content

Commit

Permalink
Merge pull request #7 from costimize/1
Browse files Browse the repository at this point in the history
Reinitialize sortable on componentDidUpdate
  • Loading branch information
cheton committed Mar 13, 2016
2 parents 5105f59 + e45d6e5 commit e09c411
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ const SortableMixin = (options = defaultOptions) => (Component) => class extends
}, 0);
};
});

const domNode = ReactDOM.findDOMNode(sortableComponent.refs[this.sortableOptions.ref] || sortableComponent);
this.sortableInstance = Sortable.create(domNode, copyOptions);
this.populatedOptions = copyOptions
this.initSortable(sortableComponent);
}
componentWillReceiveProps(nextProps) {
const sortableComponent = this.refs[refName];
Expand All @@ -120,7 +119,23 @@ const SortableMixin = (options = defaultOptions) => (Component) => class extends
sortableComponent.setState(newState);
}
}
componentDidUpdate(prevProps) {
const model = this.sortableOptions.model;
const prevItems = prevProps[model];
const currItems = this.props[model];
if(prevItems !== currItems) {
this.initSortable(this.refs[refName]);
}
}
componentWillUnmount() {
this.destroySortable();
}
initSortable(sortableComponent) {
this.destroySortable();
const domNode = ReactDOM.findDOMNode(sortableComponent.refs[this.sortableOptions.ref] || sortableComponent);
this.sortableInstance = Sortable.create(domNode, this.populatedOptions);
}
destroySortable() {
if (this.sortableInstance) {
this.sortableInstance.destroy();
this.sortableInstance = null;
Expand Down

0 comments on commit e09c411

Please sign in to comment.