@dxos/react-ui-table 0.4.9 → 0.4.10-main.068c3d8
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/dist/lib/browser/index.mjs +632 -293
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/ColumnMenu/ColumnMenu.d.ts +19 -0
- package/dist/types/src/components/ColumnMenu/ColumnMenu.d.ts.map +1 -0
- package/dist/types/src/components/{ColumnMenu.stories.d.ts → ColumnMenu/ColumnMenu.stories.d.ts} +10 -2
- package/dist/types/src/components/ColumnMenu/ColumnMenu.stories.d.ts.map +1 -0
- package/dist/types/src/components/ColumnMenu/ColumnSettingsForm.d.ts +11 -0
- package/dist/types/src/components/ColumnMenu/ColumnSettingsForm.d.ts.map +1 -0
- package/dist/types/src/components/ColumnMenu/index.d.ts +3 -0
- package/dist/types/src/components/ColumnMenu/index.d.ts.map +1 -0
- package/dist/types/src/components/Table/Table.d.ts.map +1 -1
- package/dist/types/src/components/Table/Table.stories.d.ts +6 -0
- package/dist/types/src/components/Table/Table.stories.d.ts.map +1 -1
- package/dist/types/src/components/Table/TableBody.d.ts.map +1 -1
- package/dist/types/src/components/index.d.ts +1 -1
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/helpers.d.ts +4 -0
- package/dist/types/src/helpers.d.ts.map +1 -1
- package/dist/types/src/hooks/index.d.ts +3 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -0
- package/dist/types/src/hooks/useColumnSorting.d.ts +9 -0
- package/dist/types/src/hooks/useColumnSorting.d.ts.map +1 -0
- package/dist/types/src/hooks/usePinLastRow.d.ts +3 -0
- package/dist/types/src/hooks/usePinLastRow.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +2 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/schema/SchemaToTable.stories.d.ts +11 -0
- package/dist/types/src/schema/SchemaToTable.stories.d.ts.map +1 -0
- package/dist/types/src/schema/index.d.ts +3 -0
- package/dist/types/src/schema/index.d.ts.map +1 -0
- package/dist/types/src/schema/schema.d.ts +5 -0
- package/dist/types/src/schema/schema.d.ts.map +1 -0
- package/dist/types/src/schema/schema.test.d.ts +2 -0
- package/dist/types/src/schema/schema.test.d.ts.map +1 -0
- package/dist/types/src/schema/schemaToColumns.d.ts +4 -0
- package/dist/types/src/schema/schemaToColumns.d.ts.map +1 -0
- package/dist/types/src/{schema.d.ts → schema/types.d.ts} +1 -1
- package/dist/types/src/schema/types.d.ts.map +1 -0
- package/dist/types/src/theme.d.ts +1 -0
- package/dist/types/src/theme.d.ts.map +1 -1
- package/package.json +19 -16
- package/src/components/ColumnMenu/ColumnMenu.stories.tsx +127 -0
- package/src/components/ColumnMenu/ColumnMenu.tsx +166 -0
- package/src/components/ColumnMenu/ColumnSettingsForm.tsx +214 -0
- package/src/components/ColumnMenu/index.ts +6 -0
- package/src/components/Table/Table.stories.tsx +89 -3
- package/src/components/Table/Table.tsx +28 -22
- package/src/components/Table/TableBody.tsx +6 -1
- package/src/components/index.ts +1 -1
- package/src/helpers.tsx +23 -1
- package/src/hooks/index.ts +6 -0
- package/src/hooks/useColumnSorting.ts +54 -0
- package/src/hooks/usePinLastRow.ts +64 -0
- package/src/index.ts +3 -1
- package/src/schema/SchemaToTable.stories.tsx +72 -0
- package/src/schema/index.ts +6 -0
- package/src/schema/schema.test.ts +148 -0
- package/src/schema/schema.ts +92 -0
- package/src/schema/schemaToColumns.ts +48 -0
- package/src/theme.ts +3 -2
- package/dist/types/src/components/ColumnMenu.d.ts +0 -13
- package/dist/types/src/components/ColumnMenu.d.ts.map +0 -1
- package/dist/types/src/components/ColumnMenu.stories.d.ts.map +0 -1
- package/dist/types/src/components/SortButton.d.ts +0 -8
- package/dist/types/src/components/SortButton.d.ts.map +0 -1
- package/dist/types/src/schema.d.ts.map +0 -1
- package/src/components/ColumnMenu.stories.tsx +0 -46
- package/src/components/ColumnMenu.tsx +0 -248
- package/src/components/SortButton.tsx +0 -34
- /package/src/{schema.ts → schema/types.ts} +0 -0
|
@@ -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,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
|
+
};
|
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';
|
|
@@ -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 fc from 'fast-check';
|
|
9
|
+
import React, { useRef } from 'react';
|
|
10
|
+
|
|
11
|
+
import { S } from '@dxos/echo-schema';
|
|
12
|
+
import { DensityProvider } from '@dxos/react-ui';
|
|
13
|
+
import { withTheme } from '@dxos/storybook-utils';
|
|
14
|
+
|
|
15
|
+
import { schemaToColumnDefs } from './schemaToColumns';
|
|
16
|
+
import { Table } from '../components';
|
|
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 { expect } from 'chai';
|
|
6
|
+
|
|
7
|
+
import { S } from '@dxos/echo-schema';
|
|
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,92 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as AST from '@effect/schema/AST';
|
|
6
|
+
import type * as S from '@effect/schema/Schema';
|
|
7
|
+
|
|
8
|
+
import { type ColumnType } from './types';
|
|
9
|
+
|
|
10
|
+
export type ClassifiedColumnType = ColumnType | 'display';
|
|
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
|
+
|
|
41
|
+
const isOptionalUnion = (prop: AST.PropertySignature) => AST.isUnion(prop.type) && prop.isOptional;
|
|
42
|
+
|
|
43
|
+
const unwrapOptionProperty = (prop: AST.PropertySignature) => {
|
|
44
|
+
if (!isOptionalUnion(prop)) {
|
|
45
|
+
throw new Error(`Not an optional property: ${String(prop.name)}`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (!AST.isUnion(prop.type)) {
|
|
49
|
+
throw new Error(`Not a union type: ${String(prop.name)}`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const [type, _undefinedCase] = prop.type.types;
|
|
53
|
+
return type;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const typeToColumn = (type: AST.AST): ClassifiedColumnType => {
|
|
57
|
+
if (AST.isStringKeyword(type)) {
|
|
58
|
+
return 'string';
|
|
59
|
+
} else if (AST.isNumberKeyword(type)) {
|
|
60
|
+
return 'number';
|
|
61
|
+
} else if (AST.isBooleanKeyword(type)) {
|
|
62
|
+
return 'boolean';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (AST.isRefinement(type)) {
|
|
66
|
+
return typeToColumn(type.from);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// TODO(zan): How should we be thinking about transformations?
|
|
70
|
+
// - Which of these are we storing in the database?
|
|
71
|
+
// - For types that aren't the 'DateFromString' transformation, should we be using the 'from' or 'to' type?
|
|
72
|
+
|
|
73
|
+
if (AST.isTransform(type)) {
|
|
74
|
+
const identifier = AST.getIdentifierAnnotation(type);
|
|
75
|
+
if (identifier._tag === 'Some') {
|
|
76
|
+
if (identifier.value === 'DateFromString') {
|
|
77
|
+
return 'date';
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return 'display';
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const propertyToColumn = (prop: AST.PropertySignature): ClassifiedColumnType => {
|
|
86
|
+
let type = prop.type;
|
|
87
|
+
if (prop.isOptional) {
|
|
88
|
+
type = unwrapOptionProperty(prop);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return typeToColumn(type);
|
|
92
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type ColumnDef } from '@tanstack/react-table';
|
|
6
|
+
|
|
7
|
+
import type { S } from '@dxos/echo-schema';
|
|
8
|
+
|
|
9
|
+
import { classifySchemaProperties } from './schema';
|
|
10
|
+
import { createColumnBuilder } from '../helpers';
|
|
11
|
+
|
|
12
|
+
export const schemaToColumnDefs = <T>(schema: S.Schema<T, any>): ColumnDef<T, any>[] => {
|
|
13
|
+
const { helper, builder } = createColumnBuilder<T>();
|
|
14
|
+
|
|
15
|
+
const classified = classifySchemaProperties(schema);
|
|
16
|
+
return classified.map(([name, type]) => {
|
|
17
|
+
const propertyKey = name.toString();
|
|
18
|
+
|
|
19
|
+
let column: Partial<ColumnDef<any, any>> | undefined;
|
|
20
|
+
switch (type) {
|
|
21
|
+
case 'string': {
|
|
22
|
+
column = builder.string({ label: propertyKey });
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
case 'number': {
|
|
26
|
+
column = builder.number({ label: propertyKey });
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
case 'boolean': {
|
|
30
|
+
column = builder.switch({ label: propertyKey });
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
case 'date': {
|
|
34
|
+
column = builder.date({ label: propertyKey });
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
case 'display': {
|
|
38
|
+
column = builder.display({ label: propertyKey, id: propertyKey });
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
default: {
|
|
42
|
+
throw new Error(`Unhandled column type: ${type}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return helper.accessor(propertyKey as any, column);
|
|
47
|
+
});
|
|
48
|
+
};
|
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,13 +0,0 @@
|
|
|
1
|
-
import { type HeaderContext, type RowData } from '@tanstack/react-table';
|
|
2
|
-
import { type TableDef, type ColumnProps } from '../schema';
|
|
3
|
-
export type ColumnMenuProps<TData extends RowData, TValue> = {
|
|
4
|
-
context: HeaderContext<TData, TValue>;
|
|
5
|
-
tableDefs: TableDef[];
|
|
6
|
-
tableDef: TableDef;
|
|
7
|
-
column: ColumnProps;
|
|
8
|
-
onUpdate?: (id: string, column: ColumnProps) => void;
|
|
9
|
-
onDelete?: (id: string) => void;
|
|
10
|
-
};
|
|
11
|
-
export declare const ColumnMenu: <TData extends unknown, TValue>({ column, ...props }: ColumnMenuProps<TData, TValue>) => JSX.Element;
|
|
12
|
-
export declare const ColumnPanel: <TData extends unknown, TValue>({ context, tableDefs, tableDef, column, onUpdate, onDelete, }: ColumnMenuProps<TData, TValue>) => JSX.Element;
|
|
13
|
-
//# sourceMappingURL=ColumnMenu.d.ts.map
|
|
@@ -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,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAQzE,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,WAAW,EAAmB,MAAM,WAAW,CAAC;AAG7E,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,sGAoBtB,CAAC;AAQF,eAAO,MAAM,WAAW,+IAkMvB,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;AAOpB,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;;;;;;AAE7C,wBAIE;AAEF,eAAO,MAAM,OAAO;;;;;;;;;;;;;yBAiBR,GAAG;;;;CASd,CAAC"}
|
|
@@ -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 +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,46 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import '@dxosTheme';
|
|
6
|
-
|
|
7
|
-
import React from 'react';
|
|
8
|
-
|
|
9
|
-
import { withTheme } from '@dxos/storybook-utils';
|
|
10
|
-
|
|
11
|
-
import { ColumnMenu } from './ColumnMenu';
|
|
12
|
-
import { type ColumnProps } from '../schema';
|
|
13
|
-
|
|
14
|
-
export default {
|
|
15
|
-
title: 'react-ui-table/ColumnMenu',
|
|
16
|
-
component: ColumnMenu,
|
|
17
|
-
decorators: [withTheme],
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export const Default = {
|
|
21
|
-
args: {
|
|
22
|
-
column: {
|
|
23
|
-
id: 'test',
|
|
24
|
-
label: 'test',
|
|
25
|
-
} as ColumnProps,
|
|
26
|
-
context: {
|
|
27
|
-
header: {
|
|
28
|
-
column: {
|
|
29
|
-
getCanSort: () => true,
|
|
30
|
-
getToggleSortingHandler: () => () => console.log('toggleSort'),
|
|
31
|
-
getIsSorted: () => false,
|
|
32
|
-
},
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
decorators: [
|
|
37
|
-
(Story: any) => (
|
|
38
|
-
<div className='flex flex-col items-center h-screen w-full overflow-hidden'>
|
|
39
|
-
<Story />
|
|
40
|
-
</div>
|
|
41
|
-
),
|
|
42
|
-
],
|
|
43
|
-
parameters: {
|
|
44
|
-
layout: 'fullscreen',
|
|
45
|
-
},
|
|
46
|
-
};
|