Table

Component for building tables.

The <Table> component allows to present data in a table format. It enhances the functionality of regular <table> elements with the possibility to interact and select the data, as well as, helps with accessibility by providing keyboard navigation.

You can sort with the <Table> and add a selectionMode property with which you have the possibility to select entire rows.

The component is divided into several parts. So there are the <Table.Header>, <Table.Body>, <Table.Column>, <Table.Row> and <Table.Cell>.

Import

import { Table } from '@marigold/components';

Appearance

PropertyTypeDescription
variantlinedTable | gridThe available variants of this component.
sizeThe available sizes of this component.

Props

Table

PropertyTypeDefaultDescription
selectionModenone, single, multiplenoneThe type of selection that is allowed in the collection.
onSelectionChange(keys: Selection) => anynoneHandler that is called when the selection changes.
stretchbooleanfalseStretch table to fill the container.
stickyHeaderbooleannoneMake the column sticky to the top of the table.
disabledKeysObjectnoneDisabled the selected row.
sortDescriptorObjectnoneThe current sorted column and direction.
onSortChangefunctionnoneHandler that is called when the sort direction changes.
disableKeyboardNavigationbooleanfalseDisable keyboard navigation. Use if you have input fields in your table. Be aware that this is bad for accessibility.

Table.Header

Sorry! There are currently no props available.

Table.Column

PropertyTypeDefaultDescription
keyKeynoneThe key for the Column.
aligncenter | left | right | justify | charleftControl the alignment of Column.
allowsSortingbooleantrueWhether the column allows sorting.
isRowHeaderbooleantrueWhether a column is a row header and should be announced by assistive technology during row navigation.
widthstring | numberautoSets the width of the field. You can see allowed tokens here: https://tailwindcss.com/docs/width

Table.Body

PropertyTypeDefaultDescription
keyKeynoneThe key for the table body.
itemsIterable<object>noneItem objects in the table body
loadingStateerror | idle | loading | loadingMore | filtering | sortingnoneThe current loading state of the table.
onLoadMore(property) onLoadMore?: (() => any) | undefinednoneHandler that is called when more items should be loaded, e.g. while scrolling near the bottom.

Table.Row

PropertyTypeDefaultDescription
keyKeynoneThe key for table row.
downloadstring | boolean | undefinednoneCauses the browser to download the linked URL. A string may be provided to suggest a file name .
hrefstring | undefinednoneA URL to link .
pingstring | undefinednoneA space-separated list of URLs to ping when the link is followed .
referrerPolicyReact.HTMLAttributeReferrerPolicy | undefinednoneHow much of the referrer to send when following the link.
relstring | undefinednoneThe relationship between the linked resource and the current page
targetReact.HTMLAttributeAnchorTarget | undefinednoneThe target window for the link.
textValuestring | undefinednoneA string representation of the row's contents, used for features like typeahead.

Table.Cell

Sorry! There are currently no props available.

Examples

Compact variant

In this example you can see the size compact from the theme b2b. It reduces the cell spacing. There is also another size, which is called expanded, which enlarges the cells.

NameFirstnameHouseYear of birth
PotterHarryGryffindor1980
MalfoyDracoSlytherin1980
DiggoryCedricHufflepuff1977
LovegoodLunaRavenclaw1981

Aligned Columns

In this example you can align any Table Column by using align property over <Table.Column align='right'> .

EventDateLocationPriceTicket Number
Music Festival2023-08-25Central Park$50123456789
Movie Premiere2023-09-10Red Carpet Theater$100987654321
Conference2023-10-05Convention Center$200246813579
Sports Tournament2023-11-20Stadium$75135792468

Fixed Header

We can set the header of the table to be fixed while scrolling by using stickyHeader over <Table stickyHeader> .

Loading data ⬇️ ......

Selection mode single

If you have set the selectionMode to single, then you just can select only one row of the table.

NameFirstnameHouseYear of birth
PotterHarryGryffindor1980
MalfoyDracoSlytherin1980
DiggoryCedricHufflepuff1977
LovegoodLunaRavenclaw1981

Selection mode multiple

This example shows how to use the selectionMode, but with the setting that multiple checkboxes appear and you can select as many lines as you want.

NameFirstnameHouseYear of birth
PotterHarryGryffindor1980
MalfoyDracoSlytherin1980
DiggoryCedricHufflepuff1977
LovegoodLunaRavenclaw1981

Dynamic collections

You don't have to hard code the table items, you could also create a dynamic collection and iterate through it. That's what the example shows.

NameFirstnameHouseYear of birth
PotterHarryGryffindor1980
MalfoyDracoSlytherin1980
DiggoryCedricHufflepuff1977
LovegoodLunaRavenclaw1981
Selected rows:

Disabled Rows

You can also disable certain rows. For that all you have to do is add the property disabledKeys with the certain key to the table.

NameFirstnameHouseYear of birth
PotterHarryGryffindor1980
MalfoyDracoSlytherin1980
DiggoryCedricHufflepuff1977
LovegoodLunaRavenclaw1981

Sorting

The options to sort are ascending or descending. For this purpose the properties (onSortChange, sortDescriptor) for sorting must be specified.

NameHeightMassBirth Year
Luke Skywalker1727719BBY
C-3PO16775112BBY
R2-D2963233BBY
Darth Vader20213641.9BBY
Leia Organa1504919BBY
Owen Lars17812052BBY
Beru Whitesun lars1657547BBY
R5-D49732unknown
Biggs Darklighter1838424BBY
Obi-Wan Kenobi1827757BBY

Sort:  / 

Async sorting

In this example the <Table> is also sortable but its build async. For that you must use the useAsyncList hook. The hook manages the async list data and provides convenience methods for updating the data.

NameHeightMassBirth Year

Nested Columns

Columns can be nested, which will result in more than one header row to be created. Note the usage of isRowHeader in the example below. By default, only the first column is included in the accessibility name for each row.

NameInformation
First NameLast NameAgeBirthday
SamSmith36May 3
JuliaJones24February 10
PeterParker28September 7
BruceWayne32December 18

Fixed Column width

In a Table you can specify the width of a column using width property in the Table.Column component, this allows you to set a fixed width for a specific columnm ensuring consistent column widths throughout the table .

NameFirstnameHouseYear of birth
PotterHarryGryffindor1980
MalfoyDracoSlytherin1980
DiggoryCedricHufflepuff1977
LovegoodLunaRavenclaw1981

Formatters

We have formatting helper for dates and values you can easily ensure consistent and accurate display. See NumericFormat, DateFormat for more informations.

EventDateLocationPriceTicket Number
Music FestivalFreitag, 25. August 2023Central Park$50.00123456789
Movie PremiereSonntag, 10. September 2023Red Carpet Theater$100.00987654321
ConferenceDonnerstag, 5. Oktober 2023Convention Center$200.12246813579
Sports TournamentMontag, 20. November 2023Stadium$75.00135792468

Using input fields

Example for using controlled state with input fields inside a table.

Avoid mutation

It's best practice in React to avoid mutation cause of unpredicatable side effects. For more detailed explanations see here.

NameFirstnameHouseYear of birth
PotterHarry
1980
MalfoyDraco
1980
DiggoryCedric
1977
LovegoodLuna
1981