@dxos/react-ui-table 0.4.10-main.ef6fbc2 → 0.4.10-main.f635fb0
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 +477 -308
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/{ColumnMenu.d.ts → ColumnMenu/ColumnMenu.d.ts} +1 -1
- 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} +6 -2
- package/dist/types/src/components/ColumnMenu/ColumnMenu.stories.d.ts.map +1 -0
- package/dist/types/src/components/{ColumnSettingsForm.d.ts → ColumnMenu/ColumnSettingsForm.d.ts} +5 -4
- 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 +3 -0
- package/dist/types/src/components/Table/Table.stories.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/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.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.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/{components/schema → schema}/schema.d.ts +1 -1
- package/dist/types/src/schema/schema.d.ts.map +1 -0
- package/dist/types/src/schema/schema.test.d.ts.map +1 -0
- package/dist/types/src/{components/schema → schema}/schemaToColumns.d.ts +1 -1
- 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 +15 -15
- package/src/components/{ColumnMenu.stories.tsx → ColumnMenu/ColumnMenu.stories.tsx} +31 -1
- package/src/components/{ColumnMenu.tsx → ColumnMenu/ColumnMenu.tsx} +2 -2
- package/src/components/{ColumnSettingsForm.tsx → ColumnMenu/ColumnSettingsForm.tsx} +83 -75
- package/src/components/ColumnMenu/index.ts +6 -0
- package/src/components/Table/Table.stories.tsx +58 -3
- package/src/components/Table/Table.tsx +24 -9
- package/src/components/index.ts +1 -1
- package/src/hooks/index.ts +6 -0
- package/src/hooks/usePinLastRow.ts +64 -0
- package/src/index.ts +3 -1
- package/src/{components/schema → schema}/SchemaToTable.stories.tsx +2 -2
- package/src/schema/index.ts +6 -0
- package/src/{components/schema → schema}/schema.test.ts +1 -1
- package/src/{components/schema → schema}/schema.ts +37 -41
- package/src/{components/schema → schema}/schemaToColumns.ts +7 -9
- 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/ColumnSettingsForm.d.ts.map +0 -1
- package/dist/types/src/components/hooks/useColumnSorting.d.ts.map +0 -1
- package/dist/types/src/components/hooks/usePinLastRow.d.ts +0 -3
- package/dist/types/src/components/hooks/usePinLastRow.d.ts.map +0 -1
- package/dist/types/src/components/schema/SchemaToTable.stories.d.ts.map +0 -1
- package/dist/types/src/components/schema/schema.d.ts.map +0 -1
- package/dist/types/src/components/schema/schema.test.d.ts.map +0 -1
- package/dist/types/src/components/schema/schemaToColumns.d.ts.map +0 -1
- package/dist/types/src/schema.d.ts.map +0 -1
- package/src/components/hooks/usePinLastRow.ts +0 -43
- /package/dist/types/src/{components/hooks → hooks}/useColumnSorting.d.ts +0 -0
- /package/dist/types/src/{components/schema → schema}/SchemaToTable.stories.d.ts +0 -0
- /package/dist/types/src/{components/schema → schema}/schema.test.d.ts +0 -0
- /package/src/{components/hooks → hooks}/useColumnSorting.ts +0 -0
- /package/src/{schema.ts → schema/types.ts} +0 -0
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
//
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
|
-
|
|
5
4
|
import { Check, Trash, X } from '@phosphor-icons/react';
|
|
6
5
|
import React, { type FC, type PropsWithChildren, useRef, useState } from 'react';
|
|
7
6
|
|
|
@@ -9,8 +8,8 @@ import { Button, Input, Select, Separator, useTranslation } from '@dxos/react-ui
|
|
|
9
8
|
import { getSize, mx } from '@dxos/react-ui-theme';
|
|
10
9
|
import { safeParseInt } from '@dxos/util';
|
|
11
10
|
|
|
12
|
-
import { type TableDef, type ColumnProps, type ColumnType } from '
|
|
13
|
-
import { translationKey } from '
|
|
11
|
+
import { type TableDef, type ColumnProps, type ColumnType } from '../../schema';
|
|
12
|
+
import { translationKey } from '../../translations';
|
|
14
13
|
|
|
15
14
|
const Section: FC<PropsWithChildren & { className?: string }> = ({ children, className }) => (
|
|
16
15
|
<div role='none' className={mx('p-2', className)}>
|
|
@@ -18,6 +17,15 @@ const Section: FC<PropsWithChildren & { className?: string }> = ({ children, cla
|
|
|
18
17
|
</div>
|
|
19
18
|
);
|
|
20
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
|
+
|
|
21
29
|
export const ColumnSettingsForm = ({
|
|
22
30
|
column,
|
|
23
31
|
tableDef,
|
|
@@ -25,29 +33,26 @@ export const ColumnSettingsForm = ({
|
|
|
25
33
|
onUpdate,
|
|
26
34
|
onDelete,
|
|
27
35
|
onClose,
|
|
28
|
-
}: {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const [refProp, setRefProp] = useState(column.refProp);
|
|
39
|
-
const [type, setType] = useState(String(column.type));
|
|
40
|
-
const [label, setLabel] = useState(column.label);
|
|
41
|
-
const [digits, setDigits] = useState(String(column.digits ?? '0'));
|
|
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
|
+
|
|
42
46
|
const propRef = useRef<HTMLInputElement>(null);
|
|
43
47
|
const { t } = useTranslation(translationKey);
|
|
44
48
|
|
|
45
49
|
const handleCancel = () => {
|
|
46
|
-
|
|
47
|
-
onClose;
|
|
50
|
+
onClose?.();
|
|
48
51
|
};
|
|
49
52
|
|
|
50
53
|
const handleSave = () => {
|
|
54
|
+
const { prop, refTable, refProp, type, label, digits } = formState;
|
|
55
|
+
|
|
51
56
|
// Check valid.
|
|
52
57
|
if (!prop.length || !prop.match(/^[a-zA-Z_].+/i)) {
|
|
53
58
|
propRef.current?.focus();
|
|
@@ -56,7 +61,6 @@ export const ColumnSettingsForm = ({
|
|
|
56
61
|
|
|
57
62
|
// If already exists then check same type.
|
|
58
63
|
const current = tableDef.columns.find((c) => c.id !== column.id && c.id === prop);
|
|
59
|
-
|
|
60
64
|
if (current) {
|
|
61
65
|
// TODO(burdon): Allow multiple columns with different refProps. Ensure correct table prop is updated.
|
|
62
66
|
if (current.type !== 'ref' /* || current.ref !== refTable */) {
|
|
@@ -66,7 +70,7 @@ export const ColumnSettingsForm = ({
|
|
|
66
70
|
}
|
|
67
71
|
}
|
|
68
72
|
|
|
69
|
-
onUpdate?.(
|
|
73
|
+
onUpdate?.(column.id, {
|
|
70
74
|
id: prop, // TODO(burdon): Make unique.
|
|
71
75
|
prop,
|
|
72
76
|
label,
|
|
@@ -75,8 +79,7 @@ export const ColumnSettingsForm = ({
|
|
|
75
79
|
refProp,
|
|
76
80
|
digits: safeParseInt(digits),
|
|
77
81
|
});
|
|
78
|
-
|
|
79
|
-
onClose();
|
|
82
|
+
onClose?.();
|
|
80
83
|
};
|
|
81
84
|
|
|
82
85
|
return (
|
|
@@ -86,8 +89,8 @@ export const ColumnSettingsForm = ({
|
|
|
86
89
|
<Input.Label classNames='mbe-1'>{t('column label label')}</Input.Label>
|
|
87
90
|
<Input.TextInput
|
|
88
91
|
placeholder='Column label'
|
|
89
|
-
value={label ?? ''}
|
|
90
|
-
onChange={(event) =>
|
|
92
|
+
value={formState.label ?? ''}
|
|
93
|
+
onChange={(event) => setFormState((prevState) => ({ ...prevState, label: event.target.value }))}
|
|
91
94
|
autoFocus
|
|
92
95
|
/>
|
|
93
96
|
</Input.Root>
|
|
@@ -96,14 +99,18 @@ export const ColumnSettingsForm = ({
|
|
|
96
99
|
<Input.TextInput
|
|
97
100
|
ref={propRef}
|
|
98
101
|
placeholder='Property key'
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
+
value={formState.prop ?? ''}
|
|
103
|
+
onChange={(event) =>
|
|
104
|
+
setFormState((prevState) => ({ ...prevState, prop: event.target.value.replace(/[^\w_]/g, '') }))
|
|
105
|
+
}
|
|
102
106
|
/>
|
|
103
107
|
</Input.Root>
|
|
104
108
|
<Input.Root>
|
|
105
109
|
<Input.Label classNames='mbe-1 mbs-3'>{t('column type label')}</Input.Label>
|
|
106
|
-
<Select.Root
|
|
110
|
+
<Select.Root
|
|
111
|
+
value={formState.type}
|
|
112
|
+
onValueChange={(value) => setFormState((prevState) => ({ ...prevState, type: value }))}
|
|
113
|
+
>
|
|
107
114
|
<Select.TriggerButton placeholder='Type' classNames='is-full' />
|
|
108
115
|
<Select.Portal>
|
|
109
116
|
<Select.Content>
|
|
@@ -119,35 +126,59 @@ export const ColumnSettingsForm = ({
|
|
|
119
126
|
</Select.Root>
|
|
120
127
|
</Input.Root>
|
|
121
128
|
</Section>
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
<Input.
|
|
127
|
-
|
|
128
|
-
{
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
</>
|
|
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>
|
|
133
139
|
)}
|
|
134
140
|
|
|
135
|
-
{
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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 && (
|
|
139
167
|
<Input.Root>
|
|
140
|
-
<Input.Label classNames='mbe-1 mbs-3'>Table</Input.Label>
|
|
141
|
-
<Select.Root
|
|
142
|
-
|
|
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' />
|
|
143
174
|
<Select.Portal>
|
|
144
175
|
<Select.Content>
|
|
145
176
|
<Select.Viewport>
|
|
146
177
|
{tableDefs
|
|
147
|
-
.
|
|
148
|
-
.map(({ id,
|
|
178
|
+
.find((tableDef) => tableDef.id === formState.refTable)
|
|
179
|
+
?.columns.map(({ id, label }) => (
|
|
149
180
|
<Select.Option key={id} value={id}>
|
|
150
|
-
{
|
|
181
|
+
{label ?? id}
|
|
151
182
|
</Select.Option>
|
|
152
183
|
))}
|
|
153
184
|
</Select.Viewport>
|
|
@@ -155,36 +186,13 @@ export const ColumnSettingsForm = ({
|
|
|
155
186
|
</Select.Portal>
|
|
156
187
|
</Select.Root>
|
|
157
188
|
</Input.Root>
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
<Input.Root>
|
|
161
|
-
<Input.Label classNames='mbe-1 mbs-3'>Table property</Input.Label>
|
|
162
|
-
<Select.Root value={refProp} onValueChange={setRefProp}>
|
|
163
|
-
<Select.TriggerButton placeholder='Property' classNames='is-full' />
|
|
164
|
-
<Select.Portal>
|
|
165
|
-
<Select.Content>
|
|
166
|
-
<Select.Viewport>
|
|
167
|
-
{tableDefs
|
|
168
|
-
.find((tableDef) => tableDef.id === refTable)
|
|
169
|
-
?.columns.map(({ id, label }) => (
|
|
170
|
-
<Select.Option key={id} value={id}>
|
|
171
|
-
{label ?? id}
|
|
172
|
-
</Select.Option>
|
|
173
|
-
))}
|
|
174
|
-
</Select.Viewport>
|
|
175
|
-
</Select.Content>
|
|
176
|
-
</Select.Portal>
|
|
177
|
-
</Select.Root>
|
|
178
|
-
</Input.Root>
|
|
179
|
-
)}
|
|
180
|
-
</Section>
|
|
181
|
-
</>
|
|
189
|
+
)}
|
|
190
|
+
</Section>
|
|
182
191
|
)}
|
|
183
192
|
|
|
184
193
|
<Separator classNames='mli-2' />
|
|
185
194
|
|
|
186
195
|
<Section className='space-b-1.5'>
|
|
187
|
-
{/* TODO(burdon): Style as DropdownMenuItem. */}
|
|
188
196
|
<Button variant='primary' classNames='is-full flex gap-2' onClick={handleSave}>
|
|
189
197
|
<span>{t('save label')}</span>
|
|
190
198
|
<div className='grow' />
|
|
@@ -7,10 +7,10 @@ import '@dxosTheme';
|
|
|
7
7
|
import { Plugs, PlugsConnected } from '@phosphor-icons/react';
|
|
8
8
|
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
9
9
|
|
|
10
|
-
import
|
|
10
|
+
import { create } from '@dxos/echo-schema/schema';
|
|
11
11
|
import { PublicKey } from '@dxos/keys';
|
|
12
12
|
import { faker } from '@dxos/random';
|
|
13
|
-
import { AnchoredOverflow, DensityProvider } from '@dxos/react-ui';
|
|
13
|
+
import { AnchoredOverflow, Button, DensityProvider } from '@dxos/react-ui';
|
|
14
14
|
import { withTheme } from '@dxos/storybook-utils';
|
|
15
15
|
import { range } from '@dxos/util';
|
|
16
16
|
|
|
@@ -35,7 +35,7 @@ faker.seed(911);
|
|
|
35
35
|
const createItems = (count: number) =>
|
|
36
36
|
range(count).map(
|
|
37
37
|
() =>
|
|
38
|
-
|
|
38
|
+
create<Item>({
|
|
39
39
|
publicKey: PublicKey.random(),
|
|
40
40
|
name: faker.commerce.productName(),
|
|
41
41
|
count: faker.datatype.boolean({ probability: 0.9 }) ? faker.number.int({ min: 0, max: 10_000 }) : undefined,
|
|
@@ -282,6 +282,61 @@ export const PinnedLastRow = {
|
|
|
282
282
|
},
|
|
283
283
|
};
|
|
284
284
|
|
|
285
|
+
export const InsertDelete = {
|
|
286
|
+
render: () => {
|
|
287
|
+
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
288
|
+
const [items, setItems] = useState<Item[]>(createItems(10));
|
|
289
|
+
|
|
290
|
+
const onUpdate: ValueUpdater<Item, any> = useCallback(
|
|
291
|
+
(item, prop, value) => setItems((items) => updateItems(items, item.publicKey, prop, value)),
|
|
292
|
+
[setItems],
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
const columns = useMemo(() => makeColumns(onUpdate), [onUpdate]);
|
|
296
|
+
|
|
297
|
+
const onInsertFirst = () => {
|
|
298
|
+
setItems((items) => [...createItems(1), ...items]);
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
const onInsertLast = () => {
|
|
302
|
+
setItems((items) => [...items, ...createItems(1)]);
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
const onDeleteFirst = () => {
|
|
306
|
+
setItems(([_first, ...rest]) => rest);
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const onDeleteLast = () => {
|
|
310
|
+
setItems((items) => [...items.slice(0, items.length - 1)]);
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
return (
|
|
314
|
+
<div>
|
|
315
|
+
<div className='flex flex-row gap-2'>
|
|
316
|
+
<Button onClick={onInsertFirst}>Insert first</Button>
|
|
317
|
+
<Button onClick={onInsertLast}>Insert last</Button>
|
|
318
|
+
<Button onClick={onDeleteFirst}>Delete first</Button>
|
|
319
|
+
<Button onClick={onDeleteLast}>Delete last</Button>
|
|
320
|
+
</div>
|
|
321
|
+
<div ref={containerRef} className='fixed inset-0 top-[72px] overflow-auto'>
|
|
322
|
+
<Table<Item>
|
|
323
|
+
role='grid'
|
|
324
|
+
rowsSelectable='multi'
|
|
325
|
+
keyAccessor={(row) => row.publicKey.toHex()}
|
|
326
|
+
columns={columns}
|
|
327
|
+
data={items}
|
|
328
|
+
fullWidth
|
|
329
|
+
stickyHeader
|
|
330
|
+
border
|
|
331
|
+
getScrollElement={() => containerRef.current}
|
|
332
|
+
pinLastRow
|
|
333
|
+
/>
|
|
334
|
+
</div>
|
|
335
|
+
</div>
|
|
336
|
+
);
|
|
337
|
+
},
|
|
338
|
+
};
|
|
339
|
+
|
|
285
340
|
export const Resizable = {
|
|
286
341
|
render: () => {
|
|
287
342
|
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
type RowSelectionState,
|
|
15
15
|
getGroupedRowModel,
|
|
16
16
|
getExpandedRowModel,
|
|
17
|
+
type Row,
|
|
17
18
|
} from '@tanstack/react-table';
|
|
18
19
|
import { useVirtualizer, type VirtualizerOptions } from '@tanstack/react-virtual';
|
|
19
20
|
import React, { Fragment, useCallback, useEffect, useState } from 'react';
|
|
@@ -26,8 +27,8 @@ import { type TypedTableProvider, TableProvider as UntypedTableProvider, useTabl
|
|
|
26
27
|
import { TableFooter } from './TableFooter';
|
|
27
28
|
import { TableHead } from './TableHead';
|
|
28
29
|
import { type TableProps } from './props';
|
|
30
|
+
import { usePinLastRow } from '../../hooks';
|
|
29
31
|
import { groupTh, tableRoot } from '../../theme';
|
|
30
|
-
import { usePinLastRow } from '../hooks/usePinLastRow';
|
|
31
32
|
|
|
32
33
|
export const Table = <TData extends RowData>(props: TableProps<TData>) => {
|
|
33
34
|
const {
|
|
@@ -177,12 +178,7 @@ export const Table = <TData extends RowData>(props: TableProps<TData>) => {
|
|
|
177
178
|
*/
|
|
178
179
|
const TableImpl = <TData extends RowData>(props: TableProps<TData>) => {
|
|
179
180
|
const { debug, classNames, getScrollElement, role, footer, grouping, fullWidth } = props;
|
|
180
|
-
const { table } = useTableContext();
|
|
181
|
-
|
|
182
|
-
const centerRows = table.getCenterRows();
|
|
183
|
-
const bottomRows = table.getBottomRows();
|
|
184
|
-
|
|
185
|
-
const rows = [...centerRows, ...bottomRows];
|
|
181
|
+
const { table } = useTableContext<TData>();
|
|
186
182
|
|
|
187
183
|
if (debug) {
|
|
188
184
|
return (
|
|
@@ -211,7 +207,7 @@ const TableImpl = <TData extends RowData>(props: TableProps<TData>) => {
|
|
|
211
207
|
<VirtualizedTableContent getScrollElement={getScrollElement} />
|
|
212
208
|
)
|
|
213
209
|
) : (
|
|
214
|
-
<
|
|
210
|
+
<UnvirtualizedTableContent />
|
|
215
211
|
)
|
|
216
212
|
) : (
|
|
217
213
|
<GroupedTableContent />
|
|
@@ -222,13 +218,32 @@ const TableImpl = <TData extends RowData>(props: TableProps<TData>) => {
|
|
|
222
218
|
);
|
|
223
219
|
};
|
|
224
220
|
|
|
221
|
+
const UnvirtualizedTableContent = () => {
|
|
222
|
+
const { table } = useTableContext();
|
|
223
|
+
|
|
224
|
+
const centerRows = table.getCenterRows();
|
|
225
|
+
const pinnedRows = table.getBottomRows();
|
|
226
|
+
|
|
227
|
+
const rows = [...centerRows, ...pinnedRows];
|
|
228
|
+
|
|
229
|
+
return <TableBody rows={rows} />;
|
|
230
|
+
};
|
|
231
|
+
|
|
225
232
|
const VirtualizedTableContent = ({
|
|
226
233
|
getScrollElement,
|
|
227
234
|
}: Pick<VirtualizerOptions<Element, Element>, 'getScrollElement'>) => {
|
|
228
235
|
const { table } = useTableContext();
|
|
229
236
|
|
|
230
237
|
const centerRows = table.getCenterRows();
|
|
231
|
-
|
|
238
|
+
let pinnedRows = [] as Row<unknown>[];
|
|
239
|
+
|
|
240
|
+
try {
|
|
241
|
+
// TODO(zan): Work out how to sync the row pinning with rendering
|
|
242
|
+
// This is a hack because the first call to this function after a row is deleted will throw an error
|
|
243
|
+
// because the row index is no longer in the table
|
|
244
|
+
// The pin last row hook updates the pinned row key after the row is deleted
|
|
245
|
+
pinnedRows = table.getBottomRows();
|
|
246
|
+
} catch (_) {} // Ignore error
|
|
232
247
|
|
|
233
248
|
const { getTotalSize, getVirtualItems } = useVirtualizer({
|
|
234
249
|
getScrollElement,
|
package/src/components/index.ts
CHANGED
|
@@ -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';
|
|
@@ -5,15 +5,15 @@
|
|
|
5
5
|
import '@dxosTheme';
|
|
6
6
|
|
|
7
7
|
import * as Arbitrary from '@effect/schema/Arbitrary';
|
|
8
|
-
import * as S from '@effect/schema/Schema';
|
|
9
8
|
import * as fc from 'fast-check';
|
|
10
9
|
import React, { useRef } from 'react';
|
|
11
10
|
|
|
11
|
+
import { S } from '@dxos/echo-schema';
|
|
12
12
|
import { DensityProvider } from '@dxos/react-ui';
|
|
13
13
|
import { withTheme } from '@dxos/storybook-utils';
|
|
14
14
|
|
|
15
15
|
import { schemaToColumnDefs } from './schemaToColumns';
|
|
16
|
-
import { Table } from '../
|
|
16
|
+
import { Table } from '../components';
|
|
17
17
|
|
|
18
18
|
export default {
|
|
19
19
|
title: 'react-ui-table/SchemaTable',
|
|
@@ -2,26 +2,54 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as AST from '@effect/schema/AST';
|
|
6
6
|
import type * as S from '@effect/schema/Schema';
|
|
7
7
|
|
|
8
|
-
import { type ColumnType } from '
|
|
8
|
+
import { type ColumnType } from './types';
|
|
9
9
|
|
|
10
10
|
export type ClassifiedColumnType = ColumnType | 'display';
|
|
11
11
|
|
|
12
|
+
// TODO(burdon): Rename getX? and return typed array?
|
|
13
|
+
export const classifySchemaProperties = (schema: S.Schema<any, any>): [string, ClassifiedColumnType][] => {
|
|
14
|
+
const recurse = (node: AST.AST, path: string[], acc: [string, ClassifiedColumnType][]) => {
|
|
15
|
+
const properties = AST.getPropertySignatures(node);
|
|
16
|
+
properties.forEach((prop) => {
|
|
17
|
+
const propName = prop.name.toString();
|
|
18
|
+
|
|
19
|
+
if (prop.isOptional) {
|
|
20
|
+
const unwrappedAst = unwrapOptionProperty(prop);
|
|
21
|
+
if (isStruct(unwrappedAst)) {
|
|
22
|
+
return recurse(unwrappedAst, [...path, propName], acc);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (isStruct(prop.type)) {
|
|
27
|
+
recurse(prop.type, [...path, propName], acc);
|
|
28
|
+
} else {
|
|
29
|
+
acc.push([path.concat(propName).join('.'), propertyToColumn(prop)]);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return acc;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
return recurse(schema.ast, [], []);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const isStruct = (node: AST.AST) => AST.isTypeLiteral(node);
|
|
40
|
+
|
|
12
41
|
const isOptionalUnion = (prop: AST.PropertySignature) => AST.isUnion(prop.type) && prop.isOptional;
|
|
13
42
|
|
|
14
43
|
const unwrapOptionProperty = (prop: AST.PropertySignature) => {
|
|
15
44
|
if (!isOptionalUnion(prop)) {
|
|
16
|
-
throw new Error(
|
|
45
|
+
throw new Error(`Not an optional property: ${String(prop.name)}`);
|
|
17
46
|
}
|
|
18
47
|
|
|
19
48
|
if (!AST.isUnion(prop.type)) {
|
|
20
|
-
throw new Error(
|
|
49
|
+
throw new Error(`Not a union type: ${String(prop.name)}`);
|
|
21
50
|
}
|
|
22
51
|
|
|
23
52
|
const [type, _undefinedCase] = prop.type.types;
|
|
24
|
-
|
|
25
53
|
return type;
|
|
26
54
|
};
|
|
27
55
|
|
|
@@ -44,7 +72,6 @@ const typeToColumn = (type: AST.AST): ClassifiedColumnType => {
|
|
|
44
72
|
|
|
45
73
|
if (AST.isTransform(type)) {
|
|
46
74
|
const identifier = AST.getIdentifierAnnotation(type);
|
|
47
|
-
|
|
48
75
|
if (identifier._tag === 'Some') {
|
|
49
76
|
if (identifier.value === 'DateFromString') {
|
|
50
77
|
return 'date';
|
|
@@ -55,42 +82,11 @@ const typeToColumn = (type: AST.AST): ClassifiedColumnType => {
|
|
|
55
82
|
return 'display';
|
|
56
83
|
};
|
|
57
84
|
|
|
58
|
-
const propertyToColumn = (
|
|
59
|
-
let type =
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
type = unwrapOptionProperty(property);
|
|
85
|
+
const propertyToColumn = (prop: AST.PropertySignature): ClassifiedColumnType => {
|
|
86
|
+
let type = prop.type;
|
|
87
|
+
if (prop.isOptional) {
|
|
88
|
+
type = unwrapOptionProperty(prop);
|
|
63
89
|
}
|
|
64
90
|
|
|
65
91
|
return typeToColumn(type);
|
|
66
92
|
};
|
|
67
|
-
|
|
68
|
-
const isStruct = (node: AST.AST) => AST.isTypeLiteral(node);
|
|
69
|
-
|
|
70
|
-
export const classifySchemaProperties = (schema: S.Schema<any, any>) => {
|
|
71
|
-
const recurse = (node: AST.AST, path: string[], acc: [string, ClassifiedColumnType][]) => {
|
|
72
|
-
const properties = AST.getPropertySignatures(node);
|
|
73
|
-
|
|
74
|
-
properties.forEach((prop) => {
|
|
75
|
-
const propName = prop.name.toString();
|
|
76
|
-
|
|
77
|
-
if (prop.isOptional) {
|
|
78
|
-
const unwrappedAst = unwrapOptionProperty(prop);
|
|
79
|
-
|
|
80
|
-
if (isStruct(unwrappedAst)) {
|
|
81
|
-
return recurse(unwrappedAst, [...path, propName], acc);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (isStruct(prop.type)) {
|
|
86
|
-
recurse(prop.type, [...path, propName], acc);
|
|
87
|
-
} else {
|
|
88
|
-
acc.push([path.concat(propName).join('.'), propertyToColumn(prop)]);
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
return acc;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
return recurse(schema.ast, [], []);
|
|
96
|
-
};
|