@dxos/react-ui-table 0.3.9-main.b7e6a67 → 0.3.9-main.c7cd0ec

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 +222 -156
  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/Cell/CellCombobox.d.ts.map +1 -1
  5. package/dist/types/src/components/ColumnMenu.d.ts.map +1 -1
  6. package/dist/types/src/components/ColumnMenu.stories.d.ts +1 -0
  7. package/dist/types/src/components/ColumnMenu.stories.d.ts.map +1 -1
  8. package/dist/types/src/components/Table/Table.d.ts.map +1 -1
  9. package/dist/types/src/components/Table/Table.stories.d.ts +1 -1
  10. package/dist/types/src/components/Table/Table.stories.d.ts.map +1 -1
  11. package/dist/types/src/components/Table/props.d.ts +6 -2
  12. package/dist/types/src/components/Table/props.d.ts.map +1 -1
  13. package/dist/types/src/helpers.d.ts +2 -2
  14. package/dist/types/src/helpers.d.ts.map +1 -1
  15. package/dist/types/src/index.d.ts +1 -0
  16. package/dist/types/src/index.d.ts.map +1 -1
  17. package/dist/types/src/schema.d.ts +0 -1
  18. package/dist/types/src/schema.d.ts.map +1 -1
  19. package/dist/types/src/theme.d.ts.map +1 -1
  20. package/dist/types/src/translations.d.ts +11 -0
  21. package/dist/types/src/translations.d.ts.map +1 -1
  22. package/package.json +12 -16
  23. package/src/components/Cell/CellCombobox.tsx +4 -1
  24. package/src/components/ColumnMenu.stories.tsx +3 -0
  25. package/src/components/ColumnMenu.tsx +16 -16
  26. package/src/components/Table/Table.stories.tsx +23 -12
  27. package/src/components/Table/Table.tsx +92 -26
  28. package/src/components/Table/props.ts +26 -14
  29. package/src/helpers.tsx +94 -75
  30. package/src/index.ts +1 -0
  31. package/src/schema.ts +0 -8
  32. package/src/theme.ts +2 -2
  33. package/src/translations.ts +11 -0
@@ -3,6 +3,9 @@
3
3
  //
4
4
 
5
5
  import { type RowData, type RowSelectionState, type Table, type VisibilityState } from '@tanstack/react-table';
6
+ import { type VirtualizerOptions } from '@tanstack/react-virtual';
7
+
8
+ import { type ClassNameValue } from '@dxos/react-ui-types';
6
9
 
7
10
  import { type KeyValue, type TableColumnDef } from '../../types';
8
11
 
@@ -15,6 +18,7 @@ export type TableFlags = Partial<{
15
18
  fullWidth: boolean;
16
19
  debug: boolean;
17
20
  expand: boolean;
21
+ stickyHeader: boolean;
18
22
  rowsSelectable: boolean | 'multi';
19
23
  }>;
20
24
 
@@ -27,21 +31,29 @@ export type TableCurrent<TData extends RowData> = Partial<{
27
31
  onDatumClick: (datum: TData) => void;
28
32
  }>;
29
33
 
30
- export type TableProps<TData extends RowData> = TableFlags &
34
+ export type TableProps<
35
+ TData extends RowData,
36
+ ScrollElement extends Element | Window = Element,
37
+ ItemElement extends Element = HTMLTableRowElement,
38
+ > = TableFlags &
31
39
  TableCurrent<TData> &
32
- Partial<{
33
- keyAccessor: KeyValue<TData>;
34
- data: TData[];
35
- columns: TableColumnDef<TData>[];
36
- onColumnResize: (state: Record<string, number>) => void;
37
- columnVisibility: VisibilityState;
38
- // Controllable row selection
39
- rowSelection: RowSelectionState;
40
- defaultRowSelection: RowSelectionState;
41
- onRowSelectionChange: (rowSelection: RowSelectionState) => void;
42
- // Derived from row selection
43
- onDataSelectionChange: (dataSelection: TData[]) => void;
44
- }>;
40
+ Partial<
41
+ {
42
+ keyAccessor: KeyValue<TData>;
43
+ data: TData[];
44
+ columns: TableColumnDef<TData>[];
45
+ onColumnResize: (state: Record<string, number>) => void;
46
+ columnVisibility: VisibilityState;
47
+ // Controllable row selection
48
+ rowSelection: RowSelectionState;
49
+ defaultRowSelection: RowSelectionState;
50
+ onRowSelectionChange: (rowSelection: RowSelectionState) => void;
51
+ // Derived from row selection
52
+ onDataSelectionChange: (dataSelection: TData[]) => void;
53
+ // `table` element props
54
+ classNames: ClassNameValue;
55
+ } & Pick<VirtualizerOptions<ScrollElement, ItemElement>, 'getScrollElement'>
56
+ >;
45
57
 
