2018-08-01 16:00:45 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2018-08-06 17:01:41 +00:00
|
|
|
import { Component, createRef, Fragment } from '@wordpress/element';
|
2018-08-01 16:00:45 +00:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import { IconButton } from '@wordpress/components';
|
2018-09-18 14:12:13 +00:00
|
|
|
import { find, get, noop } from 'lodash';
|
2018-08-01 16:00:45 +00:00
|
|
|
import Gridicon from 'gridicons';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-09-18 14:12:13 +00:00
|
|
|
import { withInstanceId } from '@wordpress/compose';
|
2018-08-01 16:00:45 +00:00
|
|
|
|
2018-08-06 17:01:41 +00:00
|
|
|
const ASC = 'asc';
|
|
|
|
const DESC = 'desc';
|
2018-08-01 16:00:45 +00:00
|
|
|
|
|
|
|
const getDisplay = cell => cell.display || null;
|
|
|
|
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A table component, without the Card wrapper. This is a basic table display, sortable, but no default filtering.
|
2018-09-24 15:22:14 +00:00
|
|
|
*
|
|
|
|
* 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 },
|
|
|
|
* ],
|
|
|
|
* ]
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* | Month | Orders | Revenue |
|
|
|
|
* | ---------|--------|---------|
|
|
|
|
* | January | 10 | $530.00 |
|
|
|
|
* | February | 13 | $675.00 |
|
|
|
|
* | March | 9 | $460.00 |
|
2018-08-31 17:27:21 +00:00
|
|
|
*/
|
2018-08-01 16:00:45 +00:00
|
|
|
class Table extends Component {
|
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
|
|
|
this.state = {
|
|
|
|
tabIndex: null,
|
2018-10-17 21:56:09 +00:00
|
|
|
isScrollable: false,
|
2018-08-01 16:00:45 +00:00
|
|
|
};
|
|
|
|
this.container = createRef();
|
|
|
|
this.sortBy = this.sortBy.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { scrollWidth, clientWidth } = this.container.current;
|
|
|
|
const scrollable = scrollWidth > clientWidth;
|
|
|
|
/* eslint-disable react/no-did-mount-set-state */
|
|
|
|
this.setState( {
|
|
|
|
tabIndex: scrollable ? '0' : null,
|
|
|
|
} );
|
|
|
|
/* eslint-enable react/no-did-mount-set-state */
|
2018-10-17 21:56:09 +00:00
|
|
|
this.updateTableShadow();
|
|
|
|
window.addEventListener( 'resize', this.updateTableShadow );
|
|
|
|
}
|
|
|
|
|
2018-10-17 23:12:04 +00:00
|
|
|
componentWillUnmount() {
|
2018-10-17 21:56:09 +00:00
|
|
|
window.removeEventListener( 'resize', this.updateTableShadow );
|
2018-08-01 16:00:45 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 17:01:41 +00:00
|
|
|
sortBy( key ) {
|
|
|
|
const { headers, query } = this.props;
|
|
|
|
return () => {
|
|
|
|
const currentKey =
|
|
|
|
query.orderby || get( find( headers, { defaultSort: true } ), 'key', false );
|
|
|
|
const currentDir = query.order || DESC;
|
|
|
|
let dir = DESC;
|
|
|
|
if ( key === currentKey ) {
|
|
|
|
dir = DESC === currentDir ? ASC : DESC;
|
|
|
|
}
|
|
|
|
this.props.onSort( key, dir );
|
|
|
|
};
|
2018-08-01 16:00:45 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 21:56:09 +00:00
|
|
|
updateTableShadow = () => {
|
|
|
|
const table = this.container.current;
|
|
|
|
const scrolledToEnd = table.scrollWidth - table.scrollLeft <= table.offsetWidth;
|
|
|
|
this.setState( {
|
|
|
|
isScrollable: scrolledToEnd ? false : true,
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2018-08-01 16:00:45 +00:00
|
|
|
render() {
|
2018-09-18 14:12:13 +00:00
|
|
|
const {
|
|
|
|
ariaHidden,
|
|
|
|
caption,
|
|
|
|
classNames,
|
|
|
|
headers,
|
|
|
|
instanceId,
|
|
|
|
query,
|
|
|
|
rowHeader,
|
|
|
|
rows,
|
|
|
|
} = this.props;
|
2018-08-14 16:43:35 +00:00
|
|
|
const { tabIndex } = this.state;
|
2018-10-17 21:56:09 +00:00
|
|
|
const classes = classnames( 'woocommerce-table__table', classNames, {
|
|
|
|
'is-scrollable': this.state.isScrollable,
|
|
|
|
} );
|
2018-08-06 17:01:41 +00:00
|
|
|
const sortedBy = query.orderby || get( find( headers, { defaultSort: true } ), 'key', false );
|
|
|
|
const sortDir = query.order || DESC;
|
2018-08-01 16:00:45 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={ classes }
|
|
|
|
ref={ this.container }
|
|
|
|
tabIndex={ tabIndex }
|
2018-08-31 14:43:25 +00:00
|
|
|
aria-hidden={ ariaHidden }
|
2018-09-18 14:12:13 +00:00
|
|
|
aria-labelledby={ `caption-${ instanceId }` }
|
2018-08-01 16:00:45 +00:00
|
|
|
role="group"
|
2018-10-17 21:56:09 +00:00
|
|
|
onScroll={ this.updateTableShadow }
|
2018-08-01 16:00:45 +00:00
|
|
|
>
|
|
|
|
<table>
|
2018-09-18 14:12:13 +00:00
|
|
|
<caption
|
|
|
|
id={ `caption-${ instanceId }` }
|
|
|
|
className="woocommerce-table__caption screen-reader-text"
|
|
|
|
>
|
2018-08-01 16:00:45 +00:00
|
|
|
{ caption }
|
|
|
|
{ tabIndex === '0' && <small>{ __( '(scroll to see more)', 'wc-admin' ) }</small> }
|
|
|
|
</caption>
|
|
|
|
<tbody>
|
|
|
|
<tr>
|
|
|
|
{ headers.map( ( header, i ) => {
|
2018-10-18 18:24:31 +00:00
|
|
|
const { cellClassName, isLeftAligned, isSortable, isNumeric, key, label } = header;
|
2018-09-18 14:12:13 +00:00
|
|
|
const labelId = `header-${ instanceId } -${ i }`;
|
2018-08-06 17:01:41 +00:00
|
|
|
const thProps = {
|
2018-10-18 18:24:31 +00:00
|
|
|
className: classnames( 'woocommerce-table__header', cellClassName, {
|
2018-10-11 08:30:51 +00:00
|
|
|
'is-left-aligned': isLeftAligned,
|
2018-08-06 17:01:41 +00:00
|
|
|
'is-sortable': isSortable,
|
|
|
|
'is-sorted': sortedBy === key,
|
2018-08-13 15:25:32 +00:00
|
|
|
'is-numeric': isNumeric,
|
2018-08-06 17:01:41 +00:00
|
|
|
} ),
|
|
|
|
};
|
|
|
|
if ( isSortable ) {
|
|
|
|
thProps[ 'aria-sort' ] = 'none';
|
|
|
|
if ( sortedBy === key ) {
|
|
|
|
thProps[ 'aria-sort' ] = sortDir === ASC ? 'ascending' : 'descending';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We only sort by ascending if the col is already sorted descending
|
|
|
|
const iconLabel =
|
|
|
|
sortedBy === key && sortDir !== ASC
|
|
|
|
? sprintf( __( 'Sort by %s in ascending order', 'wc-admin' ), label )
|
|
|
|
: sprintf( __( 'Sort by %s in descending order', 'wc-admin' ), label );
|
|
|
|
|
2018-08-01 16:00:45 +00:00
|
|
|
return (
|
2018-08-06 17:01:41 +00:00
|
|
|
<th role="columnheader" scope="col" key={ i } { ...thProps }>
|
2018-08-01 16:00:45 +00:00
|
|
|
{ isSortable ? (
|
2018-08-06 17:01:41 +00:00
|
|
|
<Fragment>
|
|
|
|
<IconButton
|
|
|
|
icon={
|
|
|
|
sortedBy === key && sortDir === ASC ? (
|
|
|
|
<Gridicon size={ 18 } icon="chevron-up" />
|
|
|
|
) : (
|
|
|
|
<Gridicon size={ 18 } icon="chevron-down" />
|
|
|
|
)
|
|
|
|
}
|
2018-09-18 14:12:13 +00:00
|
|
|
aria-describedby={ labelId }
|
2018-08-06 17:01:41 +00:00
|
|
|
onClick={ this.sortBy( key ) }
|
|
|
|
isDefault
|
|
|
|
>
|
|
|
|
{ label }
|
|
|
|
</IconButton>
|
2018-09-18 14:12:13 +00:00
|
|
|
<span className="screen-reader-text" id={ labelId }>
|
2018-08-06 17:01:41 +00:00
|
|
|
{ iconLabel }
|
|
|
|
</span>
|
|
|
|
</Fragment>
|
2018-08-01 16:00:45 +00:00
|
|
|
) : (
|
2018-08-06 17:01:41 +00:00
|
|
|
label
|
2018-08-01 16:00:45 +00:00
|
|
|
) }
|
|
|
|
</th>
|
|
|
|
);
|
|
|
|
} ) }
|
|
|
|
</tr>
|
|
|
|
{ rows.map( ( row, i ) => (
|
|
|
|
<tr key={ i }>
|
2018-08-13 15:25:32 +00:00
|
|
|
{ row.map( ( cell, j ) => {
|
2018-10-18 18:24:31 +00:00
|
|
|
const { cellClassName, isLeftAligned, isNumeric } = headers[ j ];
|
2018-08-16 14:37:10 +00:00
|
|
|
const isHeader = rowHeader === j;
|
|
|
|
const Cell = isHeader ? 'th' : 'td';
|
2018-10-18 18:24:31 +00:00
|
|
|
const cellClasses = classnames( 'woocommerce-table__item', cellClassName, {
|
2018-10-11 08:30:51 +00:00
|
|
|
'is-left-aligned': isLeftAligned,
|
2018-08-13 15:25:32 +00:00
|
|
|
'is-numeric': isNumeric,
|
2018-10-18 18:45:06 +00:00
|
|
|
'is-sorted': sortedBy === headers[ j ].key,
|
2018-08-13 15:25:32 +00:00
|
|
|
} );
|
|
|
|
return (
|
2018-08-16 14:37:10 +00:00
|
|
|
<Cell scope={ isHeader ? 'row' : null } key={ j } className={ cellClasses }>
|
2018-08-13 15:25:32 +00:00
|
|
|
{ getDisplay( cell ) }
|
|
|
|
</Cell>
|
|
|
|
);
|
|
|
|
} ) }
|
2018-08-01 16:00:45 +00:00
|
|
|
</tr>
|
|
|
|
) ) }
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Table.propTypes = {
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Controls 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.
|
|
|
|
*/
|
2018-08-31 14:43:25 +00:00
|
|
|
ariaHidden: PropTypes.bool,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A label for the content in this table
|
|
|
|
*/
|
2018-08-01 16:00:45 +00:00
|
|
|
caption: PropTypes.string.isRequired,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Additional CSS classes.
|
|
|
|
*/
|
2018-08-01 16:00:45 +00:00
|
|
|
className: PropTypes.string,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* An array of column headers, as objects.
|
|
|
|
*/
|
2018-08-06 17:01:41 +00:00
|
|
|
headers: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape( {
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Boolean, true if this column is the default for sorting. Only one column should have this set.
|
|
|
|
*/
|
2018-08-06 17:01:41 +00:00
|
|
|
defaultSort: PropTypes.bool,
|
2018-10-11 08:30:51 +00:00
|
|
|
/**
|
|
|
|
* Boolean, true if this column should be aligned to the left.
|
|
|
|
*/
|
|
|
|
isLeftAligned: PropTypes.bool,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Boolean, true if this column is a number value.
|
|
|
|
*/
|
|
|
|
isNumeric: PropTypes.bool,
|
|
|
|
/**
|
|
|
|
* Boolean, true if this column is sortable.
|
|
|
|
*/
|
2018-08-06 17:01:41 +00:00
|
|
|
isSortable: PropTypes.bool,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* The API parameter name for this column, passed to `orderby` when sorting via API.
|
|
|
|
*/
|
2018-08-06 17:01:41 +00:00
|
|
|
key: PropTypes.string,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* The display label for this column.
|
|
|
|
*/
|
2018-09-18 14:12:13 +00:00
|
|
|
label: PropTypes.node,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Boolean, true if this column should always display in the table (not shown in toggle-able list).
|
|
|
|
*/
|
2018-08-06 17:01:41 +00:00
|
|
|
required: PropTypes.bool,
|
|
|
|
} )
|
|
|
|
),
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A function called when sortable table headers are clicked, gets the `header.key` as argument.
|
|
|
|
*/
|
2018-08-06 17:01:41 +00:00
|
|
|
onSort: PropTypes.func,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* The query string represented in object form
|
|
|
|
*/
|
2018-08-06 17:01:41 +00:00
|
|
|
query: PropTypes.object,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* An array of arrays of display/value object pairs.
|
|
|
|
*/
|
2018-08-01 16:00:45 +00:00
|
|
|
rows: PropTypes.arrayOf(
|
|
|
|
PropTypes.arrayOf(
|
|
|
|
PropTypes.shape( {
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Display value, used for rendering- strings or elements are best here.
|
|
|
|
*/
|
2018-08-01 16:00:45 +00:00
|
|
|
display: PropTypes.node,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* "Real" value used for sorting, and should be a string or number. A column with `false` value will not be sortable.
|
|
|
|
*/
|
2018-08-01 16:00:45 +00:00
|
|
|
value: PropTypes.oneOfType( [ PropTypes.string, PropTypes.number, PropTypes.bool ] ),
|
|
|
|
} )
|
|
|
|
)
|
|
|
|
).isRequired,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Which 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.
|
|
|
|
*/
|
2018-08-01 16:00:45 +00:00
|
|
|
rowHeader: PropTypes.oneOfType( [ PropTypes.number, PropTypes.bool ] ),
|
|
|
|
};
|
|
|
|
|
|
|
|
Table.defaultProps = {
|
2018-08-31 14:43:25 +00:00
|
|
|
ariaHidden: false,
|
2018-08-01 16:00:45 +00:00
|
|
|
headers: [],
|
2018-08-06 17:01:41 +00:00
|
|
|
onSort: noop,
|
|
|
|
query: {},
|
2018-08-01 16:00:45 +00:00
|
|
|
rowHeader: 0,
|
|
|
|
};
|
|
|
|
|
2018-09-18 14:12:13 +00:00
|
|
|
export default withInstanceId( Table );
|