trotl-table 1.0.5 → 1.0.7

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 CHANGED
@@ -15,22 +15,6 @@ yarn add trotl-table
15
15
 
16
16
 
17
17
  ⚡ Quick Start
18
- Wrap your app with the provider:
19
-
20
- import { StrictMode } from "react";
21
- import { createRoot } from "react-dom/client";
22
- import App from "./App";
23
- import { TableProvider } from "trotl-table";
24
-
25
- createRoot(document.getElementById("root")).render(
26
- <StrictMode>
27
- <TableProvider>
28
- <App />
29
- </TableProvider>
30
- </StrictMode>
31
- );
32
-
33
- Render a table:
34
18
 
35
19
  import React, { useState } from "react";
36
20
  import { Table } from "trotl-table";
package/dist/index.cjs.js CHANGED
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
3
5
  var React = require('react');
4
6
  var reactDnd = require('react-dnd');
5
7
  var reactDndHtml5Backend = require('react-dnd-html5-backend');
@@ -23,144 +25,6 @@ function _interopNamespaceDefault(e) {
23
25
 
24
26
  var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
25
27
 
26
- const TableContext = /*#__PURE__*/React.createContext(null);
27
- const useTable = () => React.useContext(TableContext);
28
- const TableProvider = ({
29
- children
30
- }) => {
31
- // Stores meta per tableId
32
- const [tables, setTables] = React.useState({});
33
-
34
- // Store selected rows per table
35
- const [selectedRows, setSelectedRows] = React.useState({});
36
-
37
- // Store sorting per table
38
- const [sorting, setSorting] = React.useState({});
39
-
40
- // -----------------------------
41
- // Stable functions (useCallback)
42
- // -----------------------------
43
-
44
- const registerTable = React.useCallback((tableId, meta = {}) => {
45
- setTables(prev => ({
46
- ...prev,
47
- [tableId]: {
48
- ...(prev[tableId] || {}),
49
- ...meta
50
- }
51
- }));
52
- }, []);
53
- const unregisterTable = React.useCallback(tableId => {
54
- setTables(prev => {
55
- const newTables = {
56
- ...prev
57
- };
58
- delete newTables[tableId];
59
- return newTables;
60
- });
61
- setSelectedRows(prev => {
62
- const x = {
63
- ...prev
64
- };
65
- delete x[tableId];
66
- return x;
67
- });
68
- setSorting(prev => {
69
- const x = {
70
- ...prev
71
- };
72
- delete x[tableId];
73
- return x;
74
- });
75
- }, []);
76
- const updateTableMeta = React.useCallback((tableId, patch = {}) => {
77
- setTables(prev => ({
78
- ...prev,
79
- [tableId]: {
80
- ...(prev[tableId] || {}),
81
- ...patch
82
- }
83
- }));
84
- }, []);
85
-
86
- // -----------------------------
87
- // Manage Selected Rows
88
- // -----------------------------
89
-
90
- const toggleRowSelection = React.useCallback((tableId, rowId) => {
91
- setSelectedRows(prev => {
92
- const rowSet = new Set(prev[tableId] || []);
93
- rowSet.has(rowId) ? rowSet.delete(rowId) : rowSet.add(rowId);
94
- return {
95
- ...prev,
96
- [tableId]: Array.from(rowSet)
97
- };
98
- });
99
- }, []);
100
- const clearSelection = React.useCallback(tableId => {
101
- setSelectedRows(prev => ({
102
- ...prev,
103
- [tableId]: []
104
- }));
105
- }, []);
106
-
107
- // -----------------------------
108
- // Sorting
109
- // -----------------------------
110
-
111
- const setSort = React.useCallback((tableId, column) => {
112
- setSorting(prev => {
113
- const current = prev[tableId];
114
-
115
- // cycling: null → asc → desc → null
116
- if (!current || current.column !== column) {
117
- return {
118
- ...prev,
119
- [tableId]: {
120
- column,
121
- direction: "asc"
122
- }
123
- };
124
- }
125
- if (current.direction === "asc") {
126
- return {
127
- ...prev,
128
- [tableId]: {
129
- column,
130
- direction: "desc"
131
- }
132
- };
133
- }
134
-
135
- // if direction === "desc" → remove sorting
136
- return {
137
- ...prev,
138
- [tableId]: null
139
- };
140
- });
141
- }, []);
142
-
143
- // ------------------------------------
144
- // Memoized final context value
145
- // ------------------------------------
146
- const value = React.useMemo(() => {
147
- return {
148
- tables,
149
- selectedRows,
150
- sorting,
151
- registerTable,
152
- unregisterTable,
153
- updateTableMeta,
154
- toggleRowSelection,
155
- clearSelection,
156
- setSort
157
- };
158
- }, [tables, selectedRows, sorting, registerTable, unregisterTable, updateTableMeta, toggleRowSelection, clearSelection, setSort]);
159
- return /*#__PURE__*/React.createElement(TableContext.Provider, {
160
- value: value
161
- }, children);
162
- };
163
-
164
28
  function _classCallCheck(a, n) {
165
29
  if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
166
30
  }
@@ -9821,34 +9685,30 @@ function Table({
9821
9685
  buttons = ["view", "edit", "delete"],
9822
9686
  enableDragRow = false // new prop
9823
9687
  }) {
9824
- const ctx = useTable();
9825
- const {
9826
- registerTable,
9827
- unregisterTable,
9828
- updateTableMeta,
9829
- selectedRows,
9830
- toggleRowSelection,
9831
- clearSelection,
9832
- sorting,
9833
- setSort
9834
- } = ctx;
9835
- const tableSelected = React.useMemo(() => selectedRows[tableId] || [], [selectedRows, tableId]);
9688
+ // Local selection & sorting state (removed TableContext dependency)
9689
+ const [selectedRows, setSelectedRows] = React.useState([]);
9690
+ const [sorting, setSorting] = React.useState(null);
9691
+ const toggleRowSelection = React.useCallback(rowId => {
9692
+ setSelectedRows(prev => prev.includes(rowId) ? prev.filter(r => r !== rowId) : [...prev, rowId]);
9693
+ }, []);
9694
+ const clearSelection = React.useCallback(() => setSelectedRows([]), []);
9695
+ const setSort = column => {
9696
+ setSorting(prev => {
9697
+ if (!prev || prev.column !== column) return {
9698
+ column,
9699
+ direction: "asc"
9700
+ };
9701
+ if (prev.direction === "asc") return {
9702
+ column,
9703
+ direction: "desc"
9704
+ };
9705
+ return null;
9706
+ });
9707
+ };
9836
9708
  console.log(extraSearchTerm);
9837
9709
  const [localData, setLocalData] = React.useState(data);
9838
9710
  React.useEffect(() => setLocalData(data), [data]);
9839
9711
  const listRef = React.useRef(null);
9840
- React.useEffect(() => {
9841
- registerTable(tableId, {
9842
- columns,
9843
- rowCount: data.length
9844
- });
9845
- return () => unregisterTable(tableId);
9846
- }, []);
9847
- React.useEffect(() => {
9848
- updateTableMeta(tableId, {
9849
- rowCount: data.length
9850
- });
9851
- }, [data.length]);
9852
9712
  const normalizedGroups = React.useMemo(() => {
9853
9713
  return normalizeData(isGrouped ? localData : [], isGrouped ? [] : localData);
9854
9714
  }, [localData, isGrouped]);
@@ -9895,8 +9755,8 @@ function Table({
9895
9755
  return 0;
9896
9756
  };
9897
9757
  const sortedGroupedData = React.useMemo(() => {
9898
- const sortKey = sorting[tableId]?.column;
9899
- const direction = sorting[tableId]?.direction || "asc";
9758
+ const sortKey = sorting?.column;
9759
+ const direction = sorting?.direction || "asc";
9900
9760
  const sortRows = rows => {
9901
9761
  const arr = [...rows];
9902
9762
  if (!sortKey) return filterRows(arr); // fallback to drag order
@@ -9907,7 +9767,7 @@ function Table({
9907
9767
  ...g,
9908
9768
  rows: sortRows(g.rows || [])
9909
9769
  }));
9910
- }, [normalizedGroups, sorting, tableId, filterRows]);
9770
+ }, [normalizedGroups, sorting, filterRows]);
9911
9771
  const [expandedGroups, setExpandedGroups] = React.useState({});
9912
9772
  const toggleGroup = gid => {
9913
9773
  setExpandedGroups(prev => ({
@@ -9969,7 +9829,7 @@ function Table({
9969
9829
  }, [flattened]);
9970
9830
 
9971
9831
  // useEffect(() => setTableDataFlat(flattened), [flattened]);
9972
- React.useEffect(() => selectedRowsCallback(tableSelected), [tableSelected, selectedRowsCallback]);
9832
+ React.useEffect(() => selectedRowsCallback(selectedRows), [selectedRows, selectedRowsCallback]);
9973
9833
 
9974
9834
  // DELETE
9975
9835
  const [showConfirm, setShowConfirm] = React.useState(false);
@@ -10074,7 +9934,7 @@ function Table({
10074
9934
  if (item.type === "group") {
10075
9935
  const gid = item.groupId;
10076
9936
  const rows = groupRowsById[gid] || [];
10077
- const allSelected = rows.length > 0 && rows.every(r => tableSelected.includes(r.id));
9937
+ const allSelected = rows.length > 0 && rows.every(r => selectedRows.includes(r.id));
10078
9938
  return /*#__PURE__*/React.createElement("div", {
10079
9939
  key: key,
10080
9940
  style: style,
@@ -10112,8 +9972,8 @@ function Table({
10112
9972
  className: "table-cell checkbox-cell"
10113
9973
  }, /*#__PURE__*/React.createElement("input", {
10114
9974
  type: "checkbox",
10115
- checked: tableSelected.includes(row.id),
10116
- onChange: () => toggleRowSelection(tableId, row.id)
9975
+ checked: selectedRows.includes(row.id),
9976
+ onChange: () => toggleRowSelection(row.id)
10117
9977
  })), showKey && /*#__PURE__*/React.createElement("div", {
10118
9978
  className: "table-cell key-cell"
10119
9979
  }, visualIndex), columns.map((col, i) => /*#__PURE__*/React.createElement("div", {
@@ -10155,7 +10015,7 @@ function Table({
10155
10015
  }, content);
10156
10016
  }
10157
10017
  return content;
10158
- }, [tableDataFlat, columns, tableSelected, toggleRowSelection, groupRowsById, renderCell, showActions, showDelete, showEdit, showKey, showView, tableId, viewCallback, editCallback, enableDragRow, moveRow]);
10018
+ }, [tableDataFlat, columns, selectedRows, toggleRowSelection, groupRowsById, renderCell, showActions, showDelete, showEdit, showKey, showView, tableId, viewCallback, editCallback, enableDragRow, moveRow]);
10159
10019
  const rowHeightGetter = ({
10160
10020
  index
10161
10021
  }) => tableDataFlat[index]?.type === "group" ? groupHeaderHeight : rowHeight;
@@ -10177,13 +10037,13 @@ function Table({
10177
10037
  className: "table-cell checkbox-cell"
10178
10038
  }, /*#__PURE__*/React.createElement("input", {
10179
10039
  type: "checkbox",
10180
- checked: tableDataFlat.filter(i => i.type === "row").length > 0 && tableSelected.length === tableDataFlat.filter(i => i.type === "row").length,
10040
+ checked: tableDataFlat.filter(i => i.type === "row").length > 0 && selectedRows.length === tableDataFlat.filter(i => i.type === "row").length,
10181
10041
  onChange: e => {
10182
10042
  if (e.target.checked) {
10183
- clearSelection(tableId);
10184
- tableDataFlat.filter(i => i.type === "row").forEach(i => toggleRowSelection(tableId, i.row.id));
10043
+ clearSelection();
10044
+ tableDataFlat.filter(i => i.type === "row").forEach(i => toggleRowSelection(i.row.id));
10185
10045
  } else {
10186
- clearSelection(tableId);
10046
+ clearSelection();
10187
10047
  }
10188
10048
  }
10189
10049
  })), showKey && /*#__PURE__*/React.createElement("div", {
@@ -10191,11 +10051,11 @@ function Table({
10191
10051
  }, "#"), columns.map((col, i) => /*#__PURE__*/React.createElement("div", {
10192
10052
  key: i,
10193
10053
  className: "table-cell",
10194
- onClick: () => setSort(tableId, col.accessor),
10054
+ onClick: () => setSort(col.accessor),
10195
10055
  style: {
10196
10056
  cursor: "pointer"
10197
10057
  }
10198
- }, col.header, sorting[tableId]?.column === col.accessor && (sorting[tableId].direction === "asc" ? " ↑" : " ↓"))), showActions && /*#__PURE__*/React.createElement("div", {
10058
+ }, col.header, sorting?.column === col.accessor && (sorting.direction === "asc" ? " ↑" : " ↓"))), showActions && /*#__PURE__*/React.createElement("div", {
10199
10059
  className: "table-cell action-cell"
10200
10060
  }, "Action"))), /*#__PURE__*/React.createElement("div", {
10201
10061
  className: "main-table",
@@ -10224,8 +10084,9 @@ function Table({
10224
10084
  }, /*#__PURE__*/React.createElement("p", null, "Are you sure you want to delete ", /*#__PURE__*/React.createElement("strong", null, pendingDelete?.name || pendingDelete?.id), "?"))));
10225
10085
  }
10226
10086
 
10087
+ // src/index.js
10088
+ // if you want named import
10089
+
10227
10090
  exports.Table = Table;
10228
- exports.TableContext = TableContext;
10229
- exports.TableProvider = TableProvider;
10230
- exports.useTable = useTable;
10091
+ exports.default = Table;
10231
10092
  //# sourceMappingURL=index.cjs.js.map