@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/module.js CHANGED
@@ -1,18 +1,294 @@
1
1
  import {Section as $6555104ff085bef4$re_export$Section, useCollection as $1BfjW$useCollection} from "@react-stately/collections";
2
+ import $1BfjW$react, {useRef as $1BfjW$useRef, useState as $1BfjW$useState, useCallback as $1BfjW$useCallback, useMemo as $1BfjW$useMemo} from "react";
2
3
  import {useGridState as $1BfjW$useGridState, GridCollection as $1BfjW$GridCollection} from "@react-stately/grid";
3
- import $1BfjW$react, {useMemo as $1BfjW$useMemo} from "react";
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});
7
7
  }
8
+ var $292bc4e09cd0eb62$exports = {};
9
+
10
+ $parcel$export($292bc4e09cd0eb62$exports, "useTableColumnResizeState", () => $292bc4e09cd0eb62$export$cb895dcf85db1319);
11
+ var $30561577df230a30$exports = {};
12
+
13
+ $parcel$export($30561577df230a30$exports, "getContentWidth", () => $30561577df230a30$export$f61abf052f87399f);
14
+ $parcel$export($30561577df230a30$exports, "isStatic", () => $30561577df230a30$export$1994a077b98ee0d5);
15
+ $parcel$export($30561577df230a30$exports, "parseStaticWidth", () => $30561577df230a30$export$7bbad27896f7ae9f);
16
+ $parcel$export($30561577df230a30$exports, "getMaxWidth", () => $30561577df230a30$export$59185c62a7544aa0);
17
+ $parcel$export($30561577df230a30$exports, "getMinWidth", () => $30561577df230a30$export$f556054ce4358701);
18
+ $parcel$export($30561577df230a30$exports, "getDynamicColumnWidths", () => $30561577df230a30$export$a870e6692ac5ccb2);
19
+ function $30561577df230a30$export$f61abf052f87399f(widths) {
20
+ return Array.from(widths).map((e)=>e[1]
21
+ ).reduce((acc, cur)=>acc + cur
22
+ , 0);
23
+ }
24
+ function $30561577df230a30$export$1994a077b98ee0d5(width) {
25
+ return width != null && (!isNaN(width) || String(width).match(/^(\d+)(?=%$)/) !== null);
26
+ }
27
+ function $30561577df230a30$var$parseFractionalUnit(width) {
28
+ if (!width) return 1;
29
+ let match = width.match(/^(\d+)(?=fr$)/);
30
+ // if width is the incorrect format, just deafult it to a 1fr
31
+ if (!match) {
32
+ 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\'');
33
+ return 1;
34
+ }
35
+ return parseInt(match[0], 10);
36
+ }
37
+ function $30561577df230a30$export$7bbad27896f7ae9f(width, tableWidth) {
38
+ if (typeof width === 'string') {
39
+ let match = width.match(/^(\d+)(?=%$)/);
40
+ if (!match) throw new Error('Only percentages or numbers are supported for static column widths');
41
+ return tableWidth * (parseInt(match[0], 10) / 100);
42
+ }
43
+ return width;
44
+ }
45
+ function $30561577df230a30$export$59185c62a7544aa0(maxWidth, tableWidth) {
46
+ return maxWidth != null ? $30561577df230a30$export$7bbad27896f7ae9f(maxWidth, tableWidth) : Infinity;
47
+ }
48
+ function $30561577df230a30$export$f556054ce4358701(minWidth, tableWidth) {
49
+ return minWidth != null ? $30561577df230a30$export$7bbad27896f7ae9f(minWidth, tableWidth) : 75;
50
+ }
51
+ function $30561577df230a30$var$mapDynamicColumns(dynamicColumns, availableSpace, tableWidth) {
52
+ let fractions = dynamicColumns.reduce((sum, column)=>sum + $30561577df230a30$var$parseFractionalUnit(column.props.defaultWidth)
53
+ , 0);
54
+ let columns = dynamicColumns.map((column, index)=>{
55
+ const targetWidth = $30561577df230a30$var$parseFractionalUnit(column.props.defaultWidth) * availableSpace / fractions;
56
+ const delta = Math.max($30561577df230a30$export$f556054ce4358701(column.props.minWidth, tableWidth) - targetWidth, targetWidth - $30561577df230a30$export$59185c62a7544aa0(column.props.maxWidth, tableWidth));
57
+ return {
58
+ ...column,
59
+ index: index,
60
+ delta: delta
61
+ };
62
+ });
63
+ return columns;
64
+ }
65
+ function $30561577df230a30$var$findDynamicColumnWidths(dynamicColumns, availableSpace, tableWidth) {
66
+ let fractions = dynamicColumns.reduce((sum, col)=>sum + $30561577df230a30$var$parseFractionalUnit(col.props.defaultWidth)
67
+ , 0);
68
+ const columns = dynamicColumns.map((column)=>{
69
+ const targetWidth = $30561577df230a30$var$parseFractionalUnit(column.props.defaultWidth) * availableSpace / fractions;
70
+ let width = Math.max($30561577df230a30$export$f556054ce4358701(column.props.minWidth, tableWidth), Math.min(Math.floor(targetWidth), $30561577df230a30$export$59185c62a7544aa0(column.props.maxWidth, tableWidth)));
71
+ column.calculatedWidth = width;
72
+ availableSpace -= width;
73
+ fractions -= $30561577df230a30$var$parseFractionalUnit(column.props.defaultWidth);
74
+ return column;
75
+ });
76
+ return columns;
77
+ }
78
+ function $30561577df230a30$export$a870e6692ac5ccb2(dynamicColumns, availableSpace, tableWidth) {
79
+ let columns = $30561577df230a30$var$mapDynamicColumns(dynamicColumns, availableSpace, tableWidth);
80
+ columns.sort((a, b)=>b.delta - a.delta
81
+ );
82
+ columns = $30561577df230a30$var$findDynamicColumnWidths(columns, availableSpace, tableWidth);
83
+ columns.sort((a, b)=>a.index - b.index
84
+ );
85
+ return columns;
86
+ }
87
+
88
+
89
+
90
+ function $292bc4e09cd0eb62$export$cb895dcf85db1319(props) {
91
+ const { columns: columns1 , getDefaultWidth: getDefaultWidth , tableWidth: defaultTableWidth = null } = props;
92
+ const columnsRef = $1BfjW$useRef([]);
93
+ const tableWidth = $1BfjW$useRef(defaultTableWidth);
94
+ const isResizing = $1BfjW$useRef(null);
95
+ const startResizeContentWidth = $1BfjW$useRef();
96
+ const [columnWidths, setColumnWidths] = $1BfjW$useState(new Map(columns1.map((col)=>[
97
+ col.key,
98
+ 0
99
+ ]
100
+ )));
101
+ const columnWidthsRef = $1BfjW$useRef(columnWidths);
102
+ const affectedColumnWidthsRef = $1BfjW$useRef([]);
103
+ const [resizedColumns, setResizedColumns] = $1BfjW$useState(new Set());
104
+ const resizedColumnsRef = $1BfjW$useRef(resizedColumns);
105
+ function setColumnWidthsForRef(newWidths) {
106
+ columnWidthsRef.current = newWidths;
107
+ // new map so that change detection is triggered
108
+ setColumnWidths(newWidths);
109
+ }
110
+ /*
111
+ returns the resolved column width in this order:
112
+ previously calculated width -> controlled width prop -> uncontrolled defaultWidth prop -> dev assigned width -> default dynamic width
113
+ */ let getResolvedColumnWidth = $1BfjW$useCallback((column)=>{
114
+ let columnProps = column.props;
115
+ var _width, ref, ref1;
116
+ 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';
117
+ }, [
118
+ getDefaultWidth,
119
+ resizedColumns
120
+ ]);
121
+ let getStaticAndDynamicColumns = $1BfjW$useCallback((columns)=>columns.reduce((acc, column)=>{
122
+ let width = getResolvedColumnWidth(column);
123
+ return $30561577df230a30$export$1994a077b98ee0d5(width) ? {
124
+ ...acc,
125
+ staticColumns: [
126
+ ...acc.staticColumns,
127
+ column
128
+ ]
129
+ } : {
130
+ ...acc,
131
+ dynamicColumns: [
132
+ ...acc.dynamicColumns,
133
+ column
134
+ ]
135
+ };
136
+ }, {
137
+ staticColumns: [],
138
+ dynamicColumns: []
139
+ })
140
+ , [
141
+ getResolvedColumnWidth
142
+ ]);
143
+ let buildColumnWidths = $1BfjW$useCallback((affectedColumns, availableSpace)=>{
144
+ const widths = new Map();
145
+ let remainingSpace = availableSpace;
146
+ const { staticColumns: staticColumns , dynamicColumns: dynamicColumns } = getStaticAndDynamicColumns(affectedColumns);
147
+ staticColumns.forEach((column)=>{
148
+ let width = getResolvedColumnWidth(column);
149
+ let w = $30561577df230a30$export$7bbad27896f7ae9f(width, tableWidth.current);
150
+ widths.set(column.key, w);
151
+ remainingSpace -= w;
152
+ });
153
+ // dynamic columns
154
+ if (dynamicColumns.length > 0) {
155
+ const newColumnWidths = $30561577df230a30$export$a870e6692ac5ccb2(dynamicColumns, remainingSpace, tableWidth.current);
156
+ for (let column of newColumnWidths)widths.set(column.key, column.calculatedWidth);
157
+ }
158
+ return widths;
159
+ }, [
160
+ getStaticAndDynamicColumns,
161
+ getResolvedColumnWidth
162
+ ]);
163
+ const prevColKeys = columnsRef.current.map((col)=>col.key
164
+ );
165
+ const colKeys = columns1.map((col)=>col.key
166
+ );
167
+ // if the columns change, need to rebuild widths.
168
+ if (!colKeys.every((col, i)=>col === prevColKeys[i]
169
+ )) {
170
+ columnsRef.current = columns1;
171
+ const widths = buildColumnWidths(columns1, tableWidth.current);
172
+ setColumnWidthsForRef(widths);
173
+ }
174
+ function setTableWidth(width) {
175
+ if (width && width !== tableWidth.current) {
176
+ tableWidth.current = width;
177
+ if (!isResizing.current) {
178
+ const widths = buildColumnWidths(columns1, width);
179
+ setColumnWidthsForRef(widths);
180
+ }
181
+ }
182
+ }
183
+ function onColumnResizeStart() {
184
+ isResizing.current = true;
185
+ startResizeContentWidth.current = $30561577df230a30$export$f61abf052f87399f(columnWidthsRef.current);
186
+ }
187
+ function onColumnResize(column, width) {
188
+ let widthsObj = resizeColumn(column, width);
189
+ affectedColumnWidthsRef.current = widthsObj;
190
+ props.onColumnResize && props.onColumnResize(affectedColumnWidthsRef.current);
191
+ }
192
+ function onColumnResizeEnd() {
193
+ isResizing.current = false;
194
+ props.onColumnResizeEnd && props.onColumnResizeEnd(affectedColumnWidthsRef.current);
195
+ affectedColumnWidthsRef.current = [];
196
+ let widths = new Map(columnWidthsRef.current);
197
+ // Need to set the resizeBufferColumn or "spooky column" back to 0 since done resizing;
198
+ const bufferColumnKey = columnsRef.current[columnsRef.current.length - 1].key;
199
+ widths.set(bufferColumnKey, 0);
200
+ setColumnWidthsForRef(widths);
201
+ }
202
+ function resizeColumn(column1, newWidth) {
203
+ let boundedWidth = Math.max($30561577df230a30$export$f556054ce4358701(column1.props.minWidth, tableWidth.current), Math.min(Math.floor(newWidth), $30561577df230a30$export$59185c62a7544aa0(column1.props.maxWidth, tableWidth.current)));
204
+ // copy the columnWidths map and set the new width for the column being resized
205
+ let widths = new Map(columnWidthsRef.current);
206
+ widths.set(columnsRef.current[columnsRef.current.length - 1].key, 0);
207
+ widths.set(column1.key, boundedWidth);
208
+ // keep track of all columns that have been sized
209
+ resizedColumnsRef.current.add(column1.key);
210
+ setResizedColumns(resizedColumnsRef.current);
211
+ // get the columns affected by resize and remaining space
212
+ const resizeIndex = columnsRef.current.findIndex((col)=>col.key === column1.key
213
+ );
214
+ let affectedColumns = columnsRef.current.slice(resizeIndex + 1);
215
+ // we only care about the columns that CAN be resized, we ignore static columns.
216
+ let { dynamicColumns: dynamicColumns } = getStaticAndDynamicColumns(affectedColumns);
217
+ // available space for affected columns
218
+ let availableSpace = columnsRef.current.reduce((acc, column, index)=>{
219
+ if (index <= resizeIndex || $30561577df230a30$export$1994a077b98ee0d5(getResolvedColumnWidth(column))) return acc - widths.get(column.key);
220
+ return acc;
221
+ }, tableWidth.current);
222
+ // merge the unaffected column widths and the recalculated column widths
223
+ let recalculatedColumnWidths = buildColumnWidths(dynamicColumns, availableSpace);
224
+ widths = new Map([
225
+ ...widths,
226
+ ...recalculatedColumnWidths
227
+ ]);
228
+ if (startResizeContentWidth.current > tableWidth.current) widths.set(columnsRef.current[columnsRef.current.length - 1].key, Math.max(0, startResizeContentWidth.current - $30561577df230a30$export$f61abf052f87399f(widths)));
229
+ setColumnWidthsForRef(widths);
230
+ /*
231
+ when getting recalculated columns above, the column being resized is not considered "recalculated"
232
+ so we need to add it to the list of affected columns
233
+ */ let allAffectedColumns = [
234
+ [
235
+ column1.key,
236
+ boundedWidth
237
+ ],
238
+ ...recalculatedColumnWidths
239
+ ].map(([key, width])=>({
240
+ key: key,
241
+ width: width
242
+ })
243
+ );
244
+ return allAffectedColumns;
245
+ }
246
+ var ref2;
247
+ // This function is regenerated whenever columnWidthsRef.current changes in order to get the new correct ref value.
248
+ let getColumnWidth = $1BfjW$useCallback((key)=>(ref2 = columnWidthsRef.current.get(key)) !== null && ref2 !== void 0 ? ref2 : 0
249
+ , [
250
+ columnWidthsRef.current
251
+ ]);
252
+ let getColumnMinWidth = $1BfjW$useCallback((key)=>{
253
+ const columnIndex = columns1.findIndex((col)=>col.key === key
254
+ );
255
+ if (columnIndex === -1) return;
256
+ return $30561577df230a30$export$f556054ce4358701(columns1[columnIndex].props.minWidth, tableWidth.current);
257
+ }, [
258
+ columns1
259
+ ]);
260
+ let getColumnMaxWidth = $1BfjW$useCallback((key)=>{
261
+ const columnIndex = columns1.findIndex((col)=>col.key === key
262
+ );
263
+ if (columnIndex === -1) return;
264
+ return $30561577df230a30$export$59185c62a7544aa0(columns1[columnIndex].props.maxWidth, tableWidth.current);
265
+ }, [
266
+ columns1
267
+ ]);
268
+ return {
269
+ columnWidths: columnWidthsRef,
270
+ setTableWidth: setTableWidth,
271
+ onColumnResize: onColumnResize,
272
+ onColumnResizeStart: onColumnResizeStart,
273
+ onColumnResizeEnd: onColumnResizeEnd,
274
+ getColumnWidth: getColumnWidth,
275
+ getColumnMinWidth: getColumnMinWidth,
276
+ getColumnMaxWidth: getColumnMaxWidth,
277
+ isResizingColumn: isResizing.current
278
+ };
279
+ }
280
+
281
+
282
+
8
283
  var $4a0dd036d492cee4$exports = {};
