@react-stately/table 3.11.9-nightly.4674 → 3.11.9-nightly.4683

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.
@@ -13,9 +13,7 @@
13
13
  import {
14
14
  calculateColumnSizes,
15
15
  getMaxWidth,
16
- getMinWidth,
17
- isStatic,
18
- parseFractionalUnit
16
+ getMinWidth
19
17
  } from './TableUtils';
20
18
  import {ColumnSize, TableCollection} from '@react-types/table';
21
19
  import {GridNode} from '@react-types/grid';
@@ -80,78 +78,25 @@ export class TableColumnLayout<T> {
80
78
  return this.columnMaxWidths.get(key) ?? 0;
81
79
  }
82
80
 
83
- resizeColumnWidth(tableWidth: number, collection: TableCollection<T>, controlledWidths: Map<Key, ColumnSize>, uncontrolledWidths: Map<Key, ColumnSize>, col = null, width: number): Map<Key, ColumnSize> {
81
+ resizeColumnWidth(collection: TableCollection<T>, uncontrolledWidths: Map<Key, ColumnSize>, col: Key, width: number): Map<Key, ColumnSize> {
84
82
  let prevColumnWidths = this.columnWidths;
85
- // resizing a column
86
- let resizeIndex = Infinity;
87
- let resizingChanged = new Map<Key, ColumnSize>([...controlledWidths, ...uncontrolledWidths]);
88
- let percentKeys = new Map();
89
- let frKeysToTheRight = new Map();
90
- let minWidths = new Map();
91
- // freeze columns to the left to their previous pixel value
92
- collection.columns.forEach((column, i) => {
93
- let frKey;
94
- let frValue;
95
- minWidths.set(column.key, this.getDefaultMinWidth(collection.columns[i]));
96
- if (col !== column.key && !column.props.width && !isStatic(uncontrolledWidths.get(column.key))) {
97
- // uncontrolled don't have props.width for us, so instead get from our state
98
- frKey = column.key;
99
- frValue = parseFractionalUnit(uncontrolledWidths.get(column.key) as string);
100
- } else if (col !== column.key && !isStatic(column.props.width) && !uncontrolledWidths.get(column.key)) {
101
- // controlledWidths will be the same in the collection
102
- frKey = column.key;
103
- frValue = parseFractionalUnit(column.props.width);
104
- } else if (col !== column.key && column.props.width?.endsWith?.('%')) {
105
- percentKeys.set(column.key, column.props.width);
106
- }
107
- // don't freeze columns to the right of the resizing one
108
- if (resizeIndex < i) {
109
- if (frKey) {
110
- frKeysToTheRight.set(frKey, frValue);
111
- }
112
- return;
113
- }
114
- // we already know the new size of the resizing column
115
- if (column.key === col) {
116
- resizeIndex = i;
117
- resizingChanged.set(column.key, Math.floor(width));
118
- return;
119
- }
120
- // freeze column to previous value
121
- resizingChanged.set(column.key, prevColumnWidths.get(column.key));
122
- });
123
-
124
- // predict pixels sizes for all columns based on resize
125
- let columnWidths = calculateColumnSizes(
126
- tableWidth,
127
- collection.columns.map(col => ({...col.props, key: col.key})),
128
- resizingChanged,
129
- (i) => this.getDefaultWidth(collection.columns[i]),
130
- (i) => this.getDefaultMinWidth(collection.columns[i])
131
- );
132
-
133
- // set all new column widths for onResize event
134
- // columns going in will be the same order as the columns coming out
83
+ let freeze = true;
135
84
  let newWidths = new Map<Key, ColumnSize>();
136
- // set all column widths based on calculateColumnSize
137
- columnWidths.forEach((width, index) => {
138
- let key = collection.columns[index].key;
139
- newWidths.set(key, width);
140
- });
141
85
 
142
- // add FR's back as they were to columns to the right
143
- Array.from(frKeysToTheRight).forEach(([key]) => {
144
- newWidths.set(key, `${frKeysToTheRight.get(key)}fr`);
145
- });
86
+ width = Math.max(this.getColumnMinWidth(col), Math.min(this.getColumnMaxWidth(col), Math.floor(width)));
146
87
 
147
- // put back in percents
148
- Array.from(percentKeys).forEach(([key, width]) => {
149
- // resizing locks a column to a px width
150
- if (key === col) {
151
- return;
88
+ collection.columns.forEach(column => {
89
+ if (column.key === col) {
90
+ newWidths.set(column.key, width);
91
+ freeze = false;
92
+ } else if (freeze) {
93
+ // freeze columns to the left to their previous pixel value
94
+ newWidths.set(column.key, prevColumnWidths.get(column.key));
95
+ } else {
96
+ newWidths.set(column.key, column.props.width ?? uncontrolledWidths.get(column.key));
152
97
  }
153
- newWidths.set(key, width);
154
98
  });
99
+
155
100
  return newWidths;
156
101
  }
157
102
 
@@ -47,7 +47,9 @@ export interface TableColumnResizeState<T> {
47
47
  /** Key of the currently resizing column. */
48
48
  resizingColumn: Key | null,
49
49
  /** A reference to the table state. */
50
- tableState: TableState<T>
50
+ tableState: TableState<T>,
51
+ /** A map of the current column widths. */
52
+ columnWidths: Map<Key, number>
51
53
  }
52
54
 
53
55
  /**
@@ -105,20 +107,18 @@ export function useTableColumnResizeState<T>(props: TableColumnResizeStateProps<
105
107
  }, [setResizingColumn]);
106
108
 
107
109
  let updateResizedColumns = useCallback((key: Key, width: number): Map<Key, ColumnSize> => {
108
- let newControlled = new Map(Array.from(controlledColumns).map(([key, entry]) => [key, entry.props.width]));
109
- let newSizes = columnLayout.resizeColumnWidth(tableWidth, state.collection, newControlled, uncontrolledWidths, key, width);
110
-
110
+ let newSizes = columnLayout.resizeColumnWidth(state.collection, uncontrolledWidths, key, width);
111
111
  let map = new Map(Array.from(uncontrolledColumns).map(([key]) => [key, newSizes.get(key)]));
112
112
  map.set(key, width);
113
113
  setUncontrolledWidths(map);
114
114
  return newSizes;
115
- }, [controlledColumns, uncontrolledColumns, setUncontrolledWidths, tableWidth, columnLayout, state.collection, uncontrolledWidths]);
115
+ }, [uncontrolledColumns, setUncontrolledWidths, columnLayout, state.collection, uncontrolledWidths]);
116
116
 
117
117
  let endResize = useCallback(() => {
118
118
  setResizingColumn(null);
119
119
  }, [setResizingColumn]);
120
120
 
121
- useMemo(() =>
121
+ let columnWidths = useMemo(() =>
122
122
  columnLayout.buildColumnWidths(tableWidth, state.collection, colWidths)
123
123
  , [tableWidth, state.collection, colWidths, columnLayout]);
124
124
 
@@ -133,9 +133,11 @@ export function useTableColumnResizeState<T>(props: TableColumnResizeStateProps<
133
133
  columnLayout.getColumnMinWidth(key),
134
134
  getColumnMaxWidth: (key: Key) =>
135
135
  columnLayout.getColumnMaxWidth(key),
136
- tableState: state
136
+ tableState: state,
137
+ columnWidths
137
138
  }), [
138
139
  columnLayout,
140
+ columnWidths,
139
141
  resizingColumn,
140
142
  updateResizedColumns,
141
143
  startResize,
@@ -73,7 +73,7 @@ export function UNSTABLE_useTreeGridState<T extends object>(props: TreeGridState
73
73
  }), [children, showSelectionCheckboxes, selectionMode, showDragButtons]);
74
74
 
75
75
  let builder = useMemo(() => new CollectionBuilder<T>(), []);
76
- let nodes = useMemo(() => builder.build({children: children as ReactElement[]}, context), [builder, children, context]);
76
+ let nodes = useMemo(() => builder.build({children: children as ReactElement<any>[]}, context), [builder, children, context]);
77
77
  let treeGridCollection = useMemo(() => {
78
78
  return generateTreeGridCollection<T>(nodes, {showSelectionCheckboxes, showDragButtons, expandedKeys});
79
79
  }, [nodes, showSelectionCheckboxes, showDragButtons, expandedKeys]);