+
) : (
column.setFilterValue(e.target.value)}
- placeholder="Search..."
- className="w-full pl-2 -mt-2 text-sm font-medium border rounded-lg"
- />
-);
+ type="text"
+ value={(columnFilterValue ?? "") as string}
+ onClick={stopPropagation}
+ onChange={(e) => column.setFilterValue(e.target.value)}
+ placeholder="Search..."
+ className="w-2/3 h-5 pl-1 text-xs font-medium text-gray-600 border border-gray-300 rounded-2xl placeholder:text-gray-400"
+ />
+ );
};
+
export default FilterTable;
\ No newline at end of file
diff --git a/src/ui/table/Table.tsx b/src/ui/table/Table.tsx
index cbea7aa..f2e6e85 100644
--- a/src/ui/table/Table.tsx
+++ b/src/ui/table/Table.tsx
@@ -1,16 +1,15 @@
import React, { useEffect, useState } from 'react';
import {
- useReactTable,
- getCoreRowModel,
- getSortedRowModel,
- flexRender,
- ColumnDef,
- SortingState,
- ColumnFiltersState,
- getFilteredRowModel,
- getPaginationRowModel,
+ useReactTable,
+ getCoreRowModel,
+ getSortedRowModel,
+ flexRender,
+ ColumnDef,
+ SortingState,
+ ColumnFiltersState,
+ getFilteredRowModel,
+ getPaginationRowModel,
} from '@tanstack/react-table';
-
import { Colors } from "../../utils/enums";
import FilterTable from './FilterTable';
import Footer from '../table/Footer';
@@ -20,230 +19,215 @@ import EditableCell from './EditableCell';
export type textSize = "xxs" | "xs" | "sm" | "base" | "lg";
type TableProps
= {
- data?: TData[];
- id?: string;
- columns: ColumnDef[];
- enableSorting?: boolean;
- enableColumnFilters?: boolean;
- headerColor?: Colors;
- headerTextSize?: textSize;
- className?: string;
- pageSize?: number;
- pinFirstColumn?: boolean;
- pinLastColumn?: boolean;
- enableRowSelection?: boolean;
- selectedRow?: Record;
- columnVisibility?: Record;
- onRowSelectionChange?: (selectedState: Record) => void;
- onRowClick?: (row: TData) => void;
- getRowStyles?: (row: TData) => React.CSSProperties | undefined;
- getRowClasses?: (row: TData) => string | undefined;
- onCellEdit?: (rowIndex: string | number, columnId: any, value: any) => void
- getRowId?: (originalRow: TData, index: number) => string
+ data?: TData[];
+ id?: string;
+ columns: ColumnDef[];
+ enableSorting?: boolean;
+ enableColumnFilters?: boolean;
+ headerColor?: Colors;
+ headerTextSize?: textSize;
+ className?: string;
+ pageSize?: number;
+ pinFirstColumn?: boolean;
+ pinLastColumn?: boolean;
+ enableRowSelection?: boolean;
+ selectedRow?: Record;
+ columnVisibility?: Record;
+ onRowSelectionChange?: (selectedState: Record) => void;
+ onRowClick?: (row: TData) => void;
+ getRowStyles?: (row: TData) => React.CSSProperties | undefined;
+ getRowClasses?: (row: TData) => string | undefined;
+ onCellEdit?: (rowIndex: string | number, columnId: any, value: any) => void;
+ getRowId?: (originalRow: TData, index: number) => string;
};
function Table({
- data = [],
- columns,
- id = 'id',
- enableSorting = false,
- enableColumnFilters = false,
- headerColor = Colors.white,
- className,
- pageSize = 10,
- headerTextSize = "sm",
- pinFirstColumn = false,
- pinLastColumn = false,
- enableRowSelection = false,
- selectedRow = {},
- columnVisibility = {},
- onRowSelectionChange = (selectedState: Record) => { return null },
- onRowClick,
- getRowStyles,
- getRowClasses = (row) => 'bg-indigo-50',
- onCellEdit = () => { },
- getRowId = (originalRow, index) => { return originalRow?.[id] ?? index }
+ data = [],
+ columns,
+ id = 'id',
+ enableSorting = false,
+ enableColumnFilters = false,
+ headerColor = Colors.white,
+ className,
+ pageSize = 10,
+ headerTextSize = "sm",
+ pinFirstColumn = false,
+ pinLastColumn = false,
+ enableRowSelection = false,
+ selectedRow = {},
+ columnVisibility = {},
+ onRowSelectionChange = (selectedState: Record) => { return null },
+ onRowClick,
+ getRowStyles,
+ getRowClasses = (row) => 'bg-indigo-50',
+ onCellEdit = () => { },
+ getRowId = (originalRow, index) => { return originalRow?.[id] ?? index },
}: TableProps) {
- const [sorting, setSorting] = useState([]);
- const [columnFilters, setColumnFilters] = useState([]);
- const [rowSelection, setRowSelection] = useState(selectedRow)
- const [pagination, setPagination] = useState({
- pageIndex: 0,
- pageSize: pageSize,
- });
+ const [sorting, setSorting] = useState([]);
+ const [columnFilters, setColumnFilters] = useState([]);
+ const [rowSelection, setRowSelection] = useState(selectedRow);
+ const [pagination, setPagination] = useState({
+ pageIndex: 0,
+ pageSize: pageSize,
+ });
+
+ useEffect(() => {
+ onRowSelectionChange(rowSelection);
+ }, [JSON.stringify(rowSelection)]);
+
+ const handlePageSizeChange = (newPageSize: number) => {
+ setPagination((prev) => ({
+ ...prev,
+ pageSize: newPageSize,
+ }));
+ };
- useEffect(() => {
- onRowSelectionChange(rowSelection)
- }, [JSON.stringify(rowSelection)])
+ const selectionColumn: ColumnDef = {
+ id: 'selection',
+ header: ({ table }) => (
+
+ ),
+ cell: ({ row }) => (
+ e.stopPropagation()}
+ />
+ ),
+ };
- const handlePageSizeChange = (newPageSize: number) => {
- setPagination((prev) => ({
- ...prev,
- pageSize: newPageSize,
- }));
- };
+ const tableColumns = enableRowSelection ? [selectionColumn, ...columns] : columns;
- const selectionColumn: ColumnDef = {
- id: 'selection',
- header: ({ table }) => (
-
- ),
- cell: ({ row }) => (
- e.stopPropagation()}
- />
- ),
- };
+ const table = useReactTable({
+ data,
+ columns: tableColumns,
+ defaultColumn: {
+ cell: EditableCell,
+ },
+ state: {
+ sorting,
+ columnFilters,
+ pagination,
+ rowSelection: selectedRow,
+ columnVisibility: columnVisibility
+ },
+ getRowId: getRowId,
+ onPaginationChange: setPagination,
+ onSortingChange: setSorting,
+ onColumnFiltersChange: setColumnFilters,
+ onRowSelectionChange: setRowSelection,
+ getPaginationRowModel: getPaginationRowModel(),
+ getFilteredRowModel: getFilteredRowModel(),
+ getCoreRowModel: getCoreRowModel(),
+ getSortedRowModel: getSortedRowModel(),
+ enableRowSelection,
+ enableColumnFilters,
+ enableSorting,
+ meta: {
+ getRowStyles: (row: any) => {
+ const styles = getRowStyles ? getRowStyles(row) : undefined;
+ return styles;
+ },
+ getRowClasses: (row: any) => {
+ const classes = getRowClasses ? getRowClasses(row) : undefined;
+ return classes;
+ },
+ updateData: (
+ rowIndex: string | number,
+ columnId: any,
+ value: any
+ ) => {
+ onCellEdit(rowIndex, columnId, value);
+ }
+ }
+ });
- const tableColumns = enableRowSelection ? [selectionColumn, ...columns] : columns;
+ const textXXS = "text-[0.491rem]";
+ const headerText = headerTextSize === "xxs" ? textXXS : `text-${headerTextSize}`;
- const table = useReactTable({
- data,
- columns: tableColumns,
- defaultColumn: {
- cell: EditableCell,
- },
- state: {
- sorting,
- columnFilters,
- pagination,
- rowSelection: selectedRow,
- columnVisibility: columnVisibility
- },
- getRowId: getRowId,
- onPaginationChange: setPagination,
- onSortingChange: setSorting,
- onColumnFiltersChange: setColumnFilters,
- onRowSelectionChange: setRowSelection,
- getPaginationRowModel: getPaginationRowModel(),
- getFilteredRowModel: getFilteredRowModel(),
- getCoreRowModel: getCoreRowModel(),
- getSortedRowModel: getSortedRowModel(),
- enableRowSelection,
- enableColumnFilters,
- enableSorting,
- meta: {
- getRowStyles: (row: any) => {
- const styles = getRowStyles ? getRowStyles(row) : undefined;
- return styles;
- },
- getRowClasses: (row: any) => {
- const classes = getRowClasses ? getRowClasses(row) : undefined;
- return classes;
- },
- updateData: (
- rowIndex: string | number,
- columnId: any,
- value: any
- ) => {
- onCellEdit(rowIndex, columnId, value)
- }
- }
- });
+ const firstColumnClass = `sticky left-0 bg-${headerColor} border-b border-gray-300 text-center`;
+ const lastColumnClass = "sticky right-0 bg-white";
- const textXXS = "text-[0.491rem]";
- const headerClass = `bg-${headerColor}`;
- const headerText = headerTextSize === "xxs" ? `${textXXS}` : `text-${headerTextSize}`;
- const firstColumnClass = `sticky left-0 ${headerClass}`;
- const lastColumnClass = `sticky right-0 bg-white`;
+ const getColumnClasses = (index: number, length: number) => {
+ if (pinFirstColumn && index === 0) return firstColumnClass;
+ if (pinLastColumn && index === length - 1) return lastColumnClass;
+ return '';
+ };
- const getColumnClasses = (index: number, length: number) => {
- if (pinFirstColumn && index === 0) return firstColumnClass;
- if (pinLastColumn && index === length - 1) return lastColumnClass;
- return '';
- };
+ return (
+
+
+
+ {table.getHeaderGroups().map((headerGroup) => (
+
+ {headerGroup.headers.map((header, index) => (
+
+
+ {/* Left aligned header text */}
+
+ {flexRender(header.column.columnDef.header, header.getContext())}
+
+ {enableSorting && header.column.getCanSort() && (
+
+ {header.column.getIsSorted() === 'desc' ? : }
+
+ )}
+
+ {/* Render filter directly below the header */}
+ {header.column.getCanFilter() && (
+
+
+
+ )}
+ |
+ ))}
+
+ ))}
+
- return (
-
-
-
- {table.getHeaderGroups().map(headerGroup => (
-
-
- {headerGroup.headers.map((header, index) => (
-
-
- {flexRender(header.column.columnDef.header, header.getContext())}
- {enableSorting && header.column.getCanSort() && (
-
- {header.column.getIsSorted() === 'desc' ? (
-
- ) : (
-
- )}
-
- )}
-
- |
- ))}
-
- {headerGroup.headers.some(header => header.column.getCanFilter()) && (
-
- {headerGroup.headers.map((header, index) => (
-
- {header.column.getCanFilter() ? (
- e.stopPropagation()}>
-
-
- ) : null}
- |
- ))}
-
- )}
-
- ))}
-
-
- {table.getRowModel().rows.map((row, rowIndex) => (
-
- {
- if (onRowClick) {
- onRowClick(row.original);
- }
- }}
- >
- {row.getVisibleCells().map((cell, cellIndex) => (
-
- {flexRender(cell.column.columnDef.cell, cell.getContext())}
- |
- ))}
-
-
- ))}
-
+
+ {table.getRowModel().rows.map((row, rowIndex) => (
+
+ {
+ if (onRowClick) {
+ onRowClick(row.original);
+ }
+ }}
+ >
+ {row.getVisibleCells().map((cell, cellIndex) => (
+
+ {flexRender(cell.column.columnDef.cell, cell.getContext())}
+ |
+ ))}
+
+
+ ))}
+
+
-
-
- {data.length > 0 && table ? (
-
- ) : null}
-
-
- );
+
+
+ );
}
-export default Table;
\ No newline at end of file
+export default Table;