9
284
 
10
285
  $parcel$export($4a0dd036d492cee4$exports, "useTableState", () => $4a0dd036d492cee4$export$907bcc6c48325fd6);
11
286
 
12
287
 
13
288
 
289
+
14
290
  const $788781baa30117fa$var$ROW_HEADER_COLUMN_KEY = 'row-header-column-' + Math.random().toString(36).slice(2);
15
- // const RESIZE_BUFFER_COLUMN_KEY = 'resize-buffer-column' + Math.random().toString(36).slice(2);
291
+ const $788781baa30117fa$var$RESIZE_BUFFER_COLUMN_KEY = 'resize-buffer-column' + Math.random().toString(36).slice(2);
16
292
  function $788781baa30117fa$var$buildHeaderRows(keyMap, columnNodes) {
17
293
  let columns = [];
18
294
  let seen = new Map();
@@ -216,35 +492,38 @@ class $788781baa30117fa$export$596e1b2e2cf93690 extends $1BfjW$GridCollection {
216
492
  for (let child of node.childNodes)visit(child);
217
493
  };
218
494
  for (let node1 of nodes)visit(node1);
219
- // if (Array.from(nodes).some(node => node.props?.allowsResizing)) {
220
- // /*
221
- // If the table content width > table width, a horizontal scroll bar is present.
222
- // If a user tries to resize a column, making it smaller while they are scrolled to the
223
- // end of the content horizontally, it shrinks the total table content width, causing
224
- // things to snap around and breaks the resize behavior.
225
- // To fix this, we add a resize buffer column (aka "spooky column") to the end of the table.
226
- // The width of this column defaults to 0. If you try and shrink a column and the width of the
227
- // table contents > table width, then the "spooky column" will grow to take up the difference
228
- // so that the total table content width remains constant while you are resizing. Once you
229
- // finish resizing, the "spooky column" snaps back to 0.
230
- // */
231
- // let resizeBufferColumn: GridNode<T> = {
232
- // type: 'column',
233
- // key: RESIZE_BUFFER_COLUMN_KEY,
234
- // value: null,
235
- // textValue: '',
236
- // level: 0,
237
- // index: columns.length,
238
- // hasChildNodes: false,
239
- // rendered: null,
240
- // childNodes: [],
241
- // props: {
242
- // isResizeBuffer: true,
243
- // defaultWidth: 0
244
- // }
245
- // };
246
- // columns.push(resizeBufferColumn);
247
- // }
495
+ if (Array.from(nodes).some((node)=>{
496
+ var ref;
497
+ return (ref = node.props) === null || ref === void 0 ? void 0 : ref.allowsResizing;
498
+ })) {
499
+ /*
500
+ If the table content width > table width, a horizontal scroll bar is present.
501
+ If a user tries to resize a column, making it smaller while they are scrolled to the
502
+ end of the content horizontally, it shrinks the total table content width, causing
503
+ things to snap around and breaks the resize behavior.
504
+
505
+ To fix this, we add a resize buffer column (aka "spooky column") to the end of the table.
506
+ The width of this column defaults to 0. If you try and shrink a column and the width of the
507
+ table contents > table width, then the "spooky column" will grow to take up the difference
508
+ so that the total table content width remains constant while you are resizing. Once you
509
+ finish resizing, the "spooky column" snaps back to 0.
510
+ */ let resizeBufferColumn = {
511
+ type: 'column',
512
+ key: $788781baa30117fa$var$RESIZE_BUFFER_COLUMN_KEY,
513
+ value: null,
514
+ textValue: '',
515
+ level: 0,
516
+ index: columns.length,
517
+ hasChildNodes: false,
518
+ rendered: null,
519
+ childNodes: [],
520
+ props: {
521
+ isResizeBuffer: true,
522
+ defaultWidth: 0
523
+ }
524
+ };
525
+ columns.push(resizeBufferColumn);
526
+ }
248
527
  let headerRows = $788781baa30117fa$var$buildHeaderRows(columnKeyMap, columns);
249
528
  headerRows.forEach((row, i)=>rows.splice(i, 0, row)
250
529
  );
@@ -289,7 +568,12 @@ function $4a0dd036d492cee4$export$907bcc6c48325fd6(props) {
289
568
  ...props,
290
569
  collection: collection
291
570
  });
