summaryrefslogtreecommitdiff
path: root/lib/material/table/material-table-columns.tsx
blob: dd4057701ad7105c65cb56b04bc53189a82a954d (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
"use client"

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

import { formatDate } from "@/lib/utils"
import { Checkbox } from "@/components/ui/checkbox"
import { Button } from "@/components/ui/button"
import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header"

// Material 타입 정의 (서비스에서 반환되는 타입과 일치)
type Material = {
  id: number;
  MATKL: string | null;    // 자재그룹
  MATNR: string | null;    // 자재코드
  ZZNAME: string | null;   // 자재명
  ZZPJT: string | null;    // 프로젝트
  createdAt: Date;
  updatedAt: Date;
}

interface GetColumnsProps {
  setRowAction: React.Dispatch<React.SetStateAction<DataTableRowAction<Material> | null>>
}

/**
 * Material 테이블 컬럼 정의
 */
export function getColumns({ setRowAction }: GetColumnsProps): ColumnDef<Material>[] {
  
  // ----------------------------------------------------------------
  // 1) select 컬럼 (체크박스)
  // ----------------------------------------------------------------
  const selectColumn: ColumnDef<Material> = {
    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"
      />
    ),
    size: 40,
    enableSorting: false,
    enableHiding: false,
  }

  // ----------------------------------------------------------------
  // 2) 데이터 컬럼들
  // ----------------------------------------------------------------
  const dataColumns: ColumnDef<Material>[] = [
    {
      accessorKey: "MATKL",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="자재그룹" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("MATKL") as string | null
        return (
          <div className="font-medium">
            {value || "-"}
          </div>
        )
      },
      enableSorting: true,
      enableHiding: false,
    },
    {
      accessorKey: "MATNR",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="자재코드" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("MATNR") as string | null
        return (
          <div className="font-medium">
            {value || "-"}
          </div>
        )
      },
      enableSorting: true,
      enableHiding: false,
    },
    {
      accessorKey: "ZZNAME",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="자재명" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("ZZNAME") as string | null
        return (
          <div className="max-w-[200px] truncate">
            {value || "-"}
          </div>
        )
      },
      enableSorting: true,
    },
    {
      accessorKey: "ZZPJT",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="프로젝트" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("ZZPJT") as string | null
        return (
          <div className="max-w-[150px] truncate">
            {value || "-"}
          </div>
        )
      },
      enableSorting: true,
    },
    {
      accessorKey: "createdAt",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="생성일시" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("createdAt") as Date
        return (
          <div className="text-sm text-muted-foreground">
            {formatDate(value, "KR")}
          </div>
        )
      },
      enableSorting: true,
    },
    {
      accessorKey: "updatedAt",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="수정일시" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("updatedAt") as Date
        return (
          <div className="text-sm text-muted-foreground">
            {formatDate(value, "KR")}
          </div>
        )
      },
      enableSorting: true,
    },
  ]

  // ----------------------------------------------------------------
  // 3) actions 컬럼 (상세보기)
  // ----------------------------------------------------------------
  const actionsColumn: ColumnDef<Material> = {
    id: "actions",
    enableHiding: false,
    cell: function Cell({ row }) {
      return (
          <Button onClick={() => setRowAction({ row, type: "view" })}>
            상세
          </Button>
      )
    },
    size: 40,
  }

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