|
1 | 1 | <p>
|
2 |
| - <img width="100%" src="https://assets.solidjs.com/banner?type={{name_of_lib}}&background=tiles&project=%20" alt="{{name_of_lib}}"> |
| 2 | + <img width="100%" src="https://assets.solidjs.com/banner?type=solid-sortablejs&background=tiles&project=%20" alt="solid-sortablejs"> |
3 | 3 | </p>
|
4 | 4 |
|
5 | 5 | # {{name_of_lib}}
|
6 | 6 |
|
7 | 7 | [](https://pnpm.io/)
|
8 | 8 |
|
9 |
| -{{desc_of_lib}} |
| 9 | +Easily sort your list. solid-sortablejs is using the SortableJS library to sort your list. |
10 | 10 |
|
11 |
| -> **Note** After using this template, you have to search and replace all `{{name_of_lib}}` and similar strings |
12 |
| -> with appropriate texts. |
13 |
| -> |
14 |
| -> `{{name_of_lib}}` should be a **kebab-case** string representing the name of you monorepo. |
15 |
| -> |
16 |
| -> `{{desc_of_lib}}` should be a **Normal case** string with the description of the repository. |
17 |
| -> |
18 |
| -> `{{me}}` should be a **kebab-case** string from your profile URL. |
19 | 11 |
|
20 | 12 | ## Quick start
|
21 | 13 |
|
22 | 14 | Install it:
|
23 | 15 |
|
24 | 16 | ```bash
|
25 |
| -npm i {{name_of_lib}} |
26 |
| -# or |
27 |
| -yarn add {{name_of_lib}} |
28 |
| -# or |
29 |
| -pnpm add {{name_of_lib}} |
| 17 | +npm add solid-sortablejs |
30 | 18 | ```
|
31 | 19 |
|
32 | 20 | Use it:
|
33 | 21 |
|
34 | 22 | ```tsx
|
35 |
| -import {{name_of_lib}} from '{{name_of_lib}}' |
| 23 | +import {createStore} from 'solid-js/store' |
| 24 | +import Sortable from 'solid-sortablejs' |
| 25 | + |
| 26 | +const App = () => { |
| 27 | + |
| 28 | + const itemStyles = {background: 'green', padding: "10px", minWidth: "100px", margin: "5px", "border-radius": "4px", color: "white"}; |
| 29 | + const containerStyles = {display: "inline-block", background: 'gray', padding: "10px", "border-radius": "4px"}; |
| 30 | + |
| 31 | + const [items, setItems] = createStore([ |
| 32 | + { id: 0, name: 0 }, |
| 33 | + { id: 1, name: 1 }, |
| 34 | + { id: 2, name: 2 }, |
| 35 | + { id: 3, name: 3 }, |
| 36 | + ]) |
| 37 | + |
| 38 | + const onEnd = (event: SortableOnEndEvent<typeof items[0]>) => { |
| 39 | + // event.movedItem -> the item that was moved |
| 40 | + // event.newList -> the new list of items |
| 41 | + // event.oldIndex -> the index of the item that was moved |
| 42 | + // event.newIndex -> the index of the item that was moved to |
| 43 | + |
| 44 | + setItems(event.newList); |
| 45 | + } |
| 46 | + |
| 47 | + return ( |
| 48 | + <div style={containerStyles}> |
| 49 | + <Sortable items={items} onEnd={onEnd}> |
| 50 | + {item => <div style={itemStyles}>{item.name} {Math.random()}</div>} |
| 51 | + </Sortable> |
| 52 | + </div> |
| 53 | + ); |
| 54 | + |
| 55 | +}; |
36 | 56 | ```
|
0 commit comments