Table
表形式のデータを表示。行選択と縞模様の表示をサポート。
インストール
import { Table } from "@ginga-ui/core";基本的な使い方
シンプルなテーブルの使用例です。
| 名前 | 種類 | 更新日 | サイズ |
|---|---|---|---|
| ドキュメント | フォルダ | 2024/12/01 | 1.2 GB |
| 画像 | フォルダ | 2024/11/28 | 894 MB |
| README.md | Markdownファイル | 2024/12/15 | 4 KB |
| package.json | JSONファイル | 2024/12/10 | 2 KB |
table.tsx
import { Table } from "@ginga-ui/core";
export function BasicExample() {
return (
<Table data={sampleData} columns={columns} aria-label="ファイル一覧" />
);
}縞模様
striped を指定すると、偶数行に背景色が付いて行を目で追いやすくなります。
| 名前 | 種類 | 更新日 | サイズ |
|---|---|---|---|
| ドキュメント | フォルダ | 2024/12/01 | 1.2 GB |
| 画像 | フォルダ | 2024/11/28 | 894 MB |
| README.md | Markdownファイル | 2024/12/15 | 4 KB |
| package.json | JSONファイル | 2024/12/10 | 2 KB |
table.tsx
import { Table } from "@ginga-ui/core";
export function StripedExample() {
return (
<Table
data={sampleData}
columns={columns}
striped
aria-label="縞模様のファイル一覧"
/>
);
}行選択
行を選択できるテーブルです。
| 名前 | 種類 | 更新日 | サイズ | |
|---|---|---|---|---|
| ドキュメント | フォルダ | 2024/12/01 | 1.2 GB | |
| 画像 | フォルダ | 2024/11/28 | 894 MB | |
| README.md | Markdownファイル | 2024/12/15 | 4 KB | |
| package.json | JSONファイル | 2024/12/10 | 2 KB |
table.tsx
import { Table } from "@ginga-ui/core";
export function WithSelectionExample() {
return (
<Table
data={sampleData}
columns={columns}
selectionMode="multiple"
aria-label="選択可能なファイル一覧"
onSelectionChange={(keys) => console.log("選択:", keys)}
/>
);
}ユーザーテーブル
実用的なユーザーデータテーブルの例です。
| 名前 | メールアドレス | 権限 |
|---|---|---|
| 山田太郎 | yamada@example.com | 管理者 |
| 佐藤花子 | sato@example.com | 編集者 |
| 鈴木一郎 | suzuki@example.com | 閲覧者 |
table.tsx
import { Table } from "@ginga-ui/core";
export function UserTableExample() {
return (
<Table data={userData} columns={userColumns} aria-label="ユーザー一覧" />
);
}空の状態
emptyMessage でデータがないときに表示するメッセージを指定できます。
| 名前 | 種類 | 更新日 | サイズ |
|---|---|---|---|
ファイルがありません | |||
table.tsx
import { Table } from "@ginga-ui/core";
export function EmptyStateExample() {
return (
<Table
data={[]}
columns={columns}
emptyMessage="ファイルがありません"
aria-label="空のテーブル"
/>
);
}Props
Table
children は渡せません。行は data、列は columns で渡します。
| 名前 | 型 | 既定値 | 説明 |
|---|---|---|---|
data | T[] | - | 必須のpropsです。表示する行のデータ。 |
columns | TableColumn<T>[] | - | 必須のpropsです。列の定義。下記の TableColumn を参照してください。 |
aria-label | string | - | 必須のpropsです。表のアクセシブルな名前。 |
getRowKey | (row: T, index: number) => Key | - | 行のキーを返す関数。省略すると行の id、id がなければ index を使います。 |
selectionMode | "none" | "single" | "multiple" | "none" | 行の選択方式。選択できる場合はチェックボックスの列が追加されます。 |
onSelectionChange | (selectedRows: T[], keys: Selection) => void | - | 選択が変わったときに、選択中の行とキーを受け取って呼ばれます。 |
showHeader | boolean | true | ヘッダー行を表示します。false にすると見た目上は隠れますが、スクリーンリーダーには読み上げられます。 |
striped | boolean | false | 行の背景色を交互に変えます。 |
emptyMessage | ReactNode | - | data が空のときに表示する内容。 |
上記に加えて、React Aria の Table(新しいタブで開く) の props を受け取ります。
TableColumn<T>
| 名前 | 型 | 既定値 | 説明 |
|---|---|---|---|
key | string | - | 必須のpropsです。列を一意に識別するキー。 |
header | ReactNode | - | 必須のpropsです。ヘッダーの内容。 |
accessor | keyof T | ((row: T) => ReactNode) | - | 必須のpropsです。表示する行のプロパティ名、またはセルの内容を返す関数。 |
isRowHeader | boolean | - | この列を行見出しにします。どの列にも指定しない場合は最初の列が行見出しになります。 |
allowsSorting | boolean | - | 列を並べ替え可能にします。並べ替えは sortDescriptor と onSortChange で扱います。 |