Hoppa till huvudinnehåll

Table

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

import { Table, TableHeader, Column, TableBody, Row, Cell } from '@midas-ds/components'
<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

Varianter

För en tabell behövs data för hur kolumnerna och raderna ska få sitt innehåll. Vi kommer basera samtliga 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', isRowHeader: true },
{ 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 isRowHeader={column.isRowHeader}>{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 size='medium' om du vill ha en kompaktare tabell.

<Table
aria-label='Fruit'
size='medium'
>
...
</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 vara olika.

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

API

Table

NameTypeDefaultDescription
narrowfalse

A more compact version of the table @deprecated since v10.1.1, please use the size prop instead.

sizelarge

Row height (large: 48px, medium: 40px)

stripedfalse

Alternating colors for rows

children-

The children of the component. A function may be provided to alter the children based on component state.

selectionBehavior"toggle"

How multiple selection should behave in the collection.

disabledBehavior"selection"

Whether disabledKeys applies to all interactions, or only selection.

dragAndDropHooksDragAndDropHooks-

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

selectionMode-

The type of selection that is allowed in the collection.

disabledKeysIterable<Key>-

A list of row keys to disable.

disallowEmptySelection-

Whether the collection allows empty selection.

selectedKeys-

The currently selected keys in the collection (controlled).

defaultSelectedKeys-

The initial selected keys in the collection (uncontrolled).

escapeKeyBehavior'clearSelection'

Whether pressing the escape key should clear selection in the table or not.

Most experiences should not modify this option as it eliminates a keyboard user's ability to easily clear selection. Only use if the escape key is being handled externally or should not trigger selection clearing contextually.

shouldSelectOnPressUp-

Whether selection should occur on press up instead of press down.

sortDescriptorSortDescriptor-

The current sorted column and direction.

className-

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

style-

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

slotstring-

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