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
|
"use client"
import * as React from "react"
import { useDataTable } from "@/hooks/use-data-table"
import { DataTable } from "@/components/data-table/data-table"
import { DataTableAdvancedToolbar } from "@/components/data-table/data-table-advanced-toolbar"
import type { DataTableAdvancedFilterField } from "@/types/table"
import { getColumns } from "@/lib/docu-list-rule/combo-box-settings/table/combo-box-settings-table-columns"
import { ComboBoxOptionsDetailSheet } from "@/lib/docu-list-rule/combo-box-settings/table/combo-box-options-detail-sheet"
import { codeGroups } from "@/db/schema/docu-list-rule"
interface ComboBoxSettingsTableProps {
promises?: Promise<[{ data: typeof codeGroups.$inferSelect[]; pageCount: number }]>
}
export function ComboBoxSettingsTable({ promises }: ComboBoxSettingsTableProps) {
const rawData = React.use(promises!)
const [isDetailSheetOpen, setIsDetailSheetOpen] = React.useState(false)
const [selectedCodeGroup, setSelectedCodeGroup] = React.useState<typeof codeGroups.$inferSelect | null>(null)
// Detail 버튼 클릭 핸들러
const handleDetail = React.useCallback((codeGroup: typeof codeGroups.$inferSelect) => {
setSelectedCodeGroup(codeGroup)
setIsDetailSheetOpen(true)
}, [])
const columns = React.useMemo(() => getColumns({ onDetail: handleDetail }), [handleDetail])
// 고급 필터 필드 설정
const advancedFilterFields: DataTableAdvancedFilterField<typeof codeGroups.$inferSelect>[] = [
{ id: "groupId", label: "Group ID", type: "text" },
{ id: "description", label: "Description", type: "text" },
{ id: "codeFormat", label: "Code Format", type: "text" },
{
id: "controlType", label: "Control Type", type: "select", options: [
{ label: "Textbox", value: "textbox" },
{ label: "Combobox", value: "combobox" },
]
},
{
id: "isActive", label: "Status", type: "select", options: [
{ label: "Active", value: "true" },
{ label: "Inactive", value: "false" },
]
},
{ id: "createdAt", label: "Created At", type: "date" },
]
const { table } = useDataTable({
data: rawData[0].data as typeof codeGroups.$inferSelect[],
columns,
pageCount: rawData[0].pageCount,
enablePinning: true,
enableAdvancedFilter: true,
initialState: {
columnPinning: { right: ["actions"] },
columnFilters: [
{
id: "controlType",
value: "combobox",
},
],
},
getRowId: (originalRow) => String(originalRow.id),
shallow: false,
clearOnDefault: true,
})
// 컴포넌트 마운트 후 그룹핑 설정
React.useEffect(() => {
if (rawData[0]?.data && table.getState().grouping.length === 0) {
table.setGrouping(["projectCode"])
}
}, [table, rawData])
// 정렬 시 펼쳐진 상태 유지
React.useEffect(() => {
const currentExpanded = table.getState().expanded
if (Object.keys(currentExpanded).length > 0) {
// 약간의 지연 후 현재 펼쳐진 상태를 다시 설정
const timer = setTimeout(() => {
table.setExpanded(currentExpanded)
}, 100)
return () => clearTimeout(timer)
}
}, [table.getState().sorting, table])
return (
<>
<DataTable table={table}>
<DataTableAdvancedToolbar
table={table}
filterFields={advancedFilterFields}
>
</DataTableAdvancedToolbar>
</DataTable>
{/* Detail 시트 */}
<ComboBoxOptionsDetailSheet
open={isDetailSheetOpen}
onOpenChange={setIsDetailSheetOpen}
codeGroup={selectedCodeGroup}
onSuccess={() => {
setIsDetailSheetOpen(false)
setSelectedCodeGroup(null)
}}
/>
</>
)
}
|