Skip to content

Commit

Permalink
fix: autosizing flex columns (#381)
Browse files Browse the repository at this point in the history
* Fix autosizing flex columns, flex columns should not be initially ignored
  • Loading branch information
matttdawson authored Aug 16, 2023
1 parent 4e33f40 commit d759f98
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/components/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export const Grid = ({

const skipHeader = sizeColumns === "auto-skip-headers" && !isEmpty(params.rowData);
if (sizeColumns === "auto" || skipHeader) {
const result = autoSizeColumns({ skipHeader, userSizedColIds: userSizedColIds.current });
const result = autoSizeColumns({ skipHeader, userSizedColIds: userSizedColIds.current, includeFlex: true });
if (isEmpty(params.rowData)) {
if (!hasSetContentSizeEmpty.current && result && !hasSetContentSize.current) {
hasSetContentSizeEmpty.current = true;
Expand Down
1 change: 1 addition & 0 deletions src/contexts/GridContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface AutoSizeColumnsProps {
skipHeader?: boolean;
colIds?: Set<string> | string[];
userSizedColIds?: Set<string>;
includeFlex?: boolean;
}

export type AutoSizeColumnsResult = { width: number } | null;
Expand Down
8 changes: 6 additions & 2 deletions src/contexts/GridContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,16 @@ export const GridContextProvider = <RowType extends GridBaseRow>(props: PropsWit
* Resize columns to fit container
*/
const autoSizeColumns = useCallback(
({ skipHeader, colIds, userSizedColIds }: AutoSizeColumnsProps = {}): AutoSizeColumnsResult => {
({ skipHeader, colIds, userSizedColIds, includeFlex }: AutoSizeColumnsProps = {}): AutoSizeColumnsResult => {
if (!columnApi) return null;
const colIdsSet = colIds instanceof Set ? colIds : new Set(colIds);
const colsToResize = columnApi.getColumnState().filter((colState) => {
const colId = colState.colId;
return (isEmpty(colIdsSet) || colIdsSet.has(colId)) && !userSizedColIds?.has(colId) && !colState.flex;
return (
(isEmpty(colIdsSet) || colIdsSet.has(colId)) &&
!userSizedColIds?.has(colId) &&
(includeFlex || !colState.flex)
);
});
if (!isEmpty(colsToResize)) {
columnApi.autoSizeColumns(
Expand Down

0 comments on commit d759f98

Please sign in to comment.