Skip to content

Commit

Permalink
feat(DataTable): add expanded param to exposed table state
Browse files Browse the repository at this point in the history
  • Loading branch information
ribeirojose committed May 2, 2024
1 parent c26a17a commit 35b9369
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
39 changes: 27 additions & 12 deletions src/components/DataTable/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
getSortedRowModel,
useReactTable,
} from "@tanstack/react-table";
import React, { useEffect } from "react";
import React, { useEffect, useMemo } from "react";
import { useNavigate } from "react-router-dom";

import { TableContext } from "./TableContext";
Expand Down Expand Up @@ -55,6 +55,7 @@ export function DataTable({
columnFilters,
sorting,
grouping,
expanded,
} = tableState;

const {
Expand All @@ -64,6 +65,7 @@ export function DataTable({
setColumnFilters,
setSorting,
setGrouping,
setExpanded,
} = setTableState;

if (error) {
Expand All @@ -90,6 +92,7 @@ export function DataTable({
rowSelection,
columnFilters,
pagination,
expanded,
columnVisibility: {
...columnVisibility,
...hiddenColumns,
Expand All @@ -106,6 +109,7 @@ export function DataTable({
onPaginationChange: setPagination,
manualPagination: true,
enableRowSelection: true,
onExpandedChange: setExpanded,
onRowSelectionChange: setRowSelection,
onSortingChange: setSorting,
onColumnFiltersChange: setColumnFilters,
Expand Down Expand Up @@ -146,17 +150,28 @@ export function DataTable({
navigate,
]);

// eslint-disable-next-line react/jsx-no-constructed-context-values
const contextValue = {
data,
filters,
error,
tableState,
setTableState,
table,
searchKey,
isLoading,
};
const contextValue = useMemo(
() => ({
data,
filters,
error,
tableState,
setTableState,
table,
searchKey,
isLoading,
}),
[
data,
filters,
error,
tableState,
setTableState,
table,
searchKey,
isLoading,
]
);

return (
<TableContext.Provider value={contextValue}>
Expand Down
2 changes: 1 addition & 1 deletion src/components/DataTable/useSWRDataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const dataTableFetcher = async ([pathOrUrl, paramsObject]) => {
};

export function useSWRDataTable(path, initialSearch = {}, options = {}) {
const { tableState, setTableState } = useTableState({ ...initialSearch });
const { tableState, setTableState } = useTableState(initialSearch);

const { data, error, isLoading } = useSWR(
[
Expand Down
5 changes: 5 additions & 0 deletions src/components/DataTable/useTableState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function useTableState(
const [sorting, setSorting] = useState(initialState.sorting || []);

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

const state = useMemo(
() => ({
Expand All @@ -28,6 +29,7 @@ export function useTableState(
columnFilters,
sorting,
grouping,
expanded,
}),
[
pagination,
Expand All @@ -36,6 +38,7 @@ export function useTableState(
columnFilters,
sorting,
grouping,
expanded,
]
);

Expand All @@ -47,6 +50,7 @@ export function useTableState(
setColumnFilters,
setSorting,
setGrouping,
setExpanded,
}),
[
setPagination,
Expand All @@ -55,6 +59,7 @@ export function useTableState(
setColumnFilters,
setSorting,
setGrouping,
setExpanded,
]
);

Expand Down

0 comments on commit 35b9369

Please sign in to comment.