본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

테이블 (Table)

원문 보기 위키 갱신

Table은 정렬, 선택, 열 크기 조정, 무한 스크롤을 지원하며 행과 열로 구조화된 데이터를 표시하는 컴포넌트예요.

출처: HeroUI Table

본문

사용 방법 (Usage)

import { Table } from '@heroui/react';
{
  "preview": "table-basic",
  "minHeight": "320px"
}

구조 (Anatomy)

import { Table } from '@heroui/react';

export default () => (
  <Table>
    <Table.ScrollContainer>
      <Table.Content aria-label="Example table">
        <Table.Header>
          <Table.Column>Name</Table.Column>
          <Table.Column>Role</Table.Column>
        </Table.Header>
        <Table.Body>
          <Table.Row>
            <Table.Cell>Kate Moore</Table.Cell>
            <Table.Cell>CEO</Table.Cell>
          </Table.Row>
        </Table.Body>
      </Table.Content>
    </Table.ScrollContainer>
    <Table.Footer>{/* Optional footer content */}</Table.Footer>
  </Table>
);

예제 (Examples)

보조 변형 (Secondary Variant)

{
  "preview": "table-secondary-variant",
  "minHeight": "320px"
}

비동기 로딩 (Async Loading)

무한 스크롤에는 Table.LoadMore를 사용해요. 뷰포트에 스크롤될 때 onLoadMore를 트리거하는 센티널 행을 렌더링해요.

{
  "preview": "table-async-loading",
  "minHeight": "380px"
}

정렬 (Sorting)

Table.Column의 allowsSorting prop으로 열을 정렬 가능하게 만들 수 있어요. 정렬 상태를 관리하려면 Table.Content에서 sortDescriptor와 onSortChange를 사용해요. 라벨을 Table.SortableColumnHeader로 감싸고 열의 렌더 prop에서 전달되는 sortDirection을 전달해 기본 오름차순/내림차순 표시를 렌더링해요.

{
  "preview": "table-sorting",
  "minHeight": "380px"
}

선택 (Selection)

Table.Content의 selectionMode로 행 선택을 활성화할 수 있어요. 전체 선택 및 행별 체크박스에는 slot="selection"을 가진 Checkbox를 사용해요.

{
  "preview": "table-selection",
  "minHeight": "380px"
}

확장 가능한 행 (Expandable Rows)

행을 중첩해 계층적 데이터를 표시할 수 있어요. treeColumn prop으로 열을 지정하고, 해당 열의 셀에 slot="chevron"을 가진 Button을 렌더링해 사용자가 행을 펼치고 접을 수 있게 해요. expandedKeys prop으로 어떤 행이 펼쳐질지 제어해요.

{
  "preview": "table-expandable-rows",
  "minHeight": "420px"
}

페이지네이션 (Pagination)

Table.Footer를 사용해 테이블 아래에 페이지네이션 컴포넌트를 추가할 수 있어요.

{
  "preview": "table-pagination",
  "minHeight": "400px"
}

열 크기 조정 (Column Resizing)

테이블을 Table.ResizableContainer로 감싸고 각 크기 조정 가능한 열 안에 Table.ColumnResizer를 추가해요.

{
  "preview": "table-column-resizing",
  "minHeight": "420px"
}

빈 상태 (Empty State)

테이블에 데이터가 없을 때 사용자 지정 메시지를 표시하려면 Table.Body에서 renderEmptyState를 사용해요.

{
  "preview": "table-empty-state",
  "minHeight": "300px"
}

가상화 (Virtualization)

Table은 Virtualizer를 통해 가상화를 지원해요. 뷰포트 내에 보이는 행만 렌더링해 대용량 데이터셋을 효율적으로 렌더링할 수 있어요.

{
  "preview": "table-virtualization",
  "minHeight": "380px"
}

TanStack Table

HeroUI의 Table은 헤드리스 테이블 라이브러리 위에서 렌더링 레이어로 동작해요. 이 예제는 열 정의, 정렬, 페이지네이션에 TanStack Table을 사용하고, HeroUI는 스타일링과 접근성을 담당해요.

