Table
정렬 가능한 헤더와 행 선택 기능이 있는 손그림 스타일의 데이터 테이블입니다.
개요
Table은 정렬 가능한 열과 선택적 행 선택 기능이 있는 구조화된 데이터 행을 표시해요. 정렬과 선택은
제어되는 방식이에요 — 테이블은 전달된 rows를 전달된 순서대로 렌더링하고, onSortChange /
onSelectionChange를 통해 의도를 전달해요.
구조
스케치 스타일의 윤곽선(@ghds/sketch-core)이 시맨틱 <table>(React Native에서는 플렉스 행)을
감싸요. 정렬 가능한 헤더는 aria-sort를 가진 버튼이고, 선택적 앞 체크박스 열(모두 선택 헤더 포함)이
선택을 담당해요. 이건 M12 데이터 상태 패턴의 데이터 기반이에요.
변형 및 상태
| State | Status | Notes |
|---|---|---|
| 정렬 가능한 열 | Implemented | 헤더 버튼이 오름차순/내림차순을 토글해요; aria-sort가 반영돼요 (웹). |
| 행 선택 | Implemented | 옵션인 체크박스 열 + 중간 상태의 모두 선택. |
| 선택된 행 | Implemented | 배경이 하이라이트돼요. |
사용법
Do
- 각 행에 안정적인 id를 부여하고, 이미 정렬된 상태로 rows를 제공하세요 (테이블은 제어 방식이에요).
- 접근성 이름을 위해 caption을 제공하세요.
- 숫자 열은 align="right"으로 오른쪽 정렬하세요.
Don't
- 테이블이 데이터를 정렬하거나 페이지 매김을 할 거라고 기대하지 마세요 — onSortChange를 데이터에 연결하세요.
- 페이지 매김이나 가상화 없이 큰 데이터셋을 하나의 테이블에 넣지 마세요.
접근성
- React / Web Components: 시맨틱
<table>에<caption>,scope="col"헤더, 정렬 가능한 헤더에aria-sort(none/ascending/descending)가 있어요. 선택은 접근성 라벨이 있는 GHDS Checkbox를 사용해요 (“행 선택 …”, “모든 행 선택”). Web Components는sort-change와selection-change를 디스패치해요. - React Native: 행은
role="table"/row/columnheader/cell을 가진 플렉스Box예요; 정렬은 헤더 버튼, 선택은role="checkbox"컨트롤이에요.aria-sort는 문서화된 웹 전용 차이예요. - 접근성 가이드를 참고하세요.
| Key | Action |
|---|---|
Tab | 정렬 가능한 헤더, 선택 체크박스, 인터랙티브 셀 사이를 이동해요. |
Enter / Space | 헤더(정렬)를 활성화하거나 체크박스를 토글해요. |
콘텐츠
간결한 열 헤더와 셀 값. 불린/상태는 셀 안에 Badge로 표현해요.
셀에는 <code> 키 이름, 색상 견본, 크기 미리보기 같은 풍부한 콘텐츠를 넣을 수 있어요. React는
셀 값이 이미 ReactNode라서 요소를 그대로 넘기면 돼요. Web Components는 행 값이
string | number로 유지돼요. 대신 열에 render(value, row) 함수를 지정하고 Lit TemplateResult를
반환하세요(lit에서 html을 가져와요). render가 없는 열은 원래 값을 그대로 보여 줘요.
React Native는 셀 값이 string | number이고 텍스트로 표시돼요. 아직 풍부한 셀 콘텐츠는 지원하지 않아요.
Props API
React@ghds/react
| Prop | Type | Default | Description |
|---|---|---|---|
columns | TableColumn[] | — | 열당 { key, header, sortable?, align? }. |
rows | TableRow[] | — | { id, [key]: ReactNode } — 주어진 순서대로 렌더링돼요. |
caption | string | — | 접근성 캡션. |
sort / onSortChange | TableSort / (sort) => void | — | 제어되는 정렬 + 의도 콜백. |
selectedIds / onSelectionChange | string[] / (ids) => void | — | 제어되는 선택 (체크박스 표시). |
Web Components@ghds/web-components
| Prop | Type | Default | Description |
|---|---|---|---|
columns / rows | GhTableColumn[] / GhTableRow[] | — | 속성으로 설정돼요. 행 값은 string | number예요. |
columns[].render | (value, row) => unknown | — | 선택적인 열별 셀 렌더러. 풍부한 콘텐츠는 Lit TemplateResult로 반환해요. |
caption | string | — | 접근성 캡션. |
sort | GhTableSort | — | 현재 정렬 (속성). |
selectedIds | string[] | — | 선택된 id (속성); 체크박스를 활성화해요. |
sort-change / selection-change | CustomEvent | — | 디스패치된 의도. |
React Native@ghds/react-native
| Prop | Type | Default | Description |
|---|---|---|---|
columns / rows | TableColumn[] / TableRow[] | — | React와 같은 형태지만, 셀 값은 string | number예요(텍스트로 표시). |
caption | string | — | 접근성 이름. |
sort / onSortChange | TableSort / (sort) => void | — | 제어되는 정렬 + 의도 콜백. |
selectedIds / onSelectionChange | string[] / (ids) => void | — | 제어되는 선택. |
testID | string | — | 쿼리용 테스트 핸들. |
코드 예시
// React — @ghds/react
import { Table, type TableSort } from '@ghds/react/table';
<Table
caption="Contributors"
columns={[
{ key: 'name', header: 'Name', sortable: true },
{ key: 'commits', header: 'Commits', sortable: true, align: 'right' },
]}
rows={rows}
sort={sort}
onSortChange={setSort}
selectedIds={selected}
onSelectionChange={setSelected}
/>;
<!-- Web Components — @ghds/web-components -->
<script type="module">
import '@ghds/web-components/table';
</script>
<gh-table id="my-table" caption="Contributors"></gh-table>
<script>
const el = document.getElementById('my-table');
el.columns = [
{ key: 'name', header: 'Name', sortable: true },
{ key: 'commits', header: 'Commits', sortable: true, align: 'right' },
];
const data = [
{ id: '1', name: 'Ada', commits: 128 },
{ id: '2', name: 'Grace', commits: 342 },
];
el.rows = data;
el.selectedIds = [];
el.addEventListener('sort-change', (e) => {
el.sort = e.detail;
// gh-table은 받은 순서대로 행을 그려요. 정렬은 직접 해야 해요
const { key, direction } = e.detail;
const sign = direction === 'asc' ? 1 : -1;
el.rows = [...data].sort((a, b) => (a[key] > b[key] ? sign : a[key] < b[key] ? -sign : 0));
});
el.addEventListener('selection-change', (e) => { el.selectedIds = e.detail; });
</script>
// Web Components — 열 `render` 함수로 풍부한 셀 콘텐츠 넣기
import { html } from 'lit';
import '@ghds/web-components/table';
const el = document.querySelector('gh-table');
el.columns = [
{ key: 'token', header: 'Token', render: (value) => html`<code>${value}</code>` },
{ key: 'description', header: 'Description' },
];
el.rows = [{ id: 'bg', token: 'sys.color.bg.primary', description: 'Page background' }];
// React Native — @ghds/react-native
import { Table } from '@ghds/react-native/table';
<Table
caption="Contributors"
columns={[{ key: 'name', header: 'Name', sortable: true }]}
rows={[{ id: '1', name: 'Ada' }]}
sort={sort}
onSortChange={setSort}
/>;