Back to Woocommerce

TableCard

packages/js/components/src/table/README.md

10.8.0-dev10.0 KB
Original Source

TableCard

This is an accessible, sortable, and scrollable table for displaying tabular data (like revenue and other analytics data). It accepts headers for column headers, and rows for the table content. rowHeader can be used to define the index of the row header (or false if no header).

TableCard serves as Card wrapper & contains a card header, <Table />, <TableSummary />, and <Pagination />. This includes filtering and comparison functionality for report pages.

Usage

jsx
const headers = [
	{ key: 'month', label: 'Month' },
	{ key: 'orders', label: 'Orders' },
	{ key: 'revenue', label: 'Revenue' },
];
const rows = [
	[
		{ display: 'January', value: 1 },
		{ display: 10, value: 10 },
		{ display: '$530.00', value: 530 },
	],
	[
		{ display: 'February', value: 2 },
		{ display: 13, value: 13 },
		{ display: '$675.00', value: 675 },
	],
	[
		{ display: 'March', value: 3 },
		{ display: 9, value: 9 },
		{ display: '$460.00', value: 460 },
	],
];
const summary = [
	{ label: 'Gross Income', value: '$830.00' },
	{ label: 'Taxes', value: '$96.32' },
	{ label: 'Shipping', value: '$50.00' },
];

const [ showNotice, setShowNotice ] = useState( true );

<TableCard
	title="Revenue last week"
	rows={ rows }
	headers={ headers }
	query={ { page: 2 } }
	rowsPerPage={ 7 }
	totalRows={ 10 }
	summary={ summary }
	tablePreface={
    showNotice && (
      <Notice
        status="info"
        isDismissible={ true }
        onRemove={ () => setShowNotice( false ) }
      >
        This is an informative notice about the table.
      </Notice>
				)
  }
/>

Props

NameTypeDefaultDescription
compareByStringnullThe string to use as a query parameter when comparing row items
compareParamString'filter'Url query parameter compare function operates on
headersArraynullAn array of column headers (see Table props)
labelsObjectnullCustom labels for table header actions
idsArraynullA list of IDs, matching to the row list so that ids[ 0 ] contains the object ID for the object displayed in row[ 0 ]
isLoadingBooleanfalseDefines if the table contents are loading. It will display TablePlaceholder component instead of Table if that's the case
onQueryChangeFunctionnoopA function which returns a callback function to update the query string for a given param
onColumnsChangeFunctionnoopA function which returns a callback function which is called upon the user changing the visibility of columns
onSearchFunctionnoopA function which is called upon the user searching in the table header
onSortFunctionundefinedA function which is called upon the user changing the sorting of the table
downloadableBooleanfalseWhether the table must be downloadable. If true, the download button will appear
onClickDownloadFunctionnullA callback function called when the "download" button is pressed. Optional, if used, the download button will appear
queryObject{}An object of the query parameters passed to the page, ex { page: 2, per_page: 5 }
rowHeaderOne of type: number, bool0Which column should be the row header, defaults to the first item (0) (see Table props)
rowsArray[](required) An array of arrays of display/value object pairs (see Table props)
rowsPerPageNumbernull(required) The total number of rows to display per page
searchByStringnullThe string to use as a query parameter when searching row items
showMenuBooleantrueBoolean to determine whether or not ellipsis menu is shown
summaryArraynullAn array of objects with label & value properties, which display in a line under the table. Optional, can be left off to show no summary
titleStringnull(required) The title used in the card header, also used as the caption for the content in this table
totalRowsNumbernull(required) The total number of rows (across all pages)
baseSearchQueryObject{}Pass in query parameters to be included in the path when onSearch creates a new url
rowKeyFunction(row, index): stringnullFunction used for the row key.
tablePrefaceReactNodenullContent to be displayed before the table but after the header. Useful for notices or custom content.

labels structure

Table header action labels object with properties:

  • compareButton: String - Compare button label
  • downloadButton: String - Download button label
  • helpText: String -
  • placeholder: String -

summary structure

Array of summary items objects with properties:

  • label: ReactNode
  • value: One of type: string, number

EmptyTable

EmptyTable displays a blank space with an optional message passed as a children node with the purpose of replacing a table with no rows. It mimics the same height a table would have according to the numberOfRows prop.

Usage

jsx
<EmptyTable>
	There are no entries.
