MCPcopy Create free account
hub / github.com/adobe/react-spectrum / calculateColumnSizes

Function calculateColumnSizes

packages/react-stately/src/table/TableUtils.ts:109–272  ·  view source on GitHub ↗
(
  availableWidth: number,
  columns: IColumn[],
  changedColumns: Map<Key, ColumnSize>,
  getDefaultWidth?: (index: number) => ColumnSize | null | undefined,
  getDefaultMinWidth?: (index: number) => ColumnSize | null | undefined
)

Source from the content-addressed store, hash-verified

107 * index.
108 */
109export function calculateColumnSizes(
110 availableWidth: number,
111 columns: IColumn[],
112 changedColumns: Map<Key, ColumnSize>,
113 getDefaultWidth?: (index: number) => ColumnSize | null | undefined,
114 getDefaultMinWidth?: (index: number) => ColumnSize | null | undefined
115): number[] {
116 let originalWidth = availableWidth;
117 let flooredWidth = Math.floor(availableWidth);
118 let hasFractionalWidth = availableWidth - flooredWidth > 0;
119 availableWidth = flooredWidth;
120 let hasNonFrozenItems = false;
121 let flexItems: FlexItem[] = columns.map((column, index) => {
122 let width: ColumnSize = (
123 changedColumns.get(column.key) != null
124 ? (changedColumns.get(column.key) ?? '1fr')
125 : (column.width ?? column.defaultWidth ?? getDefaultWidth?.(index) ?? '1fr')
126 ) as ColumnSize;
127 let frozen = false;
128 let baseSize = 0;
129 let flex = 0;
130 let targetMainSize = 0;
131 if (isStatic(width)) {
132 baseSize = parseStaticWidth(width, availableWidth);
133 frozen = true;
134 } else {
135 flex = parseFractionalUnit(width);
136 if (flex <= 0) {
137 frozen = true;
138 }
139 }
140
141 let min = getMinWidth(column.minWidth ?? getDefaultMinWidth?.(index) ?? 0, availableWidth);
142 let max = getMaxWidth(column.maxWidth, availableWidth);
143 let hypotheticalMainSize = Math.max(min, Math.min(baseSize, max));
144
145 // 9.7.1
146 // We don't make use of flex basis, it's always 0, so we are always in 'grow' mode.
147 // 9.7.2
148 if (frozen) {
149 targetMainSize = hypotheticalMainSize;
150 } else if (baseSize > hypotheticalMainSize) {
151 frozen = true;
152 targetMainSize = hypotheticalMainSize;
153 }
154
155 // 9.7.3
156 if (!frozen) {
157 hasNonFrozenItems = true;
158 }
159 return {
160 frozen,
161 baseSize,
162 hypotheticalMainSize,
163 min,
164 max,
165 flex,
166 targetMainSize,

Callers 2

TableUtils.test.jsFile · 0.90
buildColumnWidthsMethod · 0.90

Calls 7

isStaticFunction · 0.85
parseStaticWidthFunction · 0.85
parseFractionalUnitFunction · 0.85
getMinWidthFunction · 0.85
getMaxWidthFunction · 0.85
cascadeRoundingFunction · 0.85
toStringMethod · 0.65

Tested by

no test coverage detected