@dxos/react-ui-table 0.4.10-main.fa5a270 → 0.4.10-main.fd4f2a3

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 (47) hide show
  1. package/dist/lib/browser/index.mjs +353 -183
  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 +8 -2
  5. package/dist/types/src/components/ColumnMenu.d.ts.map +1 -1
  6. package/dist/types/src/components/ColumnMenu.stories.d.ts +4 -0
  7. package/dist/types/src/components/ColumnMenu.stories.d.ts.map +1 -1
  8. package/dist/types/src/components/ColumnSettingsForm.d.ts +10 -0
  9. package/dist/types/src/components/ColumnSettingsForm.d.ts.map +1 -0
  10. package/dist/types/src/components/Table/Table.d.ts.map +1 -1
  11. package/dist/types/src/components/Table/Table.stories.d.ts +3 -0
  12. package/dist/types/src/components/Table/Table.stories.d.ts.map +1 -1
  13. package/dist/types/src/components/Table/TableBody.d.ts.map +1 -1
  14. package/dist/types/src/components/hooks/useColumnSorting.d.ts +9 -0
  15. package/dist/types/src/components/hooks/useColumnSorting.d.ts.map +1 -0
  16. package/dist/types/src/components/hooks/usePinLastRow.d.ts +3 -0
  17. package/dist/types/src/components/hooks/usePinLastRow.d.ts.map +1 -0
  18. package/dist/types/src/components/schema/SchemaToTable.stories.d.ts +11 -0
  19. package/dist/types/src/components/schema/SchemaToTable.stories.d.ts.map +1 -0
  20. package/dist/types/src/components/schema/schema.d.ts +5 -0
  21. package/dist/types/src/components/schema/schema.d.ts.map +1 -0
  22. package/dist/types/src/components/schema/schema.test.d.ts +2 -0
  23. package/dist/types/src/components/schema/schema.test.d.ts.map +1 -0
  24. package/dist/types/src/components/schema/schemaToColumns.d.ts +4 -0
  25. package/dist/types/src/components/schema/schemaToColumns.d.ts.map +1 -0
  26. package/dist/types/src/helpers.d.ts +4 -0
  27. package/dist/types/src/helpers.d.ts.map +1 -1
  28. package/dist/types/src/theme.d.ts +1 -0
  29. package/dist/types/src/theme.d.ts.map +1 -1
  30. package/package.json +18 -15
  31. package/src/components/ColumnMenu.stories.tsx +52 -1
  32. package/src/components/ColumnMenu.tsx +129 -211
  33. package/src/components/ColumnSettingsForm.tsx +206 -0
  34. package/src/components/Table/Table.stories.tsx +31 -0
  35. package/src/components/Table/Table.tsx +6 -15
  36. package/src/components/Table/TableBody.tsx +6 -1
  37. package/src/components/hooks/useColumnSorting.ts +54 -0
  38. package/src/components/hooks/usePinLastRow.ts +43 -0
  39. package/src/components/schema/SchemaToTable.stories.tsx +72 -0
  40. package/src/components/schema/schema.test.ts +148 -0
  41. package/src/components/schema/schema.ts +96 -0
  42. package/src/components/schema/schemaToColumns.ts +50 -0
  43. package/src/helpers.tsx +23 -1
  44. package/src/theme.ts +3 -2
  45. package/dist/types/src/components/SortButton.d.ts +0 -8
  46. package/dist/types/src/components/SortButton.d.ts.map +0 -1
  47. package/src/components/SortButton.tsx +0 -34
