Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "eslint .",
"preview": "vite preview",
"postinstall": "node scripts/setup-git-hooks.cjs",
"prepare": "husky && husky install"
"prepare": "husky"
},
"lint-staged": {
"**/*.{js,jsx}": [
Expand Down
108 changes: 108 additions & 0 deletions src/components/Table.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import {
HTMLAttributes,
ReactNode,
TableHTMLAttributes,
TdHTMLAttributes,
} from "react";

import { cn } from "@/utils/cn";

interface TableProps<T> extends TableHTMLAttributes<HTMLTableElement> {
data: T[];
headRow: () => ReactNode;
bodyRow: (params: T) => ReactNode;
footerRow?: () => ReactNode;
}

function Table<T>({
data,
className,
headRow,
bodyRow,
footerRow,
...props
}: TableProps<T>) {
return (
<div className="rounded-lg border-[1px] border-gray-200 text-black overflow-hidden">
{/* ───── 스크롤이 필요한 영역 ───── */}
<div className="overflow-x-auto">
<table
className={cn(
"table-fixed min-w-full text-left border-collapse",
className,
)}
{...props}
>
<thead className="bg-red-50">{headRow()}</thead>
<tbody className="bg-white">{data.map(bodyRow)}</tbody>
</table>
</div>

{/* ──── 스크롤과 무관한 하단 영역 ───── */}
{footerRow && (
<table className="w-full border-collapse">
<tbody>{footerRow()}</tbody>
</table>
)}
</div>
);
}

interface TrProps extends HTMLAttributes<HTMLTableRowElement> {
showLastBottomBorder?: boolean;
}

function Tr({ children, className, showLastBottomBorder, ...props }: TrProps) {
const tableRowClassName = cn(
showLastBottomBorder ? "border-b-[1px]" : "not-last:border-b-[1px]",
"border-gray-20",
className,
);

return (
<tr className={tableRowClassName} {...props}>
{children}
</tr>
);
}

function Th({
children,
className,
...props
}: TdHTMLAttributes<HTMLTableCellElement>) {
const thClassName = cn(
"px-3.5 py-3 text-sm font-normal",
"first:sticky left-0 border-gray-20 z-10 bg-red-50",
className,
);

return (
<th className={thClassName} {...props}>
{children}
</th>
);
}

interface TdProps extends TdHTMLAttributes<HTMLTableCellElement> {
noSticky?: boolean;
}

function Td({ children, className, noSticky, ...props }: TdProps) {
const tdClassName = cn(
"px-3.5 py-3 break-words whitespace-normal",
noSticky ? "" : "first:sticky left-0 z-10 bg-white first:whitespace-nowrap",
className,
);
return (
<td className={tdClassName} {...props}>
{children}
</td>
);
}

Table.Tr = Tr;
Table.Th = Th;
Table.Td = Td;

export default Table;