-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'infintescroll' into alpha
- Loading branch information
Showing
3 changed files
with
108 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export const getPersistedColumnState = ({ | ||
actualColumnKeys, | ||
persistedColumnState, | ||
}: { | ||
actualColumnKeys: string[]; | ||
persistedColumnState: any[]; | ||
}) => { | ||
if (!persistedColumnState) { | ||
return undefined; | ||
} | ||
const persistedColumnKeys = persistedColumnState.map( | ||
(col) => col.colId as string, | ||
); | ||
// we now have to sort both actualColumnKeys and persistedColumnKeys, and detect if there are differences | ||
const sortedActualColumnKeys = [...actualColumnKeys].sort(); | ||
const sortedPersistedColumnKeys = [...persistedColumnKeys].sort(); | ||
const areColumnKeysEqual = | ||
JSON.stringify(sortedActualColumnKeys) === | ||
JSON.stringify(sortedPersistedColumnKeys); | ||
if (!areColumnKeysEqual) { | ||
return undefined; | ||
} | ||
return persistedColumnState; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Column } from "ag-grid-community"; | ||
import { AgGridReact } from "ag-grid-react"; | ||
import { RefObject, useCallback, useRef } from "react"; | ||
|
||
export const useAutoFitColumns = ({ | ||
gridRef, | ||
containerRef, | ||
columnsPersistedStateRef, | ||
}: { | ||
gridRef: RefObject<AgGridReact>; | ||
containerRef: RefObject<HTMLDivElement>; | ||
columnsPersistedStateRef: RefObject<any>; | ||
}) => { | ||
const firstTimeResized = useRef(false); | ||
|
||
const remainingBlankSpace = useCallback( | ||
(allColumns: Array<Column<any>>) => { | ||
const totalColumnWidth = allColumns?.reduce( | ||
(acc, column) => acc + column.getActualWidth(), | ||
0, | ||
); | ||
const gridRefWidth = containerRef?.current?.clientWidth; | ||
if (!gridRefWidth || !totalColumnWidth) return 0; | ||
return gridRefWidth - totalColumnWidth; | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[], | ||
); | ||
|
||
const autoSizeColumnsIfNecessary = useCallback(() => { | ||
if (!columnsPersistedStateRef.current && !firstTimeResized.current) { | ||
firstTimeResized.current = true; | ||
setTimeout(() => { | ||
gridRef?.current?.api.autoSizeAllColumns(); | ||
const allColumns = gridRef?.current?.api.getAllGridColumns(); | ||
if (!allColumns) return; | ||
const blankSpace = remainingBlankSpace(allColumns); | ||
if (blankSpace > 0) { | ||
const spacePerColumn = blankSpace / (allColumns.length - 1); // we skip the first checkbox column, since it's not resizable | ||
const state = gridRef?.current?.api.getColumnState()!; | ||
const newState = state.map((col: any) => ({ | ||
...col, | ||
// colId 0 is the checkbox column | ||
width: col.colId !== "0" ? col.width + spacePerColumn : col.width, | ||
})); | ||
gridRef?.current?.api.applyColumnState({ state: newState }); | ||
} | ||
}, 50); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [remainingBlankSpace]); | ||
|
||
return { | ||
autoSizeColumnsIfNecessary, | ||
}; | ||
}; |