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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
"use client"
import * as React from "react"
import { type Column, type Table } from "@tanstack/react-table"
import { Check, ChevronsUpDown, MoveRight } from "lucide-react"
import { cn, toSentenceCase } from "@/lib/utils"
import { Button } from "@/components/ui/button"
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
CommandSeparator,
} from "@/components/ui/command"
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover"
/**
* Helper function to check if a column is a parent column (has subcolumns)
*/
function isParentColumn<TData>(column: Column<TData>): boolean {
return column.columns && column.columns.length > 0
}
/**
* Helper function to pin all subcolumns of a parent column
*/
function pinSubColumns<TData>(
column: Column<TData>,
pinType: false | "left" | "right"
): void {
// If this is a parent column, pin all its subcolumns
if (isParentColumn(column)) {
column.columns.forEach((subColumn) => {
// Recursively handle nested columns
pinSubColumns(subColumn, pinType)
})
} else {
// For leaf columns, apply the pin if possible
if (column.getCanPin?.()) {
column.pin?.(pinType)
}
}
}
/**
* Checks if all subcolumns of a parent column are pinned to the specified side
*/
function areAllSubColumnsPinned<TData>(
column: Column<TData>,
pinType: "left" | "right"
): boolean {
if (isParentColumn(column)) {
// Check if all subcolumns are pinned
return column.columns.every((subColumn) =>
areAllSubColumnsPinned(subColumn, pinType)
)
} else {
// For leaf columns, check if it's pinned to the specified side
return column.getIsPinned?.() === pinType
}
}
/**
* Helper function to get the display name of a column
*/
function getColumnDisplayName<TData>(column: Column<TData>): string {
// First try to use excelHeader from meta if available
const excelHeader = column.columnDef.meta?.excelHeader
if (excelHeader) {
return excelHeader
}
// Fall back to converting the column ID to sentence case
return toSentenceCase(column.id)
}
/**
* Array of column IDs that should be auto-pinned to the right when available
*/
const AUTO_PIN_RIGHT_COLUMNS = ['actions']
/**
* "Pin Right" Popover. Supports pinning both individual columns and header groups.
*/
export function PinRightButton<TData>({ table }: { table: Table<TData> }) {
const [open, setOpen] = React.useState(false)
const triggerRef = React.useRef<HTMLButtonElement>(null)
// Try to auto-pin actions columns if they exist
React.useEffect(() => {
AUTO_PIN_RIGHT_COLUMNS.forEach((columnId) => {
const column = table.getColumn(columnId)
if (column?.getCanPin?.()) {
column.pin?.("right")
}
})
}, [table])
// Get all columns that can be pinned (excluding auto-pinned columns)
const pinnableColumns = React.useMemo(() => {
return table.getAllColumns().filter((column) => {
// Skip auto-pinned columns
if (AUTO_PIN_RIGHT_COLUMNS.includes(column.id)) {
return false
}
// If it's a leaf column, check if it can be pinned
if (!isParentColumn(column)) {
return column.getCanPin?.()
}
// If it's a parent column, check if at least one subcolumn can be pinned
return column.columns.some((subCol) => {
if (isParentColumn(subCol)) {
// Recursively check nested columns
return subCol.columns.some(c => c.getCanPin?.())
}
return subCol.getCanPin?.()
})
})
}, [table])
// Get flat list of all leaf columns for display
const allPinnableLeafColumns = React.useMemo(() => {
const leafColumns: Column<TData>[] = []
// Function to recursively collect leaf columns
const collectLeafColumns = (column: Column<TData>) => {
if (isParentColumn(column)) {
column.columns.forEach(collectLeafColumns)
} else if (column.getCanPin?.() && !AUTO_PIN_RIGHT_COLUMNS.includes(column.id)) {
leafColumns.push(column)
}
}
// Process all columns
table.getAllColumns().forEach(collectLeafColumns)
return leafColumns
}, [table])
// Handle column pinning
const handleColumnPin = React.useCallback((column: Column<TData>) => {
// For parent columns, pin/unpin all subcolumns
if (isParentColumn(column)) {
const allPinned = areAllSubColumnsPinned(column, "right")
pinSubColumns(column, allPinned ? false : "right")
} else {
// For leaf columns, toggle pin state
const isPinned = column.getIsPinned?.() === "right"
column.pin?.(isPinned ? false : "right")
}
}, [])
// Check if a column or its subcolumns are pinned right
const isColumnPinned = React.useCallback((column: Column<TData>): boolean => {
if (isParentColumn(column)) {
return areAllSubColumnsPinned(column, "right")
} else {
return column.getIsPinned?.() === "right"
}
}, [])
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
ref={triggerRef}
variant="outline"
size="sm"
className="h-8 gap-2"
>
<MoveRight className="size-4" />
<span className="hidden sm:inline">
오른 고정
</span>
</Button>
</PopoverTrigger>
<PopoverContent
align="end"
className="w-56 p-0"
onCloseAutoFocus={() => triggerRef.current?.focus()}
>
<Command>
<CommandInput placeholder="Search columns..." />
<CommandList className="max-h-[300px]">
<CommandEmpty>No columns found.</CommandEmpty>
<CommandGroup>
{/* Parent Columns with subcolumns */}
{pinnableColumns
.filter(isParentColumn)
.map((parentColumn) => (
<React.Fragment key={parentColumn.id}>
{/* Parent column header - can pin/unpin all children at once */}
<CommandItem
onSelect={() => {
handleColumnPin(parentColumn)
}}
className="font-medium bg-muted/50"
>
<span className="truncate">
{getColumnDisplayName(parentColumn)}
</span>
<Check
className={cn(
"ml-auto size-4 shrink-0",
isColumnPinned(parentColumn) ? "opacity-100" : "opacity-0"
)}
/>
</CommandItem>
{/* Individual subcolumns */}
{parentColumn.columns
.filter(col => !isParentColumn(col) && col.getCanPin?.())
.map(subColumn => (
<CommandItem
key={subColumn.id}
onSelect={() => {
handleColumnPin(subColumn)
}}
className="pl-6 text-sm"
>
<span className="truncate">
{getColumnDisplayName(subColumn)}
</span>
<Check
className={cn(
"ml-auto size-4 shrink-0",
isColumnPinned(subColumn) ? "opacity-100" : "opacity-0"
)}
/>
</CommandItem>
))}
</React.Fragment>
))}
</CommandGroup>
{/* Separator if we have both parent columns and standalone leaf columns */}
{pinnableColumns.some(isParentColumn) &&
allPinnableLeafColumns.some(col => !pinnableColumns.find(parent =>
isParentColumn(parent) && parent.columns.includes(col))
) && (
<CommandSeparator />
)}
{/* Standalone leaf columns (not part of any parent column group) */}
<CommandGroup>
{allPinnableLeafColumns
.filter(col => !pinnableColumns.find(parent =>
isParentColumn(parent) && parent.columns.includes(col)
))
.map((column) => (
<CommandItem
key={column.id}
onSelect={() => {
handleColumnPin(column)
}}
>
<span className="truncate">
{getColumnDisplayName(column)}
</span>
<Check
className={cn(
"ml-auto size-4 shrink-0",
isColumnPinned(column) ? "opacity-100" : "opacity-0"
)}
/>
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
)
}
|