import { useState, type ReactNode } from "react"
import type { Meta, StoryObj } from "@storybook/react-vite"
import { Badge, Checkbox, Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow } from "@hm-design/ui"
import { StateBoard } from "@/components/story-board"
import { STORY_KICKER, StoryPage, StoryPageHeader, StorySection } from "@/components/story-page"
// Table is a static display primitive, so the representative Story is a board of specimens. Checkbox and Badge appear only as
// cell content (Story composition); Table has no dependency on them and no selection, sorting or paging behavior.
// The Story keeps the selected rows in local state and passes `data-state="selected"` itself.
const projects = [
{ id: "P-001", name: "한맥 노선 정비", status: "진행", amount: 1280000 },
{ id: "P-002", name: "시설물 점검 리포트", status: "완료", amount: 450000 },
{ id: "P-003", name: "교량 안전 진단", status: "대기", amount: 9800000 },
]
const won = (n: number) => n.toLocaleString("ko-KR")
function BasicTable() {
return (
번호
프로젝트
상태
금액
{projects.map((p) => (
{p.id}
{p.name}
{p.status}
{won(p.amount)}
))}
합계
{won(projects.reduce((s, p) => s + p.amount, 0))}
프로젝트별 계약 금액(원)
)
}
function SelectableTable() {
const [selected, setSelected] = useState(["P-002"])
const toggle = (id: string) => setSelected((s) => (s.includes(id) ? s.filter((x) => x !== id) : [...s, id]))
return (
setSelected(v ? projects.map((p) => p.id) : [])}
/>
프로젝트
금액
{projects.map((p) => (
toggle(p.id)} />
{p.name}
{won(p.amount)}
))}
)
}
function LongTextTable() {
return (
제목
비고
긴 텍스트는 셀 안에서 줄바꿈되고 행 높이만 늘어납니다. 공백 없는 문자열도 열 폭을 밀어내지 않습니다.
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
짧은 행
-
)
}
function WideTable() {
const cols = Array.from({ length: 10 }, (_, i) => `컬럼 ${i + 1}`)
return (
{cols.map((c) => (
{c}
))}
{Array.from({ length: 3 }, (_, r) => (
{cols.map((c) => (
{`값 ${r + 1}-${c.slice(3)}`}
))}
))}
)
}
const box = (node: ReactNode) => {node}
const sections = [
{ id: "basic", label: "Basic + Badge + numeric + footer + caption", node: },
{ id: "selected", label: "Selected row", note: "Checkbox와 data-state=\"selected\"는 Story가 조합합니다(Table에는 선택 로직이 없음)", node: },
{ id: "long", label: "Long text", node: },
{ id: "wide", label: "Horizontal overflow", note: "container가 가로로 scroll 하고 페이지는 밀리지 않습니다", node: },
]
const meta = { title: "Components/Data & Display/Table", component: Table, parameters: { layout: "padded" } } satisfies Meta
export default meta
type Story = StoryObj
const board = (...ids: string[]) => (
ids.includes(s.id)).map((s) => ({ id: s.id, label: s.label, note: s.note, wide: true, children: box(s.node) }))} />
)
// Representative Story: the common Story page; the sections group the same specimens.
export const All: Story = {
name: "Table",
parameters: { layout: "fullscreen", controls: { disable: true } },
render: () => (
{board("basic")}
{board("selected")}
{board("long", "wide")}
),
}
export const Basic: Story = { tags: ["verification"], render: () => box() }
export const Selected: Story = { tags: ["verification"], render: () => box() }
export const LongText: Story = { tags: ["verification"], name: "Long Text", render: () => box() }
export const Overflow: Story = { tags: ["verification"], name: "Horizontal Overflow", render: () => box() }