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

Function calculateColumnSizes

packages/react-stately/src/table/TableUtils.ts:109–258  ·  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 hasNonFrozenItems = false;
117 let flexItems: FlexItem[] = columns.map((column, index) => {
118 let width: ColumnSize = (
119 changedColumns.get(column.key) != null
120 ? (changedColumns.get(column.key) ?? '1fr')
121 : (column.width ?? column.defaultWidth ?? getDefaultWidth?.(index) ?? '1fr')
122 ) as ColumnSize;
123 let frozen = false;
124 let baseSize = 0;
125 let flex = 0;
126 let targetMainSize = 0;
127 if (isStatic(width)) {
128 baseSize = parseStaticWidth(width, availableWidth);
129 frozen = true;
130 } else {
131 flex = parseFractionalUnit(width);
132 if (flex <= 0) {
133 frozen = true;
134 }
135 }
136
137 let min = getMinWidth(column.minWidth ?? getDefaultMinWidth?.(index) ?? 0, availableWidth);
138 let max = getMaxWidth(column.maxWidth, availableWidth);
139 let hypotheticalMainSize = Math.max(min, Math.min(baseSize, max));
140
141 // 9.7.1
142 // We don't make use of flex basis, it's always 0, so we are always in 'grow' mode.
143 // 9.7.2
144 if (frozen) {
145 targetMainSize = hypotheticalMainSize;
146 } else if (baseSize > hypotheticalMainSize) {
147 frozen = true;
148 targetMainSize = hypotheticalMainSize;
149 }
150
151 // 9.7.3
152 if (!frozen) {
153 hasNonFrozenItems = true;
154 }
155 return {
156 frozen,
157 baseSize,
158 hypotheticalMainSize,
159 min,
160 max,
161 flex,
162 targetMainSize,
163 violation: 0
164 };
165 });
166

Callers 2

TableUtils.test.jsFile · 0.90
buildColumnWidthsMethod · 0.90

Calls 6

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

Tested by

no test coverage detected