Skip to content

Commit d2012dd

Browse files
committed
merge upstream
2 parents e6edf85 + e09c411 commit d2012dd

File tree

5 files changed

+101
-38
lines changed

5 files changed

+101
-38
lines changed

examples/app.bundle.js

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"author": "",
1010
"license": "ISC",
1111
"dependencies": {
12+
"lodash": "^4.6.1",
1213
"react": "^0.14.7",
1314
"react-dom": "^0.14.7",
1415
"react-sortablejs": "latest",

examples/src/index.jsx

+66-23
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,77 @@
1+
import random from 'lodash/random';
12
import React from 'react';
23
import ReactDOM from 'react-dom';
34
import Sortable from 'react-sortablejs';
45
import SimpleList from './simple-list';
56
import SharedGroup from './shared-group';
67

7-
ReactDOM.render(
8-
<div>
9-
<div className="container-fluid">
10-
<div className="title">Simple List</div>
11-
<div className="row">
12-
<div className="col-sm-12">
13-
<SimpleList />
14-
</div>
15-
</div>
16-
</div>
17-
<div className="container-fluid">
18-
<div className="title" style={{marginTop: 100}}>Shared Group</div>
19-
<div className="row">
20-
<div className="col-sm-6">
21-
<SharedGroup
22-
items={['Apple', 'Banaba', 'Cherry', 'Grape']}
23-
/>
8+
const items = [
9+
'Apple',
10+
'Banana',
11+
'Cherry',
12+
'Guava',
13+
'Grape',
14+
'Kiwi',
15+
'Lemon',
16+
'Melon',
17+
'Orange',
18+
'Pear',
19+
'Peach',
20+
'Strawberry'
21+
];
22+
23+
class App extends React.Component {
24+
state = {
25+
left: ['Apple', 'Banaba', 'Cherry', 'Grape'],
26+
right: ['Lemon', 'Orange', 'Pear', 'Peach']
27+
};
28+
29+
handleClick() {
30+
const i = random(0, items.length - 1);
31+
const state = this.state.left.concat(items[i]);
32+
this.setState({ left: state });
33+
}
34+
render() {
35+
return (
36+
<div>
37+
<div className="container-fluid">
38+
<div className="title">Simple List</div>
39+
<div className="row">
40+
<div className="col-sm-12">
41+
<SimpleList />
42+
</div>
43+
</div>
2444
</div>
25-
<div className="col-sm-6">
26-
<SharedGroup
27-
items={['Lemon', 'Orange', 'Pear', 'Peach']}
28-
/>
45+
<div className="container-fluid">
46+
<div className="title" style={{marginTop: 100}}>Shared Group</div>
47+
<div className="form-group">
48+
<button
49+
type="button"
50+
className="btn btn-default"
51+
onClick={::this.handleClick}
52+
>
53+
Add more items
54+
</button>
55+
</div>
56+
<div className="row">
57+
<div className="col-sm-6">
58+
<SharedGroup
59+
items={this.state.left}
60+
/>
61+
</div>
62+
<div className="col-sm-6">
63+
<SharedGroup
64+
items={this.state.right}
65+
/>
66+
</div>
67+
</div>
2968
</div>
3069
</div>
31-
</div>
32-
</div>,
70+
);
71+
}
72+
}
73+
74+
ReactDOM.render(
75+
<App />,
3376
document.getElementById('container')
3477
);

examples/src/shared-group.jsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import Sortable from 'react-sortablejs';
44
const sortableOptions = {
55
ref: 'list',
66
model: 'items',
7-
group: 'shared'
7+
group: {
8+
name: 'shared',
9+
pull: true, //'clone',
10+
put: true
11+
}
812
};
913

1014
@Sortable(sortableOptions)
@@ -24,7 +28,7 @@ export default class SharedGroup extends React.Component {
2428
const items = this.state.items.map((text, index) => (
2529
<li key={index}>{text}</li>
2630
));
27-
31+
2832
return (
2933
<ul ref="list" className="block-list">{items}</ul>
3034
);

src/index.jsx

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

4343
const SortableMixin = (options = defaultOptions) => (Component) => class extends React.Component {
44-
44+
4545
state = {
4646
sortableInstance: null
4747
};
@@ -109,10 +109,8 @@ const SortableMixin = (options = defaultOptions) => (Component) => class extends
109109
}, 0);
110110
};
111111
});
112-
113-
const domNode = ReactDOM.findDOMNode(sortableComponent.refs[this.sortableOptions.ref] || sortableComponent);
114-
const sortableInstance = Sortable.create(domNode, copyOptions);
115-
this.setState({sortableInstance});
112+
this.populatedOptions = copyOptions
113+
this.initSortable(sortableComponent);
116114
}
117115
componentWillReceiveProps(nextProps) {
118116
const sortableComponent = this.refs[refName];
@@ -125,7 +123,24 @@ const SortableMixin = (options = defaultOptions) => (Component) => class extends
125123
sortableComponent.setState(newState);
126124
}
127125
}
126+
componentDidUpdate(prevProps) {
127+
const model = this.sortableOptions.model;
128+
const prevItems = prevProps[model];
129+
const currItems = this.props[model];
130+
if(prevItems !== currItems) {
131+
this.initSortable(this.refs[refName]);
132+
}
133+
}
128134
componentWillUnmount() {
135+
this.destroySortable();
136+
}
137+
initSortable(sortableComponent) {
138+
this.destroySortable();
139+
const domNode = ReactDOM.findDOMNode(sortableComponent.refs[this.sortableOptions.ref] || sortableComponent);
140+
const sortableInstance = Sortable.create(domNode, this.populatedOptions);
141+
this.setState({sortableInstance});
142+
}
143+
destroySortable() {
129144
if (this.state.sortableInstance) {
130145
this.state.sortableInstance.destroy();
131146
this.setState({sortableInstance: null});

0 commit comments

Comments
 (0)