292
- // const tableColumnResizeState = useTableColumnResizeState({columns: collection.columns, getDefaultWidth: props.getDefaultWidth, onColumnResize: props.onColumnResize, onColumnResizeEnd: props.onColumnResizeEnd});
571
+ const tableColumnResizeState = $292bc4e09cd0eb62$export$cb895dcf85db1319({
572
+ columns: collection.columns,
573
+ getDefaultWidth: props.getDefaultWidth,
574
+ onColumnResize: props.onColumnResize,
575
+ onColumnResizeEnd: props.onColumnResizeEnd
576
+ });
293
577
  return {
294
578
  collection: collection,
295
579
  disabledKeys: disabledKeys,
@@ -302,7 +586,8 @@ function $4a0dd036d492cee4$export$907bcc6c48325fd6(props) {
302
586
  column: columnKey,
303
587
  direction: direction !== null && direction !== void 0 ? direction : ((ref = props.sortDescriptor) === null || ref === void 0 ? void 0 : ref.column) === columnKey ? $4a0dd036d492cee4$var$OPPOSITE_SORT_DIRECTION[props.sortDescriptor.direction] : 'ascending'
304
588
  });
305
- }
589
+ },
590
+ ...tableColumnResizeState
306
591
  };
307
592
  }
308
593
 
