@react-stately/table 3.2.0 → 3.2.1-nightly.3287

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/main.js CHANGED
@@ -1,6 +1,6 @@
1
1
  var $56JBj$reactstatelycollections = require("@react-stately/collections");
2
- var $56JBj$reactstatelygrid = require("@react-stately/grid");
3
2
  var $56JBj$react = require("react");
3
+ var $56JBj$reactstatelygrid = require("@react-stately/grid");
4
4
 
5
5
  function $parcel$export(e, n, v, s) {
6
6
  Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
@@ -26,14 +26,290 @@ function $parcel$interopDefault(a) {
26
26
  }
27
27
 
28
28
  $parcel$export(module.exports, "Section", () => $56JBj$reactstatelycollections.Section);
29
+ var $7aa22d80cd4ca621$exports = {};
30
+
31
+ $parcel$export($7aa22d80cd4ca621$exports, "useTableColumnResizeState", () => $7aa22d80cd4ca621$export$cb895dcf85db1319);
32
+ var $3122b463430f41b1$exports = {};
33
+
34
+ $parcel$export($3122b463430f41b1$exports, "getContentWidth", () => $3122b463430f41b1$export$f61abf052f87399f);
35
+ $parcel$export($3122b463430f41b1$exports, "isStatic", () => $3122b463430f41b1$export$1994a077b98ee0d5);
36
+ $parcel$export($3122b463430f41b1$exports, "parseStaticWidth", () => $3122b463430f41b1$export$7bbad27896f7ae9f);
37
+ $parcel$export($3122b463430f41b1$exports, "getMaxWidth", () => $3122b463430f41b1$export$59185c62a7544aa0);
38
+ $parcel$export($3122b463430f41b1$exports, "getMinWidth", () => $3122b463430f41b1$export$f556054ce4358701);
39
+ $parcel$export($3122b463430f41b1$exports, "getDynamicColumnWidths", () => $3122b463430f41b1$export$a870e6692ac5ccb2);
40
+ function $3122b463430f41b1$export$f61abf052f87399f(widths) {
41
+ return Array.from(widths).map((e)=>e[1]
42
+ ).reduce((acc, cur)=>acc + cur
43
+ , 0);
44
+ }
45
+ function $3122b463430f41b1$export$1994a077b98ee0d5(width) {
46
+ return width != null && (!isNaN(width) || String(width).match(/^(\d+)(?=%$)/) !== null);
47
+ }
48
+ function $3122b463430f41b1$var$parseFractionalUnit(width) {
49
+ if (!width) return 1;
50
+ let match = width.match(/^(\d+)(?=fr$)/);
51
+ // if width is the incorrect format, just deafult it to a 1fr
52
+ if (!match) {
53
+ console.warn(`width: ${width} is not a supported format, width should be a number (ex. 150), percentage (ex. '50%') or fr unit (ex. '2fr')`, 'defaulting to \'1fr\'');
54
+ return 1;
55
+ }
56
+ return parseInt(match[0], 10);
57
+ }
58
+ function $3122b463430f41b1$export$7bbad27896f7ae9f(width, tableWidth) {
59
+ if (typeof width === 'string') {
60
+ let match = width.match(/^(\d+)(?=%$)/);
61
+ if (!match) throw new Error('Only percentages or numbers are supported for static column widths');
62
+ return tableWidth * (parseInt(match[0], 10) / 100);
63
+ }
64
+ return width;
65
+ }
66
+ function $3122b463430f41b1$export$59185c62a7544aa0(maxWidth, tableWidth) {
67
+ return maxWidth != null ? $3122b463430f41b1$export$7bbad27896f7ae9f(maxWidth, tableWidth) : Infinity;
68
+ }
69
+ function $3122b463430f41b1$export$f556054ce4358701(minWidth, tableWidth) {
70
+ return minWidth != null ? $3122b463430f41b1$export$7bbad27896f7ae9f(minWidth, tableWidth) : 75;
71
+ }
72
+ function $3122b463430f41b1$var$mapDynamicColumns(dynamicColumns, availableSpace, tableWidth) {
73
+ let fractions = dynamicColumns.reduce((sum, column)=>sum + $3122b463430f41b1$var$parseFractionalUnit(column.props.defaultWidth)
74
+ , 0);
75
+ let columns = dynamicColumns.map((column, index)=>{
76
+ const targetWidth = $3122b463430f41b1$var$parseFractionalUnit(column.props.defaultWidth) * availableSpace / fractions;
77
+ const delta = Math.max($3122b463430f41b1$export$f556054ce4358701(column.props.minWidth, tableWidth) - targetWidth, targetWidth - $3122b463430f41b1$export$59185c62a7544aa0(column.props.maxWidth, tableWidth));
78
+ return {
79
+ ...column,
80
+ index: index,
81
+ delta: delta
82
+ };
83
+ });
84
+ return columns;
85
+ }
86
+ function $3122b463430f41b1$var$findDynamicColumnWidths(dynamicColumns, availableSpace, tableWidth) {
87
+ let fractions = dynamicColumns.reduce((sum, col)=>sum + $3122b463430f41b1$var$parseFractionalUnit(col.props.defaultWidth)
88
+ , 0);
89
+ const columns = dynamicColumns.map((column)=>{
90
+ const targetWidth = $3122b463430f41b1$var$parseFractionalUnit(column.props.defaultWidth) * availableSpace / fractions;
91
+ let width = Math.max($3122b463430f41b1$export$f556054ce4358701(column.props.minWidth, tableWidth), Math.min(Math.floor(targetWidth), $3122b463430f41b1$export$59185c62a7544aa0(column.props.maxWidth, tableWidth)));
92
+ column.calculatedWidth = width;
93
+ availableSpace -= width;
94
+ fractions -= $3122b463430f41b1$var$parseFractionalUnit(column.props.defaultWidth);
95
+ return column;
96
+ });
97
+ return columns;
98
+ }
99
+ function $3122b463430f41b1$export$a870e6692ac5ccb2(dynamicColumns, availableSpace, tableWidth) {
100
+ let columns = $3122b463430f41b1$var$mapDynamicColumns(dynamicColumns, availableSpace, tableWidth);
101
+ columns.sort((a, b)=>b.delta - a.delta
102
+ );
103
+ columns = $3122b463430f41b1$var$findDynamicColumnWidths(columns, availableSpace, tableWidth);
104
+ columns.sort((a, b)=>a.index - b.index
105
+ );
106
+ return columns;
107
+ }
108
+
109
+
110
+
111
+ function $7aa22d80cd4ca621$export$cb895dcf85db1319(props) {
112
+ const { columns: columns1 , getDefaultWidth: getDefaultWidth , tableWidth: defaultTableWidth = null } = props;
113
+ const columnsRef = $56JBj$react.useRef([]);
114
+ const tableWidth = $56JBj$react.useRef(defaultTableWidth);
115
+ const isResizing = $56JBj$react.useRef(null);
116
+ const startResizeContentWidth = $56JBj$react.useRef();
117
+ const [columnWidths, setColumnWidths] = $56JBj$react.useState(new Map(columns1.map((col)=>[
118
+ col.key,
119
+ 0
120
+ ]
121
+ )));
122
+ const columnWidthsRef = $56JBj$react.useRef(columnWidths);
123
+ const affectedColumnWidthsRef = $56JBj$react.useRef([]);
124
+ const [resizedColumns, setResizedColumns] = $56JBj$react.useState(new Set());
125
+ const resizedColumnsRef = $56JBj$react.useRef(resizedColumns);
126
+ function setColumnWidthsForRef(newWidths) {
127
+ columnWidthsRef.current = newWidths;
128
+ // new map so that change detection is triggered
129
+ setColumnWidths(newWidths);
130
+ }
131
+ /*
132
+ returns the resolved column width in this order:
133
+ previously calculated width -> controlled width prop -> uncontrolled defaultWidth prop -> dev assigned width -> default dynamic width
134
+ */ let getResolvedColumnWidth = $56JBj$react.useCallback((column)=>{
135
+ let columnProps = column.props;
136
+ var _width, ref, ref1;
137
+ return (resizedColumns === null || resizedColumns === void 0 ? void 0 : resizedColumns.has(column.key)) ? columnWidthsRef.current.get(column.key) : (ref1 = (ref = (_width = columnProps.width) !== null && _width !== void 0 ? _width : columnProps.defaultWidth) !== null && ref !== void 0 ? ref : getDefaultWidth === null || getDefaultWidth === void 0 ? void 0 : getDefaultWidth(column.props)) !== null && ref1 !== void 0 ? ref1 : '1fr';
138
+ }, [
139
+ getDefaultWidth,
140
+ resizedColumns
141
+ ]);
142
+ let getStaticAndDynamicColumns = $56JBj$react.useCallback((columns)=>columns.reduce((acc, column)=>{
143
+ let width = getResolvedColumnWidth(column);
144
+ return $3122b463430f41b1$export$1994a077b98ee0d5(width) ? {
145
+ ...acc,
146
+ staticColumns: [
147
+ ...acc.staticColumns,
148
+ column
149
+ ]
150
+ } : {
151
+ ...acc,
152
+ dynamicColumns: [
153
+ ...acc.dynamicColumns,
154
+ column
155
+ ]
156
+ };
157
+ }, {
158
+ staticColumns: [],
159
+ dynamicColumns: []
160
+ })
161
+ , [
162
+ getResolvedColumnWidth
163
+ ]);
164
+ let buildColumnWidths = $56JBj$react.useCallback((affectedColumns, availableSpace)=>{
165
+ const widths = new Map();
166
+ let remainingSpace = availableSpace;
167
+ const { staticColumns: staticColumns , dynamicColumns: dynamicColumns } = getStaticAndDynamicColumns(affectedColumns);
168
+ staticColumns.forEach((column)=>{
169
+ let width = getResolvedColumnWidth(column);
170
+ let w = $3122b463430f41b1$export$7bbad27896f7ae9f(width, tableWidth.current);
171
+ widths.set(column.key, w);
172
+ remainingSpace -= w;
173
+ });
174
+ // dynamic columns
175
+ if (dynamicColumns.length > 0) {
176
+ const newColumnWidths = $3122b463430f41b1$export$a870e6692ac5ccb2(dynamicColumns, remainingSpace, tableWidth.current);
177
+ for (let column of newColumnWidths)widths.set(column.key, column.calculatedWidth);
178
+ }
179
+ return widths;
180
+ }, [
181
+ getStaticAndDynamicColumns,
182
+ getResolvedColumnWidth
183
+ ]);
184
+ const prevColKeys = columnsRef.current.map((col)=>col.key
185
+ );
186
+ const colKeys = columns1.map((col)=>col.key
187
+ );
188
+ // if the columns change, need to rebuild widths.
189
+ if (!colKeys.every((col, i)=>col === prevColKeys[i]
190
+ )) {
191
+ columnsRef.current = columns1;
192
+ const widths = buildColumnWidths(columns1, tableWidth.current);
193
+ setColumnWidthsForRef(widths);
194
+ }
195
+ function setTableWidth(width) {
196
+ if (width && width !== tableWidth.current) {
197
+ tableWidth.current = width;
198
+ if (!isResizing.current) {
199
+ const widths = buildColumnWidths(columns1, width);
200
+ setColumnWidthsForRef(widths);
201
+ }
202
+ }
203
+ }
204
+ function onColumnResizeStart() {
205
+ isResizing.current = true;
206
+ startResizeContentWidth.current = $3122b463430f41b1$export$f61abf052f87399f(columnWidthsRef.current);
207
+ }
208
+ function onColumnResize(column, width) {
209
+ let widthsObj = resizeColumn(column, width);
210
+ affectedColumnWidthsRef.current = widthsObj;
211
+ props.onColumnResize && props.onColumnResize(affectedColumnWidthsRef.current);
212
+ }
213
+ function onColumnResizeEnd() {
214
+ isResizing.current = false;
215
+ props.onColumnResizeEnd && props.onColumnResizeEnd(affectedColumnWidthsRef.current);
216
+ affectedColumnWidthsRef.current = [];
217
+ let widths = new Map(columnWidthsRef.current);
218
+ // Need to set the resizeBufferColumn or "spooky column" back to 0 since done resizing;
219
+ const bufferColumnKey = columnsRef.current[columnsRef.current.length - 1].key;
220
+ widths.set(bufferColumnKey, 0);
221
+ setColumnWidthsForRef(widths);
222
+ }
223
+ function resizeColumn(column1, newWidth) {
224
+ let boundedWidth = Math.max($3122b463430f41b1$export$f556054ce4358701(column1.props.minWidth, tableWidth.current), Math.min(Math.floor(newWidth), $3122b463430f41b1$export$59185c62a7544aa0(column1.props.maxWidth, tableWidth.current)));
225
+ // copy the columnWidths map and set the new width for the column being resized
226
+ let widths = new Map(columnWidthsRef.current);
227
+ widths.set(columnsRef.current[columnsRef.current.length - 1].key, 0);
228
+ widths.set(column1.key, boundedWidth);
229
+ // keep track of all columns that have been sized
230
+ resizedColumnsRef.current.add(column1.key);
231
+ setResizedColumns(resizedColumnsRef.current);
232
+ // get the columns affected by resize and remaining space
233
+ const resizeIndex = columnsRef.current.findIndex((col)=>col.key === column1.key
234
+ );
235
+ let affectedColumns = columnsRef.current.slice(resizeIndex + 1);
236
+ // we only care about the columns that CAN be resized, we ignore static columns.
237
+ let { dynamicColumns: dynamicColumns } = getStaticAndDynamicColumns(affectedColumns);
238
+ // available space for affected columns
239
+ let availableSpace = columnsRef.current.reduce((acc, column, index)=>{
240
+ if (index <= resizeIndex || $3122b463430f41b1$export$1994a077b98ee0d5(getResolvedColumnWidth(column))) return acc - widths.get(column.key);
241
+ return acc;
242
+ }, tableWidth.current);
243
+ // merge the unaffected column widths and the recalculated column widths
244
+ let recalculatedColumnWidths = buildColumnWidths(dynamicColumns, availableSpace);
245
+ widths = new Map([
246
+ ...widths,
247
+ ...recalculatedColumnWidths
248
+ ]);
249
+ if (startResizeContentWidth.current > tableWidth.current) widths.set(columnsRef.current[columnsRef.current.length - 1].key, Math.max(0, startResizeContentWidth.current - $3122b463430f41b1$export$f61abf052f87399f(widths)));
250
+ setColumnWidthsForRef(widths);
251
+ /*
252
+ when getting recalculated columns above, the column being resized is not considered "recalculated"
253
+ so we need to add it to the list of affected columns
254
+ */ let allAffectedColumns = [
255
+ [
256
+ column1.key,
257
+ boundedWidth
258
+ ],
259
+ ...recalculatedColumnWidths
260
+ ].map(([key, width])=>({
261
+ key: key,
262
+ width: width
263
+ })
264
+ );
265
+ return allAffectedColumns;
266
+ }
267
+ var ref2;
268
+ // This function is regenerated whenever columnWidthsRef.current changes in order to get the new correct ref value.
269
+ let getColumnWidth = $56JBj$react.useCallback((key)=>(ref2 = columnWidthsRef.current.get(key)) !== null && ref2 !== void 0 ? ref2 : 0
270
+ , [
271
+ columnWidthsRef.current
272
+ ]);
273
+ let getColumnMinWidth = $56JBj$react.useCallback((key)=>{
274
+ const columnIndex = columns1.findIndex((col)=>col.key === key
275
+ );
276
+ if (columnIndex === -1) return;
277
+ return $3122b463430f41b1$export$f556054ce4358701(columns1[columnIndex].props.minWidth, tableWidth.current);
278
+ }, [
279
+ columns1
280
+ ]);
281
+ let getColumnMaxWidth = $56JBj$react.useCallback((key)=>{
282
+ const columnIndex = columns1.findIndex((col)=>col.key === key
283
+ );
284
+ if (columnIndex === -1) return;
285
+ return $3122b463430f41b1$export$59185c62a7544aa0(columns1[columnIndex].props.maxWidth, tableWidth.current);
286
+ }, [
287
+ columns1
288
+ ]);
289
+ return {
290
+ columnWidths: columnWidthsRef,
291
+ setTableWidth: setTableWidth,
292
+ onColumnResize: onColumnResize,
293
+ onColumnResizeStart: onColumnResizeStart,
294
+ onColumnResizeEnd: onColumnResizeEnd,
295
+ getColumnWidth: getColumnWidth,
296
+ getColumnMinWidth: getColumnMinWidth,
297
+ getColumnMaxWidth: getColumnMaxWidth,
298
+ isResizingColumn: isResizing.current
299
+ };
300
+ }
301
+
302
+
303
+
29
304
  var $e3f7784147dde23d$exports = {};
30
305
 
31
306
  $parcel$export($e3f7784147dde23d$exports, "useTableState", () => $e3f7784147dde23d$export$907bcc6c48325fd6);
32
307
 
33
308
 
34
309
 
310
+
35
311
  const $7f5a58334d8866a5$var$ROW_HEADER_COLUMN_KEY = 'row-header-column-' + Math.random().toString(36).slice(2);
36
- // const RESIZE_BUFFER_COLUMN_KEY = 'resize-buffer-column' + Math.random().toString(36).slice(2);
312
+ const $7f5a58334d8866a5$var$RESIZE_BUFFER_COLUMN_KEY = 'resize-buffer-column' + Math.random().toString(36).slice(2);
37
313
  function $7f5a58334d8866a5$var$buildHeaderRows(keyMap, columnNodes) {
38
314
  let columns = [];
39
315
  let seen = new Map();
@@ -237,35 +513,38 @@ class $7f5a58334d8866a5$export$596e1b2e2cf93690 extends $56JBj$reactstatelygrid.
237
513
  for (let child of node.childNodes)visit(child);
238
514
  };
239
515
  for (let node1 of nodes)visit(node1);
240
- // if (Array.from(nodes).some(node => node.props?.allowsResizing)) {
241
- // /*
242
- // If the table content width > table width, a horizontal scroll bar is present.
243
- // If a user tries to resize a column, making it smaller while they are scrolled to the
244
- // end of the content horizontally, it shrinks the total table content width, causing
245
- // things to snap around and breaks the resize behavior.
246
- // To fix this, we add a resize buffer column (aka "spooky column") to the end of the table.
247
- // The width of this column defaults to 0. If you try and shrink a column and the width of the
248
- // table contents > table width, then the "spooky column" will grow to take up the difference
249
- // so that the total table content width remains constant while you are resizing. Once you
250
- // finish resizing, the "spooky column" snaps back to 0.
251
- // */
252
- // let resizeBufferColumn: GridNode<T> = {
253
- // type: 'column',
254
- // key: RESIZE_BUFFER_COLUMN_KEY,
255
- // value: null,
256
- // textValue: '',
257
- // level: 0,
258
- // index: columns.length,
259
- // hasChildNodes: false,
260
- // rendered: null,
261
- // childNodes: [],
262
- // props: {
263
- // isResizeBuffer: true,
264
- // defaultWidth: 0
265
- // }
266
- // };
267
- // columns.push(resizeBufferColumn);
268
- // }
516
+ if (Array.from(nodes).some((node)=>{
517
+ var ref;
518
+ return (ref = node.props) === null || ref === void 0 ? void 0 : ref.allowsResizing;
519
+ })) {
520
+ /*
521
+ If the table content width > table width, a horizontal scroll bar is present.
522
+ If a user tries to resize a column, making it smaller while they are scrolled to the
523
+ end of the content horizontally, it shrinks the total table content width, causing
524
+ things to snap around and breaks the resize behavior.
525
+
526
+ To fix this, we add a resize buffer column (aka "spooky column") to the end of the table.
527
+ The width of this column defaults to 0. If you try and shrink a column and the width of the
528
+ table contents > table width, then the "spooky column" will grow to take up the difference
529
+ so that the total table content width remains constant while you are resizing. Once you
530
+ finish resizing, the "spooky column" snaps back to 0.
531
+ */ let resizeBufferColumn = {
532
+ type: 'column',
533
+ key: $7f5a58334d8866a5$var$RESIZE_BUFFER_COLUMN_KEY,
534
+ value: null,
535
+ textValue: '',
536
+ level: 0,
537
+ index: columns.length,
538
+ hasChildNodes: false,
539
+ rendered: null,
540
+ childNodes: [],
541
+ props: {
542
+ isResizeBuffer: true,
543
+ defaultWidth: 0
544
+ }
545
+ };
546
+ columns.push(resizeBufferColumn);
547
+ }
269
548
  let headerRows = $7f5a58334d8866a5$var$buildHeaderRows(columnKeyMap, columns);
270
549
  headerRows.forEach((row, i)=>rows.splice(i, 0, row)
271
550
  );
@@ -310,7 +589,12 @@ function $e3f7784147dde23d$export$907bcc6c48325fd6(props) {
310
589
  ...props,
311
590
  collection: collection
312
591
  });
