Skip to content

Commit

Permalink
chore: add grouping state to data table
Browse files Browse the repository at this point in the history
  • Loading branch information
devjoaov committed Apr 12, 2024
1 parent d8ae302 commit c2af845
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
25 changes: 23 additions & 2 deletions src/components/DataTable/DataTable.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
getCoreRowModel,
getExpandedRowModel,
getFacetedRowModel,
getFacetedUniqueValues,
getGroupedRowModel,
getSortedRowModel,
useReactTable,
} from "@tanstack/react-table";
Expand All @@ -14,6 +16,7 @@ import { serializeQuery } from "#/lib/serializeQuery";
export function formatRequestParams(originalObj) {
return {
...originalObj,
grouping: originalObj.grouping.length > 0 ? originalObj.grouping[0] : null,
sorting: originalObj?.sorting?.length > 0 ? originalObj.sorting[0] : null,
columnFilters: originalObj.columnFilters.reduce((acc, filter) => {
acc[filter.id] = filter.value;
Expand Down Expand Up @@ -44,15 +47,22 @@ export function DataTable({
setQueryToParams,
setSelectedData,
}) {
const { pagination, rowSelection, columnVisibility, columnFilters, sorting } =
tableState;
const {
pagination,
rowSelection,
columnVisibility,
columnFilters,
sorting,
grouping,
} = tableState;

const {
setPagination,
setRowSelection,
setColumnVisibility,
setColumnFilters,
setSorting,
setGrouping,
} = setTableState;

if (error) {
Expand All @@ -74,6 +84,7 @@ export function DataTable({
pageCount: Math.ceil((data?.total ?? 0) / pagination.pageSize),
columns,
state: {
...(grouping.length > 0 ? { grouping } : {}),
sorting,
rowSelection,
columnFilters,
Expand All @@ -83,6 +94,14 @@ export function DataTable({
...hiddenColumns,
},
},
...(grouping.length > 0
? {
onGroupingChange: setGrouping,
getExpandedRowModel: getExpandedRowModel(),
getGroupedRowModel: getGroupedRowModel(),
autoResetExpanded: false,
}
: {}),
onPaginationChange: setPagination,
manualPagination: true,
enableRowSelection: true,
Expand Down Expand Up @@ -112,6 +131,7 @@ export function DataTable({
sorting: tableState.sorting,
pageIndex: tableState.pagination.pageIndex,
pageSize: tableState.pagination.pageSize,
grouping: tableState.grouping,
});

const params = serializeQuery(formattedParams);
Expand All @@ -121,6 +141,7 @@ export function DataTable({
tableState.pagination.pageSize,
tableState.sorting,
tableState.columnFilters,
tableState.grouping,
navigate,
]);

Expand Down
2 changes: 2 additions & 0 deletions src/components/DataTable/useSWRDataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { constructFullUrlWithParams } from "#/lib/constructFullUrlWithParams";
function formatRequestParams(originalObj) {
return {
...originalObj,
grouping: originalObj.grouping.length > 0 ? originalObj.grouping[0] : null,
sorting: originalObj?.sorting?.length > 0 ? originalObj.sorting[0] : null,
columnFilters: originalObj.columnFilters.reduce((acc, filter) => {
acc[filter.id] = filter.value;
Expand Down Expand Up @@ -40,6 +41,7 @@ export function useSWRDataTable(path, initialSearch = {}, options = {}) {
pageSize: tableState.pagination.pageSize,
sorting: tableState.sorting,
columnFilters: tableState.columnFilters,
grouping: tableState.grouping,
},
],
dataTableFetcher,
Expand Down
14 changes: 13 additions & 1 deletion src/components/DataTable/useTableState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,25 @@ export function useTableState(
);
const [sorting, setSorting] = useState(initialState.sorting || []);

const [grouping, setGrouping] = useState(initialState.grouping || []);

const state = useMemo(
() => ({
pagination,
rowSelection,
columnVisibility,
columnFilters,
sorting,
grouping,
}),
[pagination, rowSelection, columnVisibility, columnFilters, sorting]
[
pagination,
rowSelection,
columnVisibility,
columnFilters,
sorting,
grouping,
]
);

const handlers = useMemo(
Expand All @@ -36,13 +46,15 @@ export function useTableState(
setColumnVisibility,
setColumnFilters,
setSorting,
setGrouping,
}),
[
setPagination,
setRowSelection,
setColumnVisibility,
setColumnFilters,
setSorting,
setGrouping,
]
);

Expand Down

0 comments on commit c2af845

Please sign in to comment.