{
  "preview": "table-tanstack-table",
  "minHeight": "420px"
}

사용자 지정 셀 (Custom Cells)

{
  "preview": "table-custom-cells",
  "minHeight": "420px"
}

커스터마이징 (Customization)

Tailwind CSS

{
  "preview": "table-custom-styles"
}

글로벌 CSS (Global CSS)

Table 컴포넌트 클래스를 커스터마이징하려면 @layer components 지시문을 사용할 수 있어요. 더 알아보기.

@layer components {
  .table-root {
    @apply relative grid w-full overflow-clip;
  }

  .table__header {
    @apply bg-gray-100;
  }

  .table__column {
    @apply px-4 py-2.5 text-left text-xs font-medium text-gray-600;
  }

  .table__row {
    @apply bg-white border-b border-gray-200;
  }

  .table__cell {
    @apply px-4 py-3 text-sm;
  }

  .table__footer {
    @apply flex items-center px-4 py-2.5;
  }
}

스타일 참조 (Styling Reference)

HeroUI는 컴포넌트 변형과 상태를 재사용하고 커스터마이징하기 쉽도록 BEM 방법론을 따르고 있어요.

CSS 클래스 (CSS Classes)

Table 컴포넌트는 다음 CSS 클래스를 사용해요 (소스 스타일 보기):

기본 클래스 (Base Classes)

  • .table-root - 루트 컨테이너 (table은 display: table을 위한 Tailwind CSS 내장 유틸리티 클래스이므로 table 대신 table-root로 명명됐어요)
  • .table__scroll-container - 사용자 지정 스크롤바가 있는 가로 스크롤 래퍼
  • .table__content - <table> 요소
  • .table__header - 헤더 행 (<thead>)
  • .table__column - 열 헤더 셀 (<th>)
  • .table__body - 본문 섹션 (<tbody>)
  • .table__row - 행 요소 (<tr>)
  • .table__cell - 데이터 셀 (<td>)
  • .table__footer - 푸터 컨테이너 (테이블 외부)

고급 클래스 (Advanced Classes)

  • .table__column-resizer - 열 크기 조정용 드래그 핸들
  • .table__resizable-container - 열 크기 조정을 활성화하는 래퍼
  • .table__load-more - 무한 스크롤용 센티널 행
  • .table__load-more-content - 로딩 표시를 위한 스타일링된 컨테이너

변형 클래스 (Variant Classes)

  • .table-root--primary - 카드형 본문이 있는 회색 배경 컨테이너 (기본값)
  • .table-root--secondary - 배경이 없고 독립적인 둥근 헤더

인터랙티브 상태 (Interactive States)

Table은 유연성을 위해 CSS 의사 클래스와 데이터 속성을 모두 지원해요:

  • 호버 (Hover): :hover 또는 [data-hovered="true"] (행 배경 변경)
  • 선택 (Selected): [data-selected="true"] (행 강조)
  • 포커스 (Focus): :focus-visible 또는 [data-focus-visible="true"] (행, 열, 셀의 안쪽 포커스 링)
  • 비활성 (Disabled): :disabled 또는 [aria-disabled="true"] (불투명도 감소)
  • 정렬 가능 (Sortable): [data-allows-sorting="true"] (열의 인터랙티브 커서)
  • 드래그 중 (Dragging): [data-dragging="true"] (불투명도 감소)
  • 드롭 대상 (Drop Target): [data-drop-target="true"] (액센트 배경)

API 참조 (API Reference)

Table

Prop Type Default Description
variant "primary" | "secondary" "primary" Visual variant. Primary has a gray background container; secondary is flat with transparent rows.
className string - Additional CSS classes for the root container
children React.ReactNode - Table content (ScrollContainer, Footer, etc.)

Table.ScrollContainer

Prop Type Default Description
className string - Additional CSS classes
children React.ReactNode - Table.Content element

Table.Content

React Aria Table를 상속해요.

