summaryrefslogtreecommitdiff
path: root/lib/docu-list-rule/document-class/table/document-class-table-columns.tsx
blob: 6b4a882f76acafcad6680c058faed4ae2e2194ed (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
"use client"

import * as React from "react"
import { type DataTableRowAction } from "@/types/table"
import { type ColumnDef } from "@tanstack/react-table"
import { Ellipsis } from "lucide-react"

import { formatDateTime } from "@/lib/utils"
import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuShortcut,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"

import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header"
import { documentClasses } from "@/db/schema/docu-list-rule"

interface GetColumnsProps {
  setRowAction: React.Dispatch<React.SetStateAction<DataTableRowAction<typeof documentClasses.$inferSelect> | null>>
  onDetail?: (documentClass: typeof documentClasses.$inferSelect) => void
}

/**
 * tanstack table 컬럼 정의
 */
export function getColumns({ setRowAction, onDetail }: GetColumnsProps): ColumnDef<typeof documentClasses.$inferSelect>[] {
  // ----------------------------------------------------------------
  // 1) select 컬럼 (체크박스)
  // ----------------------------------------------------------------
  const selectColumn: ColumnDef<typeof documentClasses.$inferSelect> = {
    id: "select",
    header: ({ table }) => (
      <Checkbox
        checked={
          table.getIsAllPageRowsSelected() ||
          (table.getIsSomePageRowsSelected() && "indeterminate")
        }
        onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
        aria-label="Select all"
        className="translate-y-0.5"
      />
    ),
    cell: ({ row }) => (
      <Checkbox
        checked={row.getIsSelected()}
        onCheckedChange={(value) => row.toggleSelected(!!value)}
        aria-label="Select row"
        className="translate-y-0.5"
      />
    ),
    maxSize: 30,
    enableSorting: false,
    enableHiding: false,
  }

  // ----------------------------------------------------------------
  // 2) actions 컬럼 (Dropdown 메뉴)
  // ----------------------------------------------------------------
  const actionsColumn: ColumnDef<typeof documentClasses.$inferSelect> = {
    id: "actions",
    enableHiding: false,
    cell: function Cell({ row }) {
      return (
        <DropdownMenu>
          <DropdownMenuTrigger asChild>
            <Button
              aria-label="Open menu"
              variant="ghost"
              className="flex size-8 p-0 data-[state=open]:bg-muted"
            >
              <Ellipsis className="size-4" aria-hidden="true" />
            </Button>
          </DropdownMenuTrigger>
          <DropdownMenuContent align="end" className="w-40">
            <DropdownMenuItem
              onSelect={() => onDetail?.(row.original)}
            >
              Detail
            </DropdownMenuItem>
            <DropdownMenuItem
              onSelect={() => setRowAction({ row, type: "update" })}
            >
              Edit
            </DropdownMenuItem>

            <DropdownMenuSeparator />
            <DropdownMenuItem
              onSelect={() => setRowAction({ row, type: "delete" })}
            >
              Delete
              <DropdownMenuShortcut>⌘⌫</DropdownMenuShortcut>
            </DropdownMenuItem>
          </DropdownMenuContent>
        </DropdownMenu>
      )
    },
    maxSize: 30,
  }

  // ----------------------------------------------------------------
  // 3) 데이터 컬럼들
  // ----------------------------------------------------------------
  const dataColumns: ColumnDef<typeof documentClasses.$inferSelect>[] = [
    {
      accessorKey: "value",
      enableResizing: true,
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="DOC_CLASS" />
      ),
      meta: {
        excelHeader: "DOC_CLASS",
        type: "text",
      },
      cell: ({ row }) => {
        const value = row.getValue("value") as string
        return <span className="">{value}</span>
      },
      minSize: 80
    },

    {
      accessorKey: "description",
      enableResizing: true,
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="Description" />
      ),
      meta: {
        excelHeader: "Description",
        type: "text",
      },
      cell: ({ row }) => {
        const description = row.getValue("description") as string
        return <span>{description || "-"}</span>
      },
      minSize: 200
    },

    {
      accessorKey: "createdAt",
      enableResizing: true,
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="createdAt" />
      ),
      meta: {
        excelHeader: "createdAt",
        type: "date",
      },
      cell: ({ row }) => {
        const dateVal = row.getValue("createdAt") as Date
        return formatDateTime(dateVal, "KR")
      },
      minSize: 80
    }
  ]

  // ----------------------------------------------------------------
  // 4) 최종 컬럼 배열: select, dataColumns, actions
  // ----------------------------------------------------------------
  return [
    selectColumn,
    ...dataColumns,
    actionsColumn,
  ]
}