@@ -520,5 +805,5 @@ let $941d1d9a6a28982a$export$f6f0c3fe4ec306ea = $941d1d9a6a28982a$var$Cell;
520
805
 
521
806
 
522
807
 
523
- export {$6555104ff085bef4$re_export$Section as Section, $4a0dd036d492cee4$export$907bcc6c48325fd6 as useTableState, $312ae3b56a94a86e$export$f850895b287ef28e as TableHeader, $4ae5314bf50db1a3$export$76ccd210b9029917 as TableBody, $1cd244557c2f97d5$export$816b5d811295e6bc as Column, $70d70eb16ea48428$export$b59bdbef9ce70de2 as Row, $941d1d9a6a28982a$export$f6f0c3fe4ec306ea as Cell};
808
+ export {$6555104ff085bef4$re_export$Section as Section, $292bc4e09cd0eb62$export$cb895dcf85db1319 as useTableColumnResizeState, $30561577df230a30$export$f61abf052f87399f as getContentWidth, $30561577df230a30$export$1994a077b98ee0d5 as isStatic, $30561577df230a30$export$7bbad27896f7ae9f as parseStaticWidth, $30561577df230a30$export$59185c62a7544aa0 as getMaxWidth, $30561577df230a30$export$f556054ce4358701 as getMinWidth, $30561577df230a30$export$a870e6692ac5ccb2 as getDynamicColumnWidths, $4a0dd036d492cee4$export$907bcc6c48325fd6 as useTableState, $312ae3b56a94a86e$export$f850895b287ef28e as TableHeader, $4ae5314bf50db1a3$export$76ccd210b9029917 as TableBody, $1cd244557c2f97d5$export$816b5d811295e6bc as Column, $70d70eb16ea48428$export$b59bdbef9ce70de2 as Row, $941d1d9a6a28982a$export$f6f0c3fe4ec306ea as Cell};
524
809
  //# sourceMappingURL=module.js.map