</EmptyTable>

Props

NameTypeDefaultDescription
numberOfRowsNumber5An integer with the number of rows the box should occupy

TablePlaceholder

TablePlaceholder behaves like Table but displays placeholder boxes instead of data. This can be used while loading.

Usage

jsx
const headers = [
	{ key: 'month', label: 'Month' },
	{ key: 'orders', label: 'Orders' },
	{ key: 'revenue', label: 'Revenue' },
];

<TablePlaceholder
	caption="Revenue last week"
	headers={ headers }
/>

Props

NameTypeDefaultDescription
queryObjectnullAn object of the query parameters passed to the page, ex { page: 2, per_page: 5 }
captionStringnull(required) A label for the content in this table
headersArraynullAn array of column headers (see Table props)
numberOfRowsNumber5An integer with the number of rows to display

TableSummary

A component to display summarized table data - the list of data passed in on a single line.

Usage

jsx
const summary = [
	{ label: 'Gross Income', value: '$830.00' },
	{ label: 'Taxes', value: '$96.32' },
	{ label: 'Shipping', value: '$50.00' },
];

<TableSummary data={ summary } />

Props

NameTypeDefaultDescription
dataArraynullAn array of objects with label & value properties, which display on a single line

TableSummaryPlaceholder

A component to display a placeholder box for TableSummary. There is no prop for this component.

Usage

jsx
<TableSummaryPlaceholder />

Table

A table component, without the Card wrapper. This is a basic table display, sortable, but no default filtering.

Row data should be passed to the component as a list of arrays, where each array is a row in the table. Headers are passed in separately as an array of objects with column-related properties. For example, this data would render the following table.

js
const headers = [ { label: 'Month' }, { label: 'Orders' }, { label: 'Revenue' } ];
const rows = [
	[
		{ display: 'January', value: 1 },
		{ display: 10, value: 10 },
		{ display: '$530.00', value: 530 },
	],
	[
		{ display: 'February', value: 2 },
		{ display: 13, value: 13 },
		{ display: '$675.00', value: 675 },
	],
	[
		{ display: 'March', value: 3 },
		{ display: 9, value: 9 },
		{ display: '$460.00', value: 460 },
	],
]
MonthOrdersRevenue
January10$530.00
February13$675.00
March9$460.00

Usage

jsx
<Table
	caption="Revenue last week"
	rows={ rows }
	headers={ headers }
	rowKey={ row => row.display }
/>

Props

NameTypeDefaultDescription
ariaHiddenBooleanfalseControls whether this component is hidden from screen readers. Used by the loading state, before there is data to read. Don't use this on real tables unless the table data is loaded elsewhere on the page
captionStringnull(required) A label for the content in this table
classNameStringnullAdditional CSS classes
headersArray[]An array of column headers, as objects
onSortFunctionnoopA function called when sortable table headers are clicked, gets the header.key as argument
queryObject{}The query string represented in object form
rowsArraynull(required) An array of arrays of display/value object pairs
rowHeaderOne of type: number, bool0Which column should be the row header, defaults to the first item (0) (but could be set to 1, if the first col is checkboxes, for example). Set to false to disable row headers
rowKeyFunction(row, index): stringnullFunction used to get the row key.
emptyMessageStringundefinedCustomize the message to show when there are no rows in the table.

headers structure

Array of column header objects with properties:

  • defaultSort: Boolean - Boolean, true if this column is the default for sorting. Only one column should have this set.
  • defaultOrder: String - String, asc|desc if this column is the default for sorting. Only one column should have this set.
  • isLeftAligned: Boolean - Boolean, true if this column should be aligned to the left. If not set, it will fallback to this value ! isNumeric, i.e. text fields are left-aligned and numeric fields are right-aligned.
  • isNumeric: Boolean - Boolean, true if this column is a number value.
  • isSortable: Boolean - Boolean, true if this column is sortable.
  • key: String - The API parameter name for this column, passed to orderby when sorting via API.
  • label: ReactNode - The display label for this column.
  • required: Boolean - Boolean, true if this column should always display in the table (not shown in toggle-able list).
  • screenReaderLabel: String - The label used for screen readers for this column.

rows structure

Array of arrays representing rows and columns. Column object properties:

  • display: ReactNode - Display value, used for rendering - strings or elements are best here.
  • value: One of type: string, number, bool