@dxos/react-ui-table 0.4.10-main.b1e8dec → 0.4.10-main.b30eb88

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.
Files changed (74) hide show
  1. package/dist/lib/browser/index.mjs +571 -359
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/types/src/components/{ColumnMenu.d.ts → ColumnMenu/ColumnMenu.d.ts} +1 -1
  5. package/dist/types/src/components/ColumnMenu/ColumnMenu.d.ts.map +1 -0
  6. package/dist/types/src/components/{ColumnMenu.stories.d.ts → ColumnMenu/ColumnMenu.stories.d.ts} +6 -2
  7. package/dist/types/src/components/ColumnMenu/ColumnMenu.stories.d.ts.map +1 -0
  8. package/dist/types/src/components/{ColumnSettingsForm.d.ts → ColumnMenu/ColumnSettingsForm.d.ts} +5 -4
  9. package/dist/types/src/components/ColumnMenu/ColumnSettingsForm.d.ts.map +1 -0
  10. package/dist/types/src/components/ColumnMenu/index.d.ts +3 -0
  11. package/dist/types/src/components/ColumnMenu/index.d.ts.map +1 -0
  12. package/dist/types/src/components/Table/Table.d.ts.map +1 -1
  13. package/dist/types/src/components/Table/Table.stories.d.ts +31 -0
  14. package/dist/types/src/components/Table/Table.stories.d.ts.map +1 -1
  15. package/dist/types/src/components/index.d.ts +1 -1
  16. package/dist/types/src/components/index.d.ts.map +1 -1
  17. package/dist/types/src/helpers.d.ts.map +1 -1
  18. package/dist/types/src/hooks/index.d.ts +5 -0
  19. package/dist/types/src/hooks/index.d.ts.map +1 -0
  20. package/dist/types/src/hooks/useColumnSizing.d.ts +12 -0
  21. package/dist/types/src/hooks/useColumnSizing.d.ts.map +1 -0
  22. package/dist/types/src/hooks/useColumnSorting.d.ts.map +1 -0
  23. package/dist/types/src/hooks/usePinLastRow.d.ts +3 -0
  24. package/dist/types/src/hooks/usePinLastRow.d.ts.map +1 -0
  25. package/dist/types/src/hooks/useRowSelection.d.ts +7 -0
  26. package/dist/types/src/hooks/useRowSelection.d.ts.map +1 -0
  27. package/dist/types/src/index.d.ts +2 -1
  28. package/dist/types/src/index.d.ts.map +1 -1
  29. package/dist/types/src/schema/SchemaToTable.stories.d.ts.map +1 -0
  30. package/dist/types/src/schema/index.d.ts +3 -0
  31. package/dist/types/src/schema/index.d.ts.map +1 -0
  32. package/dist/types/src/{components/schema → schema}/schema.d.ts +1 -1
  33. package/dist/types/src/schema/schema.d.ts.map +1 -0
  34. package/dist/types/src/schema/schema.test.d.ts.map +1 -0
  35. package/dist/types/src/{components/schema → schema}/schemaToColumns.d.ts +1 -1
  36. package/dist/types/src/schema/schemaToColumns.d.ts.map +1 -0
  37. package/dist/types/src/{schema.d.ts → schema/types.d.ts} +1 -1
  38. package/dist/types/src/schema/types.d.ts.map +1 -0
  39. package/package.json +18 -16
  40. package/src/components/{ColumnMenu.stories.tsx → ColumnMenu/ColumnMenu.stories.tsx} +31 -1
  41. package/src/components/{ColumnMenu.tsx → ColumnMenu/ColumnMenu.tsx} +36 -34
  42. package/src/components/{ColumnSettingsForm.tsx → ColumnMenu/ColumnSettingsForm.tsx} +83 -75
  43. package/src/components/ColumnMenu/index.ts +6 -0
  44. package/src/components/Table/Table.stories.tsx +162 -3
  45. package/src/components/Table/Table.tsx +35 -60
  46. package/src/components/index.ts +1 -1
  47. package/src/helpers.tsx +9 -7
  48. package/src/hooks/index.ts +8 -0
  49. package/src/hooks/useColumnSizing.ts +45 -0
  50. package/src/hooks/usePinLastRow.ts +64 -0
  51. package/src/hooks/useRowSelection.ts +39 -0
  52. package/src/index.ts +3 -1
  53. package/src/{components/schema → schema}/SchemaToTable.stories.tsx +2 -2
  54. package/src/schema/index.ts +6 -0
  55. package/src/{components/schema → schema}/schema.test.ts +1 -1
  56. package/src/{components/schema → schema}/schema.ts +37 -41
  57. package/src/{components/schema → schema}/schemaToColumns.ts +7 -9
  58. package/dist/types/src/components/ColumnMenu.d.ts.map +0 -1
  59. package/dist/types/src/components/ColumnMenu.stories.d.ts.map +0 -1
  60. package/dist/types/src/components/ColumnSettingsForm.d.ts.map +0 -1
  61. package/dist/types/src/components/hooks/useColumnSorting.d.ts.map +0 -1
  62. package/dist/types/src/components/hooks/usePinLastRow.d.ts +0 -3
  63. package/dist/types/src/components/hooks/usePinLastRow.d.ts.map +0 -1
  64. package/dist/types/src/components/schema/SchemaToTable.stories.d.ts.map +0 -1
  65. package/dist/types/src/components/schema/schema.d.ts.map +0 -1
  66. package/dist/types/src/components/schema/schema.test.d.ts.map +0 -1
  67. package/dist/types/src/components/schema/schemaToColumns.d.ts.map +0 -1
  68. package/dist/types/src/schema.d.ts.map +0 -1
  69. package/src/components/hooks/usePinLastRow.ts +0 -43
  70. /package/dist/types/src/{components/hooks → hooks}/useColumnSorting.d.ts +0 -0
  71. /package/dist/types/src/{components/schema → schema}/SchemaToTable.stories.d.ts +0 -0
  72. /package/dist/types/src/{components/schema → schema}/schema.test.d.ts +0 -0
  73. /package/src/{components/hooks → hooks}/useColumnSorting.ts +0 -0
  74. /package/src/{schema.ts → schema/types.ts} +0 -0
