@dxos/react-ui-table 0.4.10-main.0302e13 → 0.4.10-main.06ef97a
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 +540 -276
- 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} +5 -1
- 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/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.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/package.json +18 -15
- package/src/components/ColumnMenu/ColumnMenu.stories.tsx +97 -0
- package/src/components/ColumnMenu/ColumnMenu.tsx +166 -0
- package/src/components/ColumnMenu/ColumnSettingsForm.tsx +208 -0
- package/src/components/ColumnMenu/index.ts +6 -0
- package/src/components/Table/Table.tsx +1 -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/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 +93 -0
- package/src/schema/schemaToColumns.ts +47 -0
- package/src/theme.ts +1 -1
- 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/components/hooks/usePinLastRow.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/dist/types/src/{components/hooks → hooks}/usePinLastRow.d.ts +0 -0
- /package/src/{components/hooks → hooks}/usePinLastRow.ts +0 -0
- /package/src/{schema.ts → schema/types.ts} +0 -0
|
@@ -0,0 +1,166 @@
|
|
|
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.Content onCloseAutoFocus={onDropdownCloseAutoFocus} sideOffset={4} collisionPadding={8}>
|
|
70
|
+
<DropdownMenu.Viewport>
|
|
71
|
+
{canSort && (
|
|
72
|
+
<>
|
|
73
|
+
<DropdownMenu.Item onClick={() => onSelectSort('asc')}>
|
|
74
|
+
<span className='grow'>Sort ascending</span>
|
|
75
|
+
<span className='opacity-50'>
|
|
76
|
+
<ArrowUp className={getSize(4)} />
|
|
77
|
+
</span>
|
|
78
|
+
</DropdownMenu.Item>
|
|
79
|
+
<DropdownMenu.Item onClick={() => onSelectSort('desc')}>
|
|
80
|
+
<span className='grow'>Sort descending</span>
|
|
81
|
+
<span className='opacity-50'>
|
|
82
|
+
<ArrowDown className={getSize(4)} />
|
|
83
|
+
</span>
|
|
84
|
+
</DropdownMenu.Item>
|
|
85
|
+
{sortDirection !== false && (
|
|
86
|
+
<DropdownMenu.Item onClick={onClearSort}>
|
|
87
|
+
<span className='grow'>Clear sort</span>
|
|
88
|
+
<span className='opacity-50'>
|
|
89
|
+
<X className={getSize(4)} />
|
|
90
|
+
</span>
|
|
91
|
+
</DropdownMenu.Item>
|
|
92
|
+
)}
|
|
93
|
+
</>
|
|
94
|
+
)}
|
|
95
|
+
<DropdownMenu.Item onClick={onOpenColumnSettings}>
|
|
96
|
+
<span className='grow'>Column settings</span>
|
|
97
|
+
<span className='opacity-50'>
|
|
98
|
+
<GearSix className={mx(getSize(4), 'rotate-90')} />
|
|
99
|
+
</span>
|
|
100
|
+
</DropdownMenu.Item>
|
|
101
|
+
</DropdownMenu.Viewport>
|
|
102
|
+
<DropdownMenu.Arrow />
|
|
103
|
+
</DropdownMenu.Content>
|
|
104
|
+
|
|
105
|
+
<ColumnSettingsPanel {...props} column={column} onClose={() => setIsColumnSettingsOpen(false)} />
|
|
106
|
+
</Popover.Root>
|
|
107
|
+
</DropdownMenu.Root>
|
|
108
|
+
</div>
|
|
109
|
+
);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export const ColumnSettingsPanel = <TData extends RowData, TValue>({
|
|
113
|
+
tableDefs,
|
|
114
|
+
tableDef,
|
|
115
|
+
column,
|
|
116
|
+
onUpdate,
|
|
117
|
+
onDelete,
|
|
118
|
+
onClose,
|
|
119
|
+
}: ColumnMenuProps<TData, TValue> & { onClose: () => void }) => (
|
|
120
|
+
<Popover.Portal>
|
|
121
|
+
<Popover.Content>
|
|
122
|
+
<Popover.Viewport classNames='w-60'>
|
|
123
|
+
<DensityProvider density='fine'>
|
|
124
|
+
<ColumnSettingsForm
|
|
125
|
+
column={column}
|
|
126
|
+
tableDefs={tableDefs}
|
|
127
|
+
tableDef={tableDef}
|
|
128
|
+
onUpdate={onUpdate}
|
|
129
|
+
onDelete={onDelete}
|
|
130
|
+
onClose={onClose}
|
|
131
|
+
/>
|
|
132
|
+
</DensityProvider>
|
|
133
|
+
</Popover.Viewport>
|
|
134
|
+
<Popover.Arrow />
|
|
135
|
+
</Popover.Content>
|
|
136
|
+
</Popover.Portal>
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
export const SortIndicator = ({
|
|
140
|
+
direction,
|
|
141
|
+
onClick,
|
|
142
|
+
}: {
|
|
143
|
+
direction: SortDirection | false;
|
|
144
|
+
onClick: (e: any) => void;
|
|
145
|
+
}) => {
|
|
146
|
+
if (direction === false) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let icon;
|
|
151
|
+
|
|
152
|
+
switch (direction) {
|
|
153
|
+
case 'asc':
|
|
154
|
+
icon = <ArrowUp className={getSize(3)} />;
|
|
155
|
+
break;
|
|
156
|
+
case 'desc':
|
|
157
|
+
icon = <ArrowDown className={getSize(3)} />;
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return (
|
|
162
|
+
<div onClick={onClick} className='flex items-center cursor-pointer'>
|
|
163
|
+
{icon}
|
|
164
|
+
</div>
|
|
165
|
+
);
|
|
166
|
+
};
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Check, Trash, X } from '@phosphor-icons/react';
|
|
6
|
+
import React, { type FC, type PropsWithChildren, useRef, useState } from 'react';
|
|
7
|
+
|
|
8
|
+
import { Button, Input, Select, Separator, useTranslation } from '@dxos/react-ui';
|
|
9
|
+
import { getSize, mx } from '@dxos/react-ui-theme';
|
|
10
|
+
import { safeParseInt } from '@dxos/util';
|
|
11
|
+
|
|
12
|
+
import { type TableDef, type ColumnProps, type ColumnType } from '../../schema';
|
|
13
|
+
import { translationKey } from '../../translations';
|
|
14
|
+
|
|
15
|
+
const Section: FC<PropsWithChildren & { className?: string }> = ({ children, className }) => (
|
|
16
|
+
<div role='none' className={mx('p-2', className)}>
|
|
17
|
+
{children}
|
|
18
|
+
</div>
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
export type ColumnSettingsFormProps = {
|
|
22
|
+
column: ColumnProps;
|
|
23
|
+
tableDef: TableDef;
|
|
24
|
+
tableDefs: TableDef[];
|
|
25
|
+
onUpdate?: ((id: string, column: ColumnProps) => void) | undefined;
|
|
26
|
+
onDelete?: ((id: string) => void) | undefined;
|
|
27
|
+
onClose?: () => void;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const ColumnSettingsForm = ({
|
|
31
|
+
column,
|
|
32
|
+
tableDef,
|
|
33
|
+
tableDefs,
|
|
34
|
+
onUpdate,
|
|
35
|
+
onDelete,
|
|
36
|
+
onClose,
|
|
37
|
+
}: ColumnSettingsFormProps) => {
|
|
38
|
+
const [prop, setProp] = useState(column.id);
|
|
39
|
+
const [refTable, setRefTable] = useState(column.refTable);
|
|
40
|
+
const [refProp, setRefProp] = useState(column.refProp);
|
|
41
|
+
const [type, setType] = useState(String(column.type));
|
|
42
|
+
const [label, setLabel] = useState(column.label);
|
|
43
|
+
const [digits, setDigits] = useState(String(column.digits ?? '0'));
|
|
44
|
+
const propRef = useRef<HTMLInputElement>(null);
|
|
45
|
+
const { t } = useTranslation(translationKey);
|
|
46
|
+
|
|
47
|
+
const handleCancel = () => {
|
|
48
|
+
setProp(column.id);
|
|
49
|
+
onClose?.();
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const handleSave = () => {
|
|
53
|
+
// Check valid.
|
|
54
|
+
if (!prop.length || !prop.match(/^[a-zA-Z_].+/i)) {
|
|
55
|
+
propRef.current?.focus();
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// If already exists then check same type.
|
|
60
|
+
const current = tableDef.columns.find((c) => c.id !== column.id && c.id === prop);
|
|
61
|
+
|
|
62
|
+
if (current) {
|
|
63
|
+
// TODO(burdon): Allow multiple columns with different refProps. Ensure correct table prop is updated.
|
|
64
|
+
if (current.type !== 'ref' /* || current.ref !== refTable */) {
|
|
65
|
+
// TODO(burdon): Show error.
|
|
66
|
+
propRef.current?.focus();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
onUpdate?.(prop, {
|
|
72
|
+
id: prop, // TODO(burdon): Make unique.
|
|
73
|
+
prop,
|
|
74
|
+
label,
|
|
75
|
+
type: type as ColumnProps['type'],
|
|
76
|
+
refTable,
|
|
77
|
+
refProp,
|
|
78
|
+
digits: safeParseInt(digits),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
onClose?.();
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
<div className='flex flex-col gap-1'>
|
|
86
|
+
<Section>
|
|
87
|
+
<Input.Root>
|
|
88
|
+
<Input.Label classNames='mbe-1'>{t('column label label')}</Input.Label>
|
|
89
|
+
<Input.TextInput
|
|
90
|
+
placeholder='Column label'
|
|
91
|
+
value={label ?? ''}
|
|
92
|
+
onChange={(event) => setLabel(event.target.value)}
|
|
93
|
+
autoFocus
|
|
94
|
+
/>
|
|
95
|
+
</Input.Root>
|
|
96
|
+
<Input.Root>
|
|
97
|
+
<Input.Label classNames='mbe-1 mbs-3'>{t('property key label')}</Input.Label>
|
|
98
|
+
<Input.TextInput
|
|
99
|
+
ref={propRef}
|
|
100
|
+
placeholder='Property key'
|
|
101
|
+
// TODO(burdon): Provide hooks for value normalization, ENTER, ESC, etc.
|
|
102
|
+
value={prop ?? ''}
|
|
103
|
+
onChange={(event) => setProp(event.target.value.replace(/[^\w_]/g, ''))}
|
|
104
|
+
/>
|
|
105
|
+
</Input.Root>
|
|
106
|
+
<Input.Root>
|
|
107
|
+
<Input.Label classNames='mbe-1 mbs-3'>{t('column type label')}</Input.Label>
|
|
108
|
+
<Select.Root value={type} onValueChange={setType}>
|
|
109
|
+
<Select.TriggerButton placeholder='Type' classNames='is-full' />
|
|
110
|
+
<Select.Portal>
|
|
111
|
+
<Select.Content>
|
|
112
|
+
<Select.Viewport>
|
|
113
|
+
{(['number', 'boolean', 'string', 'ref'] as ColumnType[]).map((type) => (
|
|
114
|
+
<Select.Option key={type} value={type}>
|
|
115
|
+
{t(`${type} column type label`)}
|
|
116
|
+
</Select.Option>
|
|
117
|
+
))}
|
|
118
|
+
</Select.Viewport>
|
|
119
|
+
</Select.Content>
|
|
120
|
+
</Select.Portal>
|
|
121
|
+
</Select.Root>
|
|
122
|
+
</Input.Root>
|
|
123
|
+
</Section>
|
|
124
|
+
|
|
125
|
+
{type === 'number' && (
|
|
126
|
+
<>
|
|
127
|
+
<Section>
|
|
128
|
+
<Input.Root>
|
|
129
|
+
<Input.Label classNames='mbe-1 mbs-3'>{t('digits label')}</Input.Label>
|
|
130
|
+
{/* TODO(burdon): Constrain input to numbers. */}
|
|
131
|
+
<Input.TextInput value={digits ?? ''} onChange={(event) => setDigits(event.target.value)} />
|
|
132
|
+
</Input.Root>
|
|
133
|
+
</Section>
|
|
134
|
+
</>
|
|
135
|
+
)}
|
|
136
|
+
|
|
137
|
+
{/* TODO(burdon): Selectors on same line. */}
|
|
138
|
+
{type === 'ref' && (
|
|
139
|
+
<>
|
|
140
|
+
<Section>
|
|
141
|
+
<Input.Root>
|
|
142
|
+
<Input.Label classNames='mbe-1 mbs-3'>Table</Input.Label>
|
|
143
|
+
<Select.Root value={refTable} onValueChange={setRefTable}>
|
|
144
|
+
<Select.TriggerButton placeholder='Table' classNames='is-full' />
|
|
145
|
+
<Select.Portal>
|
|
146
|
+
<Select.Content>
|
|
147
|
+
<Select.Viewport>
|
|
148
|
+
{tableDefs
|
|
149
|
+
.filter((t) => t.id !== tableDef.id)
|
|
150
|
+
.map(({ id, name }) => (
|
|
151
|
+
<Select.Option key={id} value={id}>
|
|
152
|
+
{name ?? id}
|
|
153
|
+
</Select.Option>
|
|
154
|
+
))}
|
|
155
|
+
</Select.Viewport>
|
|
156
|
+
</Select.Content>
|
|
157
|
+
</Select.Portal>
|
|
158
|
+
</Select.Root>
|
|
159
|
+
</Input.Root>
|
|
160
|
+
|
|
161
|
+
{refTable && (
|
|
162
|
+
<Input.Root>
|
|
163
|
+
<Input.Label classNames='mbe-1 mbs-3'>Table property</Input.Label>
|
|
164
|
+
<Select.Root value={refProp} onValueChange={setRefProp}>
|
|
165
|
+
<Select.TriggerButton placeholder='Property' classNames='is-full' />
|
|
166
|
+
<Select.Portal>
|
|
167
|
+
<Select.Content>
|
|
168
|
+
<Select.Viewport>
|
|
169
|
+
{tableDefs
|
|
170
|
+
.find((tableDef) => tableDef.id === refTable)
|
|
171
|
+
?.columns.map(({ id, label }) => (
|
|
172
|
+
<Select.Option key={id} value={id}>
|
|
173
|
+
{label ?? id}
|
|
174
|
+
</Select.Option>
|
|
175
|
+
))}
|
|
176
|
+
</Select.Viewport>
|
|
177
|
+
</Select.Content>
|
|
178
|
+
</Select.Portal>
|
|
179
|
+
</Select.Root>
|
|
180
|
+
</Input.Root>
|
|
181
|
+
)}
|
|
182
|
+
</Section>
|
|
183
|
+
</>
|
|
184
|
+
)}
|
|
185
|
+
|
|
186
|
+
<Separator classNames='mli-2' />
|
|
187
|
+
|
|
188
|
+
<Section className='space-b-1.5'>
|
|
189
|
+
{/* TODO(burdon): Style as DropdownMenuItem. */}
|
|
190
|
+
<Button variant='primary' classNames='is-full flex gap-2' onClick={handleSave}>
|
|
191
|
+
<span>{t('save label')}</span>
|
|
192
|
+
<div className='grow' />
|
|
193
|
+
<Check className={getSize(5)} />
|
|
194
|
+
</Button>
|
|
195
|
+
<Button classNames='is-full flex gap-2' onClick={handleCancel}>
|
|
196
|
+
<span>{t('cancel label', { ns: 'os' })}</span>
|
|
197
|
+
<div className='grow' />
|
|
198
|
+
<X className={getSize(5)} />
|
|
199
|
+
</Button>
|
|
200
|
+
<Button classNames='is-full flex gap-2' onClick={() => onDelete?.(column.id)}>
|
|
201
|
+
<span>{t('delete label')}</span>
|
|
202
|
+
<div className='grow' />
|
|
203
|
+
<Trash className={getSize(5)} />
|
|
204
|
+
</Button>
|
|
205
|
+
</Section>
|
|
206
|
+
</div>
|
|
207
|
+
);
|
|
208
|
+
};
|
|
@@ -26,8 +26,8 @@ import { type TypedTableProvider, TableProvider as UntypedTableProvider, useTabl
|
|
|
26
26
|
import { TableFooter } from './TableFooter';
|
|
27
27
|
import { TableHead } from './TableHead';
|
|
28
28
|
import { type TableProps } from './props';
|
|
29
|
+
import { usePinLastRow } from '../../hooks';
|
|
29
30
|
import { groupTh, tableRoot } from '../../theme';
|
|
30
|
-
import { usePinLastRow } from '../hooks/usePinLastRow';
|
|
31
31
|
|
|
32
32
|
export const Table = <TData extends RowData>(props: TableProps<TData>) => {
|
|
33
33
|
const {
|
package/src/components/index.ts
CHANGED
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: {
|
|
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
|
}
|
|
@@ -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
|
+
};
|
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 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 '../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
|
+
};
|