46
58
  export type { RowSelectionState, VisibilityState, TableColumnDef, KeyValue };
47
59
 
package/src/helpers.tsx CHANGED
@@ -2,6 +2,7 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
+ import { useFocusFinders } from '@fluentui/react-tabster';
5
6
  import { Check, ClipboardText, type Icon, X } from '@phosphor-icons/react';
6
7
  import {
7
8
  createColumnHelper,
@@ -13,13 +14,11 @@ import {
13
14
  } from '@tanstack/react-table';
14
15
  import format from 'date-fns/format';
15
16
  import formatDistanceToNow from 'date-fns/formatDistanceToNow';
16
- import defaultsDeep from 'lodash.defaultsdeep';
17
- import React, { useRef, useState } from 'react';
17
+ import React, { useEffect, useRef, useState } from 'react';
18
18
 
19
19
  import { type PublicKey } from '@dxos/keys';
20
20
  import { type ClassNameValue, Input, Tooltip } from '@dxos/react-ui';
21
21
  import { getSize, mx } from '@dxos/react-ui-theme';
22
- import { stripUndefinedValues } from '@dxos/util';
23
22
 
24
23
  import { CellCombobox } from './components';
25
24
  import { useTableContext } from './components/Table/TableContext';
@@ -32,6 +31,7 @@ export const createColumnBuilder = <TData extends RowData>() => ({
32
31
  builder: new ColumnBuilder<TData>(),
33
32
  });
34
33
 
34
+ // TODO(thure): This interface is redundant, e.g. `classNames` == `meta.cell.classNames`
35
35
  /**
36
36
  * NOTE: Can use `meta` for custom properties.
37
37
  */
@@ -82,22 +82,15 @@ export type IconColumnOptions<TData extends RowData> = BaseColumnOptions<TData,
82
82
  };
83
83
  };
84
84
 
85
- const defaults = <TData extends RowData, TValue>(
86
- options: Partial<ColumnDef<TData, TValue>>,
87
- ...sources: Partial<ColumnDef<TData, TValue>>[]
88
- ): Partial<ColumnDef<TData, TValue>> => {
89
- return stripUndefinedValues(defaultsDeep({}, options, ...sources));
90
- };
91
-
92
85
  const ComboboxBuilderCell = <TData extends RowData>(cellContext: CellContext<TData, TData>) => {
93
86
  const { model, onUpdate } = cellContext.column.columnDef.meta as ColumnMeta<TData, TData>;
94
- return (
87
+ return model ? (
95
88
  <CellCombobox<TData>
96
- model={model!}
89
+ model={model}
97
90
  value={cellContext.getValue()}
98
91
  onValueChange={(value) => onUpdate?.(cellContext.row.original, cellContext.column.id, value)}
99
92
  />
100
- );
93
+ ) : null;
101
94
  };
102
95
 
103
96
  const StringBuilderCell = <TData extends RowData>(cellContext: CellContext<TData, string>) => {
@@ -140,10 +133,27 @@ const StringBuilderCell = <TData extends RowData>(cellContext: CellContext<TData
140
133
  );
141
134
  };
142
135
 
