@trackunit/react-table 0.0.298 → 0.0.302

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/index.cjs.js CHANGED
@@ -5,12 +5,13 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var jsxRuntime = require('react/jsx-runtime');
6
6
  var i18nLibraryTranslation = require('@trackunit/i18n-library-translation');
7
7
  var reactComponents = require('@trackunit/react-components');
8
+ var sharedUtils = require('@trackunit/shared-utils');
8
9
  var React = require('react');
9
10
  var cssClassVarianceUtilities = require('@trackunit/css-class-variance-utilities');
10
11
  var reactRouter = require('@tanstack/react-router');
11
12
  var reactTable = require('@tanstack/react-table');
12
13
  var reactTableBaseComponents = require('@trackunit/react-table-base-components');
13
- var reactVirtual = require('@tanstack/react-virtual');
14
+ var reactTablePagination = require('@trackunit/react-table-pagination');
14
15
  var reactFormComponents = require('@trackunit/react-form-components');
15
16
  var update = require('immutability-helper');
16
17
  var reactDnd = require('react-dnd');
@@ -250,7 +251,7 @@ const ActionContainerAndOverflow = ({ actions, moreActions, dataTestId }) => {
250
251
  }
251
252
  return "visible";
252
253
  };
253
- const overflowItemCount = Object.values(itemOverflowMap).filter(isOverflowing => isOverflowing).length;
254
+ const overflowItemCount = sharedUtils.objectValues(itemOverflowMap).filter(isOverflowing => isOverflowing).length;
254
255
  return (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx("div", { className: cvaActionContainerAndOverflow(), ref: overflowContainerRef, children: React.Children.map(children, child => {
255
256
  return React.cloneElement(child, {
256
257
  className: `${child.props.className} ${itemVisibilityClassName(child.props.id)}`,
@@ -275,68 +276,6 @@ const ActionSheet = ({ actions, moreActions = [], selections, resetSelection, cl
275
276
  moreActions: moreActions.map(action => actionDataToMenuItem(action, dataTestId)) })] }));
276
277
  };
277
278
 
278
- const noPagination = {
279
- isLoading: false,
280
- nextPage: () => { },
281
- previousPage: () => { },
282
- pageInfo: {
283
- hasNextPage: false,
284
- hasPreviousPage: false,
285
- endCursor: null,
286
- startCursor: null,
287
- },
288
- };
289
-
290
- /**
291
- * Custom hook for implementing infinite scrolling in a table.
292
- *
293
- * @param {InfiniteScrollProps} props - The configuration object for the infinite scroll behavior.
294
- * @returns {InfiniteScroll} An object containing properties and functions for infinite scrolling.
295
- * @description
296
- * This hook is used to implement infinite scrolling in a table. It calculates the necessary padding
297
- * for virtual rows, provides a callback for fetching more data when the user scrolls to the bottom,
298
- * and manages virtualized rows for efficient rendering.
299
- * @example
300
- * const { paddingTop, paddingBottom, fetchMoreOnBottomReached, rowVirtualizer, virtualRows } = useInfiniteScroll({
301
- * pagination: relayPaginationObject,
302
- * containerRef: tableContainerRef,
303
- * rowSize: 50,
304
- * });
305
- */
306
- const useInfiniteScroll = (props) => {
307
- //called on scroll and possibly on mount to fetch more data as the user scrolls and reaches bottom of table
308
- const fetchMoreOnBottomReached = React.useCallback((containerRefElement) => {
309
- var _a;
310
- if (containerRefElement) {
311
- const { scrollHeight, scrollTop, clientHeight } = containerRefElement;
312
- //once the user has scrolled within 300px of the bottom of the table, fetch more data if there is any
313
- if (scrollHeight - scrollTop - clientHeight < 300 &&
314
- !props.pagination.isLoading &&
315
- ((_a = props.pagination.pageInfo) === null || _a === void 0 ? void 0 : _a.hasNextPage)) {
316
- props.pagination.nextPage();
317
- }
318
- }
319
- }, [props.pagination]);
320
- const rowVirtualizer = reactVirtual.useVirtualizer({
321
- count: props.rowSize,
322
- getScrollElement: () => props.containerRef.current,
323
- estimateSize: React.useCallback((i) => props.rowHeight, [props.rowHeight]),
324
- overscan: 10,
325
- });
326
- React.useEffect(() => {
327
- // this ensures the cache is cleared if the rowHeight changes
328
- if (props.rowHeight) {
329
- rowVirtualizer.measure();
330
- }
331
- }, [props.rowHeight, rowVirtualizer]);
332
- const { getVirtualItems, getTotalSize } = rowVirtualizer;
333
- return {
334
- fetchMoreOnBottomReached,
335
- getTotalSize,
336
- getVirtualItems,
337
- };
338
- };
339
-
340
279
  /**
341
280
  * Table component for displaying large sets of data in tables with infinite scroll, sorting, filtering and others.
342
281
  *
@@ -350,8 +289,8 @@ const Table = (_a) => {
350
289
  //we need a reference to the scrolling element for logic down below
351
290
  const tableContainerRef = React.useRef(null);
352
291
  const [t] = useTranslation();
353
- const { fetchMoreOnBottomReached, getVirtualItems, getTotalSize } = useInfiniteScroll({
354
- pagination: props.pagination || noPagination,
292
+ const { fetchMoreOnBottomReached, getVirtualItems, getTotalSize } = reactTablePagination.useInfiniteScroll({
293
+ pagination: props.pagination || reactTablePagination.noPagination,
355
294
  containerRef: tableContainerRef,
356
295
  rowSize: props.getRowModel().rows.length || 0,
357
296
  rowHeight,
@@ -467,12 +406,14 @@ const ColumnFiltersDragAndDrop = ({ columns, setColumnOrder, onUserEvent, }) =>
467
406
  const moveColumn = React__namespace.useCallback((dragIndex, hoverIndex) => {
468
407
  var _a;
469
408
  const dragColumn = localColumns[dragIndex];
470
- setLocalColumns(update__default["default"](localColumns, {
471
- $splice: [
472
- [dragIndex, 1],
473
- [hoverIndex, 0, dragColumn],
474
- ],
475
- }));
409
+ if (dragColumn) {
410
+ setLocalColumns(update__default["default"](localColumns, {
411
+ $splice: [
412
+ [dragIndex, 1],
413
+ [hoverIndex, 0, dragColumn],
414
+ ],
415
+ }));
416
+ }
476
417
  onUserEvent === null || onUserEvent === void 0 ? void 0 : onUserEvent("Column Reordering", {
477
418
  columnReordered: (_a = dragColumn === null || dragColumn === void 0 ? void 0 : dragColumn.columnDef.header) !== null && _a !== void 0 ? _a : "",
478
419
  });
@@ -614,6 +555,7 @@ const useTable = (_a) => {
614
555
  var _b, _c, _d, _e;
615
556
  var { onTableStateChange, initialState, columns } = _a, reactTableProps = __rest(_a, ["onTableStateChange", "initialState", "columns"]);
616
557
  const hiddenByDefaultState = React.useMemo(() => {
558
+ // eslint-disable-next-line local-rules/prefer-custom-object-from-entries
617
559
  return Object.fromEntries(columns.map(column => { var _a, _b; return [column.id, (_b = !((_a = column.meta) === null || _a === void 0 ? void 0 : _a.hiddenByDefault)) !== null && _b !== void 0 ? _b : true]; }));
618
560
  }, [columns]);
619
561
  const [columnVisibility, setColumnVisibility] = React.useState(((_b = reactTableProps.state) === null || _b === void 0 ? void 0 : _b.columnVisibility) || (initialState === null || initialState === void 0 ? void 0 : initialState.columnVisibility) || hiddenByDefaultState || {});
@@ -628,8 +570,8 @@ const useTable = (_a) => {
628
570
  }
629
571
  }, [isFirstRender, columns]);
630
572
  React.useEffect(() => {
631
- if (initialState && Object.keys(initialState || {}).length > 0) {
632
- setColumnVisibility(Object.keys(initialState.columnVisibility || {}).length > 0
573
+ if (initialState && sharedUtils.objectKeys(initialState || {}).length > 0) {
574
+ setColumnVisibility(sharedUtils.objectKeys(initialState.columnVisibility || {}).length > 0
633
575
  ? initialState.columnVisibility
634
576
  : hiddenByDefaultState || {});
635
577
  setColumnOrder(initialState.columnOrder || []);
@@ -718,7 +660,7 @@ const useTableSelection = ({ data, defaultSelectedIds, enableRowSelection = true
718
660
  const toggleRowSelectionState = React.useCallback((id) => {
719
661
  setRowSelection(prevRowSelection => (Object.assign(Object.assign({}, prevRowSelection), { [id]: !prevRowSelection[id] })));
720
662
  }, []);
721
- const selectedIds = React.useMemo(() => Object.entries(rowSelection)
663
+ const selectedIds = React.useMemo(() => sharedUtils.objectEntries(rowSelection)
722
664
  .filter(([_, selected]) => selected)
723
665
  .map(([id]) => id), [rowSelection]);
724
666
  const columnHelper = React.useMemo(() => reactTable.createColumnHelper(), []);
package/index.esm.js CHANGED
@@ -1,14 +1,15 @@
1
1
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
2
2
  import { registerTranslations, useNamespaceTranslation } from '@trackunit/i18n-library-translation';
3
3
  import { MenuItem, Icon, Text, Button, useOverflowItems, MoreMenu, MenuList, IconButton, Card, Spinner, EmptyState, Tooltip, Popover, PopoverTrigger, PopoverContent, useIsFirstRender } from '@trackunit/react-components';
4
+ import { objectValues, objectKeys, objectEntries } from '@trackunit/shared-utils';
4
5
  import * as React from 'react';
5
- import { useMemo, Children, cloneElement, useCallback, useEffect, useRef, useState } from 'react';
6
+ import { useMemo, Children, cloneElement, useRef, useState, useEffect, useCallback } from 'react';
6
7
  import { cvaMerge } from '@trackunit/css-class-variance-utilities';
7
8
  import { Link } from '@tanstack/react-router';
8
9
  import { flexRender, useReactTable, getSortedRowModel, getCoreRowModel, createColumnHelper } from '@tanstack/react-table';
9
10
  export { createColumnHelper } from '@tanstack/react-table';
10
11
  import { TableRoot, Thead, Tr, Th, SortIndicator, ResizeHandle, Tbody, Td } from '@trackunit/react-table-base-components';
11
- import { useVirtualizer } from '@tanstack/react-virtual';
12
+ import { useInfiniteScroll, noPagination } from '@trackunit/react-table-pagination';
12
13
  import { Toggle, RadioGroup, RadioItem, Checkbox } from '@trackunit/react-form-components';
13
14
  import update from 'immutability-helper';
14
15
  import { DndProvider, useDrop, useDrag } from 'react-dnd';
@@ -225,7 +226,7 @@ const ActionContainerAndOverflow = ({ actions, moreActions, dataTestId }) => {
225
226
  }
226
227
  return "visible";
227
228
  };
228
- const overflowItemCount = Object.values(itemOverflowMap).filter(isOverflowing => isOverflowing).length;
229
+ const overflowItemCount = objectValues(itemOverflowMap).filter(isOverflowing => isOverflowing).length;
229
230
  return (jsxs(Fragment, { children: [jsx("div", { className: cvaActionContainerAndOverflow(), ref: overflowContainerRef, children: Children.map(children, child => {
230
231
  return cloneElement(child, {
231
232
  className: `${child.props.className} ${itemVisibilityClassName(child.props.id)}`,
@@ -250,68 +251,6 @@ const ActionSheet = ({ actions, moreActions = [], selections, resetSelection, cl
250
251
  moreActions: moreActions.map(action => actionDataToMenuItem(action, dataTestId)) })] }));
251
252
  };
252
253
 
253
- const noPagination = {
254
- isLoading: false,
255
- nextPage: () => { },
256
- previousPage: () => { },
257
- pageInfo: {
258
- hasNextPage: false,
259
- hasPreviousPage: false,
260
- endCursor: null,
261
- startCursor: null,
262
- },
263
- };
264
-
265
- /**
266
- * Custom hook for implementing infinite scrolling in a table.
267
- *
268
- * @param {InfiniteScrollProps} props - The configuration object for the infinite scroll behavior.
269
- * @returns {InfiniteScroll} An object containing properties and functions for infinite scrolling.
270
- * @description
271
- * This hook is used to implement infinite scrolling in a table. It calculates the necessary padding
272
- * for virtual rows, provides a callback for fetching more data when the user scrolls to the bottom,
273
- * and manages virtualized rows for efficient rendering.
274
- * @example
275
- * const { paddingTop, paddingBottom, fetchMoreOnBottomReached, rowVirtualizer, virtualRows } = useInfiniteScroll({
276
- * pagination: relayPaginationObject,
277
- * containerRef: tableContainerRef,
278
- * rowSize: 50,
279
- * });
280
- */
281
- const useInfiniteScroll = (props) => {
282
- //called on scroll and possibly on mount to fetch more data as the user scrolls and reaches bottom of table
283
- const fetchMoreOnBottomReached = useCallback((containerRefElement) => {
284
- var _a;
285
- if (containerRefElement) {
286
- const { scrollHeight, scrollTop, clientHeight } = containerRefElement;
287
- //once the user has scrolled within 300px of the bottom of the table, fetch more data if there is any
288
- if (scrollHeight - scrollTop - clientHeight < 300 &&
289
- !props.pagination.isLoading &&
290
- ((_a = props.pagination.pageInfo) === null || _a === void 0 ? void 0 : _a.hasNextPage)) {
291
- props.pagination.nextPage();
292
- }
293
- }
294
- }, [props.pagination]);
295
- const rowVirtualizer = useVirtualizer({
296
- count: props.rowSize,
297
- getScrollElement: () => props.containerRef.current,
298
- estimateSize: useCallback((i) => props.rowHeight, [props.rowHeight]),
299
- overscan: 10,
300
- });
301
- useEffect(() => {
302
- // this ensures the cache is cleared if the rowHeight changes
303
- if (props.rowHeight) {
304
- rowVirtualizer.measure();
305
- }
306
- }, [props.rowHeight, rowVirtualizer]);
307
- const { getVirtualItems, getTotalSize } = rowVirtualizer;
308
- return {
309
- fetchMoreOnBottomReached,
310
- getTotalSize,
311
- getVirtualItems,
312
- };
313
- };
314
-
315
254
  /**
316
255
  * Table component for displaying large sets of data in tables with infinite scroll, sorting, filtering and others.
317
256
  *
@@ -442,12 +381,14 @@ const ColumnFiltersDragAndDrop = ({ columns, setColumnOrder, onUserEvent, }) =>
442
381
  const moveColumn = React.useCallback((dragIndex, hoverIndex) => {
443
382
  var _a;
444
383
  const dragColumn = localColumns[dragIndex];
445
- setLocalColumns(update(localColumns, {
446
- $splice: [
447
- [dragIndex, 1],
448
- [hoverIndex, 0, dragColumn],
449
- ],
450
- }));
384
+ if (dragColumn) {
385
+ setLocalColumns(update(localColumns, {
386
+ $splice: [
387
+ [dragIndex, 1],
388
+ [hoverIndex, 0, dragColumn],
389
+ ],
390
+ }));
391
+ }
451
392
  onUserEvent === null || onUserEvent === void 0 ? void 0 : onUserEvent("Column Reordering", {
452
393
  columnReordered: (_a = dragColumn === null || dragColumn === void 0 ? void 0 : dragColumn.columnDef.header) !== null && _a !== void 0 ? _a : "",
453
394
  });
@@ -589,6 +530,7 @@ const useTable = (_a) => {
589
530
  var _b, _c, _d, _e;
590
531
  var { onTableStateChange, initialState, columns } = _a, reactTableProps = __rest(_a, ["onTableStateChange", "initialState", "columns"]);
591
532
  const hiddenByDefaultState = useMemo(() => {
533
+ // eslint-disable-next-line local-rules/prefer-custom-object-from-entries
592
534
  return Object.fromEntries(columns.map(column => { var _a, _b; return [column.id, (_b = !((_a = column.meta) === null || _a === void 0 ? void 0 : _a.hiddenByDefault)) !== null && _b !== void 0 ? _b : true]; }));
593
535
  }, [columns]);
594
536
  const [columnVisibility, setColumnVisibility] = useState(((_b = reactTableProps.state) === null || _b === void 0 ? void 0 : _b.columnVisibility) || (initialState === null || initialState === void 0 ? void 0 : initialState.columnVisibility) || hiddenByDefaultState || {});
@@ -603,8 +545,8 @@ const useTable = (_a) => {
603
545
  }
604
546
  }, [isFirstRender, columns]);
605
547
  useEffect(() => {
606
- if (initialState && Object.keys(initialState || {}).length > 0) {
607
- setColumnVisibility(Object.keys(initialState.columnVisibility || {}).length > 0
548
+ if (initialState && objectKeys(initialState || {}).length > 0) {
549
+ setColumnVisibility(objectKeys(initialState.columnVisibility || {}).length > 0
608
550
  ? initialState.columnVisibility
609
551
  : hiddenByDefaultState || {});
610
552
  setColumnOrder(initialState.columnOrder || []);
@@ -693,7 +635,7 @@ const useTableSelection = ({ data, defaultSelectedIds, enableRowSelection = true
693
635
  const toggleRowSelectionState = useCallback((id) => {
694
636
  setRowSelection(prevRowSelection => (Object.assign(Object.assign({}, prevRowSelection), { [id]: !prevRowSelection[id] })));
695
637
  }, []);
696
- const selectedIds = useMemo(() => Object.entries(rowSelection)
638
+ const selectedIds = useMemo(() => objectEntries(rowSelection)
697
639
  .filter(([_, selected]) => selected)
698
640
  .map(([id]) => id), [rowSelection]);
699
641
  const columnHelper = useMemo(() => createColumnHelper(), []);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-table",
3
- "version": "0.0.298",
3
+ "version": "0.0.302",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -17,12 +17,12 @@
17
17
  "react-dnd": "14.0.5",
18
18
  "react-dnd-html5-backend": "14.1.0",
19
19
  "jest-fetch-mock": "^3.0.3",
20
- "@tanstack/react-virtual": "3.0.0-beta.68",
21
20
  "@trackunit/react-core-contexts-api": "*",
22
21
  "@trackunit/css-class-variance-utilities": "*",
23
22
  "@trackunit/ui-icons": "*",
24
23
  "@trackunit/i18n-library-translation": "*",
25
- "@tanstack/react-router": "1.19.1"
24
+ "@tanstack/react-router": "1.19.1",
25
+ "@trackunit/shared-utils": "*"
26
26
  },
27
27
  "module": "./index.esm.js",
28
28
  "main": "./index.cjs.js"
@@ -1,2 +0,0 @@
1
- import { RelayPagination } from "@trackunit/react-table-pagination";
2
- export declare const noPagination: RelayPagination;
@@ -1,32 +0,0 @@
1
- import { VirtualItem } from "@tanstack/react-virtual";
2
- import { RelayPagination } from "@trackunit/react-table-pagination";
3
- import { RefObject } from "react";
4
- interface InfiniteScrollProps {
5
- pagination: RelayPagination;
6
- containerRef: RefObject<HTMLDivElement>;
7
- rowSize: number;
8
- rowHeight: number;
9
- }
10
- interface InfiniteScroll {
11
- fetchMoreOnBottomReached: (containerRefElement?: HTMLDivElement | null) => void;
12
- getTotalSize: () => number;
13
- getVirtualItems: () => VirtualItem[];
14
- }
15
- /**
16
- * Custom hook for implementing infinite scrolling in a table.
17
- *
18
- * @param {InfiniteScrollProps} props - The configuration object for the infinite scroll behavior.
19
- * @returns {InfiniteScroll} An object containing properties and functions for infinite scrolling.
20
- * @description
21
- * This hook is used to implement infinite scrolling in a table. It calculates the necessary padding
22
- * for virtual rows, provides a callback for fetching more data when the user scrolls to the bottom,
23
- * and manages virtualized rows for efficient rendering.
24
- * @example
25
- * const { paddingTop, paddingBottom, fetchMoreOnBottomReached, rowVirtualizer, virtualRows } = useInfiniteScroll({
26
- * pagination: relayPaginationObject,
27
- * containerRef: tableContainerRef,
28
- * rowSize: 50,
29
- * });
30
- */
31
- export declare const useInfiniteScroll: (props: InfiniteScrollProps) => InfiniteScroll;
32
- export {};