16.11.0 • Published 5 months ago

@commercetools-uikit/data-table v16.11.0

Weekly downloads
3,575
License
MIT
Repository
github
Last release
5 months ago

DataTable

Description

Shows tabular data, defined by a list of items: the rows, and another list with their corresponding definitions: the columns. Both these lists are arrays of objects.

  • The rows list defines the items you want to render, where each item only requires a unique id which is used for mapping, and the remaining properties to be shown under each column.
  • On the columns list, each object requires a unique key which should correspond to property key of the items of rows that you want to render under this column, and a label which defines the name shown on the header of the column.

These are the only requirements for rendering the most simple table which should suffice for most use cases and is scaled automatically for the available space.

For more advanced configuration and layout customization, a plethora of other options are available, including per-column specific options.

For adding a Row Selection behavior, check the useRowSelection hook which you can use to prepare your rows and columns before passing them to the DataTable component.

Installation

yarn add @commercetools-uikit/data-table
npm --save install @commercetools-uikit/data-table

Additionally install the peer dependencies (if not present)

yarn add react
npm --save install react

Usage

import DataTable from '@commercetools-uikit/data-table';

const rows = [
  { id: 'parasite', title: 'Parasite', country: 'South Korea' },
  { id: 'portrait', title: 'Portrait of a Lady on Fire', country: 'France' },
  { id: 'wat', title: 'Woman at War', country: 'Iceland' },
];

const columns = [
  { key: 'title', label: 'Title' },
  { key: 'country', label: 'Country' },
];

const Example = () => <DataTable rows={rows} columns={columns} />;

export default Example;

Properties

PropsTypeRequiredDefaultDescription
rowsArray: Row[]The list of data that needs to be rendered in the table. Each object in the list can have any shape as long as it has a unique identifier. The data is rendered by using the callback render function itemRenderer.
columnsArray: TColumn<Row>[]See signature.[]Each object requires a unique key which should correspond to property key of the items of rows that you want to render under this column, and a label which defines the name shown on the header. The list of columns to be rendered. Each column can be customized (see properties below).
footerReactNodeElement to render within the tfoot (footer) element of the table.
maxWidthunionPossible values:number , stringThe max width (a number of pixels or a css value string with units) for which the table is allowed to grow. If unset, the table will grow horizontally to fill its parent.
maxHeightunionPossible values:number , stringThe max height (a number of pixels or a css value string with units) for which the table is allowed to grow. If unset, the table will grow vertically to fill its parent and we are able to have a sticky header.
onRowClickFunctionSee signature.A callback function, called when a user clicks on a row.
isCondensedbooleantrueSet this to true to reduce the paddings of all cells, allowing the table to display more data in less space.
onColumnResizedFunctionSee signature.A callback function, called when a column has been resized. Use this callback to get the resized column widths and save them, to be able to restore the value once the user comes back to the page.
disableSelfContainmentbooleanfalseSet this to true to take control of the containment of the table and doing it on a parent element. This means that the table will grow in size without adding scrollbars on itself, both vertically and horizontally and, as a consequence, the maxHeight and maxWidth props are ignored. If you need to enforce these constraints, you must also apply them on the parent element. Additionally, the sticky behaviour of the header will get fixed relatively to the closest parent element with position: relative.
disableHeaderStickinessbooleanSet this to true to prevent the header from being sticky. The header can be sticky only if the table does not have a maxHeight set.
itemRendererFunctionSee signature.(row, column) => row[column.key]The default function used to render the content of each item in a cell. In case a column has its own renderItem render function, it will take precedence over this function.
wrapHeaderLabelsbooleantrueSet this to false to ensure that every column can render their label in one line. By default the header cell grows in height in case the label does not fit in one line.
verticalCellAlignmentunionPossible values:'top' , 'center' , 'bottom''top'The default cell vertical alignment of each row (not the table header).
horizontalCellAlignmentunionPossible values:'left' , 'center' , 'right''left'The default cell horizontal alignment. In case a column has its own align property, it will take precedence over this value.
sortedBystringThe key of the column for which the data is currently sorted by.
onSortChangeFunctionSee signature.A callback function, called when a sortable column's header is clicked. It's required when the isSortable flag is set on at least one column.
sortDirectionunionPossible values:'desc' , 'asc'The sorting direction.

Signatures

Signature columns

