@dxos/react-ui-table 0.4.10-main.dfa0529 → 0.4.10-main.e17362e

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.
@@ -14,7 +14,6 @@ import {
14
14
  type RowSelectionState,
15
15
  getGroupedRowModel,
16
16
  getExpandedRowModel,
17
- type Row,
18
17
  } from '@tanstack/react-table';
19
18
  import { useVirtualizer, type VirtualizerOptions } from '@tanstack/react-virtual';
20
19
  import React, { Fragment, useCallback, useEffect, useState } from 'react';
@@ -178,7 +177,12 @@ export const Table = <TData extends RowData>(props: TableProps<TData>) => {
178
177
  */
179
178
  const TableImpl = <TData extends RowData>(props: TableProps<TData>) => {
180
179
  const { debug, classNames, getScrollElement, role, footer, grouping, fullWidth } = props;
181
- const { table } = useTableContext<TData>();
180
+ const { table } = useTableContext();
181
+
182
+ const centerRows = table.getCenterRows();
183
+ const bottomRows = table.getBottomRows();
184
+
185
+ const rows = [...centerRows, ...bottomRows];
182
186
 
183
187
  if (debug) {
184
188
  return (
@@ -207,7 +211,7 @@ const TableImpl = <TData extends RowData>(props: TableProps<TData>) => {
207
211
  <VirtualizedTableContent getScrollElement={getScrollElement} />
208
212
  )
209
213
  ) : (
210
- <UnvirtualizedTableContent />
214
+ <TableBody rows={rows} />
211
215
  )
212
216
  ) : (
213
217
  <GroupedTableContent />
@@ -218,32 +222,13 @@ const TableImpl = <TData extends RowData>(props: TableProps<TData>) => {
218
222
  );
219
223
  };
220
224
 
221
- const UnvirtualizedTableContent = () => {
222
- const { table } = useTableContext();
223
-
224
- const centerRows = table.getCenterRows();
225
- const pinnedRows = table.getBottomRows();
226
-
227
- const rows = [...centerRows, ...pinnedRows];
228
-
229
- return <TableBody rows={rows} />;
230
- };
231
-
232
225
  const VirtualizedTableContent = ({
233
226
  getScrollElement,
234
227
  }: Pick<VirtualizerOptions<Element, Element>, 'getScrollElement'>) => {
235
228
  const { table } = useTableContext();
236
229
 
237
230
  const centerRows = table.getCenterRows();
238
- let pinnedRows = [] as Row<unknown>[];
239
-
240
- try {
241
- // TODO(zan): Work out how to sync the row pinning with rendering
242
- // This is a hack because the first call to this function after a row is deleted will throw an error
243
- // because the row index is no longer in the table
244
- // The pin last row hook updates the pinned row key after the row is deleted
245
- pinnedRows = table.getBottomRows();
246
- } catch (_) {} // Ignore error
231
+ const pinnedRows = table.getBottomRows();
247
232
 
248
233
  const { getTotalSize, getVirtualItems } = useVirtualizer({
249
234
  getScrollElement,
@@ -2,63 +2,42 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { type Row, type Table } from '@tanstack/react-table';
6
- import { useCallback, useEffect, useState } from 'react';
5
+ import { type Table } from '@tanstack/react-table';
6
+ import { useEffect, useState } from 'react';
7
7
 
8
8
  export const usePinLastRow = (
9
9
  pinLastRow: boolean | undefined,
10
10
  table: Table<any>,
11
- data: any[],
11
+ data: any,
12
12
  getScrollElement?: () => Element | null,
13
13
  ) => {
14
14
  const [pinnedRowKey, setPinnedRowKey] = useState<string>();
15
15
 
16
- // Scrolls to the bottom of a scrollable element.
17
- const scrollToBottom = useCallback(() => {
18
- const scrollElement = getScrollElement?.();
19
- if (scrollElement) {
20
- requestAnimationFrame(() => scrollElement.scrollTo({ top: scrollElement.scrollHeight }));
16
+ useEffect(() => {
17
+ if (!pinLastRow) {
18
+ return;
21
19
  }
22
- }, [getScrollElement]);
23
-
24
- const pinRow = useCallback(
25
- (rowToPin: Row<any>) => {
26
- table.resetRowPinning();
27
- rowToPin.pin('bottom');
28
- setPinnedRowKey(rowToPin.id);
29
- },
30
- [table],
31
- );
32
20
 
33
- const clearRowPinning = useCallback(() => {
34
- table.resetRowPinning();
35
- setPinnedRowKey(undefined);
36
- }, [table]);
37
-
38
- // Define the logic for pinning the last row in a callback.
39
- const pinLastRowCallback = useCallback(() => {
40
21
  const rows = table.getRowModel().rows;
22
+ const rowToPin = rows[rows.length - 1];
23
+ const rowId = rowToPin.id;
41
24
 
42
- if (rows.length === 0) {
43
- return clearRowPinning();
44
- }
45
-
46
- const lastRow = rows[rows.length - 1];
47
- if (lastRow.id === pinnedRowKey) {
25
+ if (rowId === pinnedRowKey) {
48
26
  return;
49
27
  }
50
28
 
51
- pinRow(lastRow);
29
+ table.resetRowPinning();
30
+ rowToPin.pin('bottom');
52
31
 
53
- if (pinnedRowKey !== undefined) {
54
- scrollToBottom();
55
- }
56
- }, [table, scrollToBottom, pinnedRowKey]);
32
+ const scrollElement = getScrollElement?.();
33
+ const isFirstRender = pinnedRowKey === undefined;
57
34
 
58
- // Effect to handle pinning logic when data changes.
59
- useEffect(() => {
60
- if (pinLastRow) {
61
- pinLastRowCallback();
35
+ if (scrollElement && !isFirstRender) {
36
+ requestAnimationFrame(() => {
37
+ scrollElement.scrollTo({ top: scrollElement.scrollHeight });
38
+ });
62
39
  }
63
- }, [data, pinLastRow, pinLastRowCallback]);
40
+
41
+ setPinnedRowKey(rowId);
42
+ }, [pinLastRow, table, getScrollElement, pinnedRowKey, setPinnedRowKey, data]);
64
43
  };