@dxos/react-ui-table 0.4.10-main.c42bfdb → 0.4.10-main.c61b011

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 (50) hide show
  1. package/dist/lib/browser/index.mjs +340 -213
  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/ColumnMenu.d.ts.map +1 -1
  5. package/dist/types/src/components/ColumnMenu/ColumnMenu.stories.d.ts +5 -1
  6. package/dist/types/src/components/ColumnMenu/ColumnMenu.stories.d.ts.map +1 -1
  7. package/dist/types/src/components/ColumnMenu/ColumnSettingsForm.d.ts.map +1 -1
  8. package/dist/types/src/components/Table/Table.d.ts +17 -4
  9. package/dist/types/src/components/Table/Table.d.ts.map +1 -1
  10. package/dist/types/src/components/Table/Table.stories.d.ts +32 -1
  11. package/dist/types/src/components/Table/Table.stories.d.ts.map +1 -1
  12. package/dist/types/src/components/Table/TableBody.d.ts.map +1 -1
  13. package/dist/types/src/components/Table/TableRootContext.d.ts +17 -0
  14. package/dist/types/src/components/Table/TableRootContext.d.ts.map +1 -0
  15. package/dist/types/src/components/Table/index.d.ts +1 -0
  16. package/dist/types/src/components/Table/index.d.ts.map +1 -1
  17. package/dist/types/src/components/Table/props.d.ts +2 -3
  18. package/dist/types/src/components/Table/props.d.ts.map +1 -1
  19. package/dist/types/src/helpers.d.ts.map +1 -1
  20. package/dist/types/src/hooks/index.d.ts +2 -0
  21. package/dist/types/src/hooks/index.d.ts.map +1 -1
  22. package/dist/types/src/hooks/useColumnSizing.d.ts +12 -0
  23. package/dist/types/src/hooks/useColumnSizing.d.ts.map +1 -0
  24. package/dist/types/src/hooks/usePinLastRow.d.ts +1 -1
  25. package/dist/types/src/hooks/usePinLastRow.d.ts.map +1 -1
  26. package/dist/types/src/hooks/useRowSelection.d.ts +7 -0
  27. package/dist/types/src/hooks/useRowSelection.d.ts.map +1 -0
  28. package/dist/types/src/schema/SchemaToTable.stories.d.ts.map +1 -1
  29. package/dist/types/src/schema/schema.d.ts.map +1 -1
  30. package/dist/types/src/schema/schemaToColumns.d.ts +1 -1
  31. package/dist/types/src/schema/schemaToColumns.d.ts.map +1 -1
  32. package/package.json +20 -17
  33. package/src/components/ColumnMenu/ColumnMenu.stories.tsx +30 -0
  34. package/src/components/ColumnMenu/ColumnMenu.tsx +34 -32
  35. package/src/components/ColumnMenu/ColumnSettingsForm.tsx +69 -63
  36. package/src/components/Table/Table.stories.tsx +244 -74
  37. package/src/components/Table/Table.tsx +81 -97
  38. package/src/components/Table/TableBody.tsx +6 -1
  39. package/src/components/Table/TableRootContext.ts +39 -0
  40. package/src/components/Table/index.ts +1 -0
  41. package/src/components/Table/props.ts +17 -24
  42. package/src/helpers.tsx +9 -7
  43. package/src/hooks/index.ts +2 -0
  44. package/src/hooks/useColumnSizing.ts +45 -0
  45. package/src/hooks/usePinLastRow.ts +47 -26
  46. package/src/hooks/useRowSelection.ts +39 -0
  47. package/src/schema/SchemaToTable.stories.tsx +16 -17
  48. package/src/schema/schema.test.ts +1 -1
  49. package/src/schema/schema.ts +36 -37
  50. package/src/schema/schemaToColumns.ts +2 -1
@@ -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.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>
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
- <X className={getSize(4)} />
77
+ <ArrowUp className={getSize(4)} />
90
78
  </span>
91
79
  </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>
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
 
