@dxos/react-ui-table 0.4.9 → 0.4.10-main.0be5154

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.
Files changed (33) hide show
  1. package/dist/lib/browser/index.mjs +323 -183
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/types/src/components/ColumnMenu.d.ts +8 -2
  5. package/dist/types/src/components/ColumnMenu.d.ts.map +1 -1
  6. package/dist/types/src/components/ColumnMenu.stories.d.ts +4 -0
  7. package/dist/types/src/components/ColumnMenu.stories.d.ts.map +1 -1
  8. package/dist/types/src/components/ColumnSettingsForm.d.ts +10 -0
  9. package/dist/types/src/components/ColumnSettingsForm.d.ts.map +1 -0
  10. package/dist/types/src/components/Table/Table.d.ts.map +1 -1
  11. package/dist/types/src/components/Table/Table.stories.d.ts +3 -0
  12. package/dist/types/src/components/Table/Table.stories.d.ts.map +1 -1
  13. package/dist/types/src/components/Table/TableBody.d.ts.map +1 -1
  14. package/dist/types/src/components/hooks/useColumnSorting.d.ts +9 -0
  15. package/dist/types/src/components/hooks/useColumnSorting.d.ts.map +1 -0
  16. package/dist/types/src/components/hooks/usePinLastRow.d.ts +3 -0
  17. package/dist/types/src/components/hooks/usePinLastRow.d.ts.map +1 -0
  18. package/dist/types/src/theme.d.ts +1 -0
  19. package/dist/types/src/theme.d.ts.map +1 -1
  20. package/package.json +14 -14
  21. package/src/components/ColumnMenu.stories.tsx +52 -1
  22. package/src/components/ColumnMenu.tsx +129 -211
  23. package/src/components/ColumnSettingsForm.tsx +206 -0
  24. package/src/components/Table/Table.stories.tsx +31 -0
  25. package/src/components/Table/Table.tsx +6 -15
  26. package/src/components/Table/TableBody.tsx +6 -1
  27. package/src/components/hooks/useColumnSorting.ts +54 -0
  28. package/src/components/hooks/usePinLastRow.ts +43 -0
  29. package/src/helpers.tsx +1 -1
  30. package/src/theme.ts +3 -2
  31. package/dist/types/src/components/SortButton.d.ts +0 -8
  32. package/dist/types/src/components/SortButton.d.ts.map +0 -1
  33. package/src/components/SortButton.tsx +0 -34
@@ -1,18 +1,17 @@
1
1
  //
2
- // Copyright 2023 DXOS.org
2
+ // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { Check, Trash, X, GearSix } from '@phosphor-icons/react';
6
- import { type HeaderContext, type RowData } from '@tanstack/react-table';
7
- import React, { type FC, type PropsWithChildren, useRef, useState } from 'react';
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
8
 
9
- import { Button, DensityProvider, Input, Popover, Select, Separator, useTranslation } from '@dxos/react-ui';
9
+ import { Button, DensityProvider, Popover, DropdownMenu } from '@dxos/react-ui';
10
10
  import { getSize, mx } from '@dxos/react-ui-theme';
11
- import { safeParseInt } from '@dxos/util';
12
11
 
13
- import { SortButton } from './SortButton';
14
- import { type TableDef, type ColumnProps, type ColumnType } from '../schema';
15
- import { translationKey } from '../translations';
12
+ import { ColumnSettingsForm } from './ColumnSettingsForm';
13
+ import { useColumnSorting } from './hooks/useColumnSorting';
14
+ import { type TableDef, type ColumnProps } from '../schema';
16
15
 
