react-live-data-table 1.0.12 → 1.0.14

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-live-data-table",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
4
4
  "description": "Your React component package with Tailwind",
5
5
  "main": "src/index.js",
6
6
  "module": "dist/index.esm.js",
@@ -17,18 +17,25 @@ function ReactDataTable({
17
17
  rowHeights = 40,
18
18
  headerProps = {},
19
19
  selected = {},
20
- showSelectAllCheckbox=true
20
+ showSelectAllCheckbox = true,
21
+ rowStyle = {},
22
+ rowClassName = ""
21
23
  }) {
22
24
  const tableContainerRef = React.useRef(null);
23
25
  const [data, setData] = React.useState({ pages: [], meta: { totalPages: 1 } });
24
26
  const [isFetching, setIsFetching] = React.useState(false);
25
27
  const [pageParam, setPageParam] = React.useState(1);
26
28
  const [selectedRows, setSelectedRows] = React.useState(selected);
29
+ const previousSelected = React.useRef(selected);
27
30
 
28
-
31
+
29
32
  useEffect(() => {
30
- setSelectedRows({ ...selected })
31
- }, [selected])
33
+ if (JSON.stringify(previousSelected.current) !== JSON.stringify(selected)) {
34
+ setSelectedRows({...selected});
35
+ previousSelected.current = selected;
36
+ }
37
+ }, [selected]);
38
+
32
39
 
33
40
  useEffect(() => {
34
41
  setData({ pages: [], meta: { totalPages: 1 } });
@@ -159,14 +166,9 @@ function ReactDataTable({
159
166
  }
160
167
  };
161
168
 
162
- // This is the problem area - it was causing an infinite loop
163
169
  useEffect(() => {
164
- const containerElement = tableContainerRef.current;
165
- if (containerElement && !staticData) {
166
- handleScroll(containerElement);
167
- }
168
- // Don't include 'data' as a dependency here
169
- }, [pageParam, isFetching]); // Only depend on pageParam and isFetching
170
+ handleScroll(tableContainerRef.current);
171
+ }, [data]);
170
172
 
171
173
  const flatData = data.pages.flatMap(page => page.data);
172
174
 
@@ -176,17 +178,15 @@ function ReactDataTable({
176
178
  minWidth: 50,
177
179
  textAlign: "center",
178
180
  header: ({ data }) => (
179
- showSelectAllCheckbox ? <div className="flex items-center justify-center h-[40px]">
180
- <input
181
+ <div className="flex items-center justify-center h-[40px]">
182
+ {showSelectAllCheckbox && <input
181
183
  id={data.id}
182
184
  type="checkbox"
183
185
  className='bg-gray-700 rounded-4 border-gray-200 text-blue-400 focus:ring-0 focus:ring-white'
184
186
  checked={Object.keys(selectedRows).length > 0 && data.every(row => selectedRows[row.id])}
185
187
  onChange={(e) => handleSelectAll(e.target.checked, flatData)}
186
- />
188
+ />}
187
189
  </div>
188
- :
189
- <div className="flex items-center justify-center h-[40px]"></div>
190
190
  ),
191
191
  cell: ({ row }) => {
192
192
  return (
@@ -207,7 +207,7 @@ function ReactDataTable({
207
207
  const enhancedColumns = showCheckbox ? [checkboxColumn, ...columns] : columns;
208
208
 
209
209
  return (
210
- <div className="bg-white relative w-full">
210
+ <div className="bg-white relative w-full react-live-data-table" >
211
211
  {loading && (
212
212
  <div className="absolute inset-0 bg-white/50 z-20 flex items-center justify-center">
213
213
  <svg
@@ -262,10 +262,12 @@ function ReactDataTable({
262
262
  style={{ ...headerProps.style }}
263
263
  >
264
264
  <tr>
265
- {enhancedColumns.map(column => (
265
+ {enhancedColumns.map((column, columnIndex) => (
266
266
  <th
267
267
  key={column.accessorKey || column.id}
268
- className="text-left font-normal h-[40px]"
268
+ className={`text-left font-normal h-[40px] border-b border-t border-solid border-[#e4e3e2] ${
269
+ columnIndex < enhancedColumns.length - 1 ? 'border-r' : ''
270
+ }`}
269
271
  style={{
270
272
  width: column.size,
271
273
  minWidth: column.minWidth,
@@ -279,27 +281,37 @@ function ReactDataTable({
279
281
  </thead>
280
282
  <tbody>
281
283
  {flatData.length > 0 ? (
282
- flatData.map((row, index) => (
283
- <tr
284
- key={row.id}
285
- className={`border-t border-x border-gray-200 hover:bg-[#dee1f2] h-[${rowHeights}px] ${selectedRows[row.id] ? 'bg-[#dee1f2]' : ''}`}
286
- onClick={() => handleRowClick(row, index, flatData)}
287
- >
288
- {enhancedColumns.map(column => (
289
- <td
290
- key={column.accessorKey || column.id}
291
- className={`text-left font-normal border-r ${column?.cellProps?.className || ''}`}
292
- style={{
293
- minWidth: `${column.minWidth}px`,
294
- textAlign: column?.textAlign,
295
- ...column?.cellProps?.style,
296
- }}
297
- >
298
- {typeof column.cell === 'function' ? column.cell({ row }) : null}
299
- </td>
300
- ))}
301
- </tr>
302
- ))
284
+ flatData.map((row, index) => {
285
+ const isLastRow = index === flatData.length - 1;
286
+ return (
287
+ <tr
288
+ key={row.id}
289
+ className={`border-t ${isLastRow ? 'border-b' : ''} border-gray-200 hover:bg-[#dee1f2] ${selectedRows[row.id] ? 'bg-[#dee1f2]' : ''} ${rowClassName}`}
290
+ style={{
291
+ height: `${rowHeights}px`,
292
+ ...rowStyle,
293
+ ...(typeof rowStyle === 'function' ? rowStyle(row, index) : {})
294
+ }}
295
+ onClick={() => handleRowClick(row, index, flatData)}
296
+ >
297
+ {enhancedColumns.map((column, cellIndex) => (
298
+ <td
299
+ key={column.accessorKey || column.id}
300
+ className={`text-left font-normal ${
301
+ cellIndex < enhancedColumns.length-1 ? 'border-r' : ''
302
+ } ${column?.cellProps?.className || ''}`}
303
+ style={{
304
+ minWidth: `${column.minWidth}px`,
305
+ textAlign: column?.textAlign,
306
+ ...column?.cellProps?.style,
307
+ }}
308
+ >
309
+ {typeof column.cell === 'function' ? column.cell({ row }) : null}
310
+ </td>
311
+ ))}
312
+ </tr>
313
+ );
314
+ })
303
315
  ) : (
304
316
  <tr>
305
317
  <td colSpan={enhancedColumns.length} className="text-center py-4">