@dxos/react-ui-table 0.4.10-main.b3cf40d → 0.4.10-main.bb9f1bf
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 +601 -359
- 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 +31 -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/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.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/package.json +20 -16
- package/src/components/{ColumnMenu.stories.tsx → ColumnMenu/ColumnMenu.stories.tsx} +31 -1
- package/src/components/{ColumnMenu.tsx → ColumnMenu/ColumnMenu.tsx} +36 -34
- 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 +162 -3
- package/src/components/Table/Table.tsx +35 -60
- package/src/components/index.ts +1 -1
- package/src/helpers.tsx +31 -7
- package/src/hooks/index.ts +8 -0
- package/src/hooks/useColumnSizing.ts +45 -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 +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/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/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/src/{components/hooks → hooks}/useColumnSorting.ts +0 -0
- /package/src/{schema.ts → schema/types.ts} +0 -0
|
@@ -10,8 +10,8 @@ import { Button, DensityProvider, Popover, DropdownMenu } from '@dxos/react-ui';
|
|
|
10
10
|
import { getSize, mx } from '@dxos/react-ui-theme';
|
|
11
11
|
|
|
12
12
|
import { ColumnSettingsForm } from './ColumnSettingsForm';
|
|
13
|
-
import { useColumnSorting } from '
|
|
14
|
-
import { type TableDef, type ColumnProps } from '
|
|
13
|
+
import { useColumnSorting } from '../../hooks';
|
|
14
|
+
import { type TableDef, type ColumnProps } from '../../schema';
|
|
15
15
|
|
|
16
16
|
export type ColumnMenuProps<TData extends RowData, TValue> = {
|
|
17
17
|
context: HeaderContext<TData, TValue>;
|
|
@@ -66,41 +66,43 @@ export const ColumnMenu = <TData extends RowData, TValue>({ column, ...props }:
|
|
|
66
66
|
</Popover.Anchor>
|
|
67
67
|
</DropdownMenu.Trigger>
|
|
68
68
|
|
|
69
|
-
<DropdownMenu.
|
|
70
|
-
<DropdownMenu.
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
<
|
|
75
|
-
|
|
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>
|
|
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>
|
|
88
76
|
<span className='opacity-50'>
|
|
89
|
-
<
|
|
77
|
+
<ArrowUp className={getSize(4)} />
|
|
90
78
|
</span>
|
|
91
79
|
</DropdownMenu.Item>
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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>
|
|
104
106
|
|
|
105
107
|
<ColumnSettingsPanel {...props} column={column} onClose={() => setIsColumnSettingsOpen(false)} />
|
|
106
108
|
</Popover.Root>
|
|
@@ -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,11 @@ 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';
|
|
11
|
+
import { registerSignalRuntime } from '@dxos/echo-signals/react';
|
|
11
12
|
import { PublicKey } from '@dxos/keys';
|
|
12
13
|
import { faker } from '@dxos/random';
|
|
13
|
-
import { AnchoredOverflow, DensityProvider } from '@dxos/react-ui';
|
|
14
|
+
import { AnchoredOverflow, Button, DensityProvider } from '@dxos/react-ui';
|
|
14
15
|
import { withTheme } from '@dxos/storybook-utils';
|
|
15
16
|
import { range } from '@dxos/util';
|
|
16
17
|
|
|
@@ -35,7 +36,7 @@ faker.seed(911);
|
|
|
35
36
|
const createItems = (count: number) =>
|
|
36
37
|
range(count).map(
|
|
37
38
|
() =>
|
|
38
|
-
|
|
39
|
+
create<Item>({
|
|
39
40
|
publicKey: PublicKey.random(),
|
|
40
41
|
name: faker.commerce.productName(),
|
|
41
42
|
count: faker.datatype.boolean({ probability: 0.9 }) ? faker.number.int({ min: 0, max: 10_000 }) : undefined,
|
|
@@ -282,6 +283,61 @@ export const PinnedLastRow = {
|
|
|
282
283
|
},
|
|
283
284
|
};
|
|
284
285
|
|
|
286
|
+
export const InsertDelete = {
|
|
287
|
+
render: () => {
|
|
288
|
+
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
289
|
+
const [items, setItems] = useState<Item[]>(createItems(10));
|
|
290
|
+
|
|
291
|
+
const onUpdate: ValueUpdater<Item, any> = useCallback(
|
|
292
|
+
(item, prop, value) => setItems((items) => updateItems(items, item.publicKey, prop, value)),
|
|
293
|
+
[setItems],
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
const columns = useMemo(() => makeColumns(onUpdate), [onUpdate]);
|
|
297
|
+
|
|
298
|
+
const onInsertFirst = () => {
|
|
299
|
+
setItems((items) => [...createItems(1), ...items]);
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
const onInsertLast = () => {
|
|
303
|
+
setItems((items) => [...items, ...createItems(1)]);
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const onDeleteFirst = () => {
|
|
307
|
+
setItems(([_first, ...rest]) => rest);
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
const onDeleteLast = () => {
|
|
311
|
+
setItems((items) => [...items.slice(0, items.length - 1)]);
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
return (
|
|
315
|
+
<div>
|
|
316
|
+
<div className='flex flex-row gap-2'>
|
|
317
|
+
<Button onClick={onInsertFirst}>Insert first</Button>
|
|
318
|
+
<Button onClick={onInsertLast}>Insert last</Button>
|
|
319
|
+
<Button onClick={onDeleteFirst}>Delete first</Button>
|
|
320
|
+
<Button onClick={onDeleteLast}>Delete last</Button>
|
|
321
|
+
</div>
|
|
322
|
+
<div ref={containerRef} className='fixed inset-0 top-[72px] overflow-auto'>
|
|
323
|
+
<Table<Item>
|
|
324
|
+
role='grid'
|
|
325
|
+
rowsSelectable='multi'
|
|
326
|
+
keyAccessor={(row) => row.publicKey.toHex()}
|
|
327
|
+
columns={columns}
|
|
328
|
+
data={items}
|
|
329
|
+
fullWidth
|
|
330
|
+
stickyHeader
|
|
331
|
+
border
|
|
332
|
+
getScrollElement={() => containerRef.current}
|
|
333
|
+
pinLastRow
|
|
334
|
+
/>
|
|
335
|
+
</div>
|
|
336
|
+
</div>
|
|
337
|
+
);
|
|
338
|
+
},
|
|
339
|
+
};
|
|
340
|
+
|
|
285
341
|
export const Resizable = {
|
|
286
342
|
render: () => {
|
|
287
343
|
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
@@ -338,3 +394,106 @@ export const TenThousandRows = {
|
|
|
338
394
|
);
|
|
339
395
|
},
|
|
340
396
|
};
|
|
397
|
+
|
|
398
|
+
registerSignalRuntime();
|
|
399
|
+
const state = create({ items: createItems(10) });
|
|
400
|
+
|
|
401
|
+
export const RealTimeUpdates = {
|
|
402
|
+
args: {
|
|
403
|
+
periodicMutations: true,
|
|
404
|
+
mutationInterval: 1000,
|
|
405
|
+
periodicDeletions: false,
|
|
406
|
+
deletionInterval: 5000,
|
|
407
|
+
periodicInsertions: false,
|
|
408
|
+
insertionInterval: 3000,
|
|
409
|
+
},
|
|
410
|
+
argTypes: {
|
|
411
|
+
mutationInterval: { control: 'number' },
|
|
412
|
+
periodicDeletions: { control: 'boolean' },
|
|
413
|
+
deletionInterval: { control: 'number' },
|
|
414
|
+
periodicInsertions: { control: 'boolean' },
|
|
415
|
+
insertionInterval: { control: 'number' },
|
|
416
|
+
},
|
|
417
|
+
render: ({
|
|
418
|
+
periodicMutations,
|
|
419
|
+
mutationInterval,
|
|
420
|
+
periodicDeletions,
|
|
421
|
+
deletionInterval,
|
|
422
|
+
periodicInsertions,
|
|
423
|
+
insertionInterval,
|
|
424
|
+
}: any) => {
|
|
425
|
+
useEffect(() => {
|
|
426
|
+
if (!periodicMutations) {
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const interval = setInterval(() => {
|
|
431
|
+
if (state.items.length === 0) {
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const randomIndex = Math.floor(Math.random() * state.items.length);
|
|
436
|
+
console.log('Mutating row', randomIndex);
|
|
437
|
+
state.items[randomIndex].name = faker.commerce.productName();
|
|
438
|
+
state.items[randomIndex].count = Math.floor(Math.random() * 1000);
|
|
439
|
+
state.items[randomIndex].started = new Date();
|
|
440
|
+
}, mutationInterval);
|
|
441
|
+
|
|
442
|
+
return () => clearInterval(interval);
|
|
443
|
+
}, [periodicMutations, mutationInterval, state.items]);
|
|
444
|
+
|
|
445
|
+
useEffect(() => {
|
|
446
|
+
if (!periodicInsertions) {
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
const interval = setInterval(() => {
|
|
451
|
+
console.log('Inserting...');
|
|
452
|
+
state.items.push(createItems(1)[0]);
|
|
453
|
+
}, insertionInterval);
|
|
454
|
+
|
|
455
|
+
return () => clearInterval(interval);
|
|
456
|
+
}, [periodicInsertions, insertionInterval, state.items]);
|
|
457
|
+
|
|
458
|
+
useEffect(() => {
|
|
459
|
+
if (!periodicDeletions) {
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const interval = setInterval(() => {
|
|
464
|
+
if (state.items.length === 0) {
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
console.log('Deleting');
|
|
469
|
+
|
|
470
|
+
// Randomly delete a row from state.items
|
|
471
|
+
const randomIndex = Math.floor(Math.random() * state.items.length);
|
|
472
|
+
state.items.splice(randomIndex, 1);
|
|
473
|
+
}, deletionInterval);
|
|
474
|
+
|
|
475
|
+
return () => clearInterval(interval);
|
|
476
|
+
}, [periodicDeletions, deletionInterval, state.items]);
|
|
477
|
+
|
|
478
|
+
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
479
|
+
|
|
480
|
+
const onUpdate = useCallback((...args: any[]) => {}, []);
|
|
481
|
+
const columns = useMemo(() => makeColumns(onUpdate), []);
|
|
482
|
+
|
|
483
|
+
return (
|
|
484
|
+
<div ref={containerRef} className='fixed inset-0 overflow-auto'>
|
|
485
|
+
<Table<Item>
|
|
486
|
+
role='grid'
|
|
487
|
+
rowsSelectable='multi'
|
|
488
|
+
keyAccessor={(row) => row.publicKey.toHex()}
|
|
489
|
+
columns={columns}
|
|
490
|
+
data={state.items}
|
|
491
|
+
fullWidth
|
|
492
|
+
stickyHeader
|
|
493
|
+
border
|
|
494
|
+
getScrollElement={() => containerRef.current}
|
|
495
|
+
/>
|
|
496
|
+
</div>
|
|
497
|
+
);
|
|
498
|
+
},
|
|
499
|
+
};
|