{
  /**
   * The unique key of the column that is used to identify your data type.
   * You can use this value to determine which value from a row item should be rendered.
   * <br>
   * For example, if the data is a list of users, where each user has a `firstName` property,
   * the column key should be `firstName`, which renders the correct value by default.
   * The key can also be some custom or computed value, in which case you need to provide
   * an explicit mapping of the value by implementing either the `itemRendered` function or
   * the column-specific `renderItem` function.
   */
  key: string;
  /**
   * The label of the column that will be shown on the column header.
   */
  label: ReactNode;
  /**
   * Sets a width for this column. Accepts the same values as the ones specified for
   * individual [grid-template-columns](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-columns).
   * <br>
   * For example, using `minmax` pairs (e.g. `minmax(200px, 400px)`), a combinations of
   * fraction values (`1fr`/`2fr`/etc), or fixed values such as `200px`.
   * By default, the column grows according to the content and respecting the total table available width.
   */
  width?: string;
  /**
   * Use this to override the table's own `horizontalCellAlignment` prop for this specific column.
   */
  align?: 'left' | 'center' | 'right';
  /**
   * A callback function, called when the header cell is clicked.
   */
  onClick?: (event: MouseEventHandler) => void;
  /**
   * A callback function to render the content of cells under this column, overriding
   * the default `itemRenderer` prop of the table.
   */
  renderItem?: (row: Row, isRowCollapsed: boolean) => ReactNode;
  /**
   * Use this prop to place an `Icon` or `IconButton` on the left of the column label.
   * It is advised to place these types of components through this prop instead of `label`,
   * in order to properly position and align the elements.
   * This is particularly useful for medium-sized icons which require more vertical space than the typography.
   */
  headerIcon?: ReactNode;
  /**
   * Set this to `true` to allow text content of this cell to be truncated with an ellipsis,
   * instead of breaking into multiple lines.
   * <br>
   * NOTE: when using this option, it is recommended to specify a `width` for the column, because
   * if the table doesn't have enough space for all columns, it will start clipping the columns
   * with _truncated_ content, and if no `width` is set (or the value is set `auto` -- the default)
   * it can shrink until the column disappears completely.
   * By enforcing a minimum width for these columns, the table will respect them and grow horizontally,
   * adding scrollbars if needed.
   */
  isTruncated?: boolean;
  /**
   * Set this to `true` to show a sorting button, which calls `onSortChange` upon being clicked.
   * You should enable this flag for every column you want to be able to sort.
   * When at least one column is sortable, the table props `sortBy`, `sortDirection` and `onSortChange` should be provided.
   */
  isSortable?: boolean;
  /**
   * Set this to `true` to prevent this column from being manually resized by dragging
   * the edge of the header with a mouse.
   */
  disableResizing?: boolean;
  /**
   * Set this to `true` to prevent click event propagation for this cell.
   * You might want this if you need the column to have its own call-to-action or input while
   * the row also has a defined `onRowClick`.
   */
  shouldIgnoreRowClick?: boolean;
}

Signature onRowClick

(row: Row, rowIndex: number, columnKey: string) => void

Signature onColumnResized

(args: TColumn<Row>[]) => void

Signature itemRenderer

(item: Row, column: TColumn<Row>, isRowCollapsed: boolean) => ReactNode;

Signature onSortChange

(columnKey: string, sortDirection: 'asc' | 'desc') => void
16.5.0

10 months ago

16.9.0

6 months ago

16.7.3

8 months ago

16.7.2

8 months ago

16.7.1

8 months ago

16.7.0

8 months ago

16.7.5

7 months ago

16.7.4

8 months ago

16.10.0

6 months ago

16.6.1

8 months ago

16.6.0

9 months ago

16.4.1

10 months ago

16.8.0

6 months ago

16.11.0

5 months ago

16.4.0

10 months ago

16.3.0

11 months ago

16.2.1

11 months ago

16.2.0

11 months ago

16.1.1

12 months ago

16.1.0

12 months ago

15.15.0

1 year ago

15.15.1

1 year ago

16.0.0

1 year ago

15.13.2

1 year ago

15.14.3

1 year ago

15.14.1

1 year ago

15.14.2

1 year ago

15.14.0

1 year ago

15.13.1

1 year ago

15.13.0

1 year ago

15.12.0

1 year ago

15.9.0

1 year ago

15.10.0

1 year ago

15.11.2

1 year ago

15.11.0

1 year ago

15.11.1

1 year ago

15.7.0

1 year ago

15.8.0

1 year ago

15.5.0

1 year ago

15.5.1

1 year ago

15.6.0

1 year ago

15.4.0

1 year ago

15.3.0

2 years ago

15.2.4

2 years ago

15.2.1

2 years ago

15.2.2

2 years ago

15.2.3

2 years ago

15.1.1

2 years ago

15.1.2