Prop Type Default Description
aria-label string - Accessible label for the table
selectionMode "none" | "single" | "multiple" "none" Selection behavior
selectedKeys Selection - Controlled selected keys
onSelectionChange (keys: Selection) => void - Selection change handler
sortDescriptor SortDescriptor - Current sort state
onSortChange (descriptor: SortDescriptor) => void - Sort change handler
className string - Additional CSS classes

Table.Header

React Aria TableHeader를 상속해요.

Prop Type Default Description
columns T[] - Dynamic column data for render prop pattern
children React.ReactNode | (column: T) => React.ReactNode - Static columns or render prop

Table.Column

React Aria Column을 상속해요.

Prop Type Default Description
id string - Column identifier
allowsSorting boolean false Whether the column is sortable
isRowHeader boolean false Whether this column is a row header
defaultWidth string | number - Default width for resizable columns
minWidth number - Minimum width for resizable columns
children React.ReactNode | (values: ColumnRenderProps) => React.ReactNode - Column content or render prop with sort direction

Table.Body

React Aria TableBody를 상속해요.

Prop Type Default Description
items T[] - Dynamic row data for render prop pattern
renderEmptyState () => React.ReactNode - Content to display when the table is empty
children React.ReactNode | (item: T) => React.ReactNode - Static rows or render prop

Table.Row

React Aria Row를 상속해요.

Prop Type Default Description
id string | number - Row identifier
className string - Additional CSS classes
children React.ReactNode - Row cells

Table.Cell

React Aria Cell을 상속해요.

Prop Type Default Description
className string - Additional CSS classes
children React.ReactNode - Cell content

Table.SortableColumnHeader

오름차순/내림차순 표시가 포함된 정렬 가능한 열 라벨을 렌더링해요. Table.Column 렌더 prop 콜백 안에서 사용하고 sortDirection 값을 전달해요.

Prop Type Default Description
sortDirection "ascending" | "descending" - Current sort direction. Pass through from the Table.Column render prop.
showIndicator boolean true Whether to render the sort indicator icon when a direction is set.
indicator React.ReactNode - Custom indicator element. Overrides the default chevron and receives a data-direction attribute.
className string - Additional CSS classes for the wrapper.
children React.ReactNode - Column label content

Table.Footer

Prop Type Default Description
className string - Additional CSS classes
children React.ReactNode - Footer content (e.g., pagination)

Table.ColumnResizer

React Aria ColumnResizer를 상속해요.

Prop Type Default Description
className string - Additional CSS classes

Table.ResizableContainer

React Aria ResizableTableContainer를 상속해요.

Prop Type Default Description
className string - Additional CSS classes
children React.ReactNode - Table.Content element

Table.LoadMore

React Aria TableLoadMoreItem를 상속해요.

Prop Type Default Description
isLoading boolean false Whether data is currently loading
onLoadMore () => void - Handler called when the sentinel row is visible
children React.ReactNode - Loading indicator content

Table.LoadMoreContent

Prop Type Default Description
className string - Additional CSS classes
children React.ReactNode - Loading indicator content (e.g., Spinner)

Table.Collection

React Aria Collection에서 다시 내보냅니다. 정적 셀(예: 체크박스)과 함께 행 안에 동적 셀을 렌더링하는 데 사용해요.

Prop Type Default Description
items T[] - Collection items
children (item: T) => React.ReactNode - Render prop for each item

TableLayout

Name Type Default Description
rowHeight number | undefined 48 The fixed height of a row in px.
estimatedRowHeight number | undefined — The estimated height of a row, when row heights are variable.
headingHeight number | undefined 48 The fixed height of a section header in px.
estimatedHeadingHeight number | undefined — The estimated height of a section header, when the height is variable.
loaderHeight number | undefined 48 The fixed height of a loader element in px. This loader is specifically for "load more" elements rendered when loading more rows at the root level or inside nested row/sections.
dropIndicatorThickness number | undefined 2 The thickness of the drop indicator.
gap number | undefined 0 The gap between items.
padding number | undefined 0 The padding around the list.

더 알아보기 (Learn more)

HeroUI Table에 대해 더 알아보려면 공식 문서를 확인해 주세요.