@@ -35,21 +34,25 @@ export const ColumnSettingsForm = ({
35
34
  onDelete,
36
35
  onClose,
37
36
  }: ColumnSettingsFormProps) => {
38
- const [prop, setProp] = useState(column.id);
39
- const [refTable, setRefTable] = useState(column.refTable);
40
- const [refProp, setRefProp] = useState(column.refProp);
41
- const [type, setType] = useState(String(column.type));
42
- const [label, setLabel] = useState(column.label);
43
- const [digits, setDigits] = useState(String(column.digits ?? '0'));
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
+
44
46
  const propRef = useRef<HTMLInputElement>(null);
45
47
  const { t } = useTranslation(translationKey);
46
48
 
47
49
  const handleCancel = () => {
48
- setProp(column.id);
49
50
  onClose?.();
50
51
  };
51
52
 
52
53
  const handleSave = () => {
54
+ const { prop, refTable, refProp, type, label, digits } = formState;
55
+
53
56
  // Check valid.
54
57
  if (!prop.length || !prop.match(/^[a-zA-Z_].+/i)) {
55
58
  propRef.current?.focus();
@@ -58,7 +61,6 @@ export const ColumnSettingsForm = ({
58
61
 
59
62
  // If already exists then check same type.
60
63
  const current = tableDef.columns.find((c) => c.id !== column.id && c.id === prop);
61
-
62
64
  if (current) {
63
65
  // TODO(burdon): Allow multiple columns with different refProps. Ensure correct table prop is updated.
64
66
  if (current.type !== 'ref' /* || current.ref !== refTable */) {
@@ -68,7 +70,7 @@ export const ColumnSettingsForm = ({
68
70
  }
69
71
  }
70
72
 
71
- onUpdate?.(prop, {
73
+ onUpdate?.(column.id, {
72
74
  id: prop, // TODO(burdon): Make unique.
73
75
  prop,
74
76
  label,
@@ -77,7 +79,6 @@ export const ColumnSettingsForm = ({
77
79
  refProp,
78
80
  digits: safeParseInt(digits),
79
81
  });
80
-
81
82
  onClose?.();
82
83
  };
83
84
 
@@ -88,8 +89,8 @@ export const ColumnSettingsForm = ({
88
89
  <Input.Label classNames='mbe-1'>{t('column label label')}</Input.Label>
89
90
  <Input.TextInput
90
91
  placeholder='Column label'
91
- value={label ?? ''}
92
- onChange={(event) => setLabel(event.target.value)}
92
+ value={formState.label ?? ''}
93
+ onChange={(event) => setFormState((prevState) => ({ ...prevState, label: event.target.value }))}
93
94
  autoFocus
94
95
  />
95
96
  </Input.Root>
@@ -98,14 +99,18 @@ export const ColumnSettingsForm = ({
98
99
  <Input.TextInput
99
100
  ref={propRef}
100
101
  placeholder='Property key'
101
- // TODO(burdon): Provide hooks for value normalization, ENTER, ESC, etc.
102
- value={prop ?? ''}
103
- onChange={(event) => setProp(event.target.value.replace(/[^\w_]/g, ''))}
102
+ value={formState.prop ?? ''}
103
+ onChange={(event) =>
104
+ setFormState((prevState) => ({ ...prevState, prop: event.target.value.replace(/[^\w_]/g, '') }))
105
+ }
104
106
  />
105
107
  </Input.Root>
106
108
  <Input.Root>
107
109
  <Input.Label classNames='mbe-1 mbs-3'>{t('column type label')}</Input.Label>
108
- <Select.Root value={type} onValueChange={setType}>
110
+ <Select.Root
111
+ value={formState.type}
112
+ onValueChange={(value) => setFormState((prevState) => ({ ...prevState, type: value }))}
113
+ >
109
114
  <Select.TriggerButton placeholder='Type' classNames='is-full' />
110
115
  <Select.Portal>
111
116
  <Select.Content>
@@ -121,35 +126,59 @@ export const ColumnSettingsForm = ({
121
126
  </Select.Root>
122
127
  </Input.Root>
123
128
  </Section>
124
-
125
- {type === 'number' && (
126
- <>
127
- <Section>
128
- <Input.Root>
129
- <Input.Label classNames='mbe-1 mbs-3'>{t('digits label')}</Input.Label>
130
- {/* TODO(burdon): Constrain input to numbers. */}
131
- <Input.TextInput value={digits ?? ''} onChange={(event) => setDigits(event.target.value)} />
132
- </Input.Root>
133
- </Section>
134
- </>
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>
135
139
  )}
136
140
 
137
- {/* TODO(burdon): Selectors on same line. */}
138
- {type === 'ref' && (
139
- <>
140
- <Section>
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 && (
141
167
  <Input.Root>
142
- <Input.Label classNames='mbe-1 mbs-3'>Table</Input.Label>
143
- <Select.Root value={refTable} onValueChange={setRefTable}>
144
- <Select.TriggerButton placeholder='Table' classNames='is-full' />
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' />
145
174
  <Select.Portal>
146
175
  <Select.Content>
147
176
  <Select.Viewport>
148
177
  {tableDefs
149
- .filter((t) => t.id !== tableDef.id)
150
- .map(({ id, name }) => (
178
+ .find((tableDef) => tableDef.id === formState.refTable)
179
+ ?.columns.map(({ id, label }) => (
151
180
  <Select.Option key={id} value={id}>
152
- {name ?? id}
181
+ {label ?? id}
153
182
  </Select.Option>
154
183
  ))}
155
184
  </Select.Viewport>
@@ -157,36 +186,13 @@ export const ColumnSettingsForm = ({
157
186
  </Select.Portal>
158
187
  </Select.Root>
159
188
  </Input.Root>
160
-
161
- {refTable && (
162
- <Input.Root>
163
- <Input.Label classNames='mbe-1 mbs-3'>Table property</Input.Label>
164
- <Select.Root value={refProp} onValueChange={setRefProp}>
165
- <Select.TriggerButton placeholder='Property' classNames='is-full' />
166
- <Select.Portal>
167
- <Select.Content>
168
- <Select.Viewport>
169
- {tableDefs
170
- .find((tableDef) => tableDef.id === refTable)
171
- ?.columns.map(({ id, label }) => (
172
- <Select.Option key={id} value={id}>
173
- {label ?? id}
174
- </Select.Option>
175
- ))}
176
- </Select.Viewport>
177
- </Select.Content>
178
- </Select.Portal>
179
- </Select.Root>
180
- </Input.Root>
181
- )}
182
- </Section>
183
- </>
189
+ )}
190
+ </Section>
184
191
  )}
185
192
 
186
193
  <Separator classNames='mli-2' />
187
194
 
188
195
  <Section className='space-b-1.5'>
189
- {/* TODO(burdon): Style as DropdownMenuItem. */}
190
196
  <Button variant='primary' classNames='is-full flex gap-2' onClick={handleSave}>
191
197
  <span>{t('save label')}</span>
192
198
  <div className='grow' />