From a673a3f3d39253462c9786fb2522ac9f1714d97a Mon Sep 17 00:00:00 2001 From: David Arranz Date: Tue, 20 Aug 2024 17:16:41 +0200 Subject: [PATCH] . --- .../app/quotes/components/QuotesDataTable.tsx | 37 +++++++++++++------ client/src/components/DataTable/DataTable.tsx | 15 +++++++- .../lib/hooks/useDataTable/useDataTable.tsx | 7 ++++ 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/client/src/app/quotes/components/QuotesDataTable.tsx b/client/src/app/quotes/components/QuotesDataTable.tsx index 1520ba5..61cdbf1 100644 --- a/client/src/app/quotes/components/QuotesDataTable.tsx +++ b/client/src/app/quotes/components/QuotesDataTable.tsx @@ -22,8 +22,7 @@ import { IListQuotes_Response_DTO, MoneyValue, UTCDateValue } from "@shared/cont import { ColumnDef, Row } from "@tanstack/react-table"; import { t } from "i18next"; import { FilePenLineIcon, MoreVerticalIcon } from "lucide-react"; -import { useMemo, useState } from "react"; -import { Trans } from "react-i18next"; +import { useEffect, useMemo, useState } from "react"; import { useNavigate } from "react-router-dom"; import { useQuotes } from "../hooks"; import { QuotePDFPreview } from "./QuotePDFPreview"; @@ -37,7 +36,13 @@ export const QuotesDataTable = ({ }) => { const navigate = useNavigate(); const { pagination, globalFilter, isFiltered } = useDataTableContext(); - const [focusedQuote, setFocusedQuote] = useState(undefined); + + const [activeRow, setActiveRow] = useState | undefined>(undefined); + + const resetActiveRowIndex = (id?: number) => { + setActiveRowIndex(id ? id : 0); + }; + const { useList } = useQuotes(); const { data, isPending, isError, error } = useList({ @@ -124,7 +129,7 @@ export const QuotesDataTable = ({ cell: ({ row }: { row: Row }) => (
- + -

- -

+

{t("quotes.list.columns.actions.edit")}

@@ -170,16 +173,27 @@ export const QuotesDataTable = ({ [] ); + const handleOnPaginationChange = () => { + //setActiveRow(undefined); + }; + const { table } = useDataTable({ data: data?.items ?? [], columns: columns, pageCount: data?.total_pages ?? -1, + onPaginationChange: handleOnPaginationChange, }); const handleOnRowClick = (row: Row) => { - setFocusedQuote(row.original); + setActiveRow(row); }; + useEffect(() => { + if (table && data && data?.total_pages > 0) { + setActiveRow(table.getRowModel().rows[0]); + } + }, [data, table]); + if (isError) { return ; } @@ -215,12 +229,13 @@ export const QuotesDataTable = ({ return ( - + @@ -228,7 +243,7 @@ export const QuotesDataTable = ({ {preview && } {preview && ( - + )} diff --git a/client/src/components/DataTable/DataTable.tsx b/client/src/components/DataTable/DataTable.tsx index 6905127..6ff3a8b 100644 --- a/client/src/components/DataTable/DataTable.tsx +++ b/client/src/components/DataTable/DataTable.tsx @@ -46,6 +46,7 @@ export type DataTableProps = PropsWithChildren<{ rowClassName?: string; cellClassName?: string; onRowClick?: (row: Row) => void; + activeRowIndex?: number; }>; export function DataTable({ @@ -62,6 +63,7 @@ export function DataTable({ rowClassName, cellClassName, onRowClick, + activeRowIndex, }: DataTableProps) { const headerVisible = headerOptions?.visible; @@ -106,11 +108,20 @@ export function DataTable({ {table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( (onRowClick ? onRowClick(row) : null)} + onClick={() => { + if (onRowClick) { + onRowClick(row); + } + }} tabIndex={0} key={row.id} data-state={row.getIsSelected() && "selected"} - className={cn(row.getIsSelected() ? "bg-accent" : "", rowClassName)} + className={cn( + row.getIsSelected() || activeRowIndex === row.index + ? "bg-accent cursor-pointer" + : "cursor-pointer", + rowClassName + )} > {row.getVisibleCells().map((cell) => ( diff --git a/client/src/lib/hooks/useDataTable/useDataTable.tsx b/client/src/lib/hooks/useDataTable/useDataTable.tsx index b11e73d..80c6a17 100644 --- a/client/src/lib/hooks/useDataTable/useDataTable.tsx +++ b/client/src/lib/hooks/useDataTable/useDataTable.tsx @@ -115,6 +115,8 @@ interface UseDataTableProps { * @type boolean */ enableAdvancedFilter?: boolean; + + onPaginationChange?: OnChangeFn; } export function useDataTable({ @@ -124,6 +126,7 @@ export function useDataTable({ enableSorting = false, enableHiding = false, enableRowSelection = false, + onPaginationChange, }: UseDataTableProps) { const { pagination, setPagination, sorting } = useDataTableContext(); @@ -136,6 +139,9 @@ export function useDataTable({ const newPagination = updater(pagination); setPagination(newPagination); } + if (onPaginationChange) { + onPaginationChange(updater); + } }; const sortingUpdater: OnChangeFn = (updater) => { @@ -157,6 +163,7 @@ export function useDataTable({ const table = useReactTable({ data, columns: getTableColumns(), + //autoResetPageIndex: true, pageCount: pageCount ?? -1, getCoreRowModel: getCoreRowModel(),