deno.land / x / netzo@0.5.16 / components / blocks / table / table-filters.tsx

table-filters.tsx
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import type { ComponentType, JSX } from "preact";import type { Column, Table } from "../../../deps/@tanstack/react-table.ts";import { Badge } from "../../badge.tsx";import { Button } from "../../button.tsx";import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandSeparator,} from "../../command.tsx";import { Popover, PopoverContent, PopoverTrigger } from "../../popover.tsx";import { Separator } from "../../separator.tsx";import { cn } from "../../utils.ts";
export type TableFilter<TData = unknown, TValue = unknown> = { column?: Column<TData, TValue>; title?: string; options: { label: string; value: string; icon?: ComponentType<{ className?: string }>; }[];};
export function TableFilters<TData>({ className, table,}: JSX.IntrinsicElements["div"] & { table: Table<TData> }) { if (!table.options?.initialState?.filters) { console.error(`Missing "filters" property in table.options.initialState`); return null; }
const filters = table.options.initialState?.filters as TableFilter< TData, unknown >[];
const isFiltered = table.getState().columnFilters.length > 0;
return ( <div className={cn("flex space-x-2", className)}> {filters?.map(({ column, title, options }) => table.getColumn(column) && ( <TableFilter column={table.getColumn(column)} title={title} options={options} /> ) )}
{isFiltered && ( <Button variant="ghost" onClick={() => table.resetColumnFilters()} className="h-8 px-2 lg:px-3" > Reset <i className="mdi-close w-4 h-4 ml-2" /> </Button> )} </div> );}
export function TableFilter<TData, TValue>({ column, title, options,}: TableFilter<TData, TValue>) { const facets = column?.getFacetedUniqueValues(); const selectedValues = new Set(column?.getFilterValue() as string[]);
return ( <Popover> <PopoverTrigger asChild> <Button variant="outline" size="sm" className="h-8 border-dashed"> <i className="mdi-plus-circle mr-2 h-4 w-4" /> {title} {selectedValues?.size > 0 && ( <> <Separator orientation="vertical" className="mx-2 h-4" /> <Badge variant="secondary" className="rounded-sm px-1 font-normal lg:hidden" > {selectedValues.size} </Badge> <div className="hidden space-x-1 lg:flex"> {selectedValues.size > 2 ? ( <Badge variant="secondary" className="rounded-sm px-1 font-normal" > {selectedValues.size} selected </Badge> ) : ( options .filter((option) => selectedValues.has(option.value)) .map((option) => ( <Badge variant="secondary" key={option.value} className="rounded-sm px-1 font-normal" > {option.label} </Badge> )) )} </div> </> )} </Button> </PopoverTrigger> <PopoverContent className="w-[200px] p-0" align="start"> <Command> <CommandInput placeholder={title} /> <CommandList> <CommandEmpty>No results found.</CommandEmpty> <CommandGroup> {options.map((option) => { const isSelected = selectedValues.has(option.value); return ( <CommandItem key={option.value} onSelect={() => { if (isSelected) { selectedValues.delete(option.value); } else { selectedValues.add(option.value); } const filterValues = Array.from(selectedValues); column?.setFilterValue( filterValues.length ? filterValues : undefined, ); }} > <div className={cn( "mr-2 flex h-4 w-4 items-center justify-center rounded-sm border border-primary", isSelected ? "bg-primary text-primary-foreground mdi-checkbox-marked" : "opacity-50 mdi-checkbox-blank-outline", )} /> {option.icon && ( <option.icon className="mr-2 h-4 w-4 text-muted-foreground" /> )} <span>{option.label}</span> {facets?.get(option.value) && ( <span className="ml-auto flex h-4 w-4 items-center justify-center font-mono text-xs"> {facets.get(option.value)} </span> )} </CommandItem> ); })} </CommandGroup> {selectedValues.size > 0 && ( <> <CommandSeparator /> <CommandGroup> <CommandItem onSelect={() => column?.setFilterValue(undefined)} className="justify-center text-center" > Clear filters </CommandItem> </CommandGroup> </> )} </CommandList> </Command> </PopoverContent> </Popover> );}
netzo

Version Info

Tagged at
a year ago