import React, { useState, useEffect } from "react"; import { useTableOptions } from "@/contexts/TableOptionsContext"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { ScrollArea } from "@/components/ui/scroll-area"; import { GripVertical, Eye, EyeOff } from "lucide-react"; import { ColumnVisibility } from "@/types/table-options"; interface Props { tableId: string; open: boolean; onOpenChange: (open: boolean) => void; } export const ColumnVisibilityPanel: React.FC = ({ tableId, open, onOpenChange, }) => { const { getTable } = useTableOptions(); const table = getTable(tableId); const [localColumns, setLocalColumns] = useState([]); // 테이블 정보 로드 useEffect(() => { if (table) { setLocalColumns( table.columns.map((col) => ({ columnName: col.columnName, visible: col.visible, width: col.width, order: 0, })) ); } }, [table]); const handleVisibilityChange = (columnName: string, visible: boolean) => { setLocalColumns((prev) => prev.map((col) => col.columnName === columnName ? { ...col, visible } : col ) ); }; const handleWidthChange = (columnName: string, width: number) => { setLocalColumns((prev) => prev.map((col) => col.columnName === columnName ? { ...col, width } : col ) ); }; const handleApply = () => { table?.onColumnVisibilityChange(localColumns); onOpenChange(false); }; const handleReset = () => { if (table) { setLocalColumns( table.columns.map((col) => ({ columnName: col.columnName, visible: true, width: 150, order: 0, })) ); } }; const visibleCount = localColumns.filter((col) => col.visible).length; return ( 테이블 옵션 컬럼 표시/숨기기, 순서 변경, 너비 등을 설정할 수 있습니다. 모든 테두리를 드래그하여 크기를 조정할 수 있습니다.
{/* 상태 표시 */}
{visibleCount}/{localColumns.length}개 컬럼 표시 중
{/* 컬럼 리스트 */}
{localColumns.map((col) => { const columnMeta = table?.columns.find( (c) => c.columnName === col.columnName ); return (
{/* 드래그 핸들 */} {/* 체크박스 */} handleVisibilityChange( col.columnName, checked as boolean ) } /> {/* 가시성 아이콘 */} {col.visible ? ( ) : ( )} {/* 컬럼명 */}
{columnMeta?.columnLabel}
{col.columnName}
{/* 너비 설정 */}
handleWidthChange( col.columnName, parseInt(e.target.value) || 150 ) } className="h-7 w-16 text-xs sm:h-8 sm:w-20 sm:text-sm" min={50} max={500} />
); })}
); };