313
- // const tableColumnResizeState = useTableColumnResizeState({columns: collection.columns, getDefaultWidth: props.getDefaultWidth, onColumnResize: props.onColumnResize, onColumnResizeEnd: props.onColumnResizeEnd});
592
+ const tableColumnResizeState = $7aa22d80cd4ca621$export$cb895dcf85db1319({
593
+ columns: collection.columns,
594
+ getDefaultWidth: props.getDefaultWidth,
595
+ onColumnResize: props.onColumnResize,
596
+ onColumnResizeEnd: props.onColumnResizeEnd
597
+ });
314
598
  return {
315
599
  collection: collection,
316
600
  disabledKeys: disabledKeys,
@@ -323,7 +607,8 @@ function $e3f7784147dde23d$export$907bcc6c48325fd6(props) {
323
607
  column: columnKey,
324
608
  direction: direction !== null && direction !== void 0 ? direction : ((ref = props.sortDescriptor) === null || ref === void 0 ? void 0 : ref.column) === columnKey ? $e3f7784147dde23d$var$OPPOSITE_SORT_DIRECTION[props.sortDescriptor.direction] : 'ascending'
325
609
  });
326
- }
610
+ },
611
+ ...tableColumnResizeState
327
612
  };
328
613
  }
329
614
 
@@ -539,6 +824,8 @@ let $ad4ab0a21c733e1f$export$f6f0c3fe4ec306ea = $ad4ab0a21c733e1f$var$Cell;
539
824
 
540
825
 
541
826
 
827
+ $parcel$exportWildcard(module.exports, $7aa22d80cd4ca621$exports);
828
+ $parcel$exportWildcard(module.exports, $3122b463430f41b1$exports);
542
829
  $parcel$exportWildcard(module.exports, $e3f7784147dde23d$exports);
543
830
  $parcel$exportWildcard(module.exports, $f45775f5d6f744fa$exports);
544
831
  $parcel$exportWildcard(module.exports, $6ec527db6a3a5692$exports);