added loan pages
This commit is contained in:
456
src/components/dataTable.tsx
Normal file
456
src/components/dataTable.tsx
Normal file
@@ -0,0 +1,456 @@
|
||||
import { useRef, useState } from 'react'
|
||||
import {
|
||||
flexRender,
|
||||
getCoreRowModel,
|
||||
useReactTable,
|
||||
type ColumnDef,
|
||||
type VisibilityState,
|
||||
type RowSelectionState,
|
||||
type SortingState,
|
||||
getSortedRowModel,
|
||||
} from '@tanstack/react-table'
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from '@/components/ui/table'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import {
|
||||
Pagination,
|
||||
PaginationContent,
|
||||
PaginationEllipsis,
|
||||
PaginationItem,
|
||||
PaginationLink,
|
||||
PaginationNext,
|
||||
PaginationPrevious,
|
||||
} from '@/components/ui/pagination'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { useOutsideClick } from '@/lib/hooks/useOutsideClick'
|
||||
import { ChevronDown } from 'lucide-react'
|
||||
|
||||
export type { ColumnDef }
|
||||
|
||||
// ─── Pagination ───────────────────────────────────────────────────────────────
|
||||
|
||||
interface DataTablePaginationProps {
|
||||
currentPage: number
|
||||
totalPages: number
|
||||
onPageChange: (page: number) => void
|
||||
}
|
||||
|
||||
function DataTablePagination({
|
||||
currentPage,
|
||||
totalPages,
|
||||
onPageChange,
|
||||
}: DataTablePaginationProps) {
|
||||
if (totalPages <= 1) return null
|
||||
|
||||
const getPages = (): (number | 'ellipsis')[] => {
|
||||
if (totalPages <= 7) return Array.from({ length: totalPages }, (_, i) => i + 1)
|
||||
|
||||
const pages: (number | 'ellipsis')[] = [1]
|
||||
|
||||
if (currentPage > 3) pages.push('ellipsis')
|
||||
|
||||
const start = Math.max(2, currentPage - 1)
|
||||
const end = Math.min(totalPages - 1, currentPage + 1)
|
||||
|
||||
for (let i = start; i <= end; i++) pages.push(i)
|
||||
|
||||
if (currentPage < totalPages - 2) pages.push('ellipsis')
|
||||
|
||||
pages.push(totalPages)
|
||||
return pages
|
||||
}
|
||||
|
||||
return (
|
||||
<Pagination className="justify-center mt-3">
|
||||
<PaginationContent>
|
||||
<PaginationItem>
|
||||
<PaginationPrevious
|
||||
href="#"
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
if (currentPage > 1) onPageChange(currentPage - 1)
|
||||
}}
|
||||
className={cn(currentPage === 1 && 'pointer-events-none opacity-40')}
|
||||
/>
|
||||
</PaginationItem>
|
||||
|
||||
{getPages().map((page, i) =>
|
||||
page === 'ellipsis' ? (
|
||||
<PaginationItem key={`ellipsis-${i}`}>
|
||||
<PaginationEllipsis />
|
||||
</PaginationItem>
|
||||
) : (
|
||||
<PaginationItem key={page}>
|
||||
<PaginationLink
|
||||
href="#"
|
||||
isActive={page === currentPage}
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
onPageChange(page)
|
||||
}}
|
||||
>
|
||||
{page}
|
||||
</PaginationLink>
|
||||
</PaginationItem>
|
||||
)
|
||||
)}
|
||||
|
||||
<PaginationItem>
|
||||
<PaginationNext
|
||||
href="#"
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
if (currentPage < totalPages) onPageChange(currentPage + 1)
|
||||
}}
|
||||
className={cn(currentPage === totalPages && 'pointer-events-none opacity-40')}
|
||||
/>
|
||||
</PaginationItem>
|
||||
</PaginationContent>
|
||||
</Pagination>
|
||||
)
|
||||
}
|
||||
|
||||
// ─── Select All Dropdown ──────────────────────────────────────────────────────
|
||||
|
||||
interface SelectAllDropdownProps {
|
||||
isAllPageSelected: boolean
|
||||
isSomePageSelected: boolean
|
||||
isAllTotalSelected: boolean
|
||||
totalCount: number
|
||||
pageCount: number
|
||||
onSelectPage: (checked: boolean) => void
|
||||
onSelectAll: () => void
|
||||
onClearAll: () => void
|
||||
}
|
||||
|
||||
function SelectAllDropdown({
|
||||
isAllPageSelected,
|
||||
isSomePageSelected,
|
||||
isAllTotalSelected,
|
||||
totalCount,
|
||||
pageCount,
|
||||
onSelectPage,
|
||||
onSelectAll,
|
||||
onClearAll,
|
||||
}: SelectAllDropdownProps) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const ref = useRef<HTMLDivElement>(null)
|
||||
useOutsideClick(ref, () => setOpen(false))
|
||||
|
||||
const checkboxChecked = isAllPageSelected || (isSomePageSelected && 'indeterminate')
|
||||
|
||||
return (
|
||||
<div ref={ref} className="flex items-center gap-0.5">
|
||||
<Checkbox
|
||||
checked={checkboxChecked as boolean | 'indeterminate'}
|
||||
onCheckedChange={(value) => onSelectPage(!!value)}
|
||||
aria-label="Select all on page"
|
||||
/>
|
||||
<button
|
||||
onClick={() => setOpen((v) => !v)}
|
||||
className="flex items-center text-muted-foreground hover:text-foreground transition-colors p-0.5"
|
||||
aria-label="Selection options"
|
||||
>
|
||||
<ChevronDown size={11} />
|
||||
</button>
|
||||
|
||||
{open && (
|
||||
<div className="absolute top-10 left-0 z-50 w-52 bg-card border border-border rounded-lg shadow-lg py-1">
|
||||
<button
|
||||
onClick={() => {
|
||||
onSelectPage(true)
|
||||
setOpen(false)
|
||||
}}
|
||||
className="w-full text-left px-3 py-2 text-sm hover:bg-muted/50 transition-colors"
|
||||
>
|
||||
Select this page
|
||||
<span className="ml-1 text-muted-foreground text-xs">({pageCount})</span>
|
||||
</button>
|
||||
<button
|
||||
onClick={() => {
|
||||
onSelectAll()
|
||||
setOpen(false)
|
||||
}}
|
||||
className="w-full text-left px-3 py-2 text-sm hover:bg-muted/50 transition-colors"
|
||||
>
|
||||
Select all records
|
||||
<span className="ml-1 text-muted-foreground text-xs">({totalCount})</span>
|
||||
</button>
|
||||
{(isAllPageSelected || isAllTotalSelected || isSomePageSelected) && (
|
||||
<>
|
||||
<div className="border-t border-border my-1" />
|
||||
<button
|
||||
onClick={() => {
|
||||
onClearAll()
|
||||
setOpen(false)
|
||||
}}
|
||||
className="w-full text-left px-3 py-2 text-sm text-destructive hover:bg-destructive/10 transition-colors"
|
||||
>
|
||||
Clear selection
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// ─── DataTable ────────────────────────────────────────────────────────────────
|
||||
|
||||
interface DataTableProps<TData> {
|
||||
columns: ColumnDef<TData>[]
|
||||
data: TData[]
|
||||
isLoading?: boolean
|
||||
columnVisibility?: VisibilityState
|
||||
onColumnVisibilityChange?: (visibility: VisibilityState) => void
|
||||
columnOrder?: string[]
|
||||
onColumnOrderChange?: (order: string[]) => void
|
||||
enableRowSelection?: boolean
|
||||
onRowSelectionChange?: (rows: TData[]) => void
|
||||
getRowId?: (row: TData) => string
|
||||
|
||||
// Pagination — server-side
|
||||
currentPage?: number
|
||||
totalPages?: number
|
||||
totalCount?: number
|
||||
onPageChange?: (page: number) => void
|
||||
}
|
||||
|
||||
export function DataTable<TData>({
|
||||
columns,
|
||||
data,
|
||||
isLoading = false,
|
||||
columnVisibility,
|
||||
onColumnVisibilityChange,
|
||||
columnOrder,
|
||||
onColumnOrderChange,
|
||||
enableRowSelection = false,
|
||||
onRowSelectionChange,
|
||||
getRowId,
|
||||
currentPage = 1,
|
||||
totalPages = 1,
|
||||
totalCount,
|
||||
onPageChange,
|
||||
}: DataTableProps<TData>) {
|
||||
const [sorting, setSorting] = useState<SortingState>([])
|
||||
const [rowSelection, setRowSelection] = useState<RowSelectionState>({})
|
||||
// Tracks whether ALL records (across pages) are logically selected
|
||||
const [allTotalSelected, setAllTotalSelected] = useState(false)
|
||||
|
||||
const resolvedTotalCount = totalCount ?? data.length
|
||||
|
||||
// ── Build selection column ──────────────────────────────────────────────────
|
||||
const selectionColumn: ColumnDef<TData> = {
|
||||
id: 'select',
|
||||
header: ({ table }) => (
|
||||
<div className="relative">
|
||||
<SelectAllDropdown
|
||||
isAllPageSelected={table.getIsAllPageRowsSelected()}
|
||||
isSomePageSelected={table.getIsSomePageRowsSelected()}
|
||||
isAllTotalSelected={allTotalSelected}
|
||||
totalCount={resolvedTotalCount}
|
||||
pageCount={data.length}
|
||||
onSelectPage={(checked) => {
|
||||
table.toggleAllPageRowsSelected(checked)
|
||||
if (!checked) setAllTotalSelected(false)
|
||||
}}
|
||||
onSelectAll={() => {
|
||||
table.toggleAllPageRowsSelected(true)
|
||||
setAllTotalSelected(true)
|
||||
}}
|
||||
onClearAll={() => {
|
||||
table.toggleAllPageRowsSelected(false)
|
||||
setAllTotalSelected(false)
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
),
|
||||
cell: ({ row }) => (
|
||||
<Checkbox
|
||||
checked={row.getIsSelected()}
|
||||
onCheckedChange={(value) => row.toggleSelected(!!value)}
|
||||
aria-label="Select row"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
/>
|
||||
),
|
||||
enableSorting: false,
|
||||
enableHiding: false,
|
||||
size: 48,
|
||||
}
|
||||
|
||||
const allColumns: ColumnDef<TData>[] = enableRowSelection
|
||||
? [selectionColumn, ...columns]
|
||||
: columns
|
||||
|
||||
const effectiveColumnOrder: string[] = enableRowSelection
|
||||
? columnOrder && columnOrder.length > 0
|
||||
? columnOrder[0] === 'select'
|
||||
? columnOrder
|
||||
: ['select', ...columnOrder.filter((id) => id !== 'select')]
|
||||
: []
|
||||
: (columnOrder ?? [])
|
||||
|
||||
const table = useReactTable({
|
||||
data,
|
||||
columns: allColumns,
|
||||
state: {
|
||||
sorting,
|
||||
rowSelection,
|
||||
columnVisibility: columnVisibility ?? {},
|
||||
columnOrder: effectiveColumnOrder,
|
||||
},
|
||||
getRowId,
|
||||
enableRowSelection,
|
||||
onRowSelectionChange: (updater) => {
|
||||
const newSelection =
|
||||
typeof updater === 'function' ? updater(rowSelection) : updater
|
||||
setRowSelection(newSelection)
|
||||
// If deselecting anything, clear allTotalSelected
|
||||
if (Object.keys(newSelection).length < data.length) {
|
||||
setAllTotalSelected(false)
|
||||
}
|
||||
if (onRowSelectionChange) {
|
||||
const selectedRows = data.filter((_, index) =>
|
||||
newSelection[getRowId ? getRowId(data[index]) : String(index)]
|
||||
)
|
||||
onRowSelectionChange(selectedRows)
|
||||
}
|
||||
},
|
||||
onSortingChange: setSorting,
|
||||
onColumnVisibilityChange: (updater) => {
|
||||
if (!onColumnVisibilityChange) return
|
||||
const next =
|
||||
typeof updater === 'function' ? updater(columnVisibility ?? {}) : updater
|
||||
onColumnVisibilityChange(next)
|
||||
},
|
||||
onColumnOrderChange: (updater) => {
|
||||
if (!onColumnOrderChange) return
|
||||
const next =
|
||||
typeof updater === 'function' ? updater(columnOrder ?? []) : updater
|
||||
onColumnOrderChange(next)
|
||||
},
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
getSortedRowModel: getSortedRowModel(),
|
||||
})
|
||||
|
||||
// ── Selection banner ────────────────────────────────────────────────────────
|
||||
const selectedCount = Object.keys(rowSelection).length
|
||||
const showBanner = enableRowSelection && (selectedCount > 0 || allTotalSelected)
|
||||
|
||||
return (
|
||||
<div className="space-y-0">
|
||||
{/* Selection banner */}
|
||||
{showBanner && (
|
||||
<div className="flex items-center justify-between px-3 py-2 bg-primary/5 border border-primary/20 rounded-t-lg text-sm border-b-0">
|
||||
<span className="text-primary font-medium">
|
||||
{allTotalSelected
|
||||
? `All ${resolvedTotalCount} records selected`
|
||||
: `${selectedCount} row${selectedCount !== 1 ? 's' : ''} selected`}
|
||||
</span>
|
||||
<button
|
||||
onClick={() => {
|
||||
table.toggleAllPageRowsSelected(false)
|
||||
setAllTotalSelected(false)
|
||||
}}
|
||||
className="text-xs text-muted-foreground hover:text-foreground transition-colors"
|
||||
>
|
||||
Clear
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Table */}
|
||||
<div
|
||||
className={cn(
|
||||
'border border-border overflow-hidden',
|
||||
showBanner ? 'rounded-b-lg rounded-t-none' : 'rounded-lg'
|
||||
)}
|
||||
>
|
||||
<Table>
|
||||
<TableHeader>
|
||||
{table.getHeaderGroups().map((headerGroup) => (
|
||||
<TableRow key={headerGroup.id} className="bg-muted/40 hover:bg-muted/40">
|
||||
{headerGroup.headers.map((header) => (
|
||||
<TableHead
|
||||
key={header.id}
|
||||
style={{
|
||||
width: header.getSize() !== 150 ? header.getSize() : undefined,
|
||||
}}
|
||||
className={cn(
|
||||
'text-xs font-semibold text-muted-foreground uppercase tracking-wider whitespace-nowrap',
|
||||
header.column.getCanSort() && 'cursor-pointer select-none'
|
||||
)}
|
||||
onClick={header.column.getToggleSortingHandler()}
|
||||
>
|
||||
{header.isPlaceholder
|
||||
? null
|
||||
: flexRender(header.column.columnDef.header, header.getContext())}
|
||||
</TableHead>
|
||||
))}
|
||||
</TableRow>
|
||||
))}
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{isLoading ? (
|
||||
Array.from({ length: 7 }).map((_, i) => (
|
||||
<TableRow key={i}>
|
||||
{allColumns.map((_, j) => (
|
||||
<TableCell key={j}>
|
||||
<div className="h-4 bg-muted rounded animate-pulse" />
|
||||
</TableCell>
|
||||
))}
|
||||
</TableRow>
|
||||
))
|
||||
) : table.getRowModel().rows.length === 0 ? (
|
||||
<TableRow>
|
||||
<TableCell
|
||||
colSpan={allColumns.length}
|
||||
className="text-center py-16 text-muted-foreground"
|
||||
>
|
||||
No data found
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
) : (
|
||||
table.getRowModel().rows.map((row) => (
|
||||
<TableRow
|
||||
key={row.id}
|
||||
data-state={row.getIsSelected() ? 'selected' : undefined}
|
||||
className="hover:bg-muted/30 transition-colors border-border"
|
||||
>
|
||||
{row.getVisibleCells().map((cell) => (
|
||||
<TableCell key={cell.id} className="py-3">
|
||||
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
||||
</TableCell>
|
||||
))}
|
||||
</TableRow>
|
||||
))
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
|
||||
{/* Pagination */}
|
||||
{onPageChange && (
|
||||
<DataTablePagination
|
||||
currentPage={currentPage}
|
||||
totalPages={totalPages}
|
||||
onPageChange={(page) => {
|
||||
// Clear selection on page change (server-side pattern)
|
||||
if (!allTotalSelected) {
|
||||
table.toggleAllPageRowsSelected(false)
|
||||
setRowSelection({})
|
||||
}
|
||||
onPageChange(page)
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
393
src/components/dataTableToolbar.tsx
Normal file
393
src/components/dataTableToolbar.tsx
Normal file
@@ -0,0 +1,393 @@
|
||||
import { useRef, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { SlidersHorizontal, Clock, Filter, ChevronDown, X, GripVertical } from 'lucide-react'
|
||||
import type { VisibilityState } from '@tanstack/react-table'
|
||||
import { useOutsideClick } from '@/lib/hooks/useOutsideClick'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { TableSearchInput } from '@/components/tableSearch'
|
||||
import { TableActionButton } from '@/components/tableButton'
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
|
||||
export interface ColumnMeta {
|
||||
id: string
|
||||
label: string
|
||||
}
|
||||
|
||||
export interface FilterField {
|
||||
id: string
|
||||
label: string
|
||||
options: { value: string; label: string }[]
|
||||
}
|
||||
|
||||
export interface ActiveFilter {
|
||||
fieldId: string
|
||||
value: string
|
||||
}
|
||||
|
||||
interface DataTableToolbarProps {
|
||||
searchValue: string
|
||||
onSearchChange: (value: string) => void
|
||||
searchPlaceholder?: string
|
||||
|
||||
columns: ColumnMeta[]
|
||||
columnVisibility: VisibilityState
|
||||
onColumnVisibilityChange: (visibility: VisibilityState) => void
|
||||
columnOrder: string[]
|
||||
onColumnOrderChange: (order: string[]) => void
|
||||
|
||||
filterFields?: FilterField[]
|
||||
activeFilters?: ActiveFilter[]
|
||||
onFilterChange?: (fieldId: string, value: string) => void
|
||||
onFilterReset?: () => void
|
||||
|
||||
perPageOptions?: number[]
|
||||
perPage?: number
|
||||
onPerPageChange?: (value: number) => void
|
||||
|
||||
actionLabel?: string
|
||||
onAction?: () => void
|
||||
|
||||
hideSearch?: boolean
|
||||
hideAction?: boolean
|
||||
}
|
||||
|
||||
// ─── Toggle Columns Dropdown ──────────────────────────────────────────────────
|
||||
|
||||
interface ToggleColumnsDropdownProps {
|
||||
columns: ColumnMeta[]
|
||||
columnVisibility: VisibilityState
|
||||
onColumnVisibilityChange: (visibility: VisibilityState) => void
|
||||
columnOrder: string[]
|
||||
onColumnOrderChange: (order: string[]) => void
|
||||
}
|
||||
|
||||
function ToggleColumnsDropdown({
|
||||
columns,
|
||||
columnVisibility,
|
||||
onColumnVisibilityChange,
|
||||
columnOrder,
|
||||
onColumnOrderChange,
|
||||
}: ToggleColumnsDropdownProps) {
|
||||
const { t } = useTranslation()
|
||||
const [open, setOpen] = useState(false)
|
||||
const ref = useRef<HTMLDivElement>(null)
|
||||
const dragIdRef = useRef<string | null>(null)
|
||||
const [dragOverId, setDragOverId] = useState<string | null>(null)
|
||||
|
||||
useOutsideClick(ref, () => setOpen(false))
|
||||
|
||||
const isVisible = (id: string) =>
|
||||
columnVisibility[id] === undefined ? true : columnVisibility[id]
|
||||
|
||||
const toggle = (id: string) => {
|
||||
onColumnVisibilityChange({ ...columnVisibility, [id]: !isVisible(id) })
|
||||
}
|
||||
|
||||
// columnOrder'a göre sırala; columnOrder'da olmayan kolonları sona at
|
||||
const orderedColumns = [...columns].sort((a, b) => {
|
||||
const ai = columnOrder.indexOf(a.id)
|
||||
const bi = columnOrder.indexOf(b.id)
|
||||
if (ai === -1 && bi === -1) return 0
|
||||
if (ai === -1) return 1
|
||||
if (bi === -1) return -1
|
||||
return ai - bi
|
||||
})
|
||||
|
||||
const handleDragStart = (id: string) => {
|
||||
dragIdRef.current = id
|
||||
}
|
||||
|
||||
const handleDragOver = (e: React.DragEvent, id: string) => {
|
||||
e.preventDefault()
|
||||
if (dragIdRef.current !== id) setDragOverId(id)
|
||||
}
|
||||
|
||||
const handleDrop = (targetId: string) => {
|
||||
const sourceId = dragIdRef.current
|
||||
if (!sourceId || sourceId === targetId) return
|
||||
|
||||
const next = [...columnOrder]
|
||||
const from = next.indexOf(sourceId)
|
||||
const to = next.indexOf(targetId)
|
||||
|
||||
// columnOrder'da yoksa ekle
|
||||
const safeFrom = from === -1 ? next.length : from
|
||||
const safeTo = to === -1 ? next.length : to
|
||||
|
||||
next.splice(safeFrom, 1)
|
||||
next.splice(safeTo, 0, sourceId)
|
||||
onColumnOrderChange(next)
|
||||
|
||||
dragIdRef.current = null
|
||||
setDragOverId(null)
|
||||
}
|
||||
|
||||
const handleDragEnd = () => {
|
||||
dragIdRef.current = null
|
||||
setDragOverId(null)
|
||||
}
|
||||
|
||||
return (
|
||||
<div ref={ref} className="relative">
|
||||
<button
|
||||
onClick={() => setOpen((v) => !v)}
|
||||
className={cn(
|
||||
'flex items-center gap-1.5 h-9 px-3 rounded-md border border-border',
|
||||
'text-sm text-muted-foreground bg-background',
|
||||
'hover:bg-muted/50 hover:text-foreground transition-colors',
|
||||
open && 'bg-muted/50 text-foreground border-ring'
|
||||
)}
|
||||
>
|
||||
<SlidersHorizontal size={14} />
|
||||
<ChevronDown size={12} />
|
||||
</button>
|
||||
|
||||
{open && (
|
||||
<div className="absolute right-0 mt-1.5 w-52 bg-card border border-border rounded-lg shadow-lg z-50 py-2">
|
||||
<p className="px-3 pb-1.5 text-[11px] font-semibold text-muted-foreground uppercase tracking-wider">
|
||||
{t('table.toggleColumns', 'Toggle Columns')}
|
||||
</p>
|
||||
{orderedColumns.map((col) => (
|
||||
<div
|
||||
key={col.id}
|
||||
draggable
|
||||
onDragStart={() => handleDragStart(col.id)}
|
||||
onDragOver={(e) => handleDragOver(e, col.id)}
|
||||
onDrop={() => handleDrop(col.id)}
|
||||
onDragEnd={handleDragEnd}
|
||||
className={cn(
|
||||
'flex items-center gap-2.5 px-3 py-1.5 transition-colors cursor-default',
|
||||
'hover:bg-muted/40',
|
||||
dragOverId === col.id && 'bg-primary/10 border-l-2 border-primary'
|
||||
)}
|
||||
>
|
||||
<GripVertical
|
||||
size={13}
|
||||
className="text-muted-foreground cursor-grab shrink-0 active:cursor-grabbing"
|
||||
/>
|
||||
<input
|
||||
type="checkbox"
|
||||
id={`col-toggle-${col.id}`}
|
||||
checked={isVisible(col.id)}
|
||||
onChange={() => toggle(col.id)}
|
||||
className="w-4 h-4 rounded cursor-pointer accent-primary"
|
||||
/>
|
||||
<label
|
||||
htmlFor={`col-toggle-${col.id}`}
|
||||
className="text-sm text-foreground cursor-pointer flex-1 truncate select-none"
|
||||
>
|
||||
{col.label}
|
||||
</label>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// ─── Date Filter Button ───────────────────────────────────────────────────────
|
||||
|
||||
function DateFilterButton() {
|
||||
return (
|
||||
<button
|
||||
className={cn(
|
||||
'flex items-center h-9 px-3 rounded-md border border-border',
|
||||
'text-muted-foreground bg-background',
|
||||
'hover:bg-muted/50 hover:text-foreground transition-colors'
|
||||
)}
|
||||
>
|
||||
<Clock size={14} />
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
// ─── Filter Dropdown ──────────────────────────────────────────────────────────
|
||||
|
||||
interface FilterDropdownProps {
|
||||
filterFields: FilterField[]
|
||||
activeFilters: ActiveFilter[]
|
||||
onFilterChange: (fieldId: string, value: string) => void
|
||||
onFilterReset: () => void
|
||||
perPageOptions?: number[]
|
||||
perPage?: number
|
||||
onPerPageChange?: (value: number) => void
|
||||
}
|
||||
|
||||
function FilterDropdown({
|
||||
filterFields,
|
||||
activeFilters,
|
||||
onFilterChange,
|
||||
onFilterReset,
|
||||
perPageOptions = [10, 25, 50, 100],
|
||||
perPage = 25,
|
||||
onPerPageChange,
|
||||
}: FilterDropdownProps) {
|
||||
const { t } = useTranslation()
|
||||
const [open, setOpen] = useState(false)
|
||||
const ref = useRef<HTMLDivElement>(null)
|
||||
useOutsideClick(ref, () => setOpen(false))
|
||||
|
||||
const activeCount = activeFilters.filter((f) => f.value !== '').length
|
||||
const hasActive = activeCount > 0
|
||||
|
||||
const getActiveValue = (fieldId: string) =>
|
||||
activeFilters.find((f) => f.fieldId === fieldId)?.value ?? ''
|
||||
|
||||
return (
|
||||
<div ref={ref} className="relative">
|
||||
<button
|
||||
onClick={() => setOpen((v) => !v)}
|
||||
className={cn(
|
||||
'flex items-center gap-1.5 h-9 px-3 rounded-md border border-border',
|
||||
'text-muted-foreground bg-background',
|
||||
'hover:bg-muted/50 hover:text-foreground transition-colors',
|
||||
open && 'bg-muted/50 text-foreground border-ring',
|
||||
// emerald yerine primary token
|
||||
hasActive && 'border-primary text-primary'
|
||||
)}
|
||||
>
|
||||
<Filter size={14} />
|
||||
{hasActive && (
|
||||
<span className="text-xs font-semibold bg-primary text-primary-foreground rounded-full w-4 h-4 flex items-center justify-center leading-none">
|
||||
{activeCount}
|
||||
</span>
|
||||
)}
|
||||
<ChevronDown size={12} />
|
||||
</button>
|
||||
|
||||
{open && (
|
||||
<div className="absolute right-0 mt-1.5 w-64 bg-card border border-border rounded-lg shadow-lg z-50 py-3">
|
||||
<div className="flex items-center justify-between px-3 pb-2 border-b border-border mb-2">
|
||||
<p className="text-[11px] font-semibold text-muted-foreground uppercase tracking-wider">
|
||||
{t('table.filters', 'Filters')}
|
||||
</p>
|
||||
{hasActive && (
|
||||
<button
|
||||
onClick={onFilterReset}
|
||||
className="flex items-center gap-1 text-xs text-destructive hover:text-destructive/80 transition-colors"
|
||||
>
|
||||
<X size={11} />
|
||||
{t('table.reset', 'Reset')}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="px-3 space-y-3">
|
||||
{filterFields.map((field) => (
|
||||
<div key={field.id}>
|
||||
<label className="block text-[11px] font-semibold text-muted-foreground uppercase tracking-wider mb-1">
|
||||
{field.label}
|
||||
</label>
|
||||
<select
|
||||
value={getActiveValue(field.id)}
|
||||
onChange={(e) => onFilterChange(field.id, e.target.value)}
|
||||
className={cn(
|
||||
'w-full h-8 px-2 rounded-md border border-border bg-background',
|
||||
'text-sm text-foreground focus:outline-none focus:ring-2 focus:ring-ring',
|
||||
'transition-colors appearance-none cursor-pointer'
|
||||
)}
|
||||
>
|
||||
<option value="">—</option>
|
||||
{field.options.map((opt) => (
|
||||
<option key={opt.value} value={opt.value}>
|
||||
{opt.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
))}
|
||||
|
||||
{onPerPageChange && (
|
||||
<div>
|
||||
<label className="block text-[11px] font-semibold text-muted-foreground uppercase tracking-wider mb-1">
|
||||
{t('table.perPage', 'Sahypa başyna')}
|
||||
</label>
|
||||
<select
|
||||
value={perPage}
|
||||
onChange={(e) => onPerPageChange(Number(e.target.value))}
|
||||
className={cn(
|
||||
'w-full h-8 px-2 rounded-md border border-border bg-background',
|
||||
'text-sm text-foreground focus:outline-none focus:ring-2 focus:ring-ring',
|
||||
'transition-colors appearance-none cursor-pointer'
|
||||
)}
|
||||
>
|
||||
{perPageOptions.map((n) => (
|
||||
<option key={n} value={n}>
|
||||
{n}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// ─── DataTableToolbar ─────────────────────────────────────────────────────────
|
||||
|
||||
export function DataTableToolbar({
|
||||
searchValue,
|
||||
onSearchChange,
|
||||
searchPlaceholder,
|
||||
columns,
|
||||
columnVisibility,
|
||||
onColumnVisibilityChange,
|
||||
columnOrder,
|
||||
onColumnOrderChange,
|
||||
filterFields = [],
|
||||
activeFilters = [],
|
||||
onFilterChange,
|
||||
onFilterReset,
|
||||
perPageOptions,
|
||||
perPage,
|
||||
onPerPageChange,
|
||||
actionLabel,
|
||||
onAction,
|
||||
hideSearch = false,
|
||||
hideAction = false,
|
||||
}: DataTableToolbarProps) {
|
||||
return (
|
||||
<div className={cn("flex items-center gap-3 mb-3", hideSearch ? "justify-end" : "justify-between")}>
|
||||
{!hideSearch && (
|
||||
<TableSearchInput
|
||||
value={searchValue}
|
||||
onChange={onSearchChange}
|
||||
placeholder={searchPlaceholder}
|
||||
/>
|
||||
)}
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<ToggleColumnsDropdown
|
||||
columns={columns}
|
||||
columnVisibility={columnVisibility}
|
||||
onColumnVisibilityChange={onColumnVisibilityChange}
|
||||
columnOrder={columnOrder}
|
||||
onColumnOrderChange={onColumnOrderChange}
|
||||
/>
|
||||
|
||||
<DateFilterButton />
|
||||
|
||||
{filterFields.length > 0 && onFilterChange && onFilterReset && (
|
||||
<FilterDropdown
|
||||
filterFields={filterFields}
|
||||
activeFilters={activeFilters}
|
||||
onFilterChange={onFilterChange}
|
||||
onFilterReset={onFilterReset}
|
||||
perPageOptions={perPageOptions}
|
||||
perPage={perPage}
|
||||
onPerPageChange={onPerPageChange}
|
||||
/>
|
||||
)}
|
||||
|
||||
{!hideAction && actionLabel && onAction && (
|
||||
<TableActionButton label={actionLabel} onClick={onAction} />
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
36
src/components/tableButton.tsx
Normal file
36
src/components/tableButton.tsx
Normal file
@@ -0,0 +1,36 @@
|
||||
import { Plus } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
interface TableActionButtonProps {
|
||||
label: string
|
||||
onClick: () => void
|
||||
icon?: React.ReactNode
|
||||
disabled?: boolean
|
||||
className?: string
|
||||
variant?: 'primary' | 'destructive' | 'secondary'
|
||||
}
|
||||
|
||||
export function TableActionButton({
|
||||
label,
|
||||
onClick,
|
||||
icon,
|
||||
disabled = false,
|
||||
className,
|
||||
variant = 'primary',
|
||||
}: TableActionButtonProps) {
|
||||
return (
|
||||
<Button
|
||||
onClick={onClick}
|
||||
disabled={disabled}
|
||||
variant={variant === 'destructive' ? 'destructive' : variant === 'secondary' ? 'secondary' : 'default'}
|
||||
className={cn(
|
||||
'font-medium text-sm h-9 px-4 transition-colors shadow-sm',
|
||||
className
|
||||
)}
|
||||
>
|
||||
{icon ?? <Plus size={15} className="mr-1.5" />}
|
||||
{label}
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
40
src/components/tableSearch.tsx
Normal file
40
src/components/tableSearch.tsx
Normal file
@@ -0,0 +1,40 @@
|
||||
import { Search } from 'lucide-react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
interface TableSearchInputProps {
|
||||
value: string
|
||||
onChange: (value: string) => void
|
||||
placeholder?: string
|
||||
className?: string
|
||||
}
|
||||
|
||||
export function TableSearchInput({
|
||||
value,
|
||||
onChange,
|
||||
placeholder,
|
||||
className,
|
||||
}: TableSearchInputProps) {
|
||||
const { t } = useTranslation()
|
||||
|
||||
return (
|
||||
<div className={cn('relative', className)}>
|
||||
<Search
|
||||
size={15}
|
||||
className="absolute left-3 top-1/2 -translate-y-1/2 text-muted-foreground pointer-events-none"
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
placeholder={placeholder ?? t('table.search', 'Search...')}
|
||||
className={cn(
|
||||
'h-9 w-64 rounded-md border border-border bg-background pl-9 pr-3',
|
||||
'text-sm text-foreground placeholder:text-muted-foreground',
|
||||
'focus:outline-none focus:ring-2 focus:ring-ring focus:border-transparent',
|
||||
'transition-colors'
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { cva } from "class-variance-authority"
|
||||
|
||||
export const buttonVariants = cva(
|
||||
"group/button inline-flex shrink-0 items-center justify-center rounded-lg border border-transparent bg-clip-padding text-sm font-medium whitespace-nowrap transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 active:not-aria-[haspopup]:translate-y-px disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
||||
"group/button inline-flex shrink-0 cursor-pointer items-center justify-center rounded-md border border-transparent bg-clip-padding text-sm font-medium whitespace-nowrap transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 active:not-aria-[haspopup]:translate-y-px disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
|
||||
@@ -8,7 +8,7 @@ function Input({ className, type, ...props }: React.ComponentProps<"input">) {
|
||||
type={type}
|
||||
data-slot="input"
|
||||
className={cn(
|
||||
"h-8 w-full min-w-0 rounded-lg border border-input bg-transparent px-2.5 py-1 text-base transition-colors outline-none file:inline-flex file:h-6 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed disabled:bg-input/50 disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:disabled:bg-input/80 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40",
|
||||
"h-8 w-full min-w-0 rounded-md border border-input bg-transparent px-2.5 py-1 text-base transition-colors outline-none file:inline-flex file:h-6 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed disabled:bg-input/50 disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:disabled:bg-input/80 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
|
||||
@@ -4,7 +4,7 @@ import * as React from "react"
|
||||
import { cva, type VariantProps } from "class-variance-authority"
|
||||
import { Slot } from "radix-ui"
|
||||
|
||||
import { useIsMobile } from "@/hooks/use-mobile"
|
||||
import { useIsMobile } from "@/lib/hooks/useMobile"
|
||||
import { cn } from "@/lib/utils"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Input } from "@/components/ui/input"
|
||||
|
||||
33
src/components/ui/statusBadge.tsx
Normal file
33
src/components/ui/statusBadge.tsx
Normal file
@@ -0,0 +1,33 @@
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
export type StatusBadgeVariant = 'warning' | 'success' | 'error' | 'info' | 'default'
|
||||
|
||||
interface StatusBadgeProps {
|
||||
label: string
|
||||
variant: StatusBadgeVariant
|
||||
icon?: React.ElementType
|
||||
className?: string
|
||||
}
|
||||
|
||||
const VARIANT_STYLES: Record<StatusBadgeVariant, string> = {
|
||||
warning: 'bg-amber-500/15 text-amber-500 border-amber-500/30',
|
||||
success: 'bg-emerald-500/15 text-emerald-500 border-emerald-500/30',
|
||||
error: 'bg-red-500/15 text-red-500 border-red-500/30',
|
||||
info: 'bg-blue-500/15 text-blue-500 border-blue-500/30',
|
||||
default: 'bg-muted/50 text-muted-foreground border-border',
|
||||
}
|
||||
|
||||
export function StatusBadge({ label, variant, icon: Icon, className }: StatusBadgeProps) {
|
||||
return (
|
||||
<span
|
||||
className={cn(
|
||||
'inline-flex items-center gap-1.5 px-2.5 py-1 rounded-md text-[11px] font-bold border whitespace-nowrap',
|
||||
VARIANT_STYLES[variant],
|
||||
className
|
||||
)}
|
||||
>
|
||||
{Icon && <Icon size={11} />}
|
||||
{label}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
114
src/components/ui/table.tsx
Normal file
114
src/components/ui/table.tsx
Normal file
@@ -0,0 +1,114 @@
|
||||
import * as React from "react"
|
||||
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
function Table({ className, ...props }: React.ComponentProps<"table">) {
|
||||
return (
|
||||
<div
|
||||
data-slot="table-container"
|
||||
className="relative w-full overflow-x-auto"
|
||||
>
|
||||
<table
|
||||
data-slot="table"
|
||||
className={cn("w-full caption-bottom text-sm", className)}
|
||||
{...props}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function TableHeader({ className, ...props }: React.ComponentProps<"thead">) {
|
||||
return (
|
||||
<thead
|
||||
data-slot="table-header"
|
||||
className={cn("[&_tr]:border-b", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function TableBody({ className, ...props }: React.ComponentProps<"tbody">) {
|
||||
return (
|
||||
<tbody
|
||||
data-slot="table-body"
|
||||
className={cn("[&_tr:last-child]:border-0", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function TableFooter({ className, ...props }: React.ComponentProps<"tfoot">) {
|
||||
return (
|
||||
<tfoot
|
||||
data-slot="table-footer"
|
||||
className={cn(
|
||||
"border-t bg-muted/50 font-medium [&>tr]:last:border-b-0",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function TableRow({ className, ...props }: React.ComponentProps<"tr">) {
|
||||
return (
|
||||
<tr
|
||||
data-slot="table-row"
|
||||
className={cn(
|
||||
"border-b transition-colors hover:bg-muted/50 has-aria-expanded:bg-muted/50 data-[state=selected]:bg-muted",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function TableHead({ className, ...props }: React.ComponentProps<"th">) {
|
||||
return (
|
||||
<th
|
||||
data-slot="table-head"
|
||||
className={cn(
|
||||
"h-10 px-2 text-left align-middle font-medium whitespace-nowrap text-foreground [&:has([role=checkbox])]:pr-0",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function TableCell({ className, ...props }: React.ComponentProps<"td">) {
|
||||
return (
|
||||
<td
|
||||
data-slot="table-cell"
|
||||
className={cn(
|
||||
"p-2 align-middle whitespace-nowrap [&:has([role=checkbox])]:pr-0",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function TableCaption({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentProps<"caption">) {
|
||||
return (
|
||||
<caption
|
||||
data-slot="table-caption"
|
||||
className={cn("mt-4 text-sm text-muted-foreground", className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export {
|
||||
Table,
|
||||
TableHeader,
|
||||
TableBody,
|
||||
TableFooter,
|
||||
TableHead,
|
||||
TableRow,
|
||||
TableCell,
|
||||
TableCaption,
|
||||
}
|
||||
@@ -48,7 +48,7 @@ function TooltipContent({
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
<TooltipPrimitive.Arrow className="z-50 size-2.5 translate-y-[calc(-50%_-_2px)] rotate-45 rounded-[2px] bg-foreground fill-foreground" />
|
||||
<TooltipPrimitive.Arrow className="z-50 size-2.5 translate-y-[calc(-50%-2px)] rotate-45 rounded-[2px] bg-foreground fill-foreground" />
|
||||
</TooltipPrimitive.Content>
|
||||
</TooltipPrimitive.Portal>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user