17
16
  export type ColumnMenuProps<TData extends RowData, TValue> = {
18
17
  context: HeaderContext<TData, TValue>;
@@ -25,224 +24,143 @@ export type ColumnMenuProps<TData extends RowData, TValue> = {
25
24
 
26
25
  export const ColumnMenu = <TData extends RowData, TValue>({ column, ...props }: ColumnMenuProps<TData, TValue>) => {
27
26
  const title = column.label?.length ? column.label : column.id;
28
-
29
27
  const header = props.context.header;
30
28
 
31
- const canSort = header.column.getCanSort();
32
- const sortDirection = header.column.getIsSorted();
33
- const toggleSort = header.column.getToggleSortingHandler();
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
+ );
34
51
 
35
52
  return (
36
- <div className='flex items-center gap-2'>
37
- <div className='flex-1 min-is-0 truncate' title={title}>
38
- {title}
39
- </div>
40
- <div className='flex gap-0'>
41
- {canSort && toggleSort && <SortButton sortDirection={sortDirection} onClick={toggleSort} />}
42
- <ColumnPanel {...props} column={column} />
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>
43
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>
44
108
  </div>
45
109
  );
46
110
  };
47
111
 
48
- const Section: FC<PropsWithChildren & { className?: string }> = ({ children, className }) => (
49
- <div role='none' className={mx('p-2', className)}>
50
- {children}
51
- </div>
52
- );
53
-
54
- export const ColumnPanel = <TData extends RowData, TValue>({
55
- context,
112
+ export const ColumnSettingsPanel = <TData extends RowData, TValue>({
56
113
  tableDefs,
57
114
  tableDef,
58
115
  column,
59
116
  onUpdate,
60
117
  onDelete,
61
- }: ColumnMenuProps<TData, TValue>) => {
62
- const [open, setOpen] = useState(false);
63
- const [prop, setProp] = useState(column.id);
64
- const [refTable, setRefTable] = useState(column.refTable);
65
- const [refProp, setRefProp] = useState(column.refProp);
66
- const [type, setType] = useState(String(column.type));
67
- const [label, setLabel] = useState(column.label);
68
- const [digits, setDigits] = useState(String(column.digits ?? '0'));
69
- const propRef = useRef<HTMLInputElement>(null);
70
- const { t } = useTranslation(translationKey);
71
-
72
- const handleCancel = () => {
73
- setProp(column.id);
74
- setOpen(false);
75
- };
76
-
77
- const handleSave = () => {
78
- // Check valid.
79
- if (!prop.length || !prop.match(/^[a-zA-Z_].+/i)) {
80
- propRef.current?.focus();
81
- return;
82
- }
83
-
84
- // If already exists then check same type.
85
- const current = tableDef.columns.find((c) => c.id !== column.id && c.id === prop);
86
- if (current) {
87
- // TODO(burdon): Allow multiple columns with different refProps. Ensure correct table prop is updated.
88
- if (current.type !== 'ref' /* || current.ref !== refTable */) {
89
- // TODO(burdon): Show error.
90
- propRef.current?.focus();
91
- return;
92
- }
93
- }
94
-
95
- onUpdate?.(prop, {
96
- id: prop, // TODO(burdon): Make unique.
97
- prop,
98
- label,
99
- type: type as ColumnProps['type'],
100
- refTable,
101
- refProp,
102
- digits: safeParseInt(digits),
103
- });
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
+ );
104
138
 
105
- setOpen(false);
106
- };
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
+ }
107
160
 
108
161
  return (
109
- <Popover.Root open={open} onOpenChange={(nextOpen) => setOpen(nextOpen)}>
110
- <Popover.Trigger asChild>
111
- <Button variant='ghost'>
112
- <GearSix className={mx(getSize(4), 'rotate-90')} />
113
- </Button>
114
- </Popover.Trigger>
115
- <Popover.Portal>
116
- <Popover.Content>
117
- <Popover.Viewport classNames='w-60'>
118
- <DensityProvider density='fine'>
119
- <div className='flex flex-col gap-1'>
120
- <Section>
121
- <Input.Root>
122
- <Input.Label classNames='mbe-1'>{t('column label label')}</Input.Label>
123
- <Input.TextInput
124
- placeholder='Column label'
125
- value={label ?? ''}
126
- onChange={(event) => setLabel(event.target.value)}
127
- autoFocus
128
- />
129
- </Input.Root>
130
- <Input.Root>
131
- <Input.Label classNames='mbe-1 mbs-3'>{t('property key label')}</Input.Label>
132
- <Input.TextInput
133
- ref={propRef}
134
- placeholder='Property key'
135
- // TODO(burdon): Provide hooks for value normalization, ENTER, ESC, etc.
136
- value={prop ?? ''}
137
- onChange={(event) => setProp(event.target.value.replace(/[^\w_]/g, ''))}
138
- />
139
- </Input.Root>
140
- <Input.Root>
141
- <Input.Label classNames='mbe-1 mbs-3'>{t('column type label')}</Input.Label>
142
- <Select.Root value={type} onValueChange={setType}>
143
- <Select.TriggerButton placeholder='Type' classNames='is-full' />
144
- <Select.Portal>
145
- <Select.Content>
146
- <Select.Viewport>
147
- {(['number', 'boolean', 'string', 'ref'] as ColumnType[]).map((type) => (
148
- <Select.Option key={type} value={type}>
149
- {t(`${type} column type label`)}
150
- </Select.Option>
151
- ))}
152
- </Select.Viewport>
153
- </Select.Content>
154
- </Select.Portal>
155
- </Select.Root>
156
- </Input.Root>
157
- </Section>
158
-
159
- {type === 'number' && (
160
- <>
161
- <Section>
162
- <Input.Root>
163
- <Input.Label classNames='mbe-1 mbs-3'>{t('digits label')}</Input.Label>
164
- {/* TODO(burdon): Constrain input to numbers. */}
165
- <Input.TextInput value={digits ?? ''} onChange={(event) => setDigits(event.target.value)} />
166
- </Input.Root>
167
- </Section>
168
- </>
169
- )}
170
-
171
- {/* TODO(burdon): Selectors on same line. */}
172
- {type === 'ref' && (
173
- <>
174
- <Section>
175
- <Input.Root>
176
- <Input.Label classNames='mbe-1 mbs-3'>Table</Input.Label>
177
- <Select.Root value={refTable} onValueChange={setRefTable}>
178
- <Select.TriggerButton placeholder='Table' classNames='is-full' />
179
- <Select.Portal>
180
- <Select.Content>
181
- <Select.Viewport>
182
- {tableDefs
183
- .filter((t) => t.id !== tableDef.id)
184
- .map(({ id, name }) => (
185
- <Select.Option key={id} value={id}>
186
- {name ?? id}
187
- </Select.Option>
188
- ))}
189
- </Select.Viewport>
190
- </Select.Content>
191
- </Select.Portal>
192
- </Select.Root>
193
- </Input.Root>
194
-
195
- {refTable && (
196
- <Input.Root>
197
- <Input.Label classNames='mbe-1 mbs-3'>Table property</Input.Label>
198
- <Select.Root value={refProp} onValueChange={setRefProp}>
199
- <Select.TriggerButton placeholder='Property' classNames='is-full' />
200
- <Select.Portal>
201
- <Select.Content>
202
- <Select.Viewport>
203
- {tableDefs
204
- .find((tableDef) => tableDef.id === refTable)
205
- ?.columns.map(({ id, label }) => (
206
- <Select.Option key={id} value={id}>
207
- {label ?? id}
208
- </Select.Option>
209
- ))}
210
- </Select.Viewport>
211
- </Select.Content>
212
- </Select.Portal>
213
- </Select.Root>
214
- </Input.Root>
215
- )}
216
- </Section>
217
- </>
218
- )}
219
-
220
- <Separator classNames='mli-2' />
221
-
222
- <Section className='space-b-1.5'>
223
- {/* TODO(burdon): Style as DropdownMenuItem. */}
224
- <Button variant='primary' classNames='is-full flex gap-2' onClick={handleSave}>
225
- <span>{t('save label')}</span>
226
- <div className='grow' />
227
- <Check className={getSize(5)} />
228
- </Button>
229
- <Button classNames='is-full flex gap-2' onClick={handleCancel}>
230
- <span>{t('cancel label', { ns: 'os' })}</span>
231
- <div className='grow' />
232
- <X className={getSize(5)} />
233
- </Button>
234
- <Button classNames='is-full flex gap-2' onClick={() => onDelete?.(column.id)}>
235
- <span>{t('delete label')}</span>
236
- <div className='grow' />
237
- <Trash className={getSize(5)} />
238
- </Button>
239
- </Section>
240
- </div>
241
- </DensityProvider>
242
- </Popover.Viewport>
243
- <Popover.Arrow />
244
- </Popover.Content>
245
- </Popover.Portal>
246
- </Popover.Root>
162
+ <div onClick={onClick} className='flex items-center cursor-pointer'>
163
+ {icon}
164
+ </div>
247
165
  );
248
166
  };
@@ -0,0 +1,206 @@
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 const ColumnSettingsForm = ({
22
+ column,
23
+ tableDef,
24
+ tableDefs,
25
+ onUpdate,
26
+ onDelete,
27
+ onClose,
28
+ }: {
29
+ column: ColumnProps;
30
+ tableDef: TableDef;
31
+ tableDefs: TableDef[];
32
+ onUpdate?: ((id: string, column: ColumnProps) => void) | undefined;
33
+ onDelete?: ((id: string) => void) | undefined;
34
+ onClose: () => void;
35
+ }) => {
36
+ const [prop, setProp] = useState(column.id);
37
+ const [refTable, setRefTable] = useState(column.refTable);
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'));
42
+ const propRef = useRef<HTMLInputElement>(null);
43
+ const { t } = useTranslation(translationKey);
44
+
45
+ const handleCancel = () => {
46
+ setProp(column.id);
47
+ onClose;
48
+ };
49
+
50
+ const handleSave = () => {
51
+ // Check valid.
52
+ if (!prop.length || !prop.match(/^[a-zA-Z_].+/i)) {
53
+ propRef.current?.focus();
54
+ return;
55
+ }
56
+
57
+ // If already exists then check same type.
58
+ const current = tableDef.columns.find((c) => c.id !== column.id && c.id === prop);
59
+
60
+ if (current) {
61
+ // TODO(burdon): Allow multiple columns with different refProps. Ensure correct table prop is updated.
62
+ if (current.type !== 'ref' /* || current.ref !== refTable */) {
63
+ // TODO(burdon): Show error.
64
+ propRef.current?.focus();
65
+ return;
66
+ }
67
+ }
68
+
69
+ onUpdate?.(prop, {
70
+ id: prop, // TODO(burdon): Make unique.
71
+ prop,
72
+ label,
73
+ type: type as ColumnProps['type'],
74
+ refTable,
75
+ refProp,
76
+ digits: safeParseInt(digits),
77
+ });
78
+
79
+ onClose();
80
+ };
81
+
82
+ return (
83
+ <div className='flex flex-col gap-1'>
84
+ <Section>
85
+ <Input.Root>
86
+ <Input.Label classNames='mbe-1'>{t('column label label')}</Input.Label>
87
+ <Input.TextInput
88
+ placeholder='Column label'
89
+ value={label ?? ''}
90
+ onChange={(event) => setLabel(event.target.value)}
91
+ autoFocus
92
+ />
93
+ </Input.Root>
94
+ <Input.Root>
95
+ <Input.Label classNames='mbe-1 mbs-3'>{t('property key label')}</Input.Label>
96
+ <Input.TextInput
97
+ ref={propRef}
98
+ placeholder='Property key'
99
+ // TODO(burdon): Provide hooks for value normalization, ENTER, ESC, etc.
100
+ value={prop ?? ''}
101
+ onChange={(event) => setProp(event.target.value.replace(/[^\w_]/g, ''))}
102
+ />
103
+ </Input.Root>
104
+ <Input.Root>
105
+ <Input.Label classNames='mbe-1 mbs-3'>{t('column type label')}</Input.Label>
106
+ <Select.Root value={type} onValueChange={setType}>
107
+ <Select.TriggerButton placeholder='Type' classNames='is-full' />
108
+ <Select.Portal>
109
+ <Select.Content>
110
+ <Select.Viewport>
111
+ {(['number', 'boolean', 'string', 'ref'] as ColumnType[]).map((type) => (
112
+ <Select.Option key={type} value={type}>
113
+ {t(`${type} column type label`)}
114
+ </Select.Option>
115
+ ))}
116
+ </Select.Viewport>
117
+ </Select.Content>
118
+ </Select.Portal>
119
+ </Select.Root>
120
+ </Input.Root>
121
+ </Section>
122
+
123
+ {type === 'number' && (
124
+ <>
125
+ <Section>
126
+ <Input.Root>
127
+ <Input.Label classNames='mbe-1 mbs-3'>{t('digits label')}</Input.Label>
128
+ {/* TODO(burdon): Constrain input to numbers. */}
129
+ <Input.TextInput value={digits ?? ''} onChange={(event) => setDigits(event.target.value)} />
130
+ </Input.Root>
131
+ </Section>
132
+ </>
133
+ )}
134
+
135
+ {/* TODO(burdon): Selectors on same line. */}
136
+ {type === 'ref' && (
137
+ <>
138
+ <Section>
139
+ <Input.Root>
140
+ <Input.Label classNames='mbe-1 mbs-3'>Table</Input.Label>
141
+ <Select.Root value={refTable} onValueChange={setRefTable}>
142
+ <Select.TriggerButton placeholder='Table' classNames='is-full' />
143
+ <Select.Portal>
144
+ <Select.Content>
145
+ <Select.Viewport>
146
+ {tableDefs
147
+ .filter((t) => t.id !== tableDef.id)
148
+ .map(({ id, name }) => (
149
+ <Select.Option key={id} value={id}>
150
+ {name ?? id}
151
+ </Select.Option>
152
+ ))}
153
+ </Select.Viewport>
154
+ </Select.Content>
155
+ </Select.Portal>
156
+ </Select.Root>
157
+ </Input.Root>
158
+
159
+ {refTable && (
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
+ </>
182
+ )}
183
+
184
+ <Separator classNames='mli-2' />
185
+
186
+ <Section className='space-b-1.5'>
187
+ {/* TODO(burdon): Style as DropdownMenuItem. */}
188
+ <Button variant='primary' classNames='is-full flex gap-2' onClick={handleSave}>
189
+ <span>{t('save label')}</span>
190
+ <div className='grow' />
191
+ <Check className={getSize(5)} />
192
+ </Button>
193
+ <Button classNames='is-full flex gap-2' onClick={handleCancel}>
194
+ <span>{t('cancel label', { ns: 'os' })}</span>
195
+ <div className='grow' />
196
+ <X className={getSize(5)} />
197
+ </Button>
198
+ <Button classNames='is-full flex gap-2' onClick={() => onDelete?.(column.id)}>
199
+ <span>{t('delete label')}</span>
200
+ <div className='grow' />
201
+ <Trash className={getSize(5)} />
202
+ </Button>
203
+ </Section>
204
+ </div>
205
+ );
206
+ };
@@ -251,6 +251,37 @@ export const Editable = {
251
251
  },
252
252
  };
253
253
 
254
+ export const PinnedLastRow = {
255
+ render: () => {
256
+ const containerRef = useRef<HTMLDivElement | null>(null);
257
+ const [items, setItems] = useState<Item[]>(createItems(200));
258
+
259
+ const onUpdate: ValueUpdater<Item, any> = useCallback(
260
+ (item, prop, value) => setItems((items) => updateItems(items, item.publicKey, prop, value)),
261
+ [setItems],
262
+ );
263
+
264
+ const columns = useMemo(() => makeColumns(onUpdate), [onUpdate]);
265
+
266
+ return (
267
+ <div ref={containerRef} className='fixed inset-0 overflow-auto'>
268
+ <Table<Item>
269
+ role='grid'
270
+ rowsSelectable='multi'
271
+ keyAccessor={(row) => row.publicKey.toHex()}
272
+ columns={columns}
273
+ data={items}
274
+ fullWidth
275
+ stickyHeader
276
+ border
277
+ getScrollElement={() => containerRef.current}
278
+ pinLastRow
279
+ />
280
+ </div>
281
+ );
282
+ },
283
+ };
284
+
254
285
  export const Resizable = {
255
286
  render: () => {
256
287
  const containerRef = useRef<HTMLDivElement | null>(null);
@@ -27,6 +27,7 @@ import { TableFooter } from './TableFooter';
27
27
  import { TableHead } from './TableHead';
28
28
  import { type TableProps } from './props';
29
29
  import { groupTh, tableRoot } from '../../theme';
30
+ import { usePinLastRow } from '../hooks/usePinLastRow';
30
31
 
31
32
  export const Table = <TData extends RowData>(props: TableProps<TData>) => {
32
33
  const {
@@ -149,17 +150,7 @@ export const Table = <TData extends RowData>(props: TableProps<TData>) => {
149
150
  onDataSelectionChange?.(Object.keys(rowSelection).map((id) => table.getRowModel().rowsById[id].original));
150
151
  }, [onDataSelectionChange, rowSelection, table]);
151
152
 
152
- useEffect(() => {
153
- if (!pinLastRow) {
154
- return;
155
- }
156
-
157
- // Clear row pinning
158
- table.resetRowPinning();
159
-
160
- const rows = table.getRowModel().rows;
161
- rows[rows.length - 1].pin('bottom');
162
- }, [pinLastRow, table, data]);
153
+ usePinLastRow(pinLastRow, table, data, getScrollElement);
163
154
 
164
155
  // Create additional expansion column if all columns have fixed width.
165
156
  const expand = false; // columns.map((column) => column.size).filter(Boolean).length === columns?.length;
@@ -239,11 +230,9 @@ const VirtualizedTableContent = ({
239
230
  const centerRows = table.getCenterRows();
240
231
  const pinnedRows = table.getBottomRows();
241
232
 
242
- const rows = [...centerRows, ...pinnedRows];
243
-
244
233
  const { getTotalSize, getVirtualItems } = useVirtualizer({
245
234
  getScrollElement,
246
- count: rows.length,
235
+ count: centerRows.length,
247
236
  overscan: 8,
248
237
  estimateSize: () => 40,
249
238
  });
@@ -254,6 +243,8 @@ const VirtualizedTableContent = ({
254
243
  const paddingTop = virtualRows.length > 0 ? virtualRows?.[0]?.start || 0 : 0;
255
244
  const paddingBottom = virtualRows.length > 0 ? totalSize - (virtualRows?.[virtualRows.length - 1]?.end || 0) : 0;
256
245
 
246
+ const rowsToRender = [...virtualRows.map((virtualRow) => centerRows[virtualRow.index]), ...pinnedRows];
247
+
257
248
  return (
258
249
  <>
259
250
  {paddingTop > 0 && (
@@ -263,7 +254,7 @@ const VirtualizedTableContent = ({
263
254
  </tr>
264
255
  </tbody>
265
256
  )}
266
- <TableBody rows={virtualRows.map((virtualRow) => rows[virtualRow.index])} />
257
+ <TableBody rows={rowsToRender} />
267
258
  {paddingBottom > 0 && (
268
259
  <tbody role='none'>
269
260
  <tr role='none'>