Skip to content

Commit 8ffb22b

Browse files
committed
Made more changes
1 parent f71c32c commit 8ffb22b

7 files changed

+120
-97
lines changed

README.md

+7-12
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ npm install solid-sortablejs
1919
Use it:
2020

2121
```tsx
22+
import Sortable from "../src";
2223
import {createStore} from 'solid-js/store'
23-
import Sortable from 'solid-sortablejs'
24+
import { JSX } from "solid-js";
2425

2526
const App = () => {
2627

27-
const itemStyles = {background: 'green', padding: "10px", minWidth: "100px", margin: "5px", "border-radius": "4px", color: "white"};
28+
const itemStyles: JSX.CSSProperties = {"user-select": "none", background: 'green', padding: "10px", "min-width": "100px", margin: "5px", "border-radius": "4px", color: "white"};
2829
const containerStyles = {display: "inline-block", background: 'gray', padding: "10px", "border-radius": "4px"};
2930

3031
const [items, setItems] = createStore([
@@ -34,22 +35,16 @@ const App = () => {
3435
{ id: 3, name: 3 },
3536
])
3637

37-
const onEnd = (event: SortableOnEndEvent<typeof items[0]>) => {
38-
// event.movedItem -> the item that was moved
39-
// event.newList -> the new list of items
40-
// event.oldIndex -> the index of the item that was moved
41-
// event.newIndex -> the index of the item that was moved to
42-
43-
setItems(event.newList);
44-
}
45-
4638
return (
4739
<div style={containerStyles}>
48-
<Sortable items={items} onEnd={onEnd}>
40+
<Sortable idField="id" items={items} setItems={setItems} >
4941
{item => <div style={itemStyles}>{item.name} {Math.random()}</div>}
5042
</Sortable>
5143
</div>
5244
);
5345

5446
};
47+
48+
export default App;
49+
};
5550
```

dev/App.module.css

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
.App {
2-
text-align: center;
2+
text-align: center;
33
}
44

55
.logo {
6-
animation: logo-spin infinite 20s linear;
7-
height: 40vmin;
8-
pointer-events: none;
6+
animation: logo-spin infinite 20s linear;
7+
height: 40vmin;
8+
pointer-events: none;
99
}
1010

1111
.header {
12-
background-color: #282c34;
13-
min-height: 100vh;
14-
display: flex;
15-
flex-direction: column;
16-
align-items: center;
17-
justify-content: center;
18-
font-size: calc(10px + 2vmin);
19-
color: white;
12+
background-color: #282c34;
13+
min-height: 100vh;
14+
display: flex;
15+
flex-direction: column;
16+
align-items: center;
17+
justify-content: center;
18+
font-size: calc(10px + 2vmin);
19+
color: white;
2020
}
2121

2222
.link {
23-
color: #b318f0;
23+
color: #b318f0;
2424
}
2525

2626
@keyframes logo-spin {
27-
from {
28-
transform: rotate(0deg);
29-
}
30-
to {
31-
transform: rotate(360deg);
32-
}
27+
from {
28+
transform: rotate(0deg);
29+
}
30+
to {
31+
transform: rotate(360deg);
32+
}
3333
}

dev/App.tsx

+42-11
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,67 @@
11
import Sortable from "../src";
2-
import {createStore} from 'solid-js/store'
2+
import { createStore } from "solid-js/store";
33
import { JSX } from "solid-js";
44

55
const App = () => {
6-
7-
const itemStyles: JSX.CSSProperties = {"user-select": "none", background: 'green', padding: "10px", "min-width": "100px", margin: "5px", "border-radius": "4px", color: "white"};
8-
const containerStyles = {display: "inline-block", background: 'gray', padding: "10px", "border-radius": "4px"};
6+
const itemStyles: JSX.CSSProperties = {
7+
"user-select": "none",
8+
background: "green",
9+
padding: "10px",
10+
"min-width": "100px",
11+
margin: "5px",
12+
"border-radius": "4px",
13+
color: "white",
14+
};
15+
const containerStyles = {
16+
display: "inline-block",
17+
background: "gray",
18+
padding: "10px",
19+
"border-radius": "4px",
20+
};
921

1022
const [items, setItems] = createStore([
1123
{ id: 0, name: 0 },
1224
{ id: 1, name: 1 },
1325
{ id: 2, name: 2 },
1426
{ id: 3, name: 3 },
15-
])
27+
]);
1628
const [items2, setItems2] = createStore([
1729
{ id: 4, name: 4 },
1830
{ id: 5, name: 5 },
1931
{ id: 6, name: 6 },
2032
{ id: 7, name: 7 },
21-
])
33+
]);
2234

2335
return (
2436
<div style={containerStyles}>
25-
<Sortable idField="id" style={{ background: "darkgray"}} items={items} setItems={setItems} >
26-
{item => <div style={itemStyles}>{item.name} {Math.random()}</div>}
37+
<Sortable
38+
group="test"
39+
idField="id"
40+
style={{ "min-height": "30px", background: "darkgray" }}
41+
items={items}
42+
setItems={setItems}
43+
>
44+
{(item) => (
45+
<div style={itemStyles}>
46+
{item.name} {Math.random()}
47+
</div>
48+
)}
2749
</Sortable>
28-
<Sortable idField="id" style={{ background: "darkgray", "margin-top": "102px"}} items={items2} setItems={setItems2} >
29-
{item => <div style={itemStyles}>{item.name} {Math.random()}</div>}
50+
<Sortable
51+
group="test"
52+
idField="id"
53+
style={{ "min-height": "30px", background: "darkgray", "margin-top": "102px" }}
54+
items={items2}
55+
setItems={setItems2}
56+
>
57+
{(item) => (
58+
<div style={itemStyles}>
59+
{item.name} {Math.random()}
60+
</div>
61+
)}
3062
</Sortable>
3163
</div>
3264
);
33-
3465
};
3566

