Skip to content

Commit e09c411

Browse files
committed
Merge pull request #7 from costimize/1
Reinitialize sortable on componentDidUpdate
2 parents 5105f59 + e45d6e5 commit e09c411

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/index.jsx

+18-3
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,8 @@ const SortableMixin = (options = defaultOptions) => (Component) => class extends
105105
}, 0);
106106
};
107107
});
108-
109-
const domNode = ReactDOM.findDOMNode(sortableComponent.refs[this.sortableOptions.ref] || sortableComponent);
110-
this.sortableInstance = Sortable.create(domNode, copyOptions);
108+
this.populatedOptions = copyOptions
109+
this.initSortable(sortableComponent);
111110
}
112111
componentWillReceiveProps(nextProps) {
113112
const sortableComponent = this.refs[refName];
@@ -120,7 +119,23 @@ const SortableMixin = (options = defaultOptions) => (Component) => class extends
120119
sortableComponent.setState(newState);
121120
}
122121
}
122+
componentDidUpdate(prevProps) {
123+
const model = this.sortableOptions.model;
124+
const prevItems = prevProps[model];
125+
const currItems = this.props[model];
126+
if(prevItems !== currItems) {
127+
this.initSortable(this.refs[refName]);
128+
}
129+
}
123130
componentWillUnmount() {
131+
this.destroySortable();
132+
}
133+
initSortable(sortableComponent) {
134+
this.destroySortable();
135+
const domNode = ReactDOM.findDOMNode(sortableComponent.refs[this.sortableOptions.ref] || sortableComponent);
136+
this.sortableInstance = Sortable.create(domNode, this.populatedOptions);
137+
}
138+
destroySortable() {
124139
if (this.sortableInstance) {
125140
this.sortableInstance.destroy();
126141
this.sortableInstance = null;

0 commit comments

Comments
 (0)