Skip to content

Commit

Permalink
Merge pull request #72 from bleu-fi/jose/click-805-open-thesis-when-t…
Browse files Browse the repository at this point in the history
…hesesid-page-is-accessed

feat(DataTable): add expanded param to exposed table state
  • Loading branch information
ribeirojose authored May 3, 2024
2 parents c26a17a + 35b9369 commit 610f874
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 610f874

Please sign in to comment.