From c2c2407b4fe4715cfc6307484e8a65ad1194532c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Gu=CC=88ell=20Segarra?= Date: Thu, 15 Aug 2024 10:27:00 +0200 Subject: [PATCH] feat: add sort feature to columns --- .../InfiniteTable/InfiniteTable.tsx | 45 +++++++++++++++++-- src/types/index.ts | 3 ++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/components/InfiniteTable/InfiniteTable.tsx b/src/components/InfiniteTable/InfiniteTable.tsx index ceabea7..42bf995 100644 --- a/src/components/InfiniteTable/InfiniteTable.tsx +++ b/src/components/InfiniteTable/InfiniteTable.tsx @@ -19,6 +19,7 @@ import { GridReadyEvent, IGetRowsParams, RowDoubleClickedEvent, + SortDirection, } from "ag-grid-community"; import { TableProps } from "@/types"; import { useDeepArrayMemo } from "@/hooks/useDeepArrayMemo"; @@ -33,7 +34,15 @@ export type InfiniteTableProps = Omit< TableProps, "dataSource" & "loading" & "loadingComponent" & "height" > & { - onRequestData: (startRow: number, endRow: number) => Promise; + onRequestData: ({ + startRow, + endRow, + sortFields, + }: { + startRow: number; + endRow: number; + sortFields?: Record; + }) => Promise; height?: number; onColumnChanged?: (columnsState: ColumnState[]) => void; onGetColumnsState?: () => ColumnState[] | undefined; @@ -157,6 +166,30 @@ const InfiniteTableComp = forwardRef( [onColumnChanged], ); + const onSortChanged = useCallback(() => { + gridRef.current?.api?.purgeInfiniteCache(); + }, []); + + const getSortedFields = useCallback((): + | Record + | undefined => { + const state = gridRef?.current?.api.getColumnState()!; + + const columnsWithSort = state.filter((col) => col.sort); + if (columnsWithSort.length === 0) { + return undefined; + } + const sortFields = columnsWithSort.reduce( + (acc, col) => ({ + ...acc, + [col.colId]: col.sort, + }), + {}, + ); + + return sortFields; + }, []); + const colDefs = useMemo((): ColDef[] => { const checkboxColumn = { checkboxSelection: true, @@ -186,7 +219,7 @@ const InfiniteTableComp = forwardRef( const restOfColumns = columns.map((column) => ({ field: column.key, - sortable: false, + sortable: column.isSortable, headerName: column.title, cellRenderer: column.render ? (cell: any) => column.render(cell.value) @@ -236,7 +269,11 @@ const InfiniteTableComp = forwardRef( async (params: IGetRowsParams) => { gridRef.current?.api.showLoadingOverlay(); const { startRow, endRow } = params; - const data = await onRequestData(startRow, endRow); + const data = await onRequestData({ + startRow, + endRow, + sortFields: getSortedFields(), + }); let lastRow = -1; if (data.length < endRow - startRow) { lastRow = startRow + data.length; @@ -284,6 +321,7 @@ const InfiniteTableComp = forwardRef( }, [ applyColumnState, + getSortedFields, hasStatusColumn, onGetSelectedRowKeys, onRequestData, @@ -370,6 +408,7 @@ const InfiniteTableComp = forwardRef( onBodyScroll={onBodyScroll} blockLoadDebounceMillis={DEBOUNCE_TIME} suppressDragLeaveHidesColumns={true} + onSortChanged={onSortChanged} /> {footer &&
{footer}
} diff --git a/src/types/index.ts b/src/types/index.ts index 5192936..eab55b9 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -6,6 +6,7 @@ export type TableColumn = { title: string; render?: (item: any) => React.ReactNode; sorter?: (a: any, b: any, column: string, desc: boolean) => number; + isSortable?: boolean; }; export type Sorter = { @@ -64,3 +65,5 @@ export type TableProps = { export interface TableRef { unselectAll: () => void; } + +export type SortDirection = "asc" | "desc" | null | undefined;