Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions src/hooks/useColumnsAutoSize/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,17 @@ const table = useTable({

#### Options

| Name | Type | Default | Description |
| ----------------------- | --------- | ------- | --------------------------------------------------------------------------------------------- |
| `minWidth` | `number` | `50` | Minimum width in pixels for any column |
| `maxWidth` | `number` | `500` | Maximum width in pixels for any column |
| `padding` | `number` | `16` | Padding in pixels to add to content width for cells |
| `headerPadding` | `number` | `24` | Padding in pixels to add to content width for headers |
| `sampleSize` | `number` | `100` | Number of rows to sample for width calculation (helps with performance) |
| `measureHeaderText` | `boolean` | `true` | Whether to include header text in width calculation |
| `respectExistingWidths` | `boolean` | `true` | Whether to preserve columns with predefined widths (size, width, or matching minSize/maxSize) |
| `respectResizedWidths` | `boolean` | `true` | Whether to preserve column widths that were resized by the user |
| Name | Type | Default | Description |
| ----------------------- | --------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `minWidth` | `number` | `50` | Minimum width in pixels for any column |
| `maxWidth` | `number` | `500` | Maximum width in pixels for any column |
| `padding` | `number` | `16` | Padding in pixels to add to content width for cells |
| `headerPadding` | `number` | `24` | Padding in pixels to add to content width for headers |
| `sampleSize` | `number` | `100` | Number of rows to sample for width calculation (helps with performance) |
| `measureHeaderText` | `boolean` | `true` | Whether to include header text in width calculation |
| `respectExistingWidths` | `boolean` | `true` | Whether to preserve columns with predefined widths (size, width, or matching minSize/maxSize) |
| `respectResizedWidths` | `boolean` | `true` | Whether to preserve column widths that were resized by the user |
| `measureOnce` | `boolean` | `false` | If `true`, widths are calculated only once (on first data render) and are NOT recalculated when data changes (e.g. sorting/filtering). Prevents columns from jumping on sort |

#### Returns

Expand Down Expand Up @@ -137,7 +138,7 @@ const {columnWidths, isMeasuring, columnsWithAutoSizes, setTableInstance} = useA
2. **Sampling**: To optimize performance, it only samples a subset of rows (configurable via `sampleSize`).
3. **Optimization**: Different measuring techniques are used for text vs React components.
4. **Respect for User Actions**: Preserves user-resized column widths and pre-defined column widths.
5. **Reactivity**: Automatically updates when table data or columns change.
5. **Reactivity**: Automatically updates when table data or columns change (unless `measureOnce` is enabled).

## Troubleshooting

Expand Down Expand Up @@ -182,6 +183,19 @@ experimentalUseColumnsAutoSize({
});
```

### Columns jump / resize when sorting or changing data

By default the hook recalculates widths whenever the data changes (including sorting, since the sampled rows change). To measure widths only once and keep them stable afterwards, enable `measureOnce`:

```tsx
experimentalUseColumnsAutoSize({
columns,
options: {
measureOnce: true, // calculate once, never recalc on data/sort changes
},
});
```

### Some providers needed inside the cells (for example the redux provider)

If you want certain columns to keep their specified widths, set `respectExistingWidths` to true:
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useColumnsAutoSize/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type UseColumnsAutoSizeOptions = {
measureHeaderText?: boolean;
respectExistingWidths?: boolean;
respectResizedWidths?: boolean;
measureOnce?: boolean;
};

export type CalculateColumnWidthsArgs<TData extends unknown> = UseColumnsAutoSizeOptions & {
Expand Down
7 changes: 7 additions & 0 deletions src/hooks/useColumnsAutoSize/useColumnsAutoSize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export function useColumnsAutoSize<TData extends unknown>({
typeof useTable<TData>
> | null>(null);

const hasMeasuredRef = React.useRef<boolean>(false);

const rows = tableInstance?.getRowModel().rows ?? emptyRows;

const sampledRows = rows.slice(0, options?.sampleSize ?? 100);
Expand Down Expand Up @@ -135,6 +137,7 @@ export function useColumnsAutoSize<TData extends unknown>({

setColumnWidths(newWidths);
setIsMeasuring(false);
hasMeasuredRef.current = true;
},
100,
),
Expand All @@ -146,6 +149,10 @@ export function useColumnsAutoSize<TData extends unknown>({
return;
}

if (options?.measureOnce && hasMeasuredRef.current) {
return;
}

calculateWidths.cancel();
calculateWidths({
columnSizing,
Expand Down
Loading