Hoppa till huvudinnehåll

Table

Komponent för att visualisera data. Kan kombineras med andra komponenter, till exemepel Select, för att filtrera eller SearchField för att söka osv.

<Table aria-label='Fruit'>
<TableHeader>
<Column>Name</Column>
<Column>Description</Column>
</TableHeader>
<TableBody>
<Row>
<Cell>Banana</Cell>
<Cell>A yellow fruit</Cell>
</Row>
<Row>
<Cell>Pear</Cell>
<Cell>Usually green</Cell>
</Row>
</TableBody>
</Table>
NameDescription
BananaA yellow fruit
PearUsually green

Installation

npm install @midas-ds/components
import { Table, TableHeader, Column, TableBody, Row, Cell } from '@midas-ds/components'

Varianter

För en tabell behövs data för hur kolumnerna och raderna ska populeras. Vi kommer basera samliga tabeller på följande dataset. Börja med att sätta upp dina kolumner. Nycklarna på raderna ska sedan referera till värdet för kolumnernas id, i det här fallet name och description.

const columns = [
{ name: 'Namn', id: 'name' },
{ name: 'Beskrivning', id: 'description', width: 'max-content' },
]

const rows = [
{
id: 'apple',
name: 'Apple',
description: 'Pink lady is a good one',
},
{
id: 'banana',
name: 'Banana',
description: 'A yellow fruit',
},
{
id: 'pear',
name: 'Pear',
description: 'Usually green',
},
]

Standardtabell

<Table aria-label='Fruit'>
<TableHeader columns={columns}>
{column => {
return <Column>{column.name}</Column>
}}
</TableHeader>
<TableBody items={rows}>
{item => {
return (
<Row columns={columns}>
{column => {
return <Cell>{item[column.id]}</Cell>
}}
</Row>
)
}}
</TableBody>
</Table>
NamnBeskrivning
ApplePink lady is a good one
BananaA yellow fruit
PearUsually green

Kompakt tabell

Använd narrow om du vill ha en kompaktare tabell.

<Table
aria-label='Fruit'
narrow
>
...
</Table>
NamnBeskrivning
ApplePink lady is a good one
BananaA yellow fruit
PearUsually green

Valbara rader

Tabellen har inbyggd funktion för att kunna välja en eller flera rader med selectionMode vilket kan vara antingen single eller multiple

single
<Table
aria-label='Fruit'
selectionMode='single'
>
...
</Table>
NamnBeskrivning
ApplePink lady is a good one
BananaA yellow fruit
PearUsually green
multiple
<Table
aria-label='Fruit'
selectionMode='multiple'
>
...
</Table>
NamnBeskrivning
ApplePink lady is a good one
BananaA yellow fruit
PearUsually green

Kontrollerade val

Om du själv vill ha kontroll över vilka eller vilken rad som är vald kan du använda useState

import type React from 'react'
import type { Selection } from 'react-aria-components'
const [selectedKeys, setSelectedKeys] = React.useState<Selection>(new Set(['apple']))

<Table
aria-label='Fruit'
selectedKeys={selectedKeys}
onSelectionChange={setSelectedKeys}
selectionMode='multiple'
>
...
</Table>
NamnBeskrivning
ApplePink lady is a good one
BananaA yellow fruit
PearUsually green
Valda rader: apple

Randiga rader

Använd striped om du vill att raderna ska alternera mellan vit och grå.

<Table
aria-label='Fruit'
striped
>
...
</Table>
NamnBeskrivning
ApplePink lady is a good one
BananaA yellow fruit
PearUsually green

API

Table

NameTypeDefaultDescription
narrow boolean-

@deprecated This variant will be replaced with a new scaling api accross all components.

striped boolean-

Alternating colors for rows

children ReactNode-

The elements that make up the table. Includes the TableHeader, TableBody, Columns, and Rows.

selectionBehavior SelectionBehavior"toggle"

How multiple selection should behave in the collection.

disabledBehavior DisabledBehavior"selection"

Whether disabledKeys applies to all interactions, or only selection.

dragAndDropHooks DragAndDropHooks-

The drag and drop hooks returned by useDragAndDrop used to enable drag and drop behavior for the Table.

disallowEmptySelection boolean-

Whether the collection allows empty selection.

disabledKeys Iterable<Key>-

A list of row keys to disable.

selectionMode SelectionMode-

The type of selection that is allowed in the collection.

selectedKeys Iterable<Key> | "all"-

The currently selected keys in the collection (controlled).

defaultSelectedKeys Iterable<Key> | "all"-

The initial selected keys in the collection (uncontrolled).

sortDescriptor SortDescriptor-

The current sorted column and direction.

className string | ((values: TableRenderProps & { defaultClassName: string; }) => string)-

The CSS className for the element. A function may be provided to compute the class based on component state.

style CSSProperties | ((values: TableRenderProps & { defaultStyle: CSSProperties; }) => CSSProperties)-

The inline style for the element. A function may be provided to compute the style based on component state.

slot string-

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

TableHeader

Row

Column

Cell