Skip to content

Commit f17a492

Browse files
committed
Refactor code
1 parent c0efc98 commit f17a492

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/index.jsx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,20 @@ const extend = (target, ...sources) => {
4040
return target;
4141
};
4242

43-
const SortableMixin = (sortableOptions = defaultOptions) => (Component) => class extends React.Component {
43+
const SortableMixin = (options = defaultOptions) => (Component) => class extends React.Component {
4444
sortableInstance = null;
45-
sortableOptions = sortableOptions;
45+
sortableOptions = extend({}, defaultOptions, options);
4646

4747
componentDidMount() {
48-
const wrapperComponent = this;
49-
const sortableComponent = wrapperComponent.refs[refName];
50-
const options = extend({}, defaultOptions, wrapperComponent.sortableOptions);
48+
const sortableComponent = this.refs[refName];
5149
const emitEvent = (type, evt) => {
52-
const methodName = options[type];
50+
const methodName = this.sortableOptions[type];
5351
const method = sortableComponent[methodName];
54-
method && method.call(sortableComponent, evt, wrapperComponent.sortableInstance);
52+
method && method.call(sortableComponent, evt, this.sortableInstance);
5553
};
5654

57-
let copyOptions = extend({}, options);
55+
let copyOptions = extend({}, this.sortableOptions);
56+
5857
[ // Bind callbacks
5958
'onStart',
6059
'onEnd',
@@ -68,15 +67,15 @@ const SortableMixin = (sortableOptions = defaultOptions) => (Component) => class
6867
copyOptions[name] = (evt) => {
6968
if (name === 'onStart') {
7069
_nextSibling = evt.item.nextElementSibling;
71-
_activeWrapperComponent = wrapperComponent;
70+
_activeWrapperComponent = this;
7271
} else if (name === 'onAdd' || name === 'onUpdate') {
7372
evt.from.insertBefore(evt.item, _nextSibling);
7473

7574
const oldIndex = evt.oldIndex;
7675
const newIndex = evt.newIndex;
7776
let newState = {};
7877
let remoteState = {};
79-
let items = getModelItems(wrapperComponent);
78+
let items = getModelItems(this);
8079

8180
if (name === 'onAdd') {
8281
let remoteItems = getModelItems(_activeWrapperComponent);
@@ -88,15 +87,15 @@ const SortableMixin = (sortableOptions = defaultOptions) => (Component) => class
8887
items.splice(newIndex, 0, items.splice(oldIndex, 1)[0]);
8988
}
9089

91-
newState[wrapperComponent.sortableOptions.model] = items;
90+
newState[this.sortableOptions.model] = items;
9291

9392
if (copyOptions.stateHandler) {
9493
sortableComponent[copyOptions.stateHandler](newState);
9594
} else {
9695
sortableComponent.setState(newState);
9796
}
9897

99-
if (_activeWrapperComponent !== wrapperComponent) {
98+
if (_activeWrapperComponent !== this) {
10099
_activeWrapperComponent.refs[refName].setState(remoteState);
101100
}
102101
}
@@ -107,13 +106,12 @@ const SortableMixin = (sortableOptions = defaultOptions) => (Component) => class
107106
};
108107
});
109108

110-
const domNode = ReactDOM.findDOMNode(sortableComponent.refs[options.ref] || sortableComponent);
109+
const domNode = ReactDOM.findDOMNode(sortableComponent.refs[this.sortableOptions.ref] || sortableComponent);
111110
this.sortableInstance = Sortable.create(domNode, copyOptions);
112111
}
113112
componentWillReceiveProps(nextProps) {
114-
const wrapperComponent = this;
115-
const sortableComponent = wrapperComponent.refs[refName];
116-
const model = wrapperComponent.sortableOptions.model;
113+
const sortableComponent = this.refs[refName];
114+
const model = this.sortableOptions.model;
117115
const items = nextProps[model];
118116

119117
if (items) {

0 commit comments

Comments
 (0)