@dxos/react-ui-table 0.4.9 → 0.4.10-main.05b9ab6
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 +786 -367
- 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 +17 -4
- package/dist/types/src/components/Table/Table.d.ts.map +1 -1
- package/dist/types/src/components/Table/Table.stories.d.ts +35 -1
- 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/Table/TableRootContext.d.ts +17 -0
- package/dist/types/src/components/Table/TableRootContext.d.ts.map +1 -0
- package/dist/types/src/components/Table/index.d.ts +1 -0
- package/dist/types/src/components/Table/index.d.ts.map +1 -1
- package/dist/types/src/components/Table/props.d.ts +2 -3
- package/dist/types/src/components/Table/props.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 +5 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -0
- package/dist/types/src/hooks/useColumnSizing.d.ts +12 -0
- package/dist/types/src/hooks/useColumnSizing.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/hooks/useRowSelection.d.ts +7 -0
- package/dist/types/src/hooks/useRowSelection.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 +22 -16
- package/src/components/ColumnMenu/ColumnMenu.stories.tsx +127 -0
- package/src/components/ColumnMenu/ColumnMenu.tsx +168 -0
- package/src/components/ColumnMenu/ColumnSettingsForm.tsx +214 -0
- package/src/components/ColumnMenu/index.ts +6 -0
- package/src/components/Table/Table.stories.tsx +260 -59
- package/src/components/Table/Table.tsx +82 -107
- package/src/components/Table/TableBody.tsx +12 -2
- package/src/components/Table/TableRootContext.ts +39 -0
- package/src/components/Table/index.ts +1 -0
- package/src/components/Table/props.ts +17 -24
- package/src/components/index.ts +1 -1
- package/src/helpers.tsx +32 -8
- package/src/hooks/index.ts +8 -0
- package/src/hooks/useColumnSizing.ts +45 -0
- package/src/hooks/useColumnSorting.ts +54 -0
- package/src/hooks/usePinLastRow.ts +64 -0
- package/src/hooks/useRowSelection.ts +39 -0
- package/src/index.ts +3 -1
- package/src/schema/SchemaToTable.stories.tsx +71 -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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"theme.d.ts","sourceRoot":"","sources":["../../../src/theme.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAGzD,OAAO,EAAE,KAAK,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAE9D,OAAO,EAAE,KAAK,iBAAiB,EAAE,KAAK,UAAU,EAAE,MAAM,cAAc,CAAC;AAEvE,eAAO,MAAM,UAAU,wCAAwC,CAAC;AAChE,eAAO,MAAM,WAAW,yCAAyC,CAAC;AAClE,eAAO,MAAM,YAAY,gBAAgB,CAAC;AAC1C,eAAO,MAAM,WAAW,kBAAkB,CAAC;AAC3C,eAAO,MAAM,WAAW,gBAAgB,CAAC;AAEzC,eAAO,MAAM,iBAAiB,oGACqE,CAAC;AAMpG,MAAM,MAAM,eAAe,GAAG,OAAO,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC,CAAC;AAE9D,eAAO,MAAM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C,eAAe,CAAkC,CAAC;AAE1F,eAAO,MAAM,OAAO,EAAE,iBAAiB,CAAC,eAAe,CACO,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-ui-table",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.10-main.05b9ab6",
|
|
4
4
|
"description": "A table component.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -16,34 +16,40 @@
|
|
|
16
16
|
"src"
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
|
+
"@effect/schema": "0.64.7",
|
|
19
20
|
"@fluentui/react-tabster": "^9.19.0",
|
|
20
21
|
"@radix-ui/react-context": "^1.0.0",
|
|
22
|
+
"@radix-ui/react-primitive": "^1.0.2",
|
|
23
|
+
"@radix-ui/react-slot": "^1.0.1",
|
|
21
24
|
"@radix-ui/react-use-controllable-state": "^1.0.0",
|
|
22
25
|
"@tanstack/react-table": "^8.13.2",
|
|
23
26
|
"@tanstack/react-virtual": "^3.0.1",
|
|
24
27
|
"date-fns": "^3.3.1",
|
|
25
|
-
"
|
|
26
|
-
"@dxos/
|
|
27
|
-
"@dxos/
|
|
28
|
-
"@dxos/
|
|
29
|
-
"@dxos/
|
|
30
|
-
"@dxos/react-ui
|
|
31
|
-
"@dxos/react-ui-theme": "0.4.
|
|
32
|
-
"@dxos/react-ui-types": "0.4.
|
|
33
|
-
"@dxos/util": "0.4.
|
|
28
|
+
"effect": "2.4.9",
|
|
29
|
+
"@dxos/async": "0.4.10-main.05b9ab6",
|
|
30
|
+
"@dxos/invariant": "0.4.10-main.05b9ab6",
|
|
31
|
+
"@dxos/log": "0.4.10-main.05b9ab6",
|
|
32
|
+
"@dxos/keys": "0.4.10-main.05b9ab6",
|
|
33
|
+
"@dxos/react-ui": "0.4.10-main.05b9ab6",
|
|
34
|
+
"@dxos/react-ui-theme": "0.4.10-main.05b9ab6",
|
|
35
|
+
"@dxos/react-ui-types": "0.4.10-main.05b9ab6",
|
|
36
|
+
"@dxos/util": "0.4.10-main.05b9ab6",
|
|
37
|
+
"@dxos/react-ui-searchlist": "0.4.10-main.05b9ab6"
|
|
34
38
|
},
|
|
35
39
|
"devDependencies": {
|
|
36
|
-
"@phosphor-icons/react": "^2.
|
|
40
|
+
"@phosphor-icons/react": "^2.1.5",
|
|
37
41
|
"@types/react": "^18.0.21",
|
|
38
42
|
"@types/react-dom": "^18.0.6",
|
|
43
|
+
"fast-check": "^3.15.1",
|
|
39
44
|
"react": "^18.2.0",
|
|
40
45
|
"react-dom": "^18.2.0",
|
|
41
|
-
"vite": "^5.
|
|
46
|
+
"vite": "^5.2.9",
|
|
42
47
|
"vite-plugin-turbosnap": "^1.0.2",
|
|
43
|
-
"@dxos/echo-schema": "0.4.
|
|
44
|
-
"@dxos/
|
|
45
|
-
"@dxos/
|
|
46
|
-
"@dxos/
|
|
48
|
+
"@dxos/echo-schema": "0.4.10-main.05b9ab6",
|
|
49
|
+
"@dxos/echo-signals": "0.4.10-main.05b9ab6",
|
|
50
|
+
"@dxos/random": "0.4.10-main.05b9ab6",
|
|
51
|
+
"@dxos/react-ui-theme": "0.4.10-main.05b9ab6",
|
|
52
|
+
"@dxos/storybook-utils": "0.4.10-main.05b9ab6"
|
|
47
53
|
},
|
|
48
54
|
"peerDependencies": {
|
|
49
55
|
"@phosphor-icons/react": "^2.0.5",
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import '@dxosTheme';
|
|
6
|
+
|
|
7
|
+
import { type SortDirection } from '@tanstack/react-table';
|
|
8
|
+
import React from 'react';
|
|
9
|
+
|
|
10
|
+
import { withTheme } from '@dxos/storybook-utils';
|
|
11
|
+
|
|
12
|
+
import { ColumnMenu } from './ColumnMenu';
|
|
13
|
+
import { type ColumnSettingsFormProps } from './ColumnSettingsForm';
|
|
14
|
+
import { type ColumnProps } from '../../schema';
|
|
15
|
+
|
|
16
|
+
export default {
|
|
17
|
+
title: 'react-ui-table/ColumnMenu',
|
|
18
|
+
component: ColumnMenu,
|
|
19
|
+
decorators: [withTheme],
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const defs: Pick<ColumnSettingsFormProps, 'tableDef' | 'tableDefs'> = {
|
|
23
|
+
tableDef: {
|
|
24
|
+
id: '1',
|
|
25
|
+
name: 'table 1',
|
|
26
|
+
columns: [{ id: 'col-1', prop: 'one-foo', type: 'string' }],
|
|
27
|
+
},
|
|
28
|
+
tableDefs: [
|
|
29
|
+
{
|
|
30
|
+
id: '1',
|
|
31
|
+
name: 'table 1',
|
|
32
|
+
columns: [{ id: 'col-1', prop: 'one-foo', type: 'string' }],
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
id: '2',
|
|
36
|
+
name: 'table 2',
|
|
37
|
+
columns: [{ id: 'col-1', prop: 'two-foo', type: 'string' }],
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const funcs: Pick<ColumnSettingsFormProps, 'onUpdate' | 'onDelete'> = {
|
|
43
|
+
onUpdate: (...args: any) => console.log('onUpdate args', args),
|
|
44
|
+
onDelete: (...args: any) => console.log('onDelete args', args),
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const Default = {
|
|
48
|
+
args: {
|
|
49
|
+
column: {
|
|
50
|
+
id: 'test',
|
|
51
|
+
label: 'test',
|
|
52
|
+
} as ColumnProps,
|
|
53
|
+
|
|
54
|
+
...defs,
|
|
55
|
+
...funcs,
|
|
56
|
+
|
|
57
|
+
context: {
|
|
58
|
+
header: {
|
|
59
|
+
column: {
|
|
60
|
+
getCanSort: () => true,
|
|
61
|
+
toggleSorting: (desc?: boolean) => {
|
|
62
|
+
console.log('toggleSorting', { desc });
|
|
63
|
+
},
|
|
64
|
+
getToggleSortingHandler: () => () => {
|
|
65
|
+
console.log('Sorting handler invoked');
|
|
66
|
+
},
|
|
67
|
+
getIsSorted: () => false,
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
decorators: [
|
|
73
|
+
(Story: any) => (
|
|
74
|
+
<div className='flex flex-col items-center h-screen w-full overflow-hidden'>
|
|
75
|
+
<Story />
|
|
76
|
+
</div>
|
|
77
|
+
),
|
|
78
|
+
],
|
|
79
|
+
parameters: {
|
|
80
|
+
layout: 'fullscreen',
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export const ReactiveSort = {
|
|
85
|
+
render: () => {
|
|
86
|
+
const [sort, setSort] = React.useState<SortDirection | false>(false);
|
|
87
|
+
const props = {
|
|
88
|
+
column: {
|
|
89
|
+
id: 'test',
|
|
90
|
+
label: 'test',
|
|
91
|
+
} as ColumnProps,
|
|
92
|
+
context: {
|
|
93
|
+
header: {
|
|
94
|
+
column: {
|
|
95
|
+
getCanSort: () => true,
|
|
96
|
+
toggleSorting: (desc?: boolean) => {
|
|
97
|
+
if (desc === undefined) {
|
|
98
|
+
setSort(false);
|
|
99
|
+
} else if (desc) {
|
|
100
|
+
setSort('desc');
|
|
101
|
+
} else {
|
|
102
|
+
setSort('asc');
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
getIsSorted: () => sort,
|
|
106
|
+
clearSorting: () => setSort(false),
|
|
107
|
+
getToggleSortingHandler: () => () => {
|
|
108
|
+
if (sort === 'asc') {
|
|
109
|
+
setSort('desc');
|
|
110
|
+
} else {
|
|
111
|
+
setSort(false);
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
} as any;
|
|
118
|
+
|
|
119
|
+
return (
|
|
120
|
+
<div className='flex justify-around w-full overflow-hidden'>
|
|
121
|
+
<div className='grid w-48'>
|
|
122
|
+
<ColumnMenu {...props} />
|
|
123
|
+
</div>
|
|
124
|
+
</div>
|
|
125
|
+
);
|
|
126
|
+
},
|
|
127
|
+
};
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { X, GearSix, CaretDown, ArrowDown, ArrowUp } from '@phosphor-icons/react';
|
|
6
|
+
import { type SortDirection, type HeaderContext, type RowData } from '@tanstack/react-table';
|
|
7
|
+
import React, { useRef, useState, useCallback } from 'react';
|
|
8
|
+
|
|
9
|
+
import { Button, DensityProvider, Popover, DropdownMenu } from '@dxos/react-ui';
|
|
10
|
+
import { getSize, mx } from '@dxos/react-ui-theme';
|
|
11
|
+
|
|
12
|
+
import { ColumnSettingsForm } from './ColumnSettingsForm';
|
|
13
|
+
import { useColumnSorting } from '../../hooks';
|
|
14
|
+
import { type TableDef, type ColumnProps } from '../../schema';
|
|
15
|
+
|
|
16
|
+
export type ColumnMenuProps<TData extends RowData, TValue> = {
|
|
17
|
+
context: HeaderContext<TData, TValue>;
|
|
18
|
+
tableDefs: TableDef[];
|
|
19
|
+
tableDef: TableDef;
|
|
20
|
+
column: ColumnProps;
|
|
21
|
+
onUpdate?: (id: string, column: ColumnProps) => void;
|
|
22
|
+
onDelete?: (id: string) => void;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const ColumnMenu = <TData extends RowData, TValue>({ column, ...props }: ColumnMenuProps<TData, TValue>) => {
|
|
26
|
+
const title = column.label?.length ? column.label : column.id;
|
|
27
|
+
const header = props.context.header;
|
|
28
|
+
|
|
29
|
+
const { canSort, sortDirection, onSelectSort, onToggleSort, onClearSort } = useColumnSorting(header.column);
|
|
30
|
+
|
|
31
|
+
const columnSettingsAnchorRef = useRef<any>(null);
|
|
32
|
+
|
|
33
|
+
const [isColumnSettingsOpen, setIsColumnSettingsOpen] = useState(false);
|
|
34
|
+
|
|
35
|
+
const shouldPreventFocusOnClose = useRef(false);
|
|
36
|
+
|
|
37
|
+
const onOpenColumnSettings = useCallback(() => {
|
|
38
|
+
shouldPreventFocusOnClose.current = true;
|
|
39
|
+
setIsColumnSettingsOpen(true);
|
|
40
|
+
}, [shouldPreventFocusOnClose, setIsColumnSettingsOpen]); // setIsColumnSettingsOpen is a dependency
|
|
41
|
+
|
|
42
|
+
const onDropdownCloseAutoFocus = useCallback(
|
|
43
|
+
(event: Event) => {
|
|
44
|
+
if (shouldPreventFocusOnClose.current) {
|
|
45
|
+
event.preventDefault();
|
|
46
|
+
shouldPreventFocusOnClose.current = false;
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
[shouldPreventFocusOnClose],
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
return (
|
|
53
|
+
<div className='flex items-center justify-center justify-between'>
|
|
54
|
+
<div className='flex items-center gap-1 truncate' title={title}>
|
|
55
|
+
<SortIndicator direction={sortDirection} onClick={onToggleSort} />
|
|
56
|
+
<div className='truncate'>{title}</div>
|
|
57
|
+
</div>
|
|
58
|
+
|
|
59
|
+
<DropdownMenu.Root modal={false}>
|
|
60
|
+
<Popover.Root open={isColumnSettingsOpen} onOpenChange={setIsColumnSettingsOpen} modal={false}>
|
|
61
|
+
<DropdownMenu.Trigger ref={columnSettingsAnchorRef} asChild>
|
|
62
|
+
<Popover.Anchor asChild>
|
|
63
|
+
<Button variant='ghost'>
|
|
64
|
+
<CaretDown className={getSize(4)} />
|
|
65
|
+
</Button>
|
|
66
|
+
</Popover.Anchor>
|
|
67
|
+
</DropdownMenu.Trigger>
|
|
68
|
+
|
|
69
|
+
<DropdownMenu.Portal>
|
|
70
|
+
<DropdownMenu.Content onCloseAutoFocus={onDropdownCloseAutoFocus} sideOffset={4} collisionPadding={8}>
|
|
71
|
+
<DropdownMenu.Viewport>
|
|
72
|
+
{canSort && (
|
|
73
|
+
<>
|
|
74
|
+
<DropdownMenu.Item onClick={() => onSelectSort('asc')}>
|
|
75
|
+
<span className='grow'>Sort ascending</span>
|
|
76
|
+
<span className='opacity-50'>
|
|
77
|
+
<ArrowUp className={getSize(4)} />
|
|
78
|
+
</span>
|
|
79
|
+
</DropdownMenu.Item>
|
|
80
|
+
<DropdownMenu.Item onClick={() => onSelectSort('desc')}>
|
|
81
|
+
<span className='grow'>Sort descending</span>
|
|
82
|
+
<span className='opacity-50'>
|
|
83
|
+
<ArrowDown className={getSize(4)} />
|
|
84
|
+
</span>
|
|
85
|
+
</DropdownMenu.Item>
|
|
86
|
+
{sortDirection !== false && (
|
|
87
|
+
<DropdownMenu.Item onClick={onClearSort}>
|
|
88
|
+
<span className='grow'>Clear sort</span>
|
|
89
|
+
<span className='opacity-50'>
|
|
90
|
+
<X className={getSize(4)} />
|
|
91
|
+
</span>
|
|
92
|
+
</DropdownMenu.Item>
|
|
93
|
+
)}
|
|
94
|
+
</>
|
|
95
|
+
)}
|
|
96
|
+
<DropdownMenu.Item onClick={onOpenColumnSettings}>
|
|
97
|
+
<span className='grow'>Column settings</span>
|
|
98
|
+
<span className='opacity-50'>
|
|
99
|
+
<GearSix className={mx(getSize(4), 'rotate-90')} />
|
|
100
|
+
</span>
|
|
101
|
+
</DropdownMenu.Item>
|
|
102
|
+
</DropdownMenu.Viewport>
|
|
103
|
+
<DropdownMenu.Arrow />
|
|
104
|
+
</DropdownMenu.Content>
|
|
105
|
+
</DropdownMenu.Portal>
|
|
106
|
+
|
|
107
|
+
<ColumnSettingsPanel {...props} column={column} onClose={() => setIsColumnSettingsOpen(false)} />
|
|
108
|
+
</Popover.Root>
|
|
109
|
+
</DropdownMenu.Root>
|
|
110
|
+
</div>
|
|
111
|
+
);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export const ColumnSettingsPanel = <TData extends RowData, TValue>({
|
|
115
|
+
tableDefs,
|
|
116
|
+
tableDef,
|
|
117
|
+
column,
|
|
118
|
+
onUpdate,
|
|
119
|
+
onDelete,
|
|
120
|
+
onClose,
|
|
121
|
+
}: ColumnMenuProps<TData, TValue> & { onClose: () => void }) => (
|
|
122
|
+
<Popover.Portal>
|
|
123
|
+
<Popover.Content>
|
|
124
|
+
<Popover.Viewport classNames='w-60'>
|
|
125
|
+
<DensityProvider density='fine'>
|
|
126
|
+
<ColumnSettingsForm
|
|
127
|
+
column={column}
|
|
128
|
+
tableDefs={tableDefs}
|
|
129
|
+
tableDef={tableDef}
|
|
130
|
+
onUpdate={onUpdate}
|
|
131
|
+
onDelete={onDelete}
|
|
132
|
+
onClose={onClose}
|
|
133
|
+
/>
|
|
134
|
+
</DensityProvider>
|
|
135
|
+
</Popover.Viewport>
|
|
136
|
+
<Popover.Arrow />
|
|
137
|
+
</Popover.Content>
|
|
138
|
+
</Popover.Portal>
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
export const SortIndicator = ({
|
|
142
|
+
direction,
|
|
143
|
+
onClick,
|
|
144
|
+
}: {
|
|
145
|
+
direction: SortDirection | false;
|
|
146
|
+
onClick: (e: any) => void;
|
|
147
|
+
}) => {
|
|
148
|
+
if (direction === false) {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
let icon;
|
|
153
|
+
|
|
154
|
+
switch (direction) {
|
|
155
|
+
case 'asc':
|
|
156
|
+
icon = <ArrowUp className={getSize(3)} />;
|
|
157
|
+
break;
|
|
158
|
+
case 'desc':
|
|
159
|
+
icon = <ArrowDown className={getSize(3)} />;
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return (
|
|
164
|
+
<div onClick={onClick} className='flex items-center cursor-pointer'>
|
|
165
|
+
{icon}
|
|
166
|
+
</div>
|
|
167
|
+
);
|
|
168
|
+
};
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
import { Check, Trash, X } from '@phosphor-icons/react';
|
|
5
|
+
import React, { type FC, type PropsWithChildren, useRef, useState } from 'react';
|
|
6
|
+
|
|
7
|
+
import { Button, Input, Select, Separator, useTranslation } from '@dxos/react-ui';
|
|
8
|
+
import { getSize, mx } from '@dxos/react-ui-theme';
|
|
9
|
+
import { safeParseInt } from '@dxos/util';
|
|
10
|
+
|
|
11
|
+
import { type TableDef, type ColumnProps, type ColumnType } from '../../schema';
|
|
12
|
+
import { translationKey } from '../../translations';
|
|
13
|
+
|
|
14
|
+
const Section: FC<PropsWithChildren & { className?: string }> = ({ children, className }) => (
|
|
15
|
+
<div role='none' className={mx('p-2', className)}>
|
|
16
|
+
{children}
|
|
17
|
+
</div>
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
export type ColumnSettingsFormProps = {
|
|
21
|
+
column: ColumnProps;
|
|
22
|
+
tableDef: TableDef;
|
|
23
|
+
tableDefs: TableDef[];
|
|
24
|
+
onUpdate?: ((id: string, column: ColumnProps) => void) | undefined;
|
|
25
|
+
onDelete?: ((id: string) => void) | undefined;
|
|
26
|
+
onClose?: () => void;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const ColumnSettingsForm = ({
|
|
30
|
+
column,
|
|
31
|
+
tableDef,
|
|
32
|
+
tableDefs,
|
|
33
|
+
onUpdate,
|
|
34
|
+
onDelete,
|
|
35
|
+
onClose,
|
|
36
|
+
}: ColumnSettingsFormProps) => {
|
|
37
|
+
const [formState, setFormState] = useState({
|
|
38
|
+
prop: column.id,
|
|
39
|
+
refTable: column.refTable,
|
|
40
|
+
refProp: column.refProp,
|
|
41
|
+
type: String(column.type),
|
|
42
|
+
label: column.label,
|
|
43
|
+
digits: String(column.digits ?? '0'),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const propRef = useRef<HTMLInputElement>(null);
|
|
47
|
+
const { t } = useTranslation(translationKey);
|
|
48
|
+
|
|
49
|
+
const handleCancel = () => {
|
|
50
|
+
onClose?.();
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const handleSave = () => {
|
|
54
|
+
const { prop, refTable, refProp, type, label, digits } = formState;
|
|
55
|
+
|
|
56
|
+
// Check valid.
|
|
57
|
+
if (!prop.length || !prop.match(/^[a-zA-Z_].+/i)) {
|
|
58
|
+
propRef.current?.focus();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// If already exists then check same type.
|
|
63
|
+
const current = tableDef.columns.find((c) => c.id !== column.id && c.id === prop);
|
|
64
|
+
if (current) {
|
|
65
|
+
// TODO(burdon): Allow multiple columns with different refProps. Ensure correct table prop is updated.
|
|
66
|
+
if (current.type !== 'ref' /* || current.ref !== refTable */) {
|
|
67
|
+
// TODO(burdon): Show error.
|
|
68
|
+
propRef.current?.focus();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
onUpdate?.(column.id, {
|
|
74
|
+
id: prop, // TODO(burdon): Make unique.
|
|
75
|
+
prop,
|
|
76
|
+
label,
|
|
77
|
+
type: type as ColumnProps['type'],
|
|
78
|
+
refTable,
|
|
79
|
+
refProp,
|
|
80
|
+
digits: safeParseInt(digits),
|
|
81
|
+
});
|
|
82
|
+
onClose?.();
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<div className='flex flex-col gap-1'>
|
|
87
|
+
<Section>
|
|
88
|
+
<Input.Root>
|
|
89
|
+
<Input.Label classNames='mbe-1'>{t('column label label')}</Input.Label>
|
|
90
|
+
<Input.TextInput
|
|
91
|
+
placeholder='Column label'
|
|
92
|
+
value={formState.label ?? ''}
|
|
93
|
+
onChange={(event) => setFormState((prevState) => ({ ...prevState, label: event.target.value }))}
|
|
94
|
+
autoFocus
|
|
95
|
+
/>
|
|
96
|
+
</Input.Root>
|
|
97
|
+
<Input.Root>
|
|
98
|
+
<Input.Label classNames='mbe-1 mbs-3'>{t('property key label')}</Input.Label>
|
|
99
|
+
<Input.TextInput
|
|
100
|
+
ref={propRef}
|
|
101
|
+
placeholder='Property key'
|
|
102
|
+
value={formState.prop ?? ''}
|
|
103
|
+
onChange={(event) =>
|
|
104
|
+
setFormState((prevState) => ({ ...prevState, prop: event.target.value.replace(/[^\w_]/g, '') }))
|
|
105
|
+
}
|
|
106
|
+
/>
|
|
107
|
+
</Input.Root>
|
|
108
|
+
<Input.Root>
|
|
109
|
+
<Input.Label classNames='mbe-1 mbs-3'>{t('column type label')}</Input.Label>
|
|
110
|
+
<Select.Root
|
|
111
|
+
value={formState.type}
|
|
112
|
+
onValueChange={(value) => setFormState((prevState) => ({ ...prevState, type: value }))}
|
|
113
|
+
>
|
|
114
|
+
<Select.TriggerButton placeholder='Type' classNames='is-full' />
|
|
115
|
+
<Select.Portal>
|
|
116
|
+
<Select.Content>
|
|
117
|
+
<Select.Viewport>
|
|
118
|
+
{(['number', 'boolean', 'string', 'ref'] as ColumnType[]).map((type) => (
|
|
119
|
+
<Select.Option key={type} value={type}>
|
|
120
|
+
{t(`${type} column type label`)}
|
|
121
|
+
</Select.Option>
|
|
122
|
+
))}
|
|
123
|
+
</Select.Viewport>
|
|
124
|
+
</Select.Content>
|
|
125
|
+
</Select.Portal>
|
|
126
|
+
</Select.Root>
|
|
127
|
+
</Input.Root>
|
|
128
|
+
</Section>
|
|
129
|
+
{formState.type === 'number' && (
|
|
130
|
+
<Section>
|
|
131
|
+
<Input.Root>
|
|
132
|
+
<Input.Label classNames='mbe-1 mbs-3'>{t('digits label')}</Input.Label>
|
|
133
|
+
<Input.TextInput
|
|
134
|
+
value={formState.digits ?? ''}
|
|
135
|
+
onChange={(event) => setFormState((prevState) => ({ ...prevState, digits: event.target.value }))}
|
|
136
|
+
/>
|
|
137
|
+
</Input.Root>
|
|
138
|
+
</Section>
|
|
139
|
+
)}
|
|
140
|
+
|
|
141
|
+
{formState.type === 'ref' && (
|
|
142
|
+
<Section>
|
|
143
|
+
<Input.Root>
|
|
144
|
+
<Input.Label classNames='mbe-1 mbs-3'>Table</Input.Label>
|
|
145
|
+
<Select.Root
|
|
146
|
+
value={formState.refTable}
|
|
147
|
+
onValueChange={(value) => setFormState((prevState) => ({ ...prevState, refTable: value }))}
|
|
148
|
+
>
|
|
149
|
+
<Select.TriggerButton placeholder='Table' classNames='is-full' />
|
|
150
|
+
<Select.Portal>
|
|
151
|
+
<Select.Content>
|
|
152
|
+
<Select.Viewport>
|
|
153
|
+
{tableDefs
|
|
154
|
+
.filter((t) => t.id !== tableDef.id)
|
|
155
|
+
.map(({ id, name }) => (
|
|
156
|
+
<Select.Option key={id} value={id}>
|
|
157
|
+
{name ?? id}
|
|
158
|
+
</Select.Option>
|
|
159
|
+
))}
|
|
160
|
+
</Select.Viewport>
|
|
161
|
+
</Select.Content>
|
|
162
|
+
</Select.Portal>
|
|
163
|
+
</Select.Root>
|
|
164
|
+
</Input.Root>
|
|
165
|
+
|
|
166
|
+
{formState.refTable && (
|
|
167
|
+
<Input.Root>
|
|
168
|
+
<Input.Label classNames='mbe-1 mbs-3'>Table property</Input.Label>
|
|
169
|
+
<Select.Root
|
|
170
|
+
value={formState.refProp}
|
|
171
|
+
onValueChange={(value) => setFormState((prevState) => ({ ...prevState, refProp: value }))}
|
|
172
|
+
>
|
|
173
|
+
<Select.TriggerButton placeholder='Property' classNames='is-full' />
|
|
174
|
+
<Select.Portal>
|
|
175
|
+
<Select.Content>
|
|
176
|
+
<Select.Viewport>
|
|
177
|
+
{tableDefs
|
|
178
|
+
.find((tableDef) => tableDef.id === formState.refTable)
|
|
179
|
+
?.columns.map(({ id, label }) => (
|
|
180
|
+
<Select.Option key={id} value={id}>
|
|
181
|
+
{label ?? id}
|
|
182
|
+
</Select.Option>
|
|
183
|
+
))}
|
|
184
|
+
</Select.Viewport>
|
|
185
|
+
</Select.Content>
|
|
186
|
+
</Select.Portal>
|
|
187
|
+
</Select.Root>
|
|
188
|
+
</Input.Root>
|
|
189
|
+
)}
|
|
190
|
+
</Section>
|
|
191
|
+
)}
|
|
192
|
+
|
|
193
|
+
<Separator classNames='mli-2' />
|
|
194
|
+
|
|
195
|
+
<Section className='space-b-1.5'>
|
|
196
|
+
<Button variant='primary' classNames='is-full flex gap-2' onClick={handleSave}>
|
|
197
|
+
<span>{t('save label')}</span>
|
|
198
|
+
<div className='grow' />
|
|
199
|
+
<Check className={getSize(5)} />
|
|
200
|
+
</Button>
|
|
201
|
+
<Button classNames='is-full flex gap-2' onClick={handleCancel}>
|
|
202
|
+
<span>{t('cancel label', { ns: 'os' })}</span>
|
|
203
|
+
<div className='grow' />
|
|
204
|
+
<X className={getSize(5)} />
|
|
205
|
+
</Button>
|
|
206
|
+
<Button classNames='is-full flex gap-2' onClick={() => onDelete?.(column.id)}>
|
|
207
|
+
<span>{t('delete label')}</span>
|
|
208
|
+
<div className='grow' />
|
|
209
|
+
<Trash className={getSize(5)} />
|
|
210
|
+
</Button>
|
|
211
|
+
</Section>
|
|
212
|
+
</div>
|
|
213
|
+
);
|
|
214
|
+
};
|