update select
This commit is contained in:
@@ -27,7 +27,7 @@ export function DataTableColumnHeader<TData, TValue>({
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className={cn('-ml-3 h-8 cursor-pointer', className)}
|
||||
className={cn('-ml-3 cursor-pointer', className)}
|
||||
onClick={() => column.toggleSorting(column.getIsSorted() === 'asc')}
|
||||
>
|
||||
<span>{title}</span>
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
import {
|
||||
flexRender,
|
||||
getCoreRowModel,
|
||||
getSortedRowModel,
|
||||
useReactTable,
|
||||
type ColumnDef,
|
||||
type ColumnSort,
|
||||
@@ -89,6 +88,8 @@ export function DataTable<TData, TValue>({
|
||||
})
|
||||
}
|
||||
|
||||
// table.getSelectedRowModel().flatRows.map((row) => row.original)
|
||||
|
||||
const table = useReactTable({
|
||||
data,
|
||||
columns,
|
||||
@@ -105,6 +106,7 @@ export function DataTable<TData, TValue>({
|
||||
manualSorting: true,
|
||||
manualPagination: true,
|
||||
enableRowSelection: true,
|
||||
getRowId: (row: any) => row.id,
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
onRowSelectionChange: setRowSelection,
|
||||
onSortingChange: setSorting,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { type Table } from '@tanstack/react-table'
|
||||
import { ChevronLeft, ChevronRight } from 'lucide-react'
|
||||
import { ChevronLeftIcon, ChevronRightIcon } from 'lucide-react'
|
||||
|
||||
import { Button } from '@repo/ui/components/ui/button'
|
||||
import {
|
||||
@@ -21,63 +21,54 @@ export function DataTablePagination<TData>({
|
||||
const rowCount = table.getRowCount()
|
||||
|
||||
return (
|
||||
<div className="flex items-center">
|
||||
<div className="text-muted-foreground flex-1 text-sm">
|
||||
{table.getFilteredSelectedRowModel().rows.length} de{' '}
|
||||
{table.getFilteredRowModel().rows.length} linha(s) selecionada(s).
|
||||
<div className="flex items-center justify-end gap-3 lg:gap-6">
|
||||
<div className="flex items-center gap-2">
|
||||
<p className="text-sm font-medium hidden lg:block">Itens por página</p>
|
||||
<Select
|
||||
value={String(table.getState().pagination.pageSize)}
|
||||
onValueChange={(value) => {
|
||||
table.setPageSize(Number(value))
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder={table.getState().pagination.pageSize} />
|
||||
</SelectTrigger>
|
||||
<SelectContent side="top">
|
||||
{[12, 25, 50, 100].map((pageSize) => (
|
||||
<SelectItem key={pageSize} value={String(pageSize)}>
|
||||
{pageSize}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-3 lg:gap-6">
|
||||
<div className="flex items-center space-x-2">
|
||||
<p className="text-sm font-medium hidden lg:block">
|
||||
Itens por página
|
||||
</p>
|
||||
<Select
|
||||
value={`${table.getState().pagination.pageSize}`}
|
||||
onValueChange={(value) => {
|
||||
table.setPageSize(Number(value))
|
||||
}}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder={table.getState().pagination.pageSize} />
|
||||
</SelectTrigger>
|
||||
<SelectContent side="top">
|
||||
{[12, 25, 50, 100].map((pageSize) => (
|
||||
<SelectItem key={pageSize} value={`${pageSize}`}>
|
||||
{pageSize}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<div className="text-sm font-medium">
|
||||
{(pageIndex + 1) * pageSize - pageSize + 1}-
|
||||
{Math.min((pageIndex + 1) * pageSize, rowCount)}
|
||||
</div>
|
||||
|
||||
<div className="text-sm font-medium">
|
||||
{(pageIndex + 1) * pageSize - pageSize + 1}-
|
||||
{Math.min((pageIndex + 1) * pageSize, rowCount)}
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2 *:cursor-pointer">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="size-8"
|
||||
onClick={() => table.previousPage()}
|
||||
disabled={!table.getCanPreviousPage()}
|
||||
>
|
||||
<span className="sr-only">Ir para a página anterior</span>
|
||||
<ChevronLeft />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="size-8"
|
||||
onClick={() => table.nextPage()}
|
||||
disabled={!table.getCanNextPage()}
|
||||
>
|
||||
<span className="sr-only">Ir para a próxima página</span>
|
||||
<ChevronRight />
|
||||
</Button>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 *:cursor-pointer">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="size-8"
|
||||
onClick={() => table.previousPage()}
|
||||
disabled={!table.getCanPreviousPage()}
|
||||
>
|
||||
<span className="sr-only">Ir para a página anterior</span>
|
||||
<ChevronLeftIcon />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="size-8"
|
||||
onClick={() => table.nextPage()}
|
||||
disabled={!table.getCanNextPage()}
|
||||
>
|
||||
<span className="sr-only">Ir para a próxima página</span>
|
||||
<ChevronRightIcon />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
'use client'
|
||||
|
||||
import { CalendarIcon } from 'lucide-react'
|
||||
import { useState, type ReactNode } from 'react'
|
||||
import { EllipsisIcon } from 'lucide-react'
|
||||
import { useState } from 'react'
|
||||
import { type DateRange } from 'react-day-picker'
|
||||
import { ptBR } from 'react-day-picker/locale'
|
||||
|
||||
import { Badge } from '@repo/ui/components/ui/badge'
|
||||
import { Button } from '@repo/ui/components/ui/button'
|
||||
import { Calendar } from '@repo/ui/components/ui/calendar'
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuRadioGroup,
|
||||
DropdownMenuRadioItem,
|
||||
DropdownMenuTrigger
|
||||
} from '@repo/ui/components/ui/dropdown-menu'
|
||||
import {
|
||||
Popover,
|
||||
PopoverContent,
|
||||
@@ -23,53 +30,100 @@ const formatted = new Intl.DateTimeFormat('pt-BR', {
|
||||
year: '2-digit'
|
||||
})
|
||||
|
||||
type FilterProps = {
|
||||
rangeField?: string
|
||||
dateRange?: DateRange
|
||||
}
|
||||
|
||||
type RangeCalendarFilterProps = {
|
||||
children?: ReactNode
|
||||
value?: DateRange
|
||||
title: string
|
||||
icon: React.ComponentType
|
||||
value?: FilterProps
|
||||
options: { value: string; label: string }[]
|
||||
className?: string
|
||||
onChange?: (values: DateRange | undefined) => void
|
||||
onChange?: (values: FilterProps | undefined) => void
|
||||
}
|
||||
|
||||
export function RangeCalendarFilter({
|
||||
children,
|
||||
title,
|
||||
icon: Icon,
|
||||
value = undefined,
|
||||
options = [],
|
||||
className,
|
||||
onChange
|
||||
}: RangeCalendarFilterProps) {
|
||||
const [dateRange, setDateRange] = useState<DateRange | undefined>(value)
|
||||
const [rangeField, setRangeField] = useState<string | undefined>(
|
||||
value?.rangeField || options?.[0]?.value
|
||||
)
|
||||
|
||||
const [dateRange, setDateRange] = useState<DateRange | undefined>(
|
||||
value?.dateRange
|
||||
)
|
||||
|
||||
return (
|
||||
<Popover>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className={cn('h-9 border-dashed cursor-pointer', className)}
|
||||
>
|
||||
<CalendarIcon /> Período
|
||||
{dateRange && (
|
||||
<>
|
||||
<Separator orientation="vertical" className="mx-2 h-4" />
|
||||
<div className="gap-1 flex">
|
||||
<Badge variant="outline" className="rounded-sm px-1 font-mono">
|
||||
{formatted.format(dateRange.from)}
|
||||
</Badge>
|
||||
<Badge variant="outline" className="rounded-sm px-1 font-mono">
|
||||
{formatted.format(dateRange.to)}
|
||||
</Badge>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-full p-0" align="start">
|
||||
{children && (
|
||||
<div
|
||||
className={cn(
|
||||
'h-9 border rounded-md bg-muted border-dashed flex items-center',
|
||||
className
|
||||
)}
|
||||
>
|
||||
<PopoverTrigger asChild>
|
||||
<Button variant="ghost" size="sm" className="cursor-pointer">
|
||||
<Icon /> {title}
|
||||
{dateRange && (
|
||||
<>
|
||||
<Separator orientation="vertical" className="mx-0.5 h-4" />
|
||||
<div className="gap-1 flex">
|
||||
<Badge
|
||||
variant="outline"
|
||||
className="rounded-sm px-1 font-mono"
|
||||
>
|
||||
{formatted.format(dateRange.from)}
|
||||
</Badge>
|
||||
<Badge
|
||||
variant="outline"
|
||||
className="rounded-sm px-1 font-mono"
|
||||
>
|
||||
{formatted.format(dateRange.to)}
|
||||
</Badge>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
|
||||
{dateRange && (
|
||||
<>
|
||||
{children}
|
||||
<Separator />
|
||||
<Separator orientation="vertical" className="h-4" />
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="ghost" size="sm" className="cursor-pointer">
|
||||
<EllipsisIcon />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent className="w-46" align="end">
|
||||
<DropdownMenuRadioGroup
|
||||
value={rangeField}
|
||||
onValueChange={(rangeField) => {
|
||||
setRangeField(rangeField)
|
||||
onChange?.({ rangeField, dateRange })
|
||||
}}
|
||||
className="*:cursor-pointer"
|
||||
>
|
||||
{options.map(({ label, value }, idx) => (
|
||||
<DropdownMenuRadioItem value={value} key={idx}>
|
||||
{label}
|
||||
</DropdownMenuRadioItem>
|
||||
))}
|
||||
</DropdownMenuRadioGroup>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<PopoverContent className="w-full p-0" align="start">
|
||||
<Calendar
|
||||
mode="range"
|
||||
locale={ptBR}
|
||||
@@ -91,7 +145,7 @@ export function RangeCalendarFilter({
|
||||
dateRange.to = nextDay
|
||||
}
|
||||
|
||||
onChange?.(dateRange)
|
||||
onChange?.({ rangeField, dateRange })
|
||||
setDateRange(dateRange)
|
||||
}}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user