@@ -1,33 +1,29 @@
1
1
  //
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
- import { useControllableState } from '@radix-ui/react-use-controllable-state';
5
4
  import {
6
5
  getCoreRowModel,
7
6
  getSortedRowModel,
8
7
  useReactTable,
9
- type ColumnSizingInfoState,
10
8
  type GroupingState,
11
9
  type RowData,
12
- type ColumnSizingState,
13
- type OnChangeFn,
14
- type RowSelectionState,
15
10
  getGroupedRowModel,
16
11
  getExpandedRowModel,
12
+ type Row,
17
13
  } from '@tanstack/react-table';
18
14
  import { useVirtualizer, type VirtualizerOptions } from '@tanstack/react-virtual';
19
15
  import React, { Fragment, useCallback, useEffect, useState } from 'react';
20
16
 
21
17
  import { log } from '@dxos/log';
22
- import { useDefaultValue, useOnTransition } from '@dxos/react-ui';
18
+ import { useDefaultValue } from '@dxos/react-ui';
23
19
 
24
20
  import { TableBody } from './TableBody';
25
21
  import { type TypedTableProvider, TableProvider as UntypedTableProvider, useTableContext } from './TableContext';
26
22
  import { TableFooter } from './TableFooter';
27
23
  import { TableHead } from './TableHead';
28
24
  import { type TableProps } from './props';
25
+ import { useColumnResizing, usePinLastRow, useRowSelection } from '../../hooks';
29
26
  import { groupTh, tableRoot } from '../../theme';
30
- import { usePinLastRow } from '../hooks/usePinLastRow';
31
27
 
