Skip to content

Commit

Permalink
Merge branch 'fix/adjust-sort-columns-restoration' into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
mguellsegarra committed Dec 29, 2024
2 parents f155d5e + 23fd3a3 commit 19b381f
Showing 1 changed file with 22 additions and 36 deletions.
58 changes: 22 additions & 36 deletions src/components/InfiniteTable/InfiniteTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
GridReadyEvent,
IGetRowsParams,
RowDoubleClickedEvent,
SortDirection,
} from "ag-grid-community";
import { TableProps } from "@/types";
import { useDeepArrayMemo } from "@/hooks/useDeepArrayMemo";
Expand All @@ -40,11 +39,11 @@ export type InfiniteTableProps = Omit<
onRequestData: ({
startRow,
endRow,
sortFields,
state,
}: {
startRow: number;
endRow: number;
sortFields?: Record<string, SortDirection>;
state?: ColumnState[];
}) => Promise<any[] | undefined>;
height?: number;
onColumnChanged?: (columnsState: ColumnState[]) => void;
Expand All @@ -62,6 +61,7 @@ export type InfiniteTableProps = Omit<
statusComponent?: (status: any) => ReactNode;
strings?: Record<string, string>;
showPointerCursorInRows?: boolean;
initialSortState?: ColumnState[];
};

export type InfiniteTableRef = {
Expand Down Expand Up @@ -95,6 +95,7 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
hasStatusColumn = false,
strings = {},
showPointerCursorInRows = true,
initialSortState,
} = props;

const gridRef = useRef<AgGridReact>(null);
Expand Down Expand Up @@ -206,29 +207,6 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
[debouncedOnColumnChanged],
);

const getSortedFields = useCallback(():
| Record<string, SortDirection>
| undefined => {
const state = gridRef?.current?.api.getColumnState()!;

const columnsWithSort = state
.filter((col) => col.sort)
.sort((a, b) => (a.sortIndex || 0) - (b.sortIndex || 0));

if (columnsWithSort.length === 0) {
return undefined;
}
const sortFields = columnsWithSort.reduce(
(acc, col) => ({
...acc,
[col.colId]: col.sort,
}),
{},
);

return sortFields;
}, []);

const MemoizedStatusComponent = useMemo(() => {
if (!statusComponent) return undefined;
// eslint-disable-next-line react/display-name
Expand Down Expand Up @@ -258,14 +236,22 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
const storedState = columnsPersistedStateRef.current;
const storedStateKeys = storedState?.map((col: any) => col.colId);

const restOfColumns: ColDef[] = columns.map((column) => ({
field: column.key,
sortable: column.isSortable,
headerName: column.title,
cellRenderer: column.render
? (cell: any) => column.render(cell.value)
: undefined,
}));
const restOfColumns: ColDef[] = columns.map((column) => {
const initialSort = initialSortState?.find(
(state) => state.colId === column.key,
);

return {
field: column.key,
sortable: column.isSortable,
headerName: column.title,
sort: initialSort?.sort,
sortIndex: initialSort?.sortIndex,
cellRenderer: column.render
? (cell: any) => column.render(cell.value)
: undefined,
};
});

// restOfColumns should be sorted by the order of the storedState
storedState &&
Expand Down Expand Up @@ -314,6 +300,7 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
totalRows,
selectedRowKeys?.length,
onSelectionCheckboxClicked,
initialSortState,
strings,
applyAndUpdateNewState,
applyAutoFitState,
Expand Down Expand Up @@ -351,7 +338,7 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
const data = await onRequestData({
startRow,
endRow,
sortFields: getSortedFields(),
state: gridRef.current?.api.getColumnState(),
});

if (!data) {
Expand Down Expand Up @@ -408,7 +395,6 @@ const InfiniteTableComp = forwardRef<InfiniteTableRef, InfiniteTableProps>(
},
[
onRequestData,
getSortedFields,
hasStatusColumn,
selectedRowKeys,
columnsPersistedStateRef,
Expand Down

0 comments on commit 19b381f

Please sign in to comment.