@@ -28,10 +28,15 @@ const TableBody = ({ rows }: TableBodyProps) => {
28
28
  {rows.map((row) => {
29
29
  const isCurrent = currentDatum === row.original;
30
30
  const isSelected = rowSelection?.[row.id];
31
+
32
+ const isPinned = row.getIsPinned();
33
+
34
+ const classNames = tbodyTr({ canBeCurrent, isPinned: isPinned !== false });
35
+
31
36
  return (
32
37
  <tr
33
38
  key={keyAccessor ? keyAccessor(row.original) : row.id}
34
- className={tbodyTr({ canBeCurrent })}
39
+ className={classNames}
35
40
  {...(isCurrent && { 'aria-current': 'location' })}
36
41
  {...(isSelected && { 'aria-selected': 'true' })}
37
42
  {...(canBeCurrent && {
@@ -0,0 +1,54 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { type SortDirection, type Column } from '@tanstack/react-table';
6
+ import { useState } from 'react';
7
+
8
+ export const useColumnSorting = (column: Column<any>) => {
9
+ const [sortDirection, setSortDirection] = useState<SortDirection | false>(column.getIsSorted());
10
+ const [firstSort, setFirstSort] = useState<SortDirection | undefined>();
11
+
12
+ const canSort = column.getCanSort();
13
+
14
+ const updateColumnSort = (direction: SortDirection | false) => {
15
+ setSortDirection(direction);
16
+
17
+ switch (direction) {
18
+ case false:
19
+ column.clearSorting();
20
+ break;
21
+ case 'asc':
22
+ column.toggleSorting(false); // descending false
23
+ break;
24
+ case 'desc':
25
+ column.toggleSorting(true); // descending true
26
+ break;
27
+ }
28
+ };
29
+
30
+ // Whichever sort is explicitly set first, will be the first sort direction
31
+ // When toggling, we always go to the other direction, and then to unsorted.
32
+ const onToggleSort = () => {
33
+ if (sortDirection === false) {
34
+ return;
35
+ }
36
+
37
+ if (sortDirection === firstSort) {
38
+ updateColumnSort(firstSort === 'asc' ? 'desc' : 'asc');
39
+ } else {
40
+ updateColumnSort(false);
41
+ }
42
+ };
43
+
44
+ const onSelectSort = (direction: SortDirection) => {
45
+ setFirstSort(direction);
46
+ updateColumnSort(direction);
47
+ };
48
+
49
+ const onClearSort = () => {
50
+ updateColumnSort(false);
51
+ };
52
+
53
+ return { canSort, sortDirection, onSelectSort, onToggleSort, onClearSort };
54
+ };
@@ -0,0 +1,43 @@
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
+ };
@@ -0,0 +1,72 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import '@dxosTheme';
6
+
7
+ import * as Arbitrary from '@effect/schema/Arbitrary';
8
+ import * as S from '@effect/schema/Schema';
9
+ import * as fc from 'fast-check';
10
+ import React, { useRef } from 'react';
11
+
12
+ import { DensityProvider } from '@dxos/react-ui';
13
+ import { withTheme } from '@dxos/storybook-utils';
14
+
15
+ import { schemaToColumnDefs } from './schemaToColumns';
16
+ import { Table } from '../Table/Table';
17
+
18
+ export default {
19
+ title: 'react-ui-table/SchemaTable',
20
+ args: {},
21
+ decorators: [
22
+ withTheme,
23
+ (Story: any) => (
24
+ <DensityProvider density='fine'>
25
+ <Story />
26
+ </DensityProvider>
27
+ ),
28
+ ],
29
+ };
30
+
31
+ const exampleSchema = S.struct({
32
+ field1: S.string,
33
+ field2: S.number,
34
+ field3: S.Date,
35
+ field4: S.optional(S.string.pipe(S.length(10))),
36
+ field5: S.optional(
37
+ S.struct({
38
+ innerField1: S.string,
39
+ innerField2: S.number,
40
+ }),
41
+ ),
42
+ });
43
+
44
+ type ExampleSchema = S.Schema.Type<typeof exampleSchema>;
45
+
46
+ const columns = schemaToColumnDefs(exampleSchema);
47
+
48
+ const exampleSchemaArbitrary = Arbitrary.make(exampleSchema)(fc);
49
+
50
+ const items = fc.sample(exampleSchemaArbitrary, 10);
51
+
52
+ export const SchemaTable = {
53
+ render: () => {
54
+ const containerRef = useRef<HTMLDivElement | null>(null);
55
+
56
+ return (
57
+ <div ref={containerRef} className='fixed inset-0 overflow-auto'>
58
+ <Table<ExampleSchema>
59
+ role='grid'
60
+ rowsSelectable='multi'
61
+ keyAccessor={(row) => JSON.stringify(row)}
62
+ columns={columns}
63
+ data={items}
64
+ fullWidth
65
+ stickyHeader
66
+ border
67
+ getScrollElement={() => containerRef.current}
68
+ />
69
+ </div>
70
+ );
71
+ },
72
+ };
@@ -0,0 +1,148 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import * as S from '@effect/schema/Schema';
6
+ import { expect } from 'chai';
7
+
8
+ import { describe, test } from '@dxos/test';
9
+
10
+ import { classifySchemaProperties } from './schema';
11
+
12
+ describe('schema->column-type', () => {
13
+ test('basic', () => {
14
+ const simpleSchema = S.struct({
15
+ field1: S.string,
16
+ field2: S.number,
17
+ });
18
+
19
+ const columns = classifySchemaProperties(simpleSchema);
20
+
21
+ expect(columns).to.deep.equal([
22
+ ['field1', 'string'],
23
+ ['field2', 'number'],
24
+ ]);
25
+ });
26
+
27
+ test('optional properties', () => {
28
+ const simpleSchema = S.struct({
29
+ field1: S.optional(S.string),
30
+ field2: S.optional(S.number),
31
+ });
32
+
33
+ const columns = classifySchemaProperties(simpleSchema);
34
+
35
+ expect(columns).to.deep.equal([
36
+ ['field1', 'string'],
37
+ ['field2', 'number'],
38
+ ]);
39
+ });
40
+
41
+ test('dates', () => {
42
+ const simpleSchema = S.struct({
43
+ field1: S.Date,
44
+ field2: S.optional(S.Date),
45
+ });
46
+
47
+ const columns = classifySchemaProperties(simpleSchema);
48
+
49
+ expect(columns).to.deep.equal([
50
+ ['field1', 'date'],
51
+ ['field2', 'date'],
52
+ ]);
53
+ });
54
+
55
+ test('Piped validators', () => {
56
+ const simpleSchema = S.struct({
57
+ name: S.optional(S.string.pipe(S.nonEmpty(), S.maxLength(10))),
58
+ age: S.number.pipe(S.negative()),
59
+ });
60
+
61
+ const columns = classifySchemaProperties(simpleSchema);
62
+
63
+ expect(columns).to.deep.equal([
64
+ ['name', 'string'],
65
+ ['age', 'number'],
66
+ ]);
67
+ });
68
+
69
+ test('nested schema', () => {
70
+ const simpleSchema = S.struct({
71
+ name: S.string,
72
+ nested: S.struct({
73
+ age: S.number,
74
+ }),
75
+ someUnion: S.union(S.string, S.number),
76
+ });
77
+
78
+ const columns = classifySchemaProperties(simpleSchema);
79
+
80
+ expect(columns).to.deep.equal([
81
+ ['name', 'string'],
82
+ ['nested.age', 'number'],
83
+ ['someUnion', 'display'],
84
+ ]);
85
+ });
86
+
87
+ test('deeply nested schema', () => {
88
+ const simpleSchema = S.struct({
89
+ name: S.string,
90
+ nested: S.struct({
91
+ age: S.number,
92
+ deeplyNested: S.struct({
93
+ height: S.number,
94
+ }),
95
+ }),
96
+ });
97
+
98
+ const columns = classifySchemaProperties(simpleSchema);
99
+
100
+ expect(columns).to.deep.equal([
101
+ ['name', 'string'],
102
+ ['nested.age', 'number'],
103
+ ['nested.deeplyNested.height', 'number'],
104
+ ]);
105
+ });
106
+
107
+ test('even more deeply nested schema with optionals', () => {
108
+ const simpleSchema = S.struct({
109
+ name: S.string,
110
+ nested: S.struct({
111
+ age: S.number,
112
+ deeplyNested: S.optional(
113
+ S.struct({
114
+ height: S.optional(S.number),
115
+ }),
116
+ ),
117
+ }),
118
+ });
119
+
120
+ const columns = classifySchemaProperties(simpleSchema);
121
+
122
+ expect(columns).to.deep.equal([
123
+ ['name', 'string'],
124
+ ['nested.age', 'number'],
125
+ ['nested.deeplyNested.height', 'number'],
126
+ ]);
127
+ });
128
+
129
+ test('multiple-nested schemata as sibblings', () => {
130
+ const simpleSchema = S.struct({
131
+ name: S.string,
132
+ nested: S.struct({
133
+ age: S.number,
134
+ }),
135
+ nested2: S.struct({
136
+ height: S.number,
137
+ }),
138
+ });
139
+
140
+ const columns = classifySchemaProperties(simpleSchema);
141
+
142
+ expect(columns).to.deep.equal([
143
+ ['name', 'string'],
144
+ ['nested.age', 'number'],
145
+ ['nested2.height', 'number'],
146
+ ]);
147
+ });
148
+ });
@@ -0,0 +1,96 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { AST } from '@effect/schema';
6
+ import type * as S from '@effect/schema/Schema';
7
+
8
+ import { type ColumnType } from '../../schema';
9
+
10
+ export type ClassifiedColumnType = ColumnType | 'display';
11
+
12
+ const isOptionalUnion = (prop: AST.PropertySignature) => AST.isUnion(prop.type) && prop.isOptional;
13
+
14
+ const unwrapOptionProperty = (prop: AST.PropertySignature) => {
15
+ if (!isOptionalUnion(prop)) {
16
+ throw new Error('Not an optional property');
17
+ }
18
+
19
+ if (!AST.isUnion(prop.type)) {
20
+ throw new Error('Not a union type');
21
+ }
22
+
23
+ const [type, _undefinedCase] = prop.type.types;
24
+
25
+ return type;
26
+ };
27
+
28
+ const typeToColumn = (type: AST.AST): ClassifiedColumnType => {
29
+ if (AST.isStringKeyword(type)) {
30
+ return 'string';
31
+ } else if (AST.isNumberKeyword(type)) {
32
+ return 'number';
33
+ } else if (AST.isBooleanKeyword(type)) {
34
+ return 'boolean';
35
+ }
36
+
37
+ if (AST.isRefinement(type)) {
38
+ return typeToColumn(type.from);
39
+ }
40
+
41
+ // TODO(zan): How should we be thinking about transformations?
42
+ // - Which of these are we storing in the database?
43
+ // - For types that aren't the 'DateFromString' transformation, should we be using the 'from' or 'to' type?
44
+
45
+ if (AST.isTransform(type)) {
46
+ const identifier = AST.getIdentifierAnnotation(type);
47
+
48
+ if (identifier._tag === 'Some') {
49
+ if (identifier.value === 'DateFromString') {
50
+ return 'date';
51
+ }
52
+ }
53
+ }
54
+
55
+ return 'display';
56
+ };
57
+
58
+ const propertyToColumn = (property: AST.PropertySignature): ClassifiedColumnType => {
59
+ let type = property.type;
60
+
61
+ if (property.isOptional) {
62
+ type = unwrapOptionProperty(property);
63
+ }
64
+
65
+ return typeToColumn(type);
66
+ };
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
+ };
@@ -0,0 +1,50 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import type * as S from '@effect/schema/Schema';
6
+ import { type ColumnDef } from '@tanstack/react-table';
7
+
8
+ import { classifySchemaProperties } from './schema';
9
+ import { createColumnBuilder } from '../../helpers';
10
+
11
+ export const schemaToColumnDefs = <T>(schema: S.Schema<T, any>): ColumnDef<T, any>[] => {
12
+ const classified = classifySchemaProperties(schema);
13
+
14
+ const { helper, builder } = createColumnBuilder<T>();
15
+
16
+ return classified.map(([name, type]) => {
17
+ const propertyKey = name.toString();
18
+
19
+ let column: Partial<ColumnDef<any, any>> | undefined;
20
+
21
+ switch (type) {
22
+ case 'string': {
23
+ column = builder.string({ label: propertyKey });
24
+ break;
25
+ }
26
+ case 'number': {
27
+ column = builder.number({ label: propertyKey });
28
+ break;
29
+ }
30
+ case 'boolean': {
31
+ column = builder.switch({ label: propertyKey });
32
+ break;
33
+ }
34
+ case 'date': {
35
+ column = builder.date({ label: propertyKey });
36
+ break;
37
+ }
38
+ case 'display': {
39
+ column = builder.display({ label: propertyKey, id: propertyKey });
40
+ break;
41
+ }
42
+ }
43
+
44
+ if (column === undefined) {
45
+ throw new Error(`Unhandled column type: ${type}`);
46
+ }
47
+
48
+ return helper.accessor(propertyKey as any, column);
49
+ });
50
+ };
package/src/helpers.tsx CHANGED
@@ -340,7 +340,7 @@ export class ColumnBuilder<TData extends RowData> {
340
340
  ...props.meta,
341
341
  onUpdate,
342
342
  digits,
343
- header: { classNames: 'text-end', ...props.meta?.header },
343
+ header: { ...props.meta?.header },
344
344
  cell: { ...props.meta?.cell, classNames },
345
345
  },
346
346
  };
@@ -498,4 +498,26 @@ export class ColumnBuilder<TData extends RowData> {
498
498
  ...props,
499
499
  };
500
500
  }
501
+
502
+ /**
503
+ * This display column type is for nested/unhandled data to appear as JSON.
504
+ */
505
+ display({ label, classNames, ...props }: KeyColumnOptions<TData> = {}): Partial<ColumnDef<TData, PublicKey>> {
506
+ return {
507
+ size: 120,
508
+ minSize: 120,
509
+ header: (cell) => label ?? cell.header.id,
510
+ cell: (cell) => {
511
+ const value = cell.getValue();
512
+
513
+ return (
514
+ <div className='overflow-scroll'>
515
+ <code className='text-xs'>{JSON.stringify(value)}</code>
516
+ </div>
517
+ );
518
+ },
519
+ ...props,
520
+ meta: { ...props.meta, cell: { ...props.meta?.cell, classNames: ['font-mono', textPadding, classNames] } },
521
+ };
522
+ }
501
523
  }
package/src/theme.ts CHANGED
@@ -75,13 +75,14 @@ export const theadResizeRoot: ComponentFunction<{ isResizing: boolean }> = ({ is
75
75
  export type TbodyStyleProps = Partial<TableContextValue<any>>;
76
76
  export const tbodyRoot: ComponentFunction<TbodyStyleProps> = (_props, ...etc) => mx(...etc);
77
77
 
78
- export type TbodyTrStyleProps = Partial<{ canBeCurrent?: boolean }>;
79
- export const tbodyTr: ComponentFunction<TbodyTrStyleProps> = ({ canBeCurrent }, ...etc) =>
78
+ export type TbodyTrStyleProps = Partial<{ canBeCurrent?: boolean; isPinned?: boolean }>;
79
+ export const tbodyTr: ComponentFunction<TbodyTrStyleProps> = ({ canBeCurrent, isPinned }, ...etc) =>
80
80
  mx(
81
81
  'group',
82
82
  canBeCurrent ? ghostSelectedCurrent : ghostSelected,
83
83
  canBeCurrent && focusRing,
84
84
  canBeCurrent && 'cursor-pointer rounded',
85
+ isPinned && 'sticky z-1 bottom-[-1px] base-surface',
85
86
  ...etc,
86
87
  );
87
88
 
@@ -1,8 +0,0 @@
1
- import { type SortDirection } from '@tanstack/react-table';
2
- type SortButtonProps = {
3
- sortDirection: false | SortDirection;
4
- onClick: (e: any) => void;
5
- };
6
- export declare const SortButton: ({ sortDirection, onClick }: SortButtonProps) => JSX.Element;
7
- export {};
8
- //# sourceMappingURL=SortButton.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"SortButton.d.ts","sourceRoot":"","sources":["../../../../src/components/SortButton.tsx"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAM3D,KAAK,eAAe,GAAG;IAAE,aAAa,EAAE,KAAK,GAAG,aAAa,CAAC;IAAC,OAAO,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,IAAI,CAAA;CAAE,CAAC;AAE3F,eAAO,MAAM,UAAU,+BAAgC,eAAe,gBAoBrE,CAAC"}
@@ -1,34 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { CaretUpDown, SortAscending, SortDescending } from '@phosphor-icons/react';
6
- import { type SortDirection } from '@tanstack/react-table';
7
- import React, { type ReactElement } from 'react';
8
-
9
- import { Button } from '@dxos/react-ui';
10
- import { getSize } from '@dxos/react-ui-theme';
11
-
12
- type SortButtonProps = { sortDirection: false | SortDirection; onClick: (e: any) => void };
13
-
14
- export const SortButton = ({ sortDirection, onClick }: SortButtonProps) => {
15
- let icon: ReactElement;
16
- const size = getSize(4);
17
-
18
- switch (sortDirection) {
19
- case 'asc':
20
- icon = <SortAscending className={size} />;
21
- break;
22
- case 'desc':
23
- icon = <SortDescending className={size} />;
24
- break;
25
- default:
26
- icon = <CaretUpDown className={size} />;
27
- }
28
-
29
- return (
30
- <Button variant='ghost' onClick={onClick}>
31
- {icon}
32
- </Button>
33
- );
34
- };