Skip to content

Commit 9cf6ed5

Browse files
committed
Update README.md
1 parent 75bd79f commit 9cf6ed5

File tree

1 file changed

+57
-34
lines changed

1 file changed

+57
-34
lines changed

README.md

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ A higher order React component for [Sortable](https://github.com/RubaXa/Sortable
88

99
The sample code can be found in the [examples](https://github.com/cheton/react-sortable/tree/master/examples) directory.
1010

11-
1211
## Notice
13-
There is a major breaking change since v1.0. Checkout [Migration Guide](https://github.com/i18next/i18next-scanner/wiki/Migration-Guide) while upgrading from earlier versions.
12+
There is a major breaking change since v1.0. Checkout [Migration Guide](https://github.com/react-sortable/wiki/Migration-Guide) while upgrading from earlier versions.
1413

1514
## Installation
1615

@@ -47,45 +46,51 @@ Then, include these scripts into your html file:
4746
File: sortable-list.jsx
4847
```jsx
4948
import React from 'react';
50-
import Sortable from 'react-sortable';
49+
import Sortable from 'react-sortablejs';
5150

5251
// Functional Component
53-
const SortableList = ({ items }) => {
54-
let sortable = null; // the sortable instance
55-
56-
items = items.map((val, key) => (<li key={key} data-id={val}>List Item: {val}</li>));
52+
const SortableList = ({ items, onChange }) => {
53+
let sortable = null; // sortable instance
54+
const reverseOrder = (evt) => {
55+
const order = sortable.toArray();
56+
onChange(order.reverse());
57+
};
58+
const listItems = items.map((val, key) => (<li key={key} data-id={val}>List Item: {val}</li>));
5759

5860
return (
59-
<Sortable
60-
// Sortable options (https://github.com/RubaXa/Sortable#options)
61-
options={{
62-
}}
63-
64-
// Use ref to get the sortable instance
65-
// https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute
66-
ref={(c) => {
67-
if (c) {
68-
sortable = c.sortable;
69-
}
70-
}}
71-
72-
// An optional tag to specify the wrapping element. Defaults to "div".
73-
tag="ul"
74-
75-
// The optional onChange method allows you to keep DOM nodes untouched
76-
// and render the sorted items via state change.
77-
// See an example at https://github.com/cheton/react-sortable/#controlled-component
78-
// onChange={(order) => {
79-
// this.setState({ items: order });
80-
// }}
81-
>
82-
{items}
83-
</Sortable>
61+
<div>
62+
<button type="button" onClick={reverseOrder}>Reverse Order</button>
63+
<Sortable
64+
// Sortable options (https://github.com/RubaXa/Sortable#options)
65+
options={{
66+
}}
67+
68+
// Use ref to get the sortable instance
69+
// https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute
70+
ref={(c) => {
71+
if (c) {
72+
sortable = c.sortable;
73+
}
74+
}}
75+
76+
// An optional tag to specify the wrapping element. Defaults to "div".
77+
tag="ul"
78+
79+
// The optional onChange method allows you to implement a controlled component and keep
80+
// DOM nodes untouched. You have to change state to re-render the children.
81+
onChange={(order) => {
82+
onChange(order);
83+
}}
84+
>
85+
{listItems}
86+
</Sortable>
87+
</div>
8488
);
8589
};
8690

8791
SortableList.propTypes = {
88-
items: React.PropTypes.array
92+
items: React.PropTypes.array,
93+
onChange: React.PropTypes.func
8994
};
9095

9196
export default SortableList;
@@ -97,8 +102,26 @@ import React from 'react';
97102
import ReactDOM from 'react-dom';
98103
import SortableList from './sortable-list';
99104

105+
class App extends React.Component {
106+
state = {
107+
items: [1, 2, 3, 4, 5, 6]
108+
};
109+
110+
render() {
111+
return (
112+
<SortableList
113+
items={this.state.items}
114+
onChange={(items) => {
115+
this.setState({ items });
116+
}}
117+
>
118+
</SortableList>
119+
)
120+
}
121+
};
122+
100123
ReactDOM.render(
101-
<SortableList items={[1, 2, 3, 4, 5, 6]} />,
124+
<App />,
102125
document.getElementById('container')
103126
);
104127
```

0 commit comments

Comments
 (0)