Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(DataTable): add expanded param to exposed table state #72

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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