@dxos/react-ui-table 0.3.8 → 0.3.9-main.03b62b6

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/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();
@@ -360,32 +379,32 @@ export class ColumnBuilder<TData extends RowData> {
360
379
  <Tooltip.Provider>
361
380
  <Tooltip.Root>
362
381
  <Tooltip.Trigger>{element}</Tooltip.Trigger>
363
- <Tooltip.Content side='right'>
364
- <Tooltip.Arrow />
365
- <ClipboardText
366
- onClick={(ev) => {
367
- ev.stopPropagation(); // Prevent select row.
368
- void navigator.clipboard.writeText(value.toHex());
369
- }}
370
- />
371
- </Tooltip.Content>
382
+ <Tooltip.Portal>
383
+ <Tooltip.Content side='right'>
384
+ <Tooltip.Arrow />
385
+ <ClipboardText
386
+ onClick={(ev) => {
387
+ ev.stopPropagation(); // Prevent select row.
388
+ void navigator.clipboard.writeText(value.toHex());
389
+ }}
390
+ />
391
+ </Tooltip.Content>
392
+ </Tooltip.Portal>
372
393
  </Tooltip.Root>
373
394
  </Tooltip.Provider>
374
395
  );
375
396
  },
376
- });
397
+ ...props,
398
+ meta: { ...props.meta, cell: { ...props.meta?.cell, classNames: ['font-mono', textPadding, classNames] } },
399
+ };
377
400
  }
378
401
 
379
402
  /**
380
403
  * Row selector
381
404
  */
382
- selectRow({
383
- label,
384
- classNames,
385
- onUpdate,
386
- id = 'selectRow',
387
- ...props
388
- }: SelectRowColumnOptions<TData> = {}): Parameters<ColumnHelper<TData>['display']>[0] {
405
+ selectRow({ classNames, onUpdate, id = 'selectRow' }: SelectRowColumnOptions<TData> = {}): Parameters<
406
+ ColumnHelper<TData>['display']
407
+ >[0] {
389
408
  return {
390
409
  id,
391
410
  size: 32,
@@ -416,13 +435,14 @@ export class ColumnBuilder<TData extends RowData> {
416
435
  switch({ label, classNames, onUpdate, ...props }: SwitchColumnOptions<TData> = {}): Partial<
417
436
  ColumnDef<TData, boolean>
418
437
  > {
419
- return defaults(props, {
438
+ return {
420
439
  size: 50,
421
440
  minSize: 50,
422
- meta: { onUpdate, cell: { classNames: [textPadding, classNames] } },
423
441
  header: (column) => label ?? column.header.id,
424
442
  cell: SwitchBuilderCell,
425
- });
443
+ ...props,
444
+ meta: { ...props.meta, onUpdate, cell: { ...props.meta?.cell, classNames: [textPadding, classNames] } },
445
+ };
426
446
  }
427
447
 
428
448
  /**
@@ -431,7 +451,7 @@ export class ColumnBuilder<TData extends RowData> {
431
451
  icon({ label, size, on, off, ...props }: IconColumnOptions<TData> = {}): Partial<ColumnDef<TData, boolean>> {
432
452
  const IconOn = on?.Icon ?? Check;
433
453
  const IconOff = off?.Icon ?? X;
434
- return defaults(props, {
454
+ return {
435
455
  size: size ?? 32,
436
456
  header: (column) => <div className={'justify-center'}>{label ?? column.header.id}</div>,
437
457
  cell: (cell) => {
@@ -444,6 +464,7 @@ export class ColumnBuilder<TData extends RowData> {
444
464
  return null;
445
465
  }
446
466
  },
447
- });
467
+ ...props,
468
+ };
448
469
  }
449
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
@@ -4,7 +4,15 @@
4
4
 
5
5
  import { type CellContext } from '@tanstack/react-table';
6
6
 
7
- import { chromeSurface, fixedSurface, focusRing, groupBorder, hoverColors, mx } from '@dxos/react-ui-theme';
7
+ import {
8
+ chromeSurface,
9
+ fixedSurface,
10
+ focusRing,
11
+ ghostSelected,
12
+ ghostSelectedCurrent,
13
+ groupBorder,
14
+ mx,
15
+ } from '@dxos/react-ui-theme';
8
16
  import { type ComponentFunction } from '@dxos/react-ui-types';
9
17
 
10
18
  import { type TableContextValue, type TableFlags } from './components';
@@ -35,8 +43,8 @@ export const groupTh: ComponentFunction<TableStyleProps> = (_props, ...etc) =>
35
43
 
36
44
  export type TheadStyleProps = Partial<TableFlags>;
37
45
 
38
- export const theadRoot: ComponentFunction<TheadStyleProps> = ({ header }, ...etc) =>
39
- 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);
40
48
 
41
49
  export const theadTr: ComponentFunction<TheadStyleProps> = (_props, ...etc) => mx('group', ...etc);
42
50
 
@@ -65,13 +73,12 @@ export const theadResizeThumb: ComponentFunction<TheadStyleProps> = (_props, ...
65
73
  export type TbodyStyleProps = Partial<TableContextValue<any>>;
66
74
  export const tbodyRoot: ComponentFunction<TbodyStyleProps> = (_props, ...etc) => mx(...etc);
67
75
 
68
- export type TbodyTrStyleProps = Partial<{ isSelected?: boolean; isCurrent?: boolean; canBeCurrent?: boolean }>;
69
- export const tbodyTr: ComponentFunction<TbodyTrStyleProps> = ({ isSelected, isCurrent, canBeCurrent }, ...etc) =>
76
+ export type TbodyTrStyleProps = Partial<{ canBeCurrent?: boolean }>;
77
+ export const tbodyTr: ComponentFunction<TbodyTrStyleProps> = ({ canBeCurrent }, ...etc) =>
70
78
  mx(
71
79
  'group',
72
- isSelected ? selectedRow : isCurrent && currentRow,
80
+ canBeCurrent ? ghostSelectedCurrent : ghostSelected,
73
81
  canBeCurrent && focusRing,
74
- canBeCurrent && hoverColors,
75
82
  canBeCurrent && 'cursor-pointer rounded',
76
83
  ...etc,
77
84
  );
@@ -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
  },