@dxos/react-ui-table 0.4.10-main.f0428f1 → 0.4.10-main.f635fb0

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,6 +14,7 @@ import {
14
14
  type RowSelectionState,
15
15
  getGroupedRowModel,
16
16
  getExpandedRowModel,
17
+ type Row,
17
18
  } from '@tanstack/react-table';
18
19
  import { useVirtualizer, type VirtualizerOptions } from '@tanstack/react-virtual';
19
20
  import React, { Fragment, useCallback, useEffect, useState } from 'react';
@@ -177,12 +178,7 @@ export const Table = <TData extends RowData>(props: TableProps<TData>) => {
177
178
  */
178
179
  const TableImpl = <TData extends RowData>(props: TableProps<TData>) => {
179
180
  const { debug, classNames, getScrollElement, role, footer, grouping, fullWidth } = props;
180
- const { table } = useTableContext();
181
-
182
- const centerRows = table.getCenterRows();
183
- const bottomRows = table.getBottomRows();
184
-
185
- const rows = [...centerRows, ...bottomRows];
181
+ const { table } = useTableContext<TData>();
186
182
 
187
183
  if (debug) {
188
184
  return (
@@ -211,7 +207,7 @@ const TableImpl = <TData extends RowData>(props: TableProps<TData>) => {
211
207
  <VirtualizedTableContent getScrollElement={getScrollElement} />
212
208
  )
213
209
  ) : (
214
- <TableBody rows={rows} />
210
+ <UnvirtualizedTableContent />
215
211
  )
216
212
  ) : (
217
213
  <GroupedTableContent />
@@ -222,13 +218,32 @@ const TableImpl = <TData extends RowData>(props: TableProps<TData>) => {
222
218
  );
223
219
  };
224
220
 
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
+
225
232
  const VirtualizedTableContent = ({
226
233
  getScrollElement,
227
234
  }: Pick<VirtualizerOptions<Element, Element>, 'getScrollElement'>) => {
228
235
  const { table } = useTableContext();
229
236
 
230
237
  const centerRows = table.getCenterRows();
231
- const pinnedRows = table.getBottomRows();
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
232
247
 
233
248
  const { getTotalSize, getVirtualItems } = useVirtualizer({
234
249
  getScrollElement,
@@ -2,42 +2,63 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { type Table } from '@tanstack/react-table';
6
- import { useEffect, useState } from 'react';
5
+ import { type Row, type Table } from '@tanstack/react-table';
6
+ import { useCallback, 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
- useEffect(() => {
17
- if (!pinLastRow) {
18
- return;
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 }));
19
21
  }
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
+ );
20
32
 
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(() => {
21
40
  const rows = table.getRowModel().rows;
22
- const rowToPin = rows[rows.length - 1];
23
- const rowId = rowToPin.id;
24
41
 
25
- if (rowId === pinnedRowKey) {
26
- return;
42
+ if (rows.length === 0) {
43
+ return clearRowPinning();
27
44
  }
28
45
 
29
- table.resetRowPinning();
30
- rowToPin.pin('bottom');
46
+ const lastRow = rows[rows.length - 1];
47
+ if (lastRow.id === pinnedRowKey) {
48
+ return;
49
+ }
31
50
 
32
- const scrollElement = getScrollElement?.();
33
- const isFirstRender = pinnedRowKey === undefined;
51
+ pinRow(lastRow);
34
52
 
35
- if (scrollElement && !isFirstRender) {
36
- requestAnimationFrame(() => {
37
- scrollElement.scrollTo({ top: scrollElement.scrollHeight });
38
- });
53
+ if (pinnedRowKey !== undefined) {
54
+ scrollToBottom();
39
55
  }
56
+ }, [table, scrollToBottom, pinnedRowKey]);
40
57
 
41
- setPinnedRowKey(rowId);
42
- }, [pinLastRow, table, getScrollElement, pinnedRowKey, setPinnedRowKey, data]);
58
+ // Effect to handle pinning logic when data changes.
59
+ useEffect(() => {
60
+ if (pinLastRow) {
61
+ pinLastRowCallback();
62
+ }
63
+ }, [data, pinLastRow, pinLastRowCallback]);
43
64
  };
@@ -5,10 +5,10 @@
5
5
  import '@dxosTheme';
6
6
 
7
7
  import * as Arbitrary from '@effect/schema/Arbitrary';
8
- import * as S from '@effect/schema/Schema';
9
8
  import * as fc from 'fast-check';
10
9
  import React, { useRef } from 'react';
11
10
 
11
+ import { S } from '@dxos/echo-schema';
12
12
  import { DensityProvider } from '@dxos/react-ui';
13
13
  import { withTheme } from '@dxos/storybook-utils';
14
14
 
@@ -2,9 +2,9 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import * as S from '@effect/schema/Schema';
6
5
  import { expect } from 'chai';
7
6
 
7
+ import { S } from '@dxos/echo-schema';
8
8
  import { describe, test } from '@dxos/test';
9
9
 
10
10
  import { classifySchemaProperties } from './schema';
@@ -2,26 +2,54 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { AST } from '@effect/schema';
5
+ import * as AST from '@effect/schema/AST';
6
6
  import type * as S from '@effect/schema/Schema';
7
7
 
8
8
  import { type ColumnType } from './types';
9
9
 
10
10
  export type ClassifiedColumnType = ColumnType | 'display';
11
11
 
12
+ // TODO(burdon): Rename getX? and return typed array?
13
+ export const classifySchemaProperties = (schema: S.Schema<any, any>): [string, ClassifiedColumnType][] => {
14
+ const recurse = (node: AST.AST, path: string[], acc: [string, ClassifiedColumnType][]) => {
15
+ const properties = AST.getPropertySignatures(node);
16
+ properties.forEach((prop) => {
17
+ const propName = prop.name.toString();
18
+
19
+ if (prop.isOptional) {
20
+ const unwrappedAst = unwrapOptionProperty(prop);
21
+ if (isStruct(unwrappedAst)) {
22
+ return recurse(unwrappedAst, [...path, propName], acc);
23
+ }
24
+ }
25
+
26
+ if (isStruct(prop.type)) {
27
+ recurse(prop.type, [...path, propName], acc);
28
+ } else {
29
+ acc.push([path.concat(propName).join('.'), propertyToColumn(prop)]);
30
+ }
31
+ });
32
+
33
+ return acc;
34
+ };
35
+
36
+ return recurse(schema.ast, [], []);
37
+ };
38
+
39
+ const isStruct = (node: AST.AST) => AST.isTypeLiteral(node);
40
+
12
41
  const isOptionalUnion = (prop: AST.PropertySignature) => AST.isUnion(prop.type) && prop.isOptional;
13
42
 
14
43
  const unwrapOptionProperty = (prop: AST.PropertySignature) => {
15
44
  if (!isOptionalUnion(prop)) {
16
- throw new Error('Not an optional property');
45
+ throw new Error(`Not an optional property: ${String(prop.name)}`);
17
46
  }
18
47
 
19
48
  if (!AST.isUnion(prop.type)) {
20
- throw new Error('Not a union type');
49
+ throw new Error(`Not a union type: ${String(prop.name)}`);
21
50
  }
22
51
 
23
52
  const [type, _undefinedCase] = prop.type.types;
24
-
25
53
  return type;
26
54
  };
27
55
 
@@ -54,40 +82,11 @@ const typeToColumn = (type: AST.AST): ClassifiedColumnType => {
54
82
  return 'display';
55
83
  };
56
84
 
57
- const propertyToColumn = (property: AST.PropertySignature): ClassifiedColumnType => {
58
- let type = property.type;
59
-
60
- if (property.isOptional) {
61
- type = unwrapOptionProperty(property);
85
+ const propertyToColumn = (prop: AST.PropertySignature): ClassifiedColumnType => {
86
+ let type = prop.type;
87
+ if (prop.isOptional) {
88
+ type = unwrapOptionProperty(prop);
62
89
  }
63
90
 
64
91
  return typeToColumn(type);
65
92
  };
66
-
67
- const isStruct = (node: AST.AST) => AST.isTypeLiteral(node);
68
-
69
- export const classifySchemaProperties = (schema: S.Schema<any, any>) => {
70
- const recurse = (node: AST.AST, path: string[], acc: [string, ClassifiedColumnType][]) => {
71
- const properties = AST.getPropertySignatures(node);
72
- properties.forEach((prop) => {
73
- const propName = prop.name.toString();
74
-
75
- if (prop.isOptional) {
76
- const unwrappedAst = unwrapOptionProperty(prop);
77
- if (isStruct(unwrappedAst)) {
78
- return recurse(unwrappedAst, [...path, propName], acc);
79
- }
80
- }
81
-
82
- if (isStruct(prop.type)) {
83
- recurse(prop.type, [...path, propName], acc);
84
- } else {
85
- acc.push([path.concat(propName).join('.'), propertyToColumn(prop)]);
86
- }
87
- });
88
-
89
- return acc;
90
- };
91
-
92
- return recurse(schema.ast, [], []);
93
- };
@@ -2,9 +2,10 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import type * as S from '@effect/schema/Schema';
6
5
  import { type ColumnDef } from '@tanstack/react-table';
7
6
 
7
+ import type { S } from '@dxos/echo-schema';
8
+
8
9
  import { classifySchemaProperties } from './schema';
9
10
  import { createColumnBuilder } from '../helpers';
10
11