summaryrefslogtreecommitdiff
path: root/components/data-table/expandable-data-table.tsx
blob: 9005e2fb1b08be41f690e5fd7242c56c60f3197a (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
"use client"

import * as React from "react"
import { flexRender, type Table as TanstackTable } from "@tanstack/react-table"
import { ChevronRight, ChevronUp, ChevronDown } from "lucide-react"

import { cn } from "@/lib/utils"
import { getCommonPinningStyles } from "@/lib/data-table"
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table"
import { DataTablePagination } from "@/components/data-table/data-table-pagination"
import { DataTableResizer } from "@/components/data-table/data-table-resizer"
import { useAutoSizeColumns } from "@/hooks/useAutoSizeColumns"

interface ExpandableDataTableProps<TData> extends React.HTMLAttributes<HTMLDivElement> {
  table: TanstackTable<TData>
  floatingBar?: React.ReactNode | null
  autoSizeColumns?: boolean
  compact?: boolean
  expandedRows?: Set<string>
  setExpandedRows?: React.Dispatch<React.SetStateAction<Set<string>>>
  renderExpandedContent?: (row: TData) => React.ReactNode
  expandable?: boolean // 확장 기능 활성화 여부
  maxHeight?: string | number
  expandedRowClassName?: string // 확장된 행의 커스텀 클래스
}

/**
 * 확장 가능한 데이터 테이블 - 행 확장 시 바로 아래에 컨텐츠 표시
 * 개선사항:
 * - 가로스크롤과 확장된 내용 독립성 보장
 * - 동적 높이 계산으로 세로스크롤 문제 해결
 * - 향상된 접근성 및 키보드 네비게이션
 */
export function ExpandableDataTable<TData>({
  table,
  floatingBar = null,
  autoSizeColumns = true,
  compact = false,
  expandedRows = new Set(),
  setExpandedRows,
  renderExpandedContent,
  expandable = false,
  maxHeight,
  expandedRowClassName,
  children,
  className,
  ...props
}: ExpandableDataTableProps<TData>) {

  useAutoSizeColumns(table, autoSizeColumns)

  // 스크롤 컨테이너 참조
  const scrollContainerRef = React.useRef<HTMLDivElement>(null)
  const [expandedHeights, setExpandedHeights] = React.useState<Map<string, number>>(new Map())

  // 행 확장/축소 핸들러 (개선된 버전)
  const toggleRowExpansion = React.useCallback((rowId: string, event?: React.MouseEvent) => {
    if (!setExpandedRows) return
    
    if (event) {
      event.stopPropagation()
    }
    
    const newExpanded = new Set(expandedRows)
    if (newExpanded.has(rowId)) {
      newExpanded.delete(rowId)
      // 높이 정보도 제거
      setExpandedHeights(prev => {
        const newHeights = new Map(prev)
        newHeights.delete(rowId)
        return newHeights
      })
    } else {
      newExpanded.add(rowId)
    }
    setExpandedRows(newExpanded)
  }, [expandedRows, setExpandedRows])

  // 키보드 네비게이션 핸들러
  const handleKeyDown = React.useCallback((event: React.KeyboardEvent, rowId: string) => {
    if (event.key === 'Enter' || event.key === ' ') {
      event.preventDefault()
      toggleRowExpansion(rowId)
    }
  }, [toggleRowExpansion])

  // 확장된 내용의 높이 측정 및 업데이트
  const updateExpandedHeight = React.useCallback((rowId: string, height: number) => {
    setExpandedHeights(prev => {
      if (prev.get(rowId) !== height) {
        const newHeights = new Map(prev)
        newHeights.set(rowId, height)
        return newHeights
      }
      return prev
    })
  }, [])

  // 컴팩트 모드를 위한 클래스 정의 (개선된 버전)
  const compactStyles = compact ? {
    row: "h-7",
    cell: "py-1 px-2 text-sm",
    expandedCell: "py-2 px-4",
    groupRow: "py-1 bg-muted/20 text-sm",
    emptyRow: "h-16",
  } : {
    row: "",
    cell: "",
    expandedCell: "py-0 px-0", // 패딩 제거하여 확장 컨텐츠가 전체 영역 사용
    groupRow: "bg-muted/20",
    emptyRow: "h-24",
  }

  // 확장 버튼 렌더링 함수 (접근성 개선)
  const renderExpandButton = (rowId: string) => {
    if (!expandable || !setExpandedRows) return null
    
    const isExpanded = expandedRows.has(rowId)
    
    return (
      <button
        onClick={(e) => toggleRowExpansion(rowId, e)}
        onKeyDown={(e) => handleKeyDown(e, rowId)}
        className="inline-flex items-center justify-center w-6 h-6 hover:bg-gray-100 rounded transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-1"
        aria-label={isExpanded ? "행 축소" : "행 확장"}
        aria-expanded={isExpanded}
        tabIndex={0}
        type="button"
      >
        {isExpanded ? (
          <ChevronDown className="w-4 h-4" />
        ) : (
          <ChevronRight className="w-4 h-4" />
        )}
      </button>
    )
  }

  // 확장된 내용 래퍼 컴포넌트 (높이 측정 기능 포함)
  const ExpandedContentWrapper = React.memo<{
    rowId: string
    children: React.ReactNode
  }>(({ rowId, children }) => {
    const contentRef = React.useRef<HTMLDivElement>(null)

    React.useEffect(() => {
      if (contentRef.current) {
        const resizeObserver = new ResizeObserver((entries) => {
          for (const entry of entries) {
            updateExpandedHeight(rowId, entry.contentRect.height)
          }
        })

        resizeObserver.observe(contentRef.current)
        return () => resizeObserver.disconnect()
      }
    }, [rowId])

    return (
      <div ref={contentRef} className="w-full">
        {children}
      </div>
    )
  })

  ExpandedContentWrapper.displayName = "ExpandedContentWrapper"

  return (
    <div className={cn("w-full space-y-2.5", className)} {...props}>
      {children}
      
      {/* 메인 테이블 컨테이너 - 가로스크롤 문제 해결 */}
      <div 
        ref={scrollContainerRef}
        className="relative rounded-md border"
        style={{ 
          // maxHeight: maxHeight || '35rem',
          minHeight: '200px' // 최소 높이 보장
        }}
      >
        {/* 가로스크롤 영역 */}
        <div className="overflow-x-auto overflow-y-hidden h-full">
          {/* 세로스크롤 영역 (확장된 내용 포함) */}
          <div className="overflow-y-auto h-full">
            <Table className="[&>thead]:sticky [&>thead]:top-0 [&>thead]:z-10 table-fixed">
              {/* 테이블 헤더 */}
              <TableHeader className="bg-background">
                {table.getHeaderGroups().map((headerGroup) => (
                  <TableRow key={headerGroup.id} className={compact ? "h-8" : ""}>
                    {/* 확장 버튼 컬럼 헤더 */}
                    {expandable && (
                      <TableHead 
                        className={cn("w-10 bg-background", compact ? "py-1 px-2" : "")}
                        style={{ position: 'sticky', left: 0, zIndex: 11 }}
                      />
                    )}
                    
                    {headerGroup.headers.map((header) => {
                      if (header.column.getIsGrouped()) {
                        return null
                      }

                      return (
                        <TableHead
                          key={header.id}
                          colSpan={header.colSpan}
                          data-column-id={header.column.id}
                          className={cn(
                            compact ? "py-1 px-2 text-sm" : "",
                            "bg-background"
                          )}
                          style={{
                            ...getCommonPinningStyles({ column: header.column }),
                            width: header.getSize(),
                          }}
                        >
                          <div style={{ position: "relative" }}>
                            {header.isPlaceholder
                              ? null
                              : flexRender(
                                header.column.columnDef.header,
                                header.getContext()
                              )}

                            {header.column.getCanResize() && (
                              <DataTableResizer header={header} />
                            )}
                          </div>
                        </TableHead>
                      )
                    })}
                  </TableRow>
                ))}
              </TableHeader>

              {/* 테이블 바디 */}
              <TableBody>
                {table.getRowModel().rows?.length ? (
                  table.getRowModel().rows.map((row) => {
                    const isExpanded = expandedRows.has(row.id)
                    const expandedHeight = expandedHeights.get(row.id) || 0
                    
                    // 그룹핑 헤더 Row
                    if (row.getIsGrouped()) {
                      const groupingColumnId = row.groupingColumnId ?? ""
                      const groupingColumn = table.getColumn(groupingColumnId)

                      let columnLabel = groupingColumnId
                      if (groupingColumn) {
                        const headerDef = groupingColumn.columnDef.meta?.excelHeader
                        if (typeof headerDef === "string") {
                          columnLabel = headerDef
                        }
                      }

                      return (
                        <TableRow
                          key={row.id}
                          className={compactStyles.groupRow}
                          data-state={row.getIsExpanded() && "expanded"}
                        >
                          <TableCell 
                            colSpan={table.getVisibleFlatColumns().length + (expandable ? 1 : 0)}
                            className={compact ? "py-1 px-2" : ""}
                          >
                            {row.getCanExpand() && (
                              <button
                                onClick={row.getToggleExpandedHandler()}
                                className="inline-flex items-center justify-center mr-2 w-5 h-5 hover:bg-gray-100 rounded focus:outline-none focus:ring-2 focus:ring-blue-500"
                                style={{
                                  marginLeft: `${row.depth * 1.5}rem`,
                                }}
                                aria-label={row.getIsExpanded() ? "그룹 축소" : "그룹 확장"}
                              >
                                {row.getIsExpanded() ? (
                                  <ChevronUp size={compact ? 14 : 16} />
                                ) : (
                                  <ChevronRight size={compact ? 14 : 16} />
                                )}
                              </button>
                            )}

                            <span className="font-semibold">
                              {columnLabel}: {row.getValue(groupingColumnId)}
                            </span>
                            <span className="ml-2 text-xs text-muted-foreground">
                              ({row.subRows.length} rows)
                            </span>
                          </TableCell>
                        </TableRow>
                      )
                    }

                    // 일반 Row와 확장된 컨텐츠를 함께 렌더링
                    return (
                      <React.Fragment key={row.id}>
                        {/* 메인 데이터 행 */}
                        <TableRow
                          className={cn(
                            compactStyles.row,
                            isExpanded && "bg-muted/30 border-b-0"
                          )}
                          data-state={row.getIsSelected() && "selected"}
                        >
                          {/* 확장 버튼 셀 */}
                          {expandable && (
                            <TableCell 
                              className={cn("w-10", compactStyles.cell)}
                              style={{ 
                                position: 'sticky', 
                                left: 0, 
                                zIndex: 1,
                                backgroundColor: isExpanded ? 'rgb(248 250 252)' : 'white'
                              }}
                            >
                              {renderExpandButton(row.id)}
                            </TableCell>
                          )}

                          {/* 데이터 셀들 */}
                          {row.getVisibleCells().map((cell) => {
                            if (cell.column.getIsGrouped()) {
                              return null
                            }

                            return (
                              <TableCell
                                key={cell.id}
                                data-column-id={cell.column.id}
                                className={compactStyles.cell}
                                style={{
                                  ...getCommonPinningStyles({ column: cell.column }),
                                  width: cell.column.getSize(),
                                }}
                              >
                                {flexRender(
                                  cell.column.columnDef.cell,
                                  cell.getContext()
                                )}
                              </TableCell>
                            )
                          })}
                        </TableRow>

                        {/* 확장된 컨텐츠 행 - 가로스크롤 독립성 보장 */}
                        {isExpanded && renderExpandedContent && (
                          <TableRow className="hover:bg-transparent">
                            <TableCell
                              colSpan={table.getVisibleFlatColumns().length + (expandable ? 1 : 0)}
                              className={cn(
                                compactStyles.expandedCell,
                                "border-t-0 relative",
                                expandedRowClassName
                              )}
                              style={{
                                minHeight: expandedHeight || 'auto',
                              }}
                            >
                              {/* 확장된 내용을 위한 고정 폭 컨테이너 */}
                              <div className="relative w-full">
                                {/* 가로스크롤과 독립적인 확장 영역 */}
                                <div 
                                  className="absolute left-0 right-0 top-0 border-t"
                                  style={{
                                    width: '80vw',
                                    marginLeft: 'calc(-48vw + 50%)',
                                  }}
                                >
                                  <div className="max-w-none mx-auto">
                                    <ExpandedContentWrapper rowId={row.id}>
                                      {renderExpandedContent(row.original)}
                                    </ExpandedContentWrapper>
                                  </div>
                                </div>
                                
                                {/* 높이 유지를 위한 스페이서 */}
                                <div 
                                  className="opacity-0 pointer-events-none"
                                  style={{ height: Math.max(expandedHeight, 200) }}
                                />
                              </div>
                            </TableCell>
                          </TableRow>
                        )}
                      </React.Fragment>
                    )
                  })
                ) : (
                  // 데이터가 없을 때
                  <TableRow>
                    <TableCell
                      colSpan={table.getAllColumns().length + (expandable ? 1 : 0)}
                      className={compactStyles.emptyRow + " text-center"}
                    >
                      No results.
                    </TableCell>
                  </TableRow>
                )}
              </TableBody>
            </Table>
          </div>
        </div>
      </div>

      <div className="flex flex-col gap-2.5">
        {/* Pagination */}
        <DataTablePagination table={table} />

        {/* Floating Bar (선택된 행 있을 때) */}
        {table.getFilteredSelectedRowModel().rows.length > 0 && floatingBar}
      </div>
    </div>
  )
}