@suphark/ui 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/README.md +45 -3
- package/package.json +14 -2
- package/src/components/app-shell/app-brand.tsx +81 -0
- package/src/components/app-shell/app-nav.tsx +260 -0
- package/src/components/app-shell/app-shell.tsx +270 -0
- package/src/components/app-shell/header-breadcrumbs.tsx +78 -0
- package/src/components/app-shell/index.ts +14 -0
- package/src/components/app-shell/nav-link.tsx +35 -0
- package/src/components/app-shell/types.ts +92 -0
- package/src/components/app-shell/user-menu.tsx +133 -0
- package/src/components/data-table/data-table-column-header.tsx +71 -0
- package/src/components/data-table/data-table-date-range-filter.tsx +25 -0
- package/src/components/data-table/data-table-faceted-filter.tsx +181 -0
- package/src/components/data-table/data-table-pagination.tsx +114 -0
- package/src/components/data-table/data-table-row-actions.tsx +166 -0
- package/src/components/data-table/data-table-toolbar.tsx +207 -0
- package/src/components/data-table/data-table-view-option.tsx +59 -0
- package/src/components/data-table/data-table.tsx +399 -0
- package/src/components/data-table/index.ts +9 -0
- package/src/components/data-table/types.ts +106 -0
- package/src/components/data-table/use-data-table.ts +229 -0
- package/src/components/design-system/sections/showcase-data-display.tsx +71 -0
- package/src/components/design-system/sections/showcase-guidelines-recipes.tsx +8 -8
- package/src/components/shared/button/delete-many-button.tsx +85 -0
- package/src/components/shared/resource-property-filter.tsx +399 -0
- package/src/data-table.ts +21 -0
- package/src/hooks/use-table-search-params.ts +147 -0
- package/src/index.ts +2 -1
- package/src/next.ts +8 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Button } from "@suphark/ui/components/ui/button";
|
|
4
|
+
import {
|
|
5
|
+
DropdownMenu,
|
|
6
|
+
DropdownMenuContent,
|
|
7
|
+
DropdownMenuItem,
|
|
8
|
+
DropdownMenuSeparator,
|
|
9
|
+
DropdownMenuTrigger,
|
|
10
|
+
} from "@suphark/ui/components/ui/dropdown-menu";
|
|
11
|
+
import { cn } from "@suphark/ui/lib/utils";
|
|
12
|
+
import type { Column } from "@tanstack/react-table";
|
|
13
|
+
import { ArrowDown, ArrowUp, ChevronsUpDown, EyeOff, Filter } from "lucide-react";
|
|
14
|
+
|
|
15
|
+
interface DataTableColumnHeaderProps<TData, TValue> extends React.HTMLAttributes<HTMLDivElement> {
|
|
16
|
+
column: Column<TData, TValue>;
|
|
17
|
+
title: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function DataTableColumnHeader<TData, TValue>({
|
|
21
|
+
column,
|
|
22
|
+
title,
|
|
23
|
+
className,
|
|
24
|
+
}: DataTableColumnHeaderProps<TData, TValue>) {
|
|
25
|
+
if (!column.getCanSort()) {
|
|
26
|
+
return <div className={cn(className)}>{title}</div>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<div className={cn("flex items-center gap-2", className)}>
|
|
31
|
+
<DropdownMenu>
|
|
32
|
+
<DropdownMenuTrigger
|
|
33
|
+
render={
|
|
34
|
+
<Button variant="ghost" size="sm" className="-ml-3 h-8 data-[state=open]:bg-accent" />
|
|
35
|
+
}
|
|
36
|
+
>
|
|
37
|
+
<span>{title}</span>
|
|
38
|
+
{column.getIsSorted() === "desc" ? (
|
|
39
|
+
<ArrowDown />
|
|
40
|
+
) : column.getIsSorted() === "asc" ? (
|
|
41
|
+
<ArrowUp />
|
|
42
|
+
) : (
|
|
43
|
+
<ChevronsUpDown />
|
|
44
|
+
)}
|
|
45
|
+
{column.getIsFiltered() && (
|
|
46
|
+
<Filter className="ml-1 size-3 fill-primary/10 text-primary" />
|
|
47
|
+
)}
|
|
48
|
+
</DropdownMenuTrigger>
|
|
49
|
+
<DropdownMenuContent align="start">
|
|
50
|
+
<DropdownMenuItem onClick={() => column.toggleSorting(false)}>
|
|
51
|
+
<ArrowUp />
|
|
52
|
+
Asc
|
|
53
|
+
</DropdownMenuItem>
|
|
54
|
+
<DropdownMenuItem onClick={() => column.toggleSorting(true)}>
|
|
55
|
+
<ArrowDown />
|
|
56
|
+
Desc
|
|
57
|
+
</DropdownMenuItem>
|
|
58
|
+
{column.getCanHide() && column.columnDef.enableHiding !== false && (
|
|
59
|
+
<>
|
|
60
|
+
<DropdownMenuSeparator />
|
|
61
|
+
<DropdownMenuItem onClick={() => column.toggleVisibility(false)}>
|
|
62
|
+
<EyeOff />
|
|
63
|
+
Hide
|
|
64
|
+
</DropdownMenuItem>
|
|
65
|
+
</>
|
|
66
|
+
)}
|
|
67
|
+
</DropdownMenuContent>
|
|
68
|
+
</DropdownMenu>
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { DateRangePicker } from "@suphark/ui/components/shared/date/date-range-picker";
|
|
4
|
+
import type { Column } from "@tanstack/react-table";
|
|
5
|
+
import type { DateRange } from "react-day-picker";
|
|
6
|
+
|
|
7
|
+
interface DataTableDateRangeFilterProps<TData> {
|
|
8
|
+
column?: Column<TData, unknown>;
|
|
9
|
+
title?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function DataTableDateRangeFilter<TData>({
|
|
13
|
+
column,
|
|
14
|
+
title,
|
|
15
|
+
}: DataTableDateRangeFilterProps<TData>) {
|
|
16
|
+
const filterValue = column?.getFilterValue() as DateRange | undefined;
|
|
17
|
+
|
|
18
|
+
return (
|
|
19
|
+
<DateRangePicker
|
|
20
|
+
value={filterValue}
|
|
21
|
+
onValueChange={(range) => column?.setFilterValue(range)}
|
|
22
|
+
title={title}
|
|
23
|
+
/>
|
|
24
|
+
);
|
|
25
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Badge } from "@suphark/ui/components/ui/badge";
|
|
4
|
+
import { Button } from "@suphark/ui/components/ui/button";
|
|
5
|
+
import {
|
|
6
|
+
Command,
|
|
7
|
+
CommandEmpty,
|
|
8
|
+
CommandGroup,
|
|
9
|
+
CommandInput,
|
|
10
|
+
CommandItem,
|
|
11
|
+
CommandList,
|
|
12
|
+
CommandSeparator,
|
|
13
|
+
} from "@suphark/ui/components/ui/command";
|
|
14
|
+
import { Popover, PopoverContent, PopoverTrigger } from "@suphark/ui/components/ui/popover";
|
|
15
|
+
import { Separator } from "@suphark/ui/components/ui/separator";
|
|
16
|
+
import { cn } from "@suphark/ui/lib/utils";
|
|
17
|
+
import type { Column } from "@tanstack/react-table";
|
|
18
|
+
import { Check, CirclePlus } from "lucide-react";
|
|
19
|
+
import * as React from "react";
|
|
20
|
+
|
|
21
|
+
interface DataTableFacetedFilterProps<TData, TValue> {
|
|
22
|
+
column?: Column<TData, TValue>;
|
|
23
|
+
title?: string;
|
|
24
|
+
options: {
|
|
25
|
+
label: string;
|
|
26
|
+
value: string | number | boolean;
|
|
27
|
+
icon?: React.ComponentType<{ className?: string }>;
|
|
28
|
+
}[];
|
|
29
|
+
compact?: boolean;
|
|
30
|
+
fullWidth?: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function DataTableFacetedFilter<TData, TValue>({
|
|
34
|
+
column,
|
|
35
|
+
title,
|
|
36
|
+
options,
|
|
37
|
+
compact = false,
|
|
38
|
+
fullWidth = false,
|
|
39
|
+
}: DataTableFacetedFilterProps<TData, TValue>) {
|
|
40
|
+
const facets = column?.getFacetedUniqueValues();
|
|
41
|
+
const filterValue = column?.getFilterValue();
|
|
42
|
+
const selectedValues = new Set(Array.isArray(filterValue) ? filterValue.map(String) : []);
|
|
43
|
+
|
|
44
|
+
const areAllSelected = options.length > 0 && selectedValues.size === options.length;
|
|
45
|
+
|
|
46
|
+
const sortedOptions = React.useMemo(() => {
|
|
47
|
+
return [...options].sort((a, b) => {
|
|
48
|
+
const aSelected = selectedValues.has(String(a.value));
|
|
49
|
+
const bSelected = selectedValues.has(String(b.value));
|
|
50
|
+
if (aSelected && !bSelected) return -1;
|
|
51
|
+
if (!aSelected && bSelected) return 1;
|
|
52
|
+
return 0;
|
|
53
|
+
});
|
|
54
|
+
}, [options, selectedValues]);
|
|
55
|
+
|
|
56
|
+
return (
|
|
57
|
+
<Popover>
|
|
58
|
+
<PopoverTrigger
|
|
59
|
+
render={
|
|
60
|
+
<Button
|
|
61
|
+
variant="outline"
|
|
62
|
+
size="sm"
|
|
63
|
+
className={cn(
|
|
64
|
+
"h-8 border-dashed",
|
|
65
|
+
compact && "h-7 px-2 text-xs",
|
|
66
|
+
fullWidth && "w-full justify-start",
|
|
67
|
+
)}
|
|
68
|
+
/>
|
|
69
|
+
}
|
|
70
|
+
>
|
|
71
|
+
<CirclePlus />
|
|
72
|
+
{title}
|
|
73
|
+
{selectedValues?.size > 0 && (
|
|
74
|
+
<>
|
|
75
|
+
<Separator orientation="vertical" className="mx-2 h-4" />
|
|
76
|
+
<Badge
|
|
77
|
+
variant="secondary"
|
|
78
|
+
className={cn("rounded-sm px-1 font-normal", !compact && "lg:hidden")}
|
|
79
|
+
>
|
|
80
|
+
{selectedValues.size}
|
|
81
|
+
</Badge>
|
|
82
|
+
<div className={cn("hidden gap-1 lg:flex", compact && "lg:hidden")}>
|
|
83
|
+
{selectedValues.size > 2 ? (
|
|
84
|
+
<Badge variant="secondary" className="rounded-sm px-1 font-normal">
|
|
85
|
+
เลือก {selectedValues.size} รายการ
|
|
86
|
+
</Badge>
|
|
87
|
+
) : (
|
|
88
|
+
options.reduce((acc: React.ReactNode[], option) => {
|
|
89
|
+
if (selectedValues.has(String(option.value))) {
|
|
90
|
+
acc.push(
|
|
91
|
+
<Badge
|
|
92
|
+
variant="secondary"
|
|
93
|
+
key={String(option.value)}
|
|
94
|
+
className="rounded-sm px-1 font-normal"
|
|
95
|
+
>
|
|
96
|
+
{option.label}
|
|
97
|
+
</Badge>,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
return acc;
|
|
101
|
+
}, [])
|
|
102
|
+
)}
|
|
103
|
+
</div>
|
|
104
|
+
</>
|
|
105
|
+
)}
|
|
106
|
+
</PopoverTrigger>
|
|
107
|
+
<PopoverContent className={cn("w-[200px] p-0", compact && "w-44")} align="start">
|
|
108
|
+
<Command>
|
|
109
|
+
{!compact ? <CommandInput placeholder={title} /> : null}
|
|
110
|
+
<CommandList>
|
|
111
|
+
<CommandEmpty>ไม่พบข้อมูล</CommandEmpty>
|
|
112
|
+
<CommandGroup>
|
|
113
|
+
{sortedOptions.map((option) => {
|
|
114
|
+
const valueAsString = String(option.value);
|
|
115
|
+
const isSelected = selectedValues.has(valueAsString);
|
|
116
|
+
return (
|
|
117
|
+
<CommandItem
|
|
118
|
+
key={valueAsString}
|
|
119
|
+
onSelect={() => {
|
|
120
|
+
if (isSelected) {
|
|
121
|
+
selectedValues.delete(valueAsString);
|
|
122
|
+
} else {
|
|
123
|
+
selectedValues.add(valueAsString);
|
|
124
|
+
}
|
|
125
|
+
const filterValues = Array.from(selectedValues);
|
|
126
|
+
column?.setFilterValue(filterValues.length ? filterValues : undefined);
|
|
127
|
+
}}
|
|
128
|
+
>
|
|
129
|
+
<div
|
|
130
|
+
className={cn(
|
|
131
|
+
"flex size-4 items-center justify-center rounded-[4px] border",
|
|
132
|
+
isSelected
|
|
133
|
+
? "border-primary bg-primary text-primary-foreground"
|
|
134
|
+
: "border-input [&_svg]:invisible",
|
|
135
|
+
)}
|
|
136
|
+
>
|
|
137
|
+
<Check className="size-3.5 text-primary-foreground" />
|
|
138
|
+
</div>
|
|
139
|
+
{option.icon && <option.icon className="size-4 text-muted-foreground" />}
|
|
140
|
+
<span>{option.label}</span>
|
|
141
|
+
{facets?.get(valueAsString) && (
|
|
142
|
+
<span className="ml-auto flex size-4 items-center justify-center font-mono text-muted-foreground text-xs">
|
|
143
|
+
{facets.get(valueAsString)}
|
|
144
|
+
</span>
|
|
145
|
+
)}
|
|
146
|
+
</CommandItem>
|
|
147
|
+
);
|
|
148
|
+
})}
|
|
149
|
+
</CommandGroup>
|
|
150
|
+
{!compact && (selectedValues.size > 0 || !areAllSelected) && (
|
|
151
|
+
<>
|
|
152
|
+
<CommandSeparator />
|
|
153
|
+
<CommandGroup>
|
|
154
|
+
{areAllSelected ? (
|
|
155
|
+
<CommandItem
|
|
156
|
+
onSelect={() => column?.setFilterValue(undefined)}
|
|
157
|
+
className="justify-center text-center"
|
|
158
|
+
>
|
|
159
|
+
ล้างตัวกรอง
|
|
160
|
+
</CommandItem>
|
|
161
|
+
) : (
|
|
162
|
+
<CommandItem
|
|
163
|
+
key="select-all"
|
|
164
|
+
onSelect={() => {
|
|
165
|
+
const allValues = options.map((option) => String(option.value));
|
|
166
|
+
column?.setFilterValue(allValues);
|
|
167
|
+
}}
|
|
168
|
+
className="justify-center text-center"
|
|
169
|
+
>
|
|
170
|
+
เลือกทั้งหมด
|
|
171
|
+
</CommandItem>
|
|
172
|
+
)}
|
|
173
|
+
</CommandGroup>
|
|
174
|
+
</>
|
|
175
|
+
)}
|
|
176
|
+
</CommandList>
|
|
177
|
+
</Command>
|
|
178
|
+
</PopoverContent>
|
|
179
|
+
</Popover>
|
|
180
|
+
);
|
|
181
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Button } from "@suphark/ui/components/ui/button";
|
|
4
|
+
import {
|
|
5
|
+
Select,
|
|
6
|
+
SelectContent,
|
|
7
|
+
SelectItem,
|
|
8
|
+
SelectTrigger,
|
|
9
|
+
SelectValue,
|
|
10
|
+
} from "@suphark/ui/components/ui/select";
|
|
11
|
+
import { cn } from "@suphark/ui/lib/utils";
|
|
12
|
+
import type { Table } from "@tanstack/react-table";
|
|
13
|
+
import { ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight } from "lucide-react";
|
|
14
|
+
|
|
15
|
+
interface DataTablePaginationProps<TData> {
|
|
16
|
+
table: Table<TData>;
|
|
17
|
+
pageSizeOptions?: number[];
|
|
18
|
+
onPageSizeChange?: (pageSize: number) => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function DataTablePagination<TData>({
|
|
22
|
+
table,
|
|
23
|
+
pageSizeOptions = [10, 20, 30, 40, 50],
|
|
24
|
+
onPageSizeChange,
|
|
25
|
+
}: DataTablePaginationProps<TData>) {
|
|
26
|
+
const isRowSelectionEnabled = table.options.enableRowSelection !== false;
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<div
|
|
30
|
+
className={cn(
|
|
31
|
+
"flex flex-col items-center justify-between gap-4 px-2 sm:flex-row sm:gap-0",
|
|
32
|
+
!isRowSelectionEnabled && "sm:justify-end",
|
|
33
|
+
)}
|
|
34
|
+
>
|
|
35
|
+
{isRowSelectionEnabled && (
|
|
36
|
+
<div className="flex-1 text-muted-foreground text-sm max-sm:text-center">
|
|
37
|
+
{table.getFilteredSelectedRowModel().rows.length} of{" "}
|
|
38
|
+
{table.getFilteredRowModel().rows.length} row(s) selected.
|
|
39
|
+
</div>
|
|
40
|
+
)}
|
|
41
|
+
<div className="flex flex-wrap items-center justify-center gap-4 sm:gap-6 lg:gap-8">
|
|
42
|
+
<div className="flex items-center gap-2">
|
|
43
|
+
<p className="font-medium text-sm max-sm:hidden">Rows per page</p>
|
|
44
|
+
<Select
|
|
45
|
+
value={`${table.getState().pagination.pageSize}`}
|
|
46
|
+
onValueChange={(value) => {
|
|
47
|
+
const newSize = Number(value);
|
|
48
|
+
table.setPageSize(newSize);
|
|
49
|
+
if (onPageSizeChange) {
|
|
50
|
+
onPageSizeChange(newSize);
|
|
51
|
+
}
|
|
52
|
+
}}
|
|
53
|
+
>
|
|
54
|
+
<SelectTrigger className="h-8 w-[70px]">
|
|
55
|
+
<SelectValue placeholder={table.getState().pagination.pageSize} />
|
|
56
|
+
</SelectTrigger>
|
|
57
|
+
<SelectContent side="top">
|
|
58
|
+
{pageSizeOptions.map((pageSize) => (
|
|
59
|
+
<SelectItem key={pageSize} value={`${pageSize}`}>
|
|
60
|
+
{pageSize}
|
|
61
|
+
</SelectItem>
|
|
62
|
+
))}
|
|
63
|
+
</SelectContent>
|
|
64
|
+
</Select>
|
|
65
|
+
</div>
|
|
66
|
+
<div className="flex w-[100px] items-center justify-center font-medium text-sm">
|
|
67
|
+
Page {table.getState().pagination.pageIndex + 1} of {table.getPageCount()}
|
|
68
|
+
</div>
|
|
69
|
+
<div className="flex items-center gap-2">
|
|
70
|
+
<Button
|
|
71
|
+
variant="outline"
|
|
72
|
+
size="icon"
|
|
73
|
+
className="hidden size-8 lg:flex"
|
|
74
|
+
onClick={() => table.setPageIndex(0)}
|
|
75
|
+
disabled={!table.getCanPreviousPage()}
|
|
76
|
+
>
|
|
77
|
+
<span className="sr-only">Go to first page</span>
|
|
78
|
+
<ChevronsLeft />
|
|
79
|
+
</Button>
|
|
80
|
+
<Button
|
|
81
|
+
variant="outline"
|
|
82
|
+
size="icon"
|
|
83
|
+
className="size-8"
|
|
84
|
+
onClick={() => table.previousPage()}
|
|
85
|
+
disabled={!table.getCanPreviousPage()}
|
|
86
|
+
>
|
|
87
|
+
<span className="sr-only">Go to previous page</span>
|
|
88
|
+
<ChevronLeft />
|
|
89
|
+
</Button>
|
|
90
|
+
<Button
|
|
91
|
+
variant="outline"
|
|
92
|
+
size="icon"
|
|
93
|
+
className="size-8"
|
|
94
|
+
onClick={() => table.nextPage()}
|
|
95
|
+
disabled={!table.getCanNextPage()}
|
|
96
|
+
>
|
|
97
|
+
<span className="sr-only">Go to next page</span>
|
|
98
|
+
<ChevronRight />
|
|
99
|
+
</Button>
|
|
100
|
+
<Button
|
|
101
|
+
variant="outline"
|
|
102
|
+
size="icon"
|
|
103
|
+
className="hidden size-8 lg:flex"
|
|
104
|
+
onClick={() => table.setPageIndex(table.getPageCount() - 1)}
|
|
105
|
+
disabled={!table.getCanNextPage()}
|
|
106
|
+
>
|
|
107
|
+
<span className="sr-only">Go to last page</span>
|
|
108
|
+
<ChevronsRight />
|
|
109
|
+
</Button>
|
|
110
|
+
</div>
|
|
111
|
+
</div>
|
|
112
|
+
</div>
|
|
113
|
+
);
|
|
114
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { DeleteButton } from "@suphark/ui/components/shared/button/delete-button";
|
|
4
|
+
import {
|
|
5
|
+
AlertDialog,
|
|
6
|
+
AlertDialogCancel,
|
|
7
|
+
AlertDialogContent,
|
|
8
|
+
AlertDialogDescription,
|
|
9
|
+
AlertDialogFooter,
|
|
10
|
+
AlertDialogHeader,
|
|
11
|
+
AlertDialogTitle,
|
|
12
|
+
} from "@suphark/ui/components/ui/alert-dialog";
|
|
13
|
+
import { Button } from "@suphark/ui/components/ui/button";
|
|
14
|
+
import {
|
|
15
|
+
DropdownMenu,
|
|
16
|
+
DropdownMenuContent,
|
|
17
|
+
DropdownMenuGroup,
|
|
18
|
+
DropdownMenuItem,
|
|
19
|
+
DropdownMenuSeparator,
|
|
20
|
+
DropdownMenuTrigger,
|
|
21
|
+
} from "@suphark/ui/components/ui/dropdown-menu";
|
|
22
|
+
import type { Row } from "@tanstack/react-table";
|
|
23
|
+
import { Ellipsis, Pen, Trash } from "lucide-react";
|
|
24
|
+
import { useState, useTransition } from "react";
|
|
25
|
+
import { toast } from "sonner";
|
|
26
|
+
|
|
27
|
+
interface ActionState<T> {
|
|
28
|
+
error?: string;
|
|
29
|
+
data?: T;
|
|
30
|
+
success?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface DataTableRowActionsProps<TData> {
|
|
34
|
+
row: Row<TData>;
|
|
35
|
+
/**
|
|
36
|
+
* Optional custom menu items to display before the standard Edit/Delete actions.
|
|
37
|
+
*/
|
|
38
|
+
children?: React.ReactNode;
|
|
39
|
+
/**
|
|
40
|
+
* Optional component to render for the edit dialog.
|
|
41
|
+
* If not provided, the "Edit" action will not be shown.
|
|
42
|
+
*/
|
|
43
|
+
EditDialog?: React.ComponentType<{
|
|
44
|
+
initialData?: TData;
|
|
45
|
+
open?: boolean;
|
|
46
|
+
onOpenChange?: (open: boolean) => void;
|
|
47
|
+
children?: React.ReactNode;
|
|
48
|
+
}>;
|
|
49
|
+
/**
|
|
50
|
+
* Optional server action to call for deletion.
|
|
51
|
+
* If not provided, the "Delete" action will not be shown.
|
|
52
|
+
*/
|
|
53
|
+
deleteAction?: (id: string) => Promise<ActionState<unknown>>;
|
|
54
|
+
/**
|
|
55
|
+
* The name of the resource to display in toast messages (e.g. "User Status").
|
|
56
|
+
*/
|
|
57
|
+
resourceName: string;
|
|
58
|
+
/**
|
|
59
|
+
* The key to access the name/title of the item for display. Defaults to "name".
|
|
60
|
+
*/
|
|
61
|
+
nameKey?: keyof TData;
|
|
62
|
+
/**
|
|
63
|
+
* The key to access the unique ID of the item. Defaults to "id".
|
|
64
|
+
*/
|
|
65
|
+
idKey?: keyof TData;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function DataTableRowActions<TData>({
|
|
69
|
+
row,
|
|
70
|
+
children,
|
|
71
|
+
EditDialog,
|
|
72
|
+
deleteAction,
|
|
73
|
+
resourceName,
|
|
74
|
+
nameKey = "name" as keyof TData,
|
|
75
|
+
idKey = "id" as keyof TData,
|
|
76
|
+
}: DataTableRowActionsProps<TData>) {
|
|
77
|
+
const item = row.original;
|
|
78
|
+
const itemRecord = item as Record<string, unknown>;
|
|
79
|
+
const itemName = String(
|
|
80
|
+
itemRecord[nameKey as string] ?? itemRecord.nameThai ?? itemRecord.name ?? "",
|
|
81
|
+
);
|
|
82
|
+
const [isPending, startTransition] = useTransition();
|
|
83
|
+
const [isEditDialogOpen, setIsEditDialogOpen] = useState(false);
|
|
84
|
+
const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false);
|
|
85
|
+
|
|
86
|
+
const handleDelete = () => {
|
|
87
|
+
if (!deleteAction) return;
|
|
88
|
+
|
|
89
|
+
startTransition(() => {
|
|
90
|
+
const promise = deleteAction(String(itemRecord[idKey as string])).then((result) => {
|
|
91
|
+
if (result.error) throw new Error(result.error);
|
|
92
|
+
setIsDeleteDialogOpen(false);
|
|
93
|
+
return result;
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
toast.promise(promise, {
|
|
97
|
+
loading: `Deleting ${resourceName.toLowerCase()} "${itemName}"...`,
|
|
98
|
+
success: `${resourceName} "${itemName}" deleted successfully.`,
|
|
99
|
+
error: (err: Error) => err.message,
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
return (
|
|
105
|
+
<div className="flex justify-end">
|
|
106
|
+
<DropdownMenu>
|
|
107
|
+
<DropdownMenuTrigger
|
|
108
|
+
render={
|
|
109
|
+
<Button variant="ghost" className="flex h-8 w-8 p-0 data-[state=open]:bg-muted" />
|
|
110
|
+
}
|
|
111
|
+
>
|
|
112
|
+
<Ellipsis className="h-4 w-4" />
|
|
113
|
+
<span className="sr-only">Open menu</span>
|
|
114
|
+
</DropdownMenuTrigger>
|
|
115
|
+
<DropdownMenuContent align="end" className="w-[160px]">
|
|
116
|
+
{children}
|
|
117
|
+
{children && (EditDialog || deleteAction) && <DropdownMenuSeparator />}
|
|
118
|
+
|
|
119
|
+
<DropdownMenuGroup>
|
|
120
|
+
{EditDialog && (
|
|
121
|
+
<DropdownMenuItem onClick={() => setIsEditDialogOpen(true)}>
|
|
122
|
+
<Pen className="mr-2 h-3.5 w-3.5 text-muted-foreground/70" />
|
|
123
|
+
Edit
|
|
124
|
+
</DropdownMenuItem>
|
|
125
|
+
)}
|
|
126
|
+
|
|
127
|
+
{EditDialog && deleteAction && <DropdownMenuSeparator />}
|
|
128
|
+
|
|
129
|
+
{deleteAction && (
|
|
130
|
+
<DropdownMenuItem
|
|
131
|
+
className="text-destructive focus:text-destructive"
|
|
132
|
+
onClick={() => setIsDeleteDialogOpen(true)}
|
|
133
|
+
>
|
|
134
|
+
<Trash className="mr-2 h-3.5 w-3.5 text-muted-foreground/70" />
|
|
135
|
+
Delete
|
|
136
|
+
</DropdownMenuItem>
|
|
137
|
+
)}
|
|
138
|
+
</DropdownMenuGroup>
|
|
139
|
+
</DropdownMenuContent>
|
|
140
|
+
</DropdownMenu>
|
|
141
|
+
|
|
142
|
+
{EditDialog && (
|
|
143
|
+
<EditDialog initialData={item} open={isEditDialogOpen} onOpenChange={setIsEditDialogOpen} />
|
|
144
|
+
)}
|
|
145
|
+
|
|
146
|
+
{deleteAction && (
|
|
147
|
+
<AlertDialog open={isDeleteDialogOpen} onOpenChange={setIsDeleteDialogOpen}>
|
|
148
|
+
<AlertDialogContent>
|
|
149
|
+
<AlertDialogHeader>
|
|
150
|
+
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
|
|
151
|
+
<AlertDialogDescription>
|
|
152
|
+
This action cannot be undone. This will permanently delete the{" "}
|
|
153
|
+
{resourceName.toLowerCase()} “{itemName}” and remove it from our
|
|
154
|
+
servers.
|
|
155
|
+
</AlertDialogDescription>
|
|
156
|
+
</AlertDialogHeader>
|
|
157
|
+
<AlertDialogFooter>
|
|
158
|
+
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
|
159
|
+
<DeleteButton onClick={handleDelete} isLoading={isPending} />
|
|
160
|
+
</AlertDialogFooter>
|
|
161
|
+
</AlertDialogContent>
|
|
162
|
+
</AlertDialog>
|
|
163
|
+
)}
|
|
164
|
+
</div>
|
|
165
|
+
);
|
|
166
|
+
}
|