3667
export default App;

dev/vite.config.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { defineConfig } from "vite"
2-
import solidPlugin from "vite-plugin-solid"
1+
import { defineConfig } from "vite";
2+
import solidPlugin from "vite-plugin-solid";
33

44
export default defineConfig({
5-
plugins: [
6-
solidPlugin(),
7-
],
5+
plugins: [solidPlugin()],
86
build: {
97
target: "esnext",
108
},
11-
})
9+
});

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.0.1",
2+
"version": "2.0.0",
33
"name": "solid-sortablejs",
44
"description": "A sortable library for solid",
55
"license": "MIT",

rollup.config.ts rollup.config.js

File renamed without changes.

src/index.tsx

+47-48
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,86 @@
1-
import {createContext, For, JSX, JSXElement, onCleanup, onMount, Setter} from 'solid-js';
2-
import SortableJs from 'sortablejs';
3-
4-
5-
1+
import { createContext, For, JSX, JSXElement, onCleanup, onMount, Setter } from "solid-js";
2+
import SortableJs from "sortablejs";
63

74
interface SortableProps<T> {
5+
delay?: number;
6+
delayOnTouchOnly?: boolean;
7+
group?: string;
88
class?: string;
99
style?: JSX.CSSProperties;
10-
items: T[]
10+
items: T[];
1111
idField: keyof T;
12-
setItems: Setter<T[]>
13-
children: (item: T) => JSXElement
14-
}
15-
16-
17-
18-
const SortableContext = createContext();
19-
20-
21-
export function SortableProvider (props: {children: JSXElement}) {
22-
const itemMap: Record<string, string> = {};
23-
24-
const sortable = {
25-
itemMap,
26-
add() {
27-
}
28-
}
29-
30-
return (
31-
<SortableContext.Provider value={{}}>
32-
{props.children}
33-
</SortableContext.Provider>
34-
)
12+
setItems: Setter<T[]>;
13+
children: (item: T) => JSXElement;
3514
}
3615

37-
3816
const dragging = {
39-
item: undefined as any
40-
}
17+
item: undefined as any,
18+
};
4119
export default function Sortable<T>(props: SortableProps<T>) {
4220
let sortableContainerRef: HTMLDivElement | undefined;
4321

4422
onMount(() => {
4523
const sortable = SortableJs.create(sortableContainerRef!, {
46-
group: "test",
24+
delay: props.delay,
25+
delayOnTouchOnly: props.delayOnTouchOnly,
26+
group: props.group,
4727
animation: 150,
4828
onStart(event) {
4929
dragging.item = props.items[parseInt(event.item.dataset.index!)];
50-
5130
},
5231
onAdd(evt) {
5332
const children = [...evt.to?.children!] as HTMLSpanElement[];
54-
const newItems = children.map(v => props.items.find(item => item[props.idField].toString() === v.dataset.id!) || dragging.item);
55-
56-
33+
const newItems = children.map(
34+
(v) =>
35+
props.items.find(
36+
(item) => (item[props.idField] as string).toString() === v.dataset.id!,
37+
) || dragging.item,
38+
);
5739
// from: where it came from
5840
// to: added to
59-
6041
children.splice(evt.newIndex!, 1);
61-
evt.to?.replaceChildren(...children)
62-
props.setItems(newItems);
42+
evt.to?.replaceChildren(...children);
6343

44+
props.setItems(newItems as T[]);
45+
},
46+
onRemove(evt) {
47+
// from: where it removed from
48+
// to: where it added to
49+
const children = [...evt.from?.children!] as HTMLSpanElement[];
50+
const newItems = children.map((v) =>
51+
props.items.find((item) => (item[props.idField] as string).toString() === v.dataset.id!),
52+
);
53+
54+
children.splice(evt.oldIndex!, 0, evt.item);
55+
evt.from.replaceChildren(...children);
56+
props.setItems(newItems as T[]);
6457
},
6558
onEnd(evt) {
6659
const children = [...sortableContainerRef?.children!] as HTMLSpanElement[];
67-
const newItems = children.map(v => props.items.find(item => item[props.idField].toString() === v.dataset.id!));
68-
children.sort((a, b) => parseInt(a.dataset.index!) - parseInt(b.dataset.index!))
60+
const newItems = children.map((v) =>
61+
props.items.find((item) => (item[props.idField] as string).toString() === v.dataset.id!),
62+
);
63+
children.sort((a, b) => parseInt(a.dataset.index!) - parseInt(b.dataset.index!));
6964
sortableContainerRef?.replaceChildren(...children);
70-
props.setItems(newItems);
65+
props.setItems(newItems as T[]);
7166
dragging.item = undefined;
7267
},
73-
})
68+
});
7469

7570
onCleanup(() => {
7671
sortable.destroy();
77-
})
78-
})
72+
});
73+
});
7974

8075
return (
8176
<div style={props.style} ref={sortableContainerRef} class="sortablejs">
8277
<For each={props.items}>
83-
{(item, i) => <div data-id={item[props.idField]} data-index={i()}>{props.children(item)}</div>}
78+
{(item, i) => (
79+
<div data-id={item[props.idField]} data-index={i()}>
80+
{props.children(item)}
81+
</div>
82+
)}
8483
</For>
8584
</div>
86-
)
85+
);
8786
}

0 commit comments

Comments
 (0)