136
+ const parseNumber = (text?: string) => (text?.trim().length ? Number(text) : NaN);
137
+
138
+ const displayNumber = (value?: number, digits?: number) =>
139
+ value || value === 0
140
+ ? value.toLocaleString(undefined, {
141
+ minimumFractionDigits: digits ?? 0,
142
+ maximumFractionDigits: digits ?? 2,
143
+ })
144
+ : '';
145
+
143
146
  const NumberBuilderCell = <TData extends RowData>(cellContext: CellContext<TData, number>) => {
144
- const { onUpdate, cell, digits } = cellContext.column.columnDef.meta as ColumnMeta<TData, string>;
147
+ const { onUpdate, digits } = cellContext.column.columnDef.meta as ColumnMeta<TData, string>;
145
148
  const value = cellContext.getValue();
146
- const [text, setText] = useState<string>();
149
+ const [text, setText] = useState<string>(displayNumber(value, digits));
150
+ const { findPrevFocusable } = useFocusFinders();
151
+ const ref = useRef<HTMLInputElement | null>(null);
152
+
153
+ // Update display value if `digits` changes and input is not in focus.
154
+ useEffect(() => {
155
+ (!document.activeElement || document.activeElement !== ref.current) && setText(displayNumber(value, digits));
156
+ }, [digits, value]);
147
157
 
148
158
  const handleEdit = () => {
149
159
  if (onUpdate) {
@@ -153,39 +163,34 @@ const NumberBuilderCell = <TData extends RowData>(cellContext: CellContext<TData
153
163
 
154
164
  // TODO(burdon): Property is encoded as float (e.g., 6.1 => 6.099)
155
165
  const handleSave = () => {
156
- const value = text?.trim().length ? Number(text) : NaN;
157
- onUpdate?.(cellContext.row.original, cellContext.column.id, isNaN(value) ? undefined : value);
158
- setText(undefined);
166
+ const nextValue = parseNumber(text);
167
+ onUpdate?.(cellContext.row.original, cellContext.column.id, isNaN(nextValue) ? undefined : nextValue);
168
+ setText(displayNumber(nextValue, digits));
169
+ // Shift focus only if input is focused on save (e.g. on `Enter`).
170
+ ref.current && document.activeElement === ref.current && findPrevFocusable(ref.current)?.focus();
159
171
  };
160
172
 
161
- const handleCancel = () => {
162
- setText(undefined);
173
+ const _handleCancel = () => {
174
+ setText(displayNumber(value, digits));
175
+ ref.current && findPrevFocusable(ref.current)?.focus();
163
176
  };
164
177
 
165
- if (text !== undefined) {
166
- return (
167
- <Input.Root>
168
- <Input.TextInput
169
- value={text}
170
- classNames={['is-full text-end font-mono']}
171
- onBlur={handleSave}
172
- onChange={(event) => setText(event.target.value)}
173
- onKeyDown={(event) => (event.key === 'Escape' && handleCancel()) || (event.key === 'Enter' && handleSave())}
174
- />
175
- </Input.Root>
176
- );
177
- }
178
-
179
178
  return (
180
- <div
181
- className={mx('is-full text-end font-mono empty:after:content-[" "]', textPadding, cell?.classNames)}
182
- onClick={handleEdit}
183
- >
184
- {value?.toLocaleString(undefined, {
185
- minimumFractionDigits: digits ?? 0,
186
- maximumFractionDigits: digits ?? 2,
187
- })}
188
- </div>
179
+ <Input.Root>
180
+ <Input.TextInput
181
+ ref={ref}
182
+ variant='subdued'
183
+ value={text}
184
+ classNames={['is-full text-end font-mono', textPadding]}
185
+ onBlur={handleSave}
186
+ onFocus={handleEdit}
187
+ onChange={({ target: { value } }) => setText(value)}
188
+ onKeyDown={(event) => {
189
+ // TODO(thure): `Escape` is reserved for blurring the input and moving focus to the cell
190
+ return /* (event.key === 'Escape' && handleCancel()) || */ event.key === 'Enter' && handleSave();
191
+ }}
192
+ />
193
+ </Input.Root>
189
194
  );
190
195
  };
191
196
 
@@ -245,13 +250,8 @@ export class ColumnBuilder<TData extends RowData> {
245
250
  onUpdate,
246
251
  ...props
247
252
  }: ComboboxColumnOptions<TData>): Partial<ColumnDef<TData, any>> {
248
- return defaults(props, {
253
+ return {
249
254
  minSize: 100,
250
- meta: {
251
- model,
252
- onUpdate,
253
- cell: { classNames },
254
- },
255
255
  header: (column) => {
256
256
  return label ?? column.header.id;
257
257
  },
@@ -262,7 +262,14 @@ export class ColumnBuilder<TData extends RowData> {
262
262
  const cellValue = cellContext.getValue();
263
263
  return <div className={mx('truncate', classNames)}>{cellValue ? model?.getText(cellValue) : ''}</div>;
264
264
  },
265
- });
265
+ ...props,
266
+ meta: {
267
+ ...props.meta,
268
+ model,
269
+ onUpdate,
270
+ cell: { classNames, ...props.meta?.cell },
271
+ },
272
+ };
266
273
  }
267
274
 
268
275
  /**
@@ -271,9 +278,8 @@ export class ColumnBuilder<TData extends RowData> {
271
278
  string({ label, classNames, onUpdate, ...props }: StringColumnOptions<TData> = {}): Partial<
272
279
  ColumnDef<TData, string>
273
280
  > {
274
- return defaults(props, {
281
+ return {
275
282
  minSize: 100,
276
- meta: { onUpdate, cell: { classNames } },
277
283
  // TODO(burdon): Default.
278
284
  header: (column) => {
279
285
  return label ?? column.header.id;
@@ -284,7 +290,13 @@ export class ColumnBuilder<TData extends RowData> {
284
290
  const value = cell.getValue();
285
291
  return <div className={mx('truncate', textPadding, classNames)}>{value}</div>;
286
292
  },
287
- });
293
+ ...props,
294
+ meta: {
295
+ ...props.meta,
296
+ onUpdate,
297
+ cell: { ...props.meta?.cell, classNames },
298
+ },
299
+ };
288
300
  }
289
301
 
290
302
  /**
@@ -293,13 +305,20 @@ export class ColumnBuilder<TData extends RowData> {
293
305
  number({ label, minSize, classNames, digits, onUpdate, ...props }: NumberColumnOptions<TData> = {}): Partial<
294
306
  ColumnDef<TData, number>
295
307
  > {
296
- return defaults(props, {
308
+ return {
297
309
  size: 100,
298
310
  minSize: 100,
299
- meta: { onUpdate, digits, header: { classNames: 'text-end' }, cell: { classNames } },
300
311
  header: (cell) => label ?? cell.header.id,
301
312
  cell: NumberBuilderCell,
302
- });
313
+ ...props,
314
+ meta: {
315
+ ...props.meta,
316
+ onUpdate,
317
+ digits,
318
+ header: { classNames: 'text-end', ...props.meta?.header },
319
+ cell: { ...props.meta?.cell, classNames },
320
+ },
321
+ };
303
322
  }
304
323
 
305
324
  /**
@@ -309,10 +328,9 @@ export class ColumnBuilder<TData extends RowData> {
309
328
  date({ label, format: formatSpec, relative, classNames, ...props }: DateColumnOptions<TData> = {}): Partial<
310
329
  ColumnDef<TData, Date>
311
330
  > {
312
- return defaults(props, {
331
+ return {
313
332
  size: 220, // TODO(burdon): Depends on format.
314
333
  minSize: 100,
315
- meta: { cell: { classNames } },
316
334
  header: (cell) => label ?? cell.header.id,
317
335
  cell: (cell) => {
318
336
  const value = cell.getValue();
@@ -332,17 +350,18 @@ export class ColumnBuilder<TData extends RowData> {
332
350
  return null;
333
351
  }
334
352
  },
335
- });
353
+ ...props,
354
+ meta: { ...props.meta, cell: { ...props.meta?.cell, classNames } },
355
+ };
336
356
  }
337
357
 
338
358
  /**
339
359
  * PublicKey with tooltip.
340
360
  */
341
- key({ label, tooltip, ...props }: KeyColumnOptions<TData> = {}): Partial<ColumnDef<TData, PublicKey>> {
342
- return defaults(props, {
361
+ key({ label, tooltip, classNames, ...props }: KeyColumnOptions<TData> = {}): Partial<ColumnDef<TData, PublicKey>> {
362
+ return {
343
363
  size: 94,
344
364
  minSize: 94,
345
- meta: { cell: { classNames: ['font-mono', textPadding] } },
346
365
  header: (cell) => label ?? cell.header.id,
347
366
  cell: (cell) => {
348
367
  const value = cell.getValue();
@@ -375,19 +394,17 @@ export class ColumnBuilder<TData extends RowData> {
375
394
  </Tooltip.Provider>
376
395
  );
377
396
  },
378
- });
397
+ ...props,
398
+ meta: { ...props.meta, cell: { ...props.meta?.cell, classNames: ['font-mono', textPadding, classNames] } },
399
+ };
379
400
  }
380
401
 
381
402
  /**
382
403
  * Row selector
383
404
  */
384
- selectRow({
385
- label,
386
- classNames,
387
- onUpdate,
388
- id = 'selectRow',
389
- ...props
390
- }: SelectRowColumnOptions<TData> = {}): Parameters<ColumnHelper<TData>['display']>[0] {
405
+ selectRow({ classNames, onUpdate, id = 'selectRow' }: SelectRowColumnOptions<TData> = {}): Parameters<
406
+ ColumnHelper<TData>['display']
407
+ >[0] {
391
408
  return {
392
409
  id,
393
410
  size: 32,
@@ -418,13 +435,14 @@ export class ColumnBuilder<TData extends RowData> {
418
435
  switch({ label, classNames, onUpdate, ...props }: SwitchColumnOptions<TData> = {}): Partial<
419
436
  ColumnDef<TData, boolean>
420
437
  > {
421
- return defaults(props, {
438
+ return {
422
439
  size: 50,
423
440
  minSize: 50,
424
- meta: { onUpdate, cell: { classNames: [textPadding, classNames] } },
425
441
  header: (column) => label ?? column.header.id,
426
442
  cell: SwitchBuilderCell,
427
- });
443
+ ...props,
444
+ meta: { ...props.meta, onUpdate, cell: { ...props.meta?.cell, classNames: [textPadding, classNames] } },
445
+ };
428
446
  }
429
447
 
430
448
  /**
@@ -433,7 +451,7 @@ export class ColumnBuilder<TData extends RowData> {
433
451
  icon({ label, size, on, off, ...props }: IconColumnOptions<TData> = {}): Partial<ColumnDef<TData, boolean>> {
434
452
  const IconOn = on?.Icon ?? Check;
435
453
  const IconOff = off?.Icon ?? X;
436
- return defaults(props, {
454
+ return {
437
455
  size: size ?? 32,
438
456
  header: (column) => <div className={'justify-center'}>{label ?? column.header.id}</div>,
439
457
  cell: (cell) => {
@@ -446,6 +464,7 @@ export class ColumnBuilder<TData extends RowData> {
446
464
  return null;
447
465
  }
448
466
  },
449
- });
467
+ ...props,
468
+ };
450
469
  }
451
470
  }
package/src/index.ts CHANGED
@@ -6,4 +6,5 @@ export * from './components';
6
6
  export * from './helpers';
7
7
  export * from './schema';
8
8
  export * from './theme';
9
+ export { default as translations } from './translations';
9
10
  export * from './types';
package/src/schema.ts CHANGED
@@ -10,14 +10,6 @@ export type TableDef = {
10
10
 
11
11
  export type ColumnType = 'number' | 'boolean' | 'date' | 'string' | 'ref';
12
12
 
13
- export const columnTypes = new Map<ColumnType, string>([
14
- ['string', 'Text'],
15
- ['boolean', 'Checkbox'],
16
- ['number', 'Number'],
17
- ['date', 'Date'],
18
- ['ref', 'Reference'],
19
- ]);
20
-
21
13
  export type ColumnProps = {
22
14
  id: string;
23
15
  prop: string;
package/src/theme.ts CHANGED
@@ -43,8 +43,8 @@ export const groupTh: ComponentFunction<TableStyleProps> = (_props, ...etc) =>
43
43
 
44
44
  export type TheadStyleProps = Partial<TableFlags>;
45
45
 
46
- export const theadRoot: ComponentFunction<TheadStyleProps> = ({ header }, ...etc) =>
47
- mx(header ? 'sticky block-start-0 z-10' : 'collapse', header && fixedSurface, ...etc);
46
+ export const theadRoot: ComponentFunction<TheadStyleProps> = ({ header, stickyHeader }, ...etc) =>
47
+ mx(header ? stickyHeader && 'sticky block-start-[--sticky-top] z-[1]' : 'collapse', header && fixedSurface, ...etc);
48
48
 
49
49
  export const theadTr: ComponentFunction<TheadStyleProps> = (_props, ...etc) => mx('group', ...etc);
50
50
 
@@ -9,6 +9,17 @@ export default [
9
9
  'en-US': {
10
10
  [translationKey]: {
11
11
  'no selection label': ' ',
12
+ 'number column type label': 'Number',
13
+ 'boolean column type label': 'Toggle',
14
+ 'date column type label': 'Date',
15
+ 'string column type label': 'Text',
16
+ 'ref column type label': 'Reference',
17
+ 'digits label': 'Decimal places',
18
+ 'save label': 'Save',
19
+ 'delete label': 'Delete',
20
+ 'column label label': 'Label',
21
+ 'property key label': 'Property',
22
+ 'column type label': 'Type',
12
23
  },
13
24
  },
14
25
  },