2 years ago

15.1.0

2 years ago

15.2.0

2 years ago

15.0.0

2 years ago

14.0.2

2 years ago

14.0.3

2 years ago

14.0.6

2 years ago

13.0.4

2 years ago

14.0.0

2 years ago

14.0.1

2 years ago

13.0.2

2 years ago

13.0.0

2 years ago

13.0.1

2 years ago

12.2.8

2 years ago

12.2.9

2 years ago

12.2.5

2 years ago

12.2.6

2 years ago

12.2.7

2 years ago

12.2.4

3 years ago

12.2.3

3 years ago

12.2.2

3 years ago

12.2.1

3 years ago

12.2.0

3 years ago

12.1.0

3 years ago

12.0.12

3 years ago

12.0.8

3 years ago

12.0.7

3 years ago

12.0.3

3 years ago

12.0.4

3 years ago

12.0.2

3 years ago

12.0.0

3 years ago

11.3.0

3 years ago

11.2.1

3 years ago

11.2.0

3 years ago

11.0.2

3 years ago

11.0.1

3 years ago

10.47.4

3 years ago

10.47.3

3 years ago

10.47.0

3 years ago

10.46.3

3 years ago

10.46.2

3 years ago

10.46.1

3 years ago

10.45.0

3 years ago

10.44.4

3 years ago

10.44.2

3 years ago

10.44.1

3 years ago

10.44.0

3 years ago

10.43.3

3 years ago

10.43.2

3 years ago

10.43.1

3 years ago

10.42.3

3 years ago

10.42.2

3 years ago

10.42.0

3 years ago

10.42.1

3 years ago

10.41.0

3 years ago

10.40.0

3 years ago

10.40.1

3 years ago

10.39.8

3 years ago

10.39.7

3 years ago

10.39.6

3 years ago

10.39.4

3 years ago

10.39.3

3 years ago

10.39.2

3 years ago

10.39.1

4 years ago

10.38.0

4 years ago

10.37.2

4 years ago

10.36.0

4 years ago

10.35.1

4 years ago

10.33.0

4 years ago

10.32.0

4 years ago

10.31.0

4 years ago

10.30.6

4 years ago

10.30.5

4 years ago

10.30.3

4 years ago

10.30.4

4 years ago

10.30.2

4 years ago

10.30.1

4 years ago

10.30.0

4 years ago

10.29.1

4 years ago

10.29.0

4 years ago

10.28.0

4 years ago

10.27.1

4 years ago

10.27.0

4 years ago

10.24.0

4 years ago

10.23.0

4 years ago

10.22.2

4 years ago

10.22.1

4 years ago

10.22.0

4 years ago

10.21.0

4 years ago

10.20.0

4 years ago

10.19.1

4 years ago

10.18.7

4 years ago

10.18.7-canary.5

4 years ago

10.18.7-canary.6

4 years ago

10.18.7-canary.7

4 years ago

10.18.7-canary.4

4 years ago

10.18.7-canary.3

4 years ago

10.18.7-canary.2

4 years ago

10.18.7-canary.1

4 years ago

10.18.7-canary.0

4 years ago

10.18.6

4 years ago

10.18.6-canary.1

4 years ago

10.18.6-canary.0

4 years ago

10.18.5-canary.4

4 years ago

10.18.5

4 years ago

10.18.5-canary.3

4 years ago

10.18.5-canary.2

4 years ago

10.18.5-canary.1

4 years ago

10.18.5-canary.0

4 years ago

10.18.4

4 years ago

10.18.4-canary.9

4 years ago

10.18.4-canary.8

4 years ago

10.18.4-canary.7

4 years ago

10.18.4-canary.6

4 years ago

10.18.4-canary.3

4 years ago

10.18.4-canary.4

4 years ago

10.18.4-canary.5

4 years ago

10.18.4-canary.1

4 years ago

10.18.4-canary.2

4 years ago

10.18.4-canary.0

4 years ago

10.18.3

4 years ago

10.18.3-canary.2

4 years ago

10.18.3-canary.3

4 years ago

10.18.3-canary.1

4 years ago

10.18.3-canary.0

4 years ago

10.18.2-canary.3

4 years ago

10.18.2-canary.2

4 years ago

10.18.2-canary.1

4 years ago

10.17.1-canary.3

4 years ago

10.18.2-canary.0

4 years ago

10.17.1-canary.2

4 years ago

10.18.1-canary.0

4 years ago

10.18.0

4 years ago

10.17.1-canary.1

4 years ago

10.18.0-alpha.0

4 years ago

10.17.1-canary.0

4 years ago