flexitablesort 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,21 +18,40 @@ Peer dependencies: `react` and `react-dom` >= 17.0.0
|
|
|
18
18
|
## Quick Start
|
|
19
19
|
|
|
20
20
|
```jsx
|
|
21
|
+
import { useState } from "react";
|
|
21
22
|
import {
|
|
22
23
|
TableContainer, TableHeader, ColumnCell,
|
|
23
24
|
TableBody, BodyRow, RowCell,
|
|
24
25
|
} from "flexitablesort";
|
|
25
26
|
|
|
27
|
+
const INIT_COLS = [
|
|
28
|
+
{ id: "name", label: "Name", width: 150 },
|
|
29
|
+
{ id: "age", label: "Age", width: 100 },
|
|
30
|
+
{ id: "email",label: "Email", width: 200 },
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
const INIT_ROWS = [
|
|
34
|
+
{ id: "1", name: "Alice", age: 28, email: "alice@example.com" },
|
|
35
|
+
{ id: "2", name: "Bob", age: 34, email: "bob@example.com" },
|
|
36
|
+
{ id: "3", name: "Carol", age: 22, email: "carol@example.com" },
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
function arrayMove(arr, from, to) {
|
|
40
|
+
const next = [...arr];
|
|
41
|
+
const [item] = next.splice(from, 1);
|
|
42
|
+
next.splice(to, 0, item);
|
|
43
|
+
return next;
|
|
44
|
+
}
|
|
45
|
+
|
|
26
46
|
function MyTable() {
|
|
27
|
-
const [
|
|
28
|
-
const [
|
|
47
|
+
const [cols, setCols] = useState(INIT_COLS);
|
|
48
|
+
const [rows, setRows] = useState(INIT_ROWS);
|
|
29
49
|
|
|
30
50
|
const handleDragEnd = ({ sourceIndex, targetIndex, dragType }) => {
|
|
31
|
-
if (dragType === "
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
setRows(next);
|
|
51
|
+
if (dragType === "column") {
|
|
52
|
+
setCols(arrayMove(cols, sourceIndex, targetIndex));
|
|
53
|
+
} else {
|
|
54
|
+
setRows(arrayMove(rows, sourceIndex, targetIndex));
|
|
36
55
|
}
|
|
37
56
|
};
|
|
38
57
|
|
|
@@ -40,8 +59,8 @@ function MyTable() {
|
|
|
40
59
|
<TableContainer onDragEnd={handleDragEnd}>
|
|
41
60
|
<TableHeader>
|
|
42
61
|
{cols.map((col, i) => (
|
|
43
|
-
<ColumnCell key={col.id} id={col.id} index={i} width={
|
|
44
|
-
{col.
|
|
62
|
+
<ColumnCell key={col.id} id={col.id} index={i} width={col.width}>
|
|
63
|
+
{col.label}
|
|
45
64
|
</ColumnCell>
|
|
46
65
|
))}
|
|
47
66
|
</TableHeader>
|
|
@@ -49,7 +68,7 @@ function MyTable() {
|
|
|
49
68
|
{rows.map((row, ri) => (
|
|
50
69
|
<BodyRow key={row.id} id={row.id} index={ri}>
|
|
51
70
|
{cols.map((col, ci) => (
|
|
52
|
-
<RowCell key={col.id} index={ci} width={
|
|
71
|
+
<RowCell key={col.id} index={ci} width={col.width}>
|
|
53
72
|
{row[col.id]}
|
|
54
73
|
</RowCell>
|
|
55
74
|
))}
|