Skip to content

Commit e6edf85

Browse files
committed
Pass sortable isntance to component as a prop
1 parent 373e67f commit e6edf85

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/index.jsx

+12-7
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,19 @@ const extend = (target, ...sources) => {
4141
};
4242

4343
const SortableMixin = (options = defaultOptions) => (Component) => class extends React.Component {
44-
sortableInstance = null;
44+
45+
state = {
46+
sortableInstance: null
47+
};
48+
4549
sortableOptions = extend({}, defaultOptions, options);
4650

4751
componentDidMount() {
4852
const sortableComponent = this.refs[refName];
4953
const emitEvent = (type, evt) => {
5054
const methodName = this.sortableOptions[type];
5155
const method = sortableComponent[methodName];
52-
method && method.call(sortableComponent, evt, this.sortableInstance);
56+
method && method.call(sortableComponent, evt, this.state.sortableInstance);
5357
};
5458

5559
let copyOptions = extend({}, this.sortableOptions);
@@ -107,7 +111,8 @@ const SortableMixin = (options = defaultOptions) => (Component) => class extends
107111
});
108112

109113
const domNode = ReactDOM.findDOMNode(sortableComponent.refs[this.sortableOptions.ref] || sortableComponent);
110-
this.sortableInstance = Sortable.create(domNode, copyOptions);
114+
const sortableInstance = Sortable.create(domNode, copyOptions);
115+
this.setState({sortableInstance});
111116
}
112117
componentWillReceiveProps(nextProps) {
113118
const sortableComponent = this.refs[refName];
@@ -121,15 +126,15 @@ const SortableMixin = (options = defaultOptions) => (Component) => class extends
121126
}
122127
}
123128
componentWillUnmount() {
124-
if (this.sortableInstance) {
125-
this.sortableInstance.destroy();
126-
this.sortableInstance = null;
129+
if (this.state.sortableInstance) {
130+
this.state.sortableInstance.destroy();
131+
this.setState({sortableInstance: null});
127132
}
128133
}
129134

130135
render() {
131136
return (
132-
<Component ref={refName} {...this.props} />
137+
<Component ref={refName} sortableInstance={this.state.sortableInstance} {...this.props} />
133138
);
134139
}
135140
};

0 commit comments

Comments
 (0)