Skip to content

Commit f66b0f5

Browse files
committed
Update examples
1 parent 2bdb29e commit f66b0f5

File tree

10 files changed

+141
-220
lines changed

10 files changed

+141
-220
lines changed

examples/.babelrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

examples/.eslintrc

Lines changed: 0 additions & 101 deletions
This file was deleted.

examples/package.json

Lines changed: 0 additions & 33 deletions
This file was deleted.

examples/src/immutable-store.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import _ from 'lodash';
2+
import events from 'events';
3+
4+
class ImmutableStore extends events.EventEmitter {
5+
state = {};
6+
7+
constructor(state = {}) {
8+
super();
9+
10+
this.state = state;
11+
}
12+
get(key, defaultValue) {
13+
return _.get(this.state, key, defaultValue);
14+
}
15+
set(key, value) {
16+
this.state = _.merge({}, this.state, _.set({}, key, value));
17+
this.emit('change', this.state);
18+
return this.state;
19+
}
20+
unset(key, options) {
21+
let state = _.extend({}, this.state);
22+
_.unset(state, key);
23+
this.state = state;
24+
this.emit('change', this.state);
25+
return this.state;
26+
}
27+
replace(key, value) {
28+
let state = _.extend({}, this.state);
29+
_.unset(state, key);
30+
this.state = _.merge({}, state, _.set({}, key, value));
31+
this.emit('change', this.state);
32+
return this.state;
33+
}
34+
clear() {
35+
this.state = {};
36+
this.emit('change', this.state);
37+
return this.state;
38+
}
39+
}
40+
41+
export default ImmutableStore;

examples/src/index.jsx

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,45 @@
1+
import extend from 'lodash/extend';
12
import random from 'lodash/random';
23
import React from 'react';
34
import ReactDOM from 'react-dom';
4-
import Sortable from 'react-sortablejs';
5+
import Sortable from '../../src';
56
import SimpleList from './simple-list';
67
import SharedGroup from './shared-group';
7-
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-
];
8+
import store from './store';
229

2310
class App extends React.Component {
2411
state = {
25-
left: ['Apple', 'Banaba', 'Cherry', 'Grape'],
26-
right: ['Lemon', 'Orange', 'Pear', 'Peach']
12+
simpleList: store.get('simpleList'),
13+
sharedGroup: store.get('sharedGroup')
2714
};
2815

29-
handleClick() {
16+
componentDidMount() {
17+
store.on('change', () => {
18+
this.setState({
19+
simpleList: store.get('simpleList'),
20+
sharedGroup: store.get('sharedGroup')
21+
});
22+
});
23+
}
24+
addMoreItems() {
25+
const items = [
26+
'Apple',
27+
'Banana',
28+
'Cherry',
29+
'Guava',
30+
'Grape',
31+
'Kiwi',
32+
'Lemon',
33+
'Melon',
34+
'Orange',
35+
'Pear',
36+
'Peach',
37+
'Strawberry'
38+
];
3039
const i = random(0, items.length - 1);
31-
const state = this.state.left.concat(items[i]);
32-
this.setState({ left: state });
40+
const sharedGroup = extend({}, this.state.sharedGroup);
41+
sharedGroup.left = sharedGroup.left.concat(items[i]);
42+
this.setState({ sharedGroup: sharedGroup });
3343
}
3444
render() {
3545
return (
@@ -38,7 +48,12 @@ class App extends React.Component {
3848
<div className="title">Simple List</div>
3949
<div className="row">
4050
<div className="col-sm-12">
41-
<SimpleList />
51+
<SimpleList
52+
items={this.state.simpleList}
53+
onChange={(items) => {
54+
store.set('simpleList', items);
55+
}}
56+
/>
4257
</div>
4358
</div>
4459
</div>
@@ -48,20 +63,26 @@ class App extends React.Component {
4863
<button
4964
type="button"
5065
className="btn btn-default"
51-
onClick={::this.handleClick}
66+
onClick={::this.addMoreItems}
5267
>
5368
Add more items
5469
</button>
5570
</div>
5671
<div className="row">
5772
<div className="col-sm-6">
5873
<SharedGroup
59-
items={this.state.left}
74+
items={this.state.sharedGroup.left}
75+
onChange={(items) => {
76+
store.replace('sharedGroup.left', items);
77+
}}
6078
/>
6179
</div>
6280
<div className="col-sm-6">
6381
<SharedGroup
64-
items={this.state.right}
82+
items={this.state.sharedGroup.right}
83+
onChange={(items) => {
84+
store.replace('sharedGroup.right', items);
85+
}}
6586
/>
6687
</div>
6788
</div>

examples/src/shared-group.jsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react';
2-
import Sortable from 'react-sortablejs';
2+
import Sortable from '../../src';
3+
import store from './store';
34

45
const sortableOptions = {
56
ref: 'list',
6-
model: 'items',
77
group: {
88
name: 'shared',
99
pull: true, //'clone',
@@ -13,19 +13,8 @@ const sortableOptions = {
1313

1414
@Sortable(sortableOptions)
1515
export default class SharedGroup extends React.Component {
16-
static propTypes = {
17-
items: React.PropTypes.array
18-
};
19-
static defaultProps = {
20-
items: []
21-
};
22-
23-
state = {
24-
items: this.props.items
25-
};
26-
2716
render() {
28-
const items = this.state.items.map((text, index) => (
17+
const items = this.props.items.map((text, index) => (
2918
<li key={index}>{text}</li>
3019
));
3120

examples/src/simple-list.jsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import React from 'react';
2-
import Sortable from 'react-sortablejs';
2+
import Sortable from '../../src';
3+
import store from './store';
34

45
const sortableOptions = {
5-
ref: 'list',
6-
model: 'items'
6+
ref: 'list'
77
};
88

99
@Sortable(sortableOptions)
1010
export default class SimpleList extends React.Component {
11-
state = {
12-
items: [1, 2, 3, 4, 5, 6]
13-
};
14-
1511
render() {
16-
const items = this.state.items.map((text, index) => (
12+
const items = this.props.items.map((text, index) => (
1713
<li key={index}>List Item {text}</li>
1814
));
1915

examples/src/store.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import ImmutableStore from './immutable-store';
2+
3+
const store = new ImmutableStore({
4+
simpleList: [1, 2, 3, 4, 5, 6],
5+
sharedGroup: {
6+
left: ['Apple', 'Banaba', 'Cherry', 'Grape'],
7+
right: ['Lemon', 'Orange', 'Pear', 'Peach']
8+
}
9+
});
10+
11+
export default store;

examples/webpack.config.babel.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)