32
28
  export const Table = <TData extends RowData>(props: TableProps<TData>) => {
33
29
  const {
@@ -43,57 +39,22 @@ export const Table = <TData extends RowData>(props: TableProps<TData>) => {
43
39
  } = props;
44
40
 
45
41
  const columns = useDefaultValue(props.columns, []);
46
- const data = useDefaultValue(props.data, []);
42
+ const incomingData = useDefaultValue(props.data, []);
47
43
 
48
- const TableProvider = UntypedTableProvider as TypedTableProvider<TData>;
49
-
50
- const [columnsInitialised, setColumnsInitialised] = useState(false);
51
- const [columnSizing, setColumnSizing] = useState<ColumnSizingState>({});
52
-
53
- useEffect(() => {
54
- if (columnsInitialised) {
55
- return;
56
- }
57
-
58
- setColumnSizing(
59
- columns
60
- .filter((column) => !!column.size && (column as any).prop !== undefined)
61
- .reduce<ColumnSizingState>((state, column) => {
62
- state[(column as any).prop] = column.size!;
63
- return state;
64
- }, {}),
65
- );
66
-
67
- setColumnsInitialised(true);
68
- }, [columns, setColumnSizing]);
44
+ const [data, setData] = useState([...incomingData]);
69
45
 
70
- const [columnSizingInfo, setColumnSizingInfo] = useState<ColumnSizingInfoState>({} as ColumnSizingInfoState);
46
+ // Reactivity workaround: https://github.com/dxos/dxos/issues/6376.
47
+ const forceUpdate = useCallback(() => setData([...incomingData]), [incomingData]);
48
+ useEffect(() => forceUpdate(), [JSON.stringify(incomingData), forceUpdate]);
71
49
 
72
- // Notify on column resize.
73
- const notifyColumnResize = useCallback(() => onColumnResize?.(columnSizing), [onColumnResize, columnSizing]);
74
- useOnTransition(columnSizingInfo.isResizingColumn, (v) => typeof v === 'string', false, notifyColumnResize);
50
+ const TableProvider = UntypedTableProvider as TypedTableProvider<TData>;
75
51
 
76
- const [rowSelection = {}, setRowSelection] = useControllableState({
77
- prop: props.rowSelection,
78
- onChange: props.onRowSelectionChange,
79
- defaultProp: props.defaultRowSelection,
52
+ const { columnSizing, setColumnSizing, columnSizingInfo, setColumnSizingInfo } = useColumnResizing({
53
+ columns,
54
+ onColumnResize,
80
55
  });
81
56
 
82
- // TODO(thure): Does @tanstack/react-table really need this intervention? It did seem necessary to enforce single-selection...
83
- const handleRowSelectionChange = useCallback<OnChangeFn<RowSelectionState>>(
84
- (updaterOrValue) => {
85
- const nextRowSelection = typeof updaterOrValue === 'function' ? updaterOrValue(rowSelection) : updaterOrValue;
86
- if (rowsSelectable === 'multi') {
87
- setRowSelection(nextRowSelection);
88
- } else if (rowsSelectable) {
89
- const nextRowSelectionKey = Object.keys(nextRowSelection).filter((id) => !rowSelection[id])[0];
90
- setRowSelection(nextRowSelectionKey ? { [nextRowSelectionKey]: true } : {});
91
- } else {
92
- setRowSelection({});
93
- }
94
- },
95
- [rowsSelectable, setRowSelection, rowSelection],
96
- );
57
+ const { rowSelection, handleRowSelectionChange } = useRowSelection(props);
97
58
 
98
59
  const [grouping, handleGroupingChange] = useState<GroupingState>(props.grouping ?? []);
99
60
  useEffect(() => handleGroupingChange(props.grouping ?? []), [handleGroupingChange, props.grouping]);
@@ -177,12 +138,7 @@ export const Table = <TData extends RowData>(props: TableProps<TData>) => {
177
138
  */
178
139
  const TableImpl = <TData extends RowData>(props: TableProps<TData>) => {
179
140
  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];
141
+ const { table } = useTableContext<TData>();
186
142
 
187
143
  if (debug) {
188
144
  return (
@@ -211,7 +167,7 @@ const TableImpl = <TData extends RowData>(props: TableProps<TData>) => {
211
167
  <VirtualizedTableContent getScrollElement={getScrollElement} />
212
168
  )
213
169
  ) : (
214
- <TableBody rows={rows} />
170
+ <UnvirtualizedTableContent />
215
171
  )
216
172
  ) : (
217
173
  <GroupedTableContent />
@@ -222,13 +178,32 @@ const TableImpl = <TData extends RowData>(props: TableProps<TData>) => {
222
178
  );
223
179
  };
224
180
 
181
+ const UnvirtualizedTableContent = () => {
182
+ const { table } = useTableContext();
183
+
184
+ const centerRows = table.getCenterRows();
185
+ const pinnedRows = table.getBottomRows();
186
+
187
+ const rows = [...centerRows, ...pinnedRows];
188
+
189
+ return <TableBody rows={rows} />;
190
+ };
191
+
225
192
  const VirtualizedTableContent = ({
226
193
  getScrollElement,
227
194
  }: Pick<VirtualizerOptions<Element, Element>, 'getScrollElement'>) => {
228
195
  const { table } = useTableContext();
229
196
 
230
197
  const centerRows = table.getCenterRows();
231
- const pinnedRows = table.getBottomRows();
198
+ let pinnedRows = [] as Row<unknown>[];
199
+
200
+ try {
201
+ // TODO(zan): Work out how to sync the row pinning with rendering
202
+ // This is a hack because the first call to this function after a row is deleted will throw an error
203
+ // because the row index is no longer in the table
204
+ // The pin last row hook updates the pinned row key after the row is deleted
205
+ pinnedRows = table.getBottomRows();
206
+ } catch (_) {} // Ignore error
232
207
 
233
208
  const { getTotalSize, getVirtualItems } = useVirtualizer({
234
209
  getScrollElement,
@@ -2,6 +2,6 @@
2
2
  // Copyright 2022 DXOS.org
3
3
  //
4
4
 
5
+ export * from './Cell';
5
6
  export * from './ColumnMenu';
6
7
  export * from './Table';
7
- export * from './Cell';
package/src/helpers.tsx CHANGED
@@ -14,7 +14,7 @@ import {
14
14
  } from '@tanstack/react-table';
15
15
  import { format } from 'date-fns/format';
16
16
  import { formatDistanceToNow } from 'date-fns/formatDistanceToNow';
17
- import React, { useEffect, useRef, useState } from 'react';
17
+ import React, { useCallback, useEffect, useRef, useState } from 'react';
18
18
 
19
19
  import { type PublicKey } from '@dxos/keys';
20
20
  import { log } from '@dxos/log';
@@ -102,14 +102,16 @@ const StringBuilderCell = <TData extends RowData>(cellContext: CellContext<TData
102
102
 
103
103
  // https://tanstack.com/table/v8/docs/examples/react/editable-data
104
104
  const initialValue = cellContext.getValue();
105
- const [value, setValue] = useState(() => {
105
+
106
+ const getInitialValue = useCallback(() => {
106
107
  // TODO(burdon): Temporary shim for automerge.
107
- if ((initialValue as any)?.content) {
108
- return (initialValue as any)?.content;
109
- }
108
+ return (initialValue as any)?.content ?? initialValue;
109
+ }, [initialValue]);
110
+
111
+ const [value, setValue] = useState(getInitialValue);
110
112
 
111
- return initialValue;
112
- });
113
+ // Update value if initialValue changes externally (cell reactivity).
114
+ useEffect(() => setValue(getInitialValue()), [getInitialValue]);
113
115
 
114
116
  const handleSave = () => {
115
117
  ref.current && document.activeElement === ref.current && findPrevFocusable(ref.current)?.focus();
@@ -0,0 +1,8 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ export * from './useColumnSizing';
6
+ export * from './useColumnSorting';
7
+ export * from './usePinLastRow';
8
+ export * from './useRowSelection';
@@ -0,0 +1,45 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+ import { type ColumnSizingInfoState, type ColumnSizingState } from '@tanstack/react-table';
5
+ import { useCallback, useEffect, useState } from 'react';
6
+
7
+ import { useOnTransition } from '@dxos/react-ui';
8
+
9
+ import { type TableColumnDef } from '../components/Table';
10
+
11
+ export const useColumnResizing = ({
12
+ columns,
13
+ onColumnResize,
14
+ }: {
15
+ columns: TableColumnDef<any>[];
16
+ onColumnResize: ((state: Record<string, number>) => void) | undefined;
17
+ }) => {
18
+ const [columnsInitialised, setColumnsInitialised] = useState(false);
19
+ const [columnSizing, setColumnSizing] = useState<ColumnSizingState>({});
20
+
21
+ useEffect(() => {
22
+ if (columnsInitialised) {
23
+ return;
24
+ }
25
+
26
+ setColumnSizing(
27
+ columns
28
+ .filter((column) => !!column.size && (column as any).prop !== undefined)
29
+ .reduce<ColumnSizingState>((state, column) => {
30
+ state[(column as any).prop] = column.size!;
31
+ return state;
32
+ }, {}),
33
+ );
34
+
35
+ setColumnsInitialised(true);
36
+ }, [columns, setColumnSizing]);
37
+
38
+ const [columnSizingInfo, setColumnSizingInfo] = useState<ColumnSizingInfoState>({} as ColumnSizingInfoState);
39
+
40
+ // Notify on column resize.
41
+ const notifyColumnResize = useCallback(() => onColumnResize?.(columnSizing), [onColumnResize, columnSizing]);
42
+ useOnTransition(columnSizingInfo.isResizingColumn, (v) => typeof v === 'string', false, notifyColumnResize);
43
+
44
+ return { columnSizing, columnSizingInfo, setColumnSizing, setColumnSizingInfo };
45
+ };
@@ -0,0 +1,64 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { type Row, type Table } from '@tanstack/react-table';
6
+ import { useCallback, useEffect, useState } from 'react';
7
+
8
+ export const usePinLastRow = (
9
+ pinLastRow: boolean | undefined,
10
+ table: Table<any>,
11
+ data: any[],
12
+ getScrollElement?: () => Element | null,
13
+ ) => {
14
+ const [pinnedRowKey, setPinnedRowKey] = useState<string>();
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 }));
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
+ );
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(() => {
40
+ const rows = table.getRowModel().rows;
41
+
42
+ if (rows.length === 0) {
43
+ return clearRowPinning();
44
+ }
45
+
46
+ const lastRow = rows[rows.length - 1];
47
+ if (lastRow.id === pinnedRowKey) {
48
+ return;
49
+ }
50
+
51
+ pinRow(lastRow);
52
+
53
+ if (pinnedRowKey !== undefined) {
54
+ scrollToBottom();
55
+ }
56
+ }, [table, scrollToBottom, pinnedRowKey]);
57
+
58
+ // Effect to handle pinning logic when data changes.
59
+ useEffect(() => {
60
+ if (pinLastRow) {
61
+ pinLastRowCallback();
62
+ }
63
+ }, [data, pinLastRow, pinLastRowCallback]);
64
+ };
@@ -0,0 +1,39 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+ import { useControllableState } from '@radix-ui/react-use-controllable-state';
5
+ import { type OnChangeFn, type RowSelectionState } from '@tanstack/react-table';
6
+ import { useCallback } from 'react';
7
+
8
+ import { type TableProps } from '../components/Table';
9
+
10
+ export const useRowSelection = (
11
+ props: Pick<
12
+ TableProps<any>,
13
+ 'rowsSelectable' | 'defaultRowSelection' | 'rowSelection' | 'onRowSelectionChange' | 'rowsSelectable'
14
+ >,
15
+ ) => {
16
+ const [rowSelection = {}, setRowSelection] = useControllableState({
17
+ prop: props.rowSelection,
18
+ onChange: props.onRowSelectionChange,
19
+ defaultProp: props.defaultRowSelection,
20
+ });
21
+
22
+ // TODO(thure): Does @tanstack/react-table really need this intervention? It did seem necessary to enforce single-selection...
23
+ const handleRowSelectionChange = useCallback<OnChangeFn<RowSelectionState>>(
24
+ (updaterOrValue) => {
25
+ const nextRowSelection = typeof updaterOrValue === 'function' ? updaterOrValue(rowSelection) : updaterOrValue;
26
+ if (props.rowsSelectable === 'multi') {
27
+ setRowSelection(nextRowSelection);
28
+ } else if (props.rowsSelectable) {
29
+ const nextRowSelectionKey = Object.keys(nextRowSelection).filter((id) => !rowSelection[id])[0];
30
+ setRowSelection(nextRowSelectionKey ? { [nextRowSelectionKey]: true } : {});
31
+ } else {
32
+ setRowSelection({});
33
+ }
34
+ },
35
+ [props.rowsSelectable, setRowSelection, rowSelection],
36
+ );
37
+
38
+ return { rowSelection, handleRowSelectionChange };
39
+ };
package/src/index.ts CHANGED
@@ -2,9 +2,11 @@
2
2
  // Copyright 2022 DXOS.org
3
3
  //
4
4
 
5
+ export { default as translations } from './translations';
6
+
5
7
  export * from './components';
6
8
  export * from './helpers';
9
+ export * from './hooks';
7
10
  export * from './schema';
8
11
  export * from './theme';
9
- export { default as translations } from './translations';
10
12
  export * from './types';
@@ -5,15 +5,15 @@
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
 
15
15
  import { schemaToColumnDefs } from './schemaToColumns';
16
- import { Table } from '../Table/Table';
16
+ import { Table } from '../components';
17
17
 
18
18
  export default {
19
19
  title: 'react-ui-table/SchemaTable',
@@ -0,0 +1,6 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ export * from './schemaToColumns';
6
+ export * from './types';
@@ -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
- import { type ColumnType } from '../../schema';
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
 
@@ -44,7 +72,6 @@ const typeToColumn = (type: AST.AST): ClassifiedColumnType => {
44
72
 
45
73
  if (AST.isTransform(type)) {
46
74
  const identifier = AST.getIdentifierAnnotation(type);
47
-
48
75
  if (identifier._tag === 'Some') {
49
76
  if (identifier.value === 'DateFromString') {
50
77
  return 'date';
@@ -55,42 +82,11 @@ const typeToColumn = (type: AST.AST): ClassifiedColumnType => {
55
82
  return 'display';
56
83
  };
57
84
 
58
- const propertyToColumn = (property: AST.PropertySignature): ClassifiedColumnType => {
59
- let type = property.type;
60
-
61
- if (property.isOptional) {
62
- type = unwrapOptionProperty(property);
85
+ const propertyToColumn = (prop: AST.PropertySignature): ClassifiedColumnType => {
86
+ let type = prop.type;
87
+ if (prop.isOptional) {
88
+ type = unwrapOptionProperty(prop);
63
89
  }
64
90
 
65
91
  return typeToColumn(type);
66
92
  };
67
-
68
- const isStruct = (node: AST.AST) => AST.isTypeLiteral(node);
69
-
70
- export const classifySchemaProperties = (schema: S.Schema<any, any>) => {
71
- const recurse = (node: AST.AST, path: string[], acc: [string, ClassifiedColumnType][]) => {
72
- const properties = AST.getPropertySignatures(node);
73
-
74
- properties.forEach((prop) => {
75
- const propName = prop.name.toString();
76
-
77
- if (prop.isOptional) {
78
- const unwrappedAst = unwrapOptionProperty(prop);
79
-
80
- if (isStruct(unwrappedAst)) {
81
- return recurse(unwrappedAst, [...path, propName], acc);
82
- }
83
- }
84
-
85
- if (isStruct(prop.type)) {
86
- recurse(prop.type, [...path, propName], acc);
87
- } else {
88
- acc.push([path.concat(propName).join('.'), propertyToColumn(prop)]);
89
- }
90
- });
91
-
92
- return acc;
93
- };
94
-
95
- return recurse(schema.ast, [], []);
96
- };
@@ -2,22 +2,21 @@
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
- import { createColumnBuilder } from '../../helpers';
10
+ import { createColumnBuilder } from '../helpers';
10
11
 
11
12
  export const schemaToColumnDefs = <T>(schema: S.Schema<T, any>): ColumnDef<T, any>[] => {
12
- const classified = classifySchemaProperties(schema);
13
-
14
13
  const { helper, builder } = createColumnBuilder<T>();
15
14
 
15
+ const classified = classifySchemaProperties(schema);
16
16
  return classified.map(([name, type]) => {
17
17
  const propertyKey = name.toString();
18
18
 
19
19
  let column: Partial<ColumnDef<any, any>> | undefined;
20
-
21
20
  switch (type) {
22
21
  case 'string': {
23
22
  column = builder.string({ label: propertyKey });
@@ -39,10 +38,9 @@ export const schemaToColumnDefs = <T>(schema: S.Schema<T, any>): ColumnDef<T, an
39
38
  column = builder.display({ label: propertyKey, id: propertyKey });
40
39
  break;
41
40
  }
42
- }
43
-
44
- if (column === undefined) {
45
- throw new Error(`Unhandled column type: ${type}`);
41
+ default: {
42
+ throw new Error(`Unhandled column type: ${type}`);
43
+ }
46
44
  }
47
45
 
48
46
  return helper.accessor(propertyKey as any, column);
@@ -1 +0,0 @@
1
- {"version":3,"file":"ColumnMenu.d.ts","sourceRoot":"","sources":["../../../../src/components/ColumnMenu.tsx"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,aAAa,EAAE,KAAK,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAQ7F,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;AAE5D,MAAM,MAAM,eAAe,CAAC,KAAK,SAAS,OAAO,EAAE,MAAM,IAAI;IAC3D,OAAO,EAAE,aAAa,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IACtC,SAAS,EAAE,QAAQ,EAAE,CAAC;IACtB,QAAQ,EAAE,QAAQ,CAAC;IACnB,MAAM,EAAE,WAAW,CAAC;IACpB,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,KAAK,IAAI,CAAC;IACrD,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAC;CACjC,CAAC;AAEF,eAAO,MAAM,UAAU,sGAqFtB,CAAC;AAEF,eAAO,MAAM,mBAAmB;aAOe,MAAM,IAAI;iBAkBxD,CAAC;AAEF,eAAO,MAAM,aAAa;eAIb,aAAa,GAAG,KAAK;iBACnB,GAAG,KAAK,IAAI;wBAsB1B,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"ColumnMenu.stories.d.ts","sourceRoot":"","sources":["../../../../src/components/ColumnMenu.stories.tsx"],"names":[],"mappings":"AAIA,OAAO,YAAY,CAAC;AAQpB,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;;;;;;AAE7C,wBAIE;AAEF,eAAO,MAAM,OAAO;;;;;;;2CAUa,OAAO;;;;;;;yBAY5B,GAAG;;;;CASd,CAAC;AAEF,eAAO,MAAM,YAAY;;CA2CxB,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"ColumnSettingsForm.d.ts","sourceRoot":"","sources":["../../../../src/components/ColumnSettingsForm.tsx"],"names":[],"mappings":"AAWA,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,WAAW,EAAmB,MAAM,WAAW,CAAC;AAS7E,eAAO,MAAM,kBAAkB;YAQrB,WAAW;cACT,QAAQ;eACP,QAAQ,EAAE;qBACJ,MAAM,UAAU,WAAW,KAAK,IAAI;qBACpC,MAAM,KAAK,IAAI;aACvB,MAAM,IAAI;iBA4KpB,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"useColumnSorting.d.ts","sourceRoot":"","sources":["../../../../../src/components/hooks/useColumnSorting.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAGxE,eAAO,MAAM,gBAAgB,WAAY,OAAO,GAAG,CAAC;;;8BAoCjB,aAAa;;;CAU/C,CAAC"}
@@ -1,3 +0,0 @@
1
- import { type Table } from '@tanstack/react-table';
2
- export declare const usePinLastRow: (pinLastRow: boolean | undefined, table: Table<any>, data: any, getScrollElement?: () => Element | null) => void;
3
- //# sourceMappingURL=usePinLastRow.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"usePinLastRow.d.ts","sourceRoot":"","sources":["../../../../../src/components/hooks/usePinLastRow.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAGnD,eAAO,MAAM,aAAa,eACZ,OAAO,GAAG,SAAS,SACxB,MAAM,GAAG,CAAC,QACX,GAAG,qBACU,MAAM,OAAO,GAAG,IAAI,SA+BxC,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"SchemaToTable.stories.d.ts","sourceRoot":"","sources":["../../../../../src/components/schema/SchemaToTable.stories.tsx"],"names":[],"mappings":"AAIA,OAAO,YAAY,CAAC;;;;;;AAapB,wBAWE;AAuBF,eAAO,MAAM,WAAW;;CAoBvB,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../../../../src/components/schema/schema.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,KAAK,CAAC,MAAM,uBAAuB,CAAC;AAEhD,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C,MAAM,MAAM,oBAAoB,GAAG,UAAU,GAAG,SAAS,CAAC;AA4D1D,eAAO,MAAM,wBAAwB,WAAY,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,qCA0BlE,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"schema.test.d.ts","sourceRoot":"","sources":["../../../../../src/components/schema/schema.test.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"schemaToColumns.d.ts","sourceRoot":"","sources":["../../../../../src/components/schema/schemaToColumns.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,KAAK,CAAC,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAKvD,eAAO,MAAM,kBAAkB,6DAuC9B,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../../src/schema.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,QAAQ,GAAG;IACrB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,WAAW,EAAE,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,SAAS,GAAG,MAAM,GAAG,QAAQ,GAAG,KAAK,CAAC;AAE1E,MAAM,MAAM,WAAW,GAAG;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,UAAU,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IAGf,MAAM,CAAC,EAAE,MAAM,CAAC;IAGhB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IAGjB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC"}
@@ -1,43 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { type Table } from '@tanstack/react-table';
6
- import { useEffect, useState } from 'react';
7
-
8
- export const usePinLastRow = (
9
- pinLastRow: boolean | undefined,
10
- table: Table<any>,
11
- data: any,
12
- getScrollElement?: () => Element | null,
13
- ) => {
14
- const [pinnedRowKey, setPinnedRowKey] = useState<string>();
15
-
16
- useEffect(() => {
17
- if (!pinLastRow) {
18
- return;
19
- }
20
-
21
- const rows = table.getRowModel().rows;
22
- const rowToPin = rows[rows.length - 1];
23
- const rowId = rowToPin.id;
24
-
25
- if (rowId === pinnedRowKey) {
26
- return;
27
- }
28
-
29
- table.resetRowPinning();
30
- rowToPin.pin('bottom');
31
-
32
- const scrollElement = getScrollElement?.();
33
- const isFirstRender = pinnedRowKey === undefined;
34
-
35
- if (scrollElement && !isFirstRender) {
36
- requestAnimationFrame(() => {
37
- scrollElement.scrollTo({ top: scrollElement.scrollHeight });
38
- });
39
- }
40
-
41
- setPinnedRowKey(rowId);
42
- }, [pinLastRow, table, getScrollElement, pinnedRowKey, setPinnedRowKey, data]);
43
- };