-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from bleu-fi/joao/click-684-novo-layout-para-m…
…onitoramento-tributario Refactor swrdatatable to be a composable table
- Loading branch information
Showing
11 changed files
with
345 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { | ||
getCoreRowModel, | ||
getFacetedRowModel, | ||
getFacetedUniqueValues, | ||
getSortedRowModel, | ||
useReactTable, | ||
} from "@tanstack/react-table"; | ||
import React, { useEffect } from "react"; | ||
|
||
import { useNavigate } from "react-router"; | ||
import { TableContext } from "./TableContext"; | ||
import { serializeQuery } from "#/lib/serializeQuery"; | ||
|
||
export function formatRequestParams(originalObj) { | ||
return { | ||
...originalObj, | ||
sorting: originalObj?.sorting?.length > 0 ? originalObj.sorting[0] : null, | ||
columnFilters: originalObj.columnFilters.reduce((acc, filter) => { | ||
acc[filter.id] = filter.value; | ||
return acc; | ||
}, {}), | ||
}; | ||
} | ||
|
||
const defaultFilterFn = (row, id, filterValue) => | ||
row.getValue(id).includes(filterValue); | ||
|
||
const buildColumns = (columnsConfig) => { | ||
if (!columnsConfig) return []; | ||
|
||
return columnsConfig.map((columnConfig) => ({ | ||
...columnConfig, | ||
filterFn: columnConfig.filterable ? defaultFilterFn : null, | ||
})); | ||
}; | ||
|
||
export function DataTable({ | ||
children, | ||
data, | ||
error, | ||
tableState, | ||
setTableState, | ||
buildTableColumns = buildColumns, | ||
setQueryToParams, | ||
setSelectedData, | ||
}) { | ||
const { pagination, rowSelection, columnVisibility, columnFilters, sorting } = | ||
tableState; | ||
|
||
const { | ||
setPagination, | ||
setRowSelection, | ||
setColumnVisibility, | ||
setColumnFilters, | ||
setSorting, | ||
} = setTableState; | ||
|
||
if (error) { | ||
return <div>Error: {error.message}</div>; | ||
} | ||
|
||
// @ts-ignore | ||
const columns = buildTableColumns(data?.columns, data?.filters); | ||
const filters = data?.filters; | ||
const searchKey = data?.search?.key; | ||
|
||
const hiddenColumns = columns | ||
.filter((c) => c.hide) | ||
.map((c) => ({ [c.accessorKey]: false })) | ||
.reduce((acc, obj) => Object.assign(acc, obj), {}); | ||
|
||
const table = useReactTable({ | ||
data: data?.data ?? [], | ||
pageCount: Math.ceil((data?.total ?? 0) / pagination.pageSize), | ||
columns, | ||
state: { | ||
sorting, | ||
rowSelection, | ||
columnFilters, | ||
pagination, | ||
columnVisibility: { | ||
...columnVisibility, | ||
...hiddenColumns, | ||
}, | ||
}, | ||
onPaginationChange: setPagination, | ||
manualPagination: true, | ||
enableRowSelection: true, | ||
onRowSelectionChange: setRowSelection, | ||
onSortingChange: setSorting, | ||
onColumnFiltersChange: setColumnFilters, | ||
onColumnVisibilityChange: setColumnVisibility, | ||
getCoreRowModel: getCoreRowModel(), | ||
getSortedRowModel: getSortedRowModel(), | ||
getFacetedRowModel: getFacetedRowModel(), | ||
getFacetedUniqueValues: getFacetedUniqueValues(), | ||
}); | ||
|
||
React.useEffect(() => { | ||
if (setSelectedData) | ||
setSelectedData( | ||
table.getSelectedRowModel().flatRows.map((row) => row.original) | ||
); | ||
}, [rowSelection, table, setSelectedData]); | ||
|
||
const navigate = useNavigate(); | ||
useEffect(() => { | ||
if (!setQueryToParams) return; | ||
|
||
const formattedParams = formatRequestParams({ | ||
columnFilters: tableState.columnFilters, | ||
sorting: tableState.sorting, | ||
pageIndex: tableState.pagination.pageIndex, | ||
pageSize: tableState.pagination.pageSize, | ||
}); | ||
|
||
const params = serializeQuery(formattedParams); | ||
navigate(`?${params}`, { replace: true }); | ||
}, [ | ||
tableState.pagination.pageIndex, | ||
tableState.pagination.pageSize, | ||
tableState.sorting, | ||
tableState.columnFilters, | ||
navigate, | ||
]); | ||
|
||
// eslint-disable-next-line react/jsx-no-constructed-context-values | ||
const contextValue = { | ||
data, | ||
filters, | ||
error, | ||
tableState, | ||
setTableState, | ||
table, | ||
searchKey, | ||
}; | ||
|
||
return ( | ||
<TableContext.Provider value={contextValue}> | ||
{children} | ||
</TableContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/components/DataTable/SWRDataTable/SWRDataTableBody.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* eslint-disable no-restricted-syntax */ | ||
import React from "react"; | ||
import { flexRender } from "@tanstack/react-table"; | ||
|
||
import { Trans } from "react-i18next"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { TableBody, TableCell, TableRow } from "#/components/ui"; | ||
|
||
import { useTableContext } from "../TableContext"; | ||
|
||
export function SWRDataTableBody({ hasDetails }) { | ||
// @ts-expect-error TS(2339) FIXME: Property 'table' does not exist on type '{}'. | ||
const { table } = useTableContext(); | ||
|
||
const navigate = useNavigate(); | ||
return ( | ||
<TableBody> | ||
{table.getRowModel().rows?.length ? ( | ||
table.getRowModel().rows.map((row) => ( | ||
<TableRow | ||
key={row.id} | ||
data-state={row.getIsSelected() && "selected"} | ||
onClick={() => { | ||
if (hasDetails) { | ||
navigate(`${row.original.id}`); | ||
} | ||
}} | ||
> | ||
{row.getVisibleCells().map((cell) => ( | ||
<TableCell key={cell.id}> | ||
{flexRender(cell.column.columnDef.cell, cell.getContext())} | ||
</TableCell> | ||
))} | ||
</TableRow> | ||
)) | ||
) : ( | ||
<TableRow> | ||
<TableCell className="h-24 text-center"> | ||
<Trans>No results found</Trans>. | ||
</TableCell> | ||
</TableRow> | ||
)} | ||
</TableBody> | ||
); | ||
} |
31 changes: 31 additions & 0 deletions
31
src/components/DataTable/SWRDataTable/SWRDataTableHeader.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* eslint-disable no-restricted-syntax */ | ||
import React from "react"; | ||
import { flexRender } from "@tanstack/react-table"; | ||
|
||
import { TableHead, TableHeader, TableRow } from "#/components/ui"; | ||
|
||
import { useTableContext } from "../TableContext"; | ||
|
||
export function SWRDataTableHeader() { | ||
// @ts-expect-error TS(2339) FIXME: Property 'table' does not exist on type '{}'. | ||
const { table } = useTableContext(); | ||
|
||
return ( | ||
<TableHeader> | ||
{table.getHeaderGroups().map((headerGroup) => ( | ||
<TableRow key={headerGroup.id}> | ||
{headerGroup.headers.map((header) => ( | ||
<TableHead key={header.id}> | ||
{header.isPlaceholder | ||
? null | ||
: flexRender( | ||
header.column.columnDef.header, | ||
header.getContext() | ||
)} | ||
</TableHead> | ||
))} | ||
</TableRow> | ||
))} | ||
</TableHeader> | ||
); | ||
} |
Oops, something went wrong.