flexitablesort 1.0.1 → 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.
Files changed (2) hide show
  1. package/README.md +34 -18
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,13 +1,10 @@
1
- # FlexiTableSort
1
+ # flexitablesort
2
2
 
3
- ![npm](https://img.shields.io/npm/v/flexitablesort?color=blue&label=npm%20version&style=flat-square)
4
- ![downloads](https://img.shields.io/npm/dw/flexitablesort?color=brightgreen&style=flat-square)
5
- ![license](https://img.shields.io/npm/l/flexitablesort?style=flat-square)
6
- ![typescript](https://img.shields.io/badge/types-yes-brightgreen?style=flat-square)
7
- ![stars](https://img.shields.io/github/stars/samiodeh1337/sortable-table?style=social)
8
- ![build](https://github.com/samiodeh1337/sortable-table/actions/workflows/deploy-docs.yml/badge.svg)
3
+ [![npm version](https://img.shields.io/npm/v/flexitablesort)](https://www.npmjs.com/package/flexitablesort)
4
+ [![npm bundle size](https://img.shields.io/bundlephobia/minzip/flexitablesort)](https://bundlephobia.com/package/flexitablesort)
5
+ [![license](https://img.shields.io/npm/l/flexitablesort)](https://github.com/samiodeh1337/sortable-table/blob/main/LICENSE)
6
+ [![npm downloads](https://img.shields.io/npm/dm/flexitablesort)](https://www.npmjs.com/package/flexitablesort)
9
7
 
10
- ---
11
8
  Drag-and-drop row and column reordering for React tables. Smooth animations, auto-scroll, virtual scrolling support, and zero external UI dependencies.
12
9
 
13
10
  ## Install
@@ -21,21 +18,40 @@ Peer dependencies: `react` and `react-dom` >= 17.0.0
21
18
  ## Quick Start
22
19
 
23
20
  ```jsx
21
+ import { useState } from "react";
24
22
  import {
25
23
  TableContainer, TableHeader, ColumnCell,
26
24
  TableBody, BodyRow, RowCell,
27
25
  } from "flexitablesort";
28
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
+
29
46
  function MyTable() {
30
- const [rows, setRows] = useState(data);
31
- const [cols, setCols] = useState(columns);
47
+ const [cols, setCols] = useState(INIT_COLS);
48
+ const [rows, setRows] = useState(INIT_ROWS);
32
49
 
33
50
  const handleDragEnd = ({ sourceIndex, targetIndex, dragType }) => {
34
- if (dragType === "row") {
35
- const next = [...rows];
36
- const [moved] = next.splice(sourceIndex, 1);
37
- next.splice(targetIndex, 0, moved);
38
- setRows(next);
51
+ if (dragType === "column") {
52
+ setCols(arrayMove(cols, sourceIndex, targetIndex));
53
+ } else {
54
+ setRows(arrayMove(rows, sourceIndex, targetIndex));
39
55
  }
40
56
  };
41
57
 
@@ -43,8 +59,8 @@ function MyTable() {
43
59
  <TableContainer onDragEnd={handleDragEnd}>
44
60
  <TableHeader>
45
61
  {cols.map((col, i) => (
46
- <ColumnCell key={col.id} id={col.id} index={i} width={150}>
47
- {col.title}
62
+ <ColumnCell key={col.id} id={col.id} index={i} width={col.width}>
63
+ {col.label}
48
64
  </ColumnCell>
49
65
  ))}
50
66
  </TableHeader>
@@ -52,7 +68,7 @@ function MyTable() {
52
68
  {rows.map((row, ri) => (
53
69
  <BodyRow key={row.id} id={row.id} index={ri}>
54
70
  {cols.map((col, ci) => (
55
- <RowCell key={col.id} index={ci} width={150}>
71
+ <RowCell key={col.id} index={ci} width={col.width}>
56
72
  {row[col.id]}
57
73
  </RowCell>
58
74
  ))}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flexitablesort",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "type": "module",
5
5
  "description": "Sortable table with draggable rows and columns",
6
6
  "main": "dist/index.cjs.js",