2018-06-20 15:09:37 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-08-01 16:00:45 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2018-09-18 14:12:13 +00:00
|
|
|
import classnames from 'classnames';
|
2018-08-02 22:21:37 +00:00
|
|
|
import { Component } from '@wordpress/element';
|
2018-10-24 07:50:05 +00:00
|
|
|
import { find, findIndex, first, isEqual, noop, partial, uniq } from 'lodash';
|
2018-09-19 17:16:52 +00:00
|
|
|
import { IconButton, ToggleControl } from '@wordpress/components';
|
2018-06-20 15:09:37 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2018-08-01 16:00:45 +00:00
|
|
|
import Card from 'components/card';
|
2018-09-19 17:16:52 +00:00
|
|
|
import CompareButton from 'components/filters/compare/button';
|
2018-10-17 23:01:57 +00:00
|
|
|
import DowloadIcon from './download-icon';
|
2018-08-31 17:19:13 +00:00
|
|
|
import EllipsisMenu from 'components/ellipsis-menu';
|
2018-10-24 07:48:20 +00:00
|
|
|
import { downloadCSVFile, generateCSVDataFromTable, generateCSVFileName } from 'lib/csv';
|
2018-09-18 14:12:13 +00:00
|
|
|
import { getIdsFromQuery } from 'lib/nav-utils';
|
2018-08-31 17:19:13 +00:00
|
|
|
import MenuItem from 'components/ellipsis-menu/menu-item';
|
|
|
|
import MenuTitle from 'components/ellipsis-menu/menu-title';
|
2018-08-01 16:00:45 +00:00
|
|
|
import Pagination from 'components/pagination';
|
2018-10-10 14:12:00 +00:00
|
|
|
import Search from 'components/search';
|
2018-08-01 16:00:45 +00:00
|
|
|
import Table from './table';
|
|
|
|
import TableSummary from './summary';
|
2018-06-20 15:09:37 +00:00
|
|
|
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-08-02 22:21:37 +00:00
|
|
|
class TableCard extends Component {
|
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
2018-09-18 14:12:13 +00:00
|
|
|
const { compareBy, query } = props;
|
2018-08-02 22:21:37 +00:00
|
|
|
this.state = {
|
2018-10-24 07:50:05 +00:00
|
|
|
showCols: props.headers.map( ( { hiddenByDefault } ) => ! hiddenByDefault ),
|
2018-09-18 14:12:13 +00:00
|
|
|
selectedRows: getIdsFromQuery( query[ compareBy ] ),
|
2018-08-02 22:21:37 +00:00
|
|
|
};
|
|
|
|
this.toggleCols = this.toggleCols.bind( this );
|
2018-10-24 07:48:20 +00:00
|
|
|
this.onClickDownload = this.onClickDownload.bind( this );
|
2018-09-18 14:12:13 +00:00
|
|
|
this.onCompare = this.onCompare.bind( this );
|
2018-10-10 14:12:00 +00:00
|
|
|
this.onSearch = this.onSearch.bind( this );
|
2018-09-18 14:12:13 +00:00
|
|
|
this.selectRow = this.selectRow.bind( this );
|
|
|
|
this.selectAllRows = this.selectAllRows.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate( { query: prevQuery } ) {
|
|
|
|
const { compareBy, query } = this.props;
|
|
|
|
const prevIds = getIdsFromQuery( prevQuery[ compareBy ] );
|
|
|
|
const currentIds = getIdsFromQuery( query[ compareBy ] );
|
|
|
|
if ( ! isEqual( prevIds.sort(), currentIds.sort() ) ) {
|
|
|
|
/* eslint-disable react/no-did-update-set-state */
|
|
|
|
this.setState( { selectedRows: currentIds } );
|
|
|
|
/* eslint-enable react/no-did-update-set-state */
|
|
|
|
}
|
2018-08-02 22:21:37 +00:00
|
|
|
}
|
2018-06-20 15:09:37 +00:00
|
|
|
|
2018-08-06 17:01:41 +00:00
|
|
|
toggleCols( selected ) {
|
|
|
|
const { headers, query, onQueryChange } = this.props;
|
2018-08-02 22:21:37 +00:00
|
|
|
return () => {
|
2018-08-06 17:01:41 +00:00
|
|
|
// Handle hiding a sorted column
|
|
|
|
if ( query.orderby ) {
|
|
|
|
const sortBy = findIndex( headers, { key: query.orderby } );
|
|
|
|
if ( sortBy === selected ) {
|
|
|
|
const defaultSort = find( headers, { defaultSort: true } ) || first( headers ) || {};
|
|
|
|
onQueryChange( 'sort' )( defaultSort.key, 'desc' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 22:21:37 +00:00
|
|
|
this.setState( prevState => ( {
|
2018-08-06 17:01:41 +00:00
|
|
|
showCols: prevState.showCols.map(
|
|
|
|
( toggled, i ) => ( selected === i ? ! toggled : toggled )
|
|
|
|
),
|
2018-08-02 22:21:37 +00:00
|
|
|
} ) );
|
|
|
|
};
|
|
|
|
}
|
2018-06-20 15:09:37 +00:00
|
|
|
|
2018-10-24 07:48:20 +00:00
|
|
|
onClickDownload() {
|
|
|
|
const { headers, query, onClickDownload, rows, title } = this.props;
|
|
|
|
|
|
|
|
// @TODO The current implementation only downloads the contents displayed in the table.
|
|
|
|
// Another solution is required when the data set is larger (see #311).
|
|
|
|
downloadCSVFile(
|
|
|
|
generateCSVFileName( title, query ),
|
|
|
|
generateCSVDataFromTable( headers, rows )
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( onClickDownload ) {
|
|
|
|
onClickDownload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 14:12:13 +00:00
|
|
|
onCompare() {
|
|
|
|
const { compareBy, onQueryChange } = this.props;
|
|
|
|
const { selectedRows } = this.state;
|
|
|
|
if ( compareBy ) {
|
|
|
|
onQueryChange( 'compare' )( compareBy, selectedRows.join( ',' ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 14:12:00 +00:00
|
|
|
onSearch( value ) {
|
|
|
|
const { compareBy, onQueryChange } = this.props;
|
|
|
|
const { selectedRows } = this.state;
|
|
|
|
if ( compareBy ) {
|
|
|
|
const ids = value.map( v => v.id );
|
|
|
|
onQueryChange( 'compare' )( compareBy, [ ...selectedRows, ...ids ].join( ',' ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 22:21:37 +00:00
|
|
|
filterCols( rows = [] ) {
|
|
|
|
const { showCols } = this.state;
|
|
|
|
// Header is a 1d array
|
2018-09-03 15:09:09 +00:00
|
|
|
if ( ! Array.isArray( first( rows ) ) ) {
|
2018-08-02 22:21:37 +00:00
|
|
|
return rows.filter( ( col, i ) => showCols[ i ] );
|
|
|
|
}
|
|
|
|
// Rows is a 2d array
|
|
|
|
return rows.map( row => row.filter( ( col, i ) => showCols[ i ] ) );
|
|
|
|
}
|
2018-07-13 19:36:41 +00:00
|
|
|
|
2018-09-18 14:12:13 +00:00
|
|
|
selectAllRows( event ) {
|
|
|
|
const { ids } = this.props;
|
|
|
|
if ( event.target.checked ) {
|
|
|
|
this.setState( {
|
|
|
|
selectedRows: ids,
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
this.setState( {
|
|
|
|
selectedRows: [],
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
selectRow( i, event ) {
|
|
|
|
const { ids } = this.props;
|
|
|
|
if ( event.target.checked ) {
|
|
|
|
this.setState( ( { selectedRows } ) => ( {
|
|
|
|
selectedRows: uniq( [ ids[ i ], ...selectedRows ] ),
|
|
|
|
} ) );
|
|
|
|
} else {
|
|
|
|
this.setState( ( { selectedRows } ) => {
|
|
|
|
const index = selectedRows.indexOf( ids[ i ] );
|
|
|
|
return {
|
|
|
|
selectedRows: [ ...selectedRows.slice( 0, index ), ...selectedRows.slice( index + 1 ) ],
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getCheckbox( i ) {
|
|
|
|
const { ids = [] } = this.props;
|
|
|
|
const { selectedRows } = this.state;
|
|
|
|
const isChecked = -1 !== selectedRows.indexOf( ids[ i ] );
|
|
|
|
return {
|
|
|
|
display: (
|
|
|
|
<input type="checkbox" onChange={ partial( this.selectRow, i ) } checked={ isChecked } />
|
|
|
|
),
|
|
|
|
value: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getAllCheckbox() {
|
|
|
|
const { ids = [] } = this.props;
|
|
|
|
const { selectedRows } = this.state;
|
|
|
|
const isAllChecked = ids.length === selectedRows.length;
|
|
|
|
return {
|
2018-10-18 18:24:31 +00:00
|
|
|
cellClassName: 'is-checkbox-column',
|
2018-09-18 14:12:13 +00:00
|
|
|
label: (
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
onChange={ this.selectAllRows }
|
|
|
|
aria-label={ __( 'Select All' ) }
|
|
|
|
checked={ isAllChecked }
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
required: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-02 22:21:37 +00:00
|
|
|
render() {
|
2018-09-03 15:25:38 +00:00
|
|
|
const {
|
2018-09-18 14:12:13 +00:00
|
|
|
compareBy,
|
2018-10-24 07:48:20 +00:00
|
|
|
downloadable,
|
2018-10-10 14:59:22 +00:00
|
|
|
labels = {},
|
2018-09-03 15:25:38 +00:00
|
|
|
onClickDownload,
|
|
|
|
onQueryChange,
|
|
|
|
query,
|
|
|
|
rowHeader,
|
2018-09-18 14:12:13 +00:00
|
|
|
rowsPerPage,
|
2018-09-03 15:25:38 +00:00
|
|
|
summary,
|
|
|
|
title,
|
|
|
|
totalRows,
|
|
|
|
} = this.props;
|
2018-09-19 17:16:52 +00:00
|
|
|
const { selectedRows, showCols } = this.state;
|
2018-08-02 22:21:37 +00:00
|
|
|
const allHeaders = this.props.headers;
|
2018-09-18 14:12:13 +00:00
|
|
|
let headers = this.filterCols( this.props.headers );
|
|
|
|
let rows = this.filterCols( this.props.rows );
|
|
|
|
if ( compareBy ) {
|
|
|
|
rows = rows.map( ( row, i ) => {
|
|
|
|
return [ this.getCheckbox( i ), ...row ];
|
|
|
|
} );
|
|
|
|
headers = [ this.getAllCheckbox(), ...headers ];
|
|
|
|
}
|
|
|
|
|
|
|
|
const className = classnames( {
|
|
|
|
'woocommerce-table': true,
|
|
|
|
'has-compare': !! compareBy,
|
|
|
|
} );
|
2018-08-02 22:21:37 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Card
|
2018-09-18 14:12:13 +00:00
|
|
|
className={ className }
|
2018-08-02 22:21:37 +00:00
|
|
|
title={ title }
|
2018-09-18 14:12:13 +00:00
|
|
|
action={ [
|
|
|
|
compareBy && (
|
2018-09-19 17:16:52 +00:00
|
|
|
<CompareButton
|
|
|
|
key="compare"
|
|
|
|
count={ selectedRows.length }
|
2018-10-10 14:12:00 +00:00
|
|
|
helpText={
|
|
|
|
labels.helpText || __( 'Select at least two items to compare', 'wc-admin' )
|
|
|
|
}
|
2018-09-19 17:16:52 +00:00
|
|
|
onClick={ this.onCompare }
|
|
|
|
>
|
2018-10-10 14:12:00 +00:00
|
|
|
{ labels.compareButton || __( 'Compare', 'wc-admin' ) }
|
2018-09-19 17:16:52 +00:00
|
|
|
</CompareButton>
|
2018-09-18 14:12:13 +00:00
|
|
|
),
|
|
|
|
compareBy && (
|
2018-10-10 14:12:00 +00:00
|
|
|
<Search
|
|
|
|
key="search"
|
|
|
|
placeholder={ labels.placeholder || __( 'Search by item name', 'wc-admin' ) }
|
|
|
|
type={ compareBy + 's' }
|
|
|
|
onChange={ this.onSearch }
|
|
|
|
/>
|
2018-09-18 14:12:13 +00:00
|
|
|
),
|
2018-10-24 07:48:20 +00:00
|
|
|
( downloadable || onClickDownload ) && (
|
2018-09-18 14:12:13 +00:00
|
|
|
<IconButton
|
|
|
|
key="download"
|
2018-10-10 14:12:00 +00:00
|
|
|
className="woocommerce-table__download-button"
|
2018-10-24 07:48:20 +00:00
|
|
|
onClick={ this.onClickDownload }
|
2018-10-17 23:01:57 +00:00
|
|
|
isLink
|
2018-09-18 14:12:13 +00:00
|
|
|
>
|
2018-10-17 23:01:57 +00:00
|
|
|
<DowloadIcon />
|
2018-10-10 14:12:00 +00:00
|
|
|
{ labels.downloadButton || __( 'Download', 'wc-admin' ) }
|
2018-08-02 22:21:37 +00:00
|
|
|
</IconButton>
|
2018-09-18 14:12:13 +00:00
|
|
|
),
|
|
|
|
] }
|
2018-08-02 22:21:37 +00:00
|
|
|
menu={
|
|
|
|
<EllipsisMenu label={ __( 'Choose which values to display', 'wc-admin' ) }>
|
|
|
|
<MenuTitle>{ __( 'Columns:', 'wc-admin' ) }</MenuTitle>
|
2018-08-06 17:01:41 +00:00
|
|
|
{ allHeaders.map( ( { label, required }, i ) => {
|
|
|
|
if ( required ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<MenuItem key={ i } onInvoke={ this.toggleCols( i ) }>
|
|
|
|
<ToggleControl
|
|
|
|
label={ label }
|
|
|
|
checked={ !! showCols[ i ] }
|
|
|
|
onChange={ this.toggleCols( i ) }
|
|
|
|
/>
|
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
} ) }
|
2018-08-02 22:21:37 +00:00
|
|
|
</EllipsisMenu>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Table
|
|
|
|
rows={ rows }
|
|
|
|
headers={ headers }
|
|
|
|
rowHeader={ rowHeader }
|
|
|
|
caption={ title }
|
2018-08-06 17:01:41 +00:00
|
|
|
query={ query }
|
|
|
|
onSort={ onQueryChange( 'sort' ) }
|
2018-08-02 22:21:37 +00:00
|
|
|
/>
|
|
|
|
|
|
|
|
{ summary && <TableSummary data={ summary } /> }
|
|
|
|
|
|
|
|
<Pagination
|
|
|
|
page={ parseInt( query.page ) || 1 }
|
2018-09-03 15:25:38 +00:00
|
|
|
perPage={ rowsPerPage }
|
|
|
|
total={ totalRows }
|
2018-08-02 22:21:37 +00:00
|
|
|
onPageChange={ onQueryChange( 'page' ) }
|
|
|
|
onPerPageChange={ onQueryChange( 'per_page' ) }
|
|
|
|
/>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-06-20 15:09:37 +00:00
|
|
|
|
2018-08-01 16:00:45 +00:00
|
|
|
TableCard.propTypes = {
|
2018-09-18 14:12:13 +00:00
|
|
|
/**
|
|
|
|
* The string to use as a query parameter when comparing row items.
|
|
|
|
*/
|
|
|
|
compareBy: PropTypes.string,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* An array of column headers (see `Table` props).
|
|
|
|
*/
|
2018-08-06 17:01:41 +00:00
|
|
|
headers: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape( {
|
2018-10-24 07:50:05 +00:00
|
|
|
hiddenByDefault: PropTypes.bool,
|
2018-08-06 17:01:41 +00:00
|
|
|
defaultSort: PropTypes.bool,
|
|
|
|
isSortable: PropTypes.bool,
|
|
|
|
key: PropTypes.string,
|
|
|
|
label: PropTypes.string,
|
|
|
|
required: PropTypes.bool,
|
|
|
|
} )
|
|
|
|
),
|
2018-10-10 14:12:00 +00:00
|
|
|
/**
|
|
|
|
* Custom labels for table header actions.
|
|
|
|
*/
|
|
|
|
labels: PropTypes.shape( {
|
|
|
|
compareButton: PropTypes.string,
|
|
|
|
downloadButton: PropTypes.string,
|
|
|
|
helpText: PropTypes.string,
|
|
|
|
placeholder: PropTypes.string,
|
|
|
|
} ),
|
2018-09-18 14:12:13 +00:00
|
|
|
/**
|
|
|
|
* A list of IDs, matching to the row list so that ids[ 0 ] contains the object ID for the object displayed in row[ 0 ].
|
|
|
|
*/
|
|
|
|
ids: PropTypes.arrayOf( PropTypes.number ),
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A function which returns a callback function to update the query string for a given `param`.
|
|
|
|
*/
|
2018-08-01 16:00:45 +00:00
|
|
|
onQueryChange: PropTypes.func,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
2018-10-24 07:48:20 +00:00
|
|
|
* Whether the table must be downloadable. If true, the download button will appear.
|
|
|
|
*/
|
|
|
|
downloadable: PropTypes.bool,
|
|
|
|
/**
|
|
|
|
* A callback function called when the "download" button is pressed. Optional, if used, the download button will appear.
|
2018-08-31 17:27:21 +00:00
|
|
|
*/
|
2018-08-01 16:00:45 +00:00
|
|
|
onClickDownload: PropTypes.func,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* An object of the query parameters passed to the page, ex `{ page: 2, per_page: 5 }`.
|
|
|
|
*/
|
2018-08-01 16:00:45 +00:00
|
|
|
query: PropTypes.object,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* An array of arrays of display/value object pairs (see `Table` props).
|
|
|
|
*/
|
2018-06-20 15:09:37 +00:00
|
|
|
rowHeader: PropTypes.oneOfType( [ PropTypes.number, PropTypes.bool ] ),
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* Which column should be the row header, defaults to the first item (`0`) (see `Table` props).
|
|
|
|
*/
|
2018-08-01 16:00:45 +00:00
|
|
|
rows: PropTypes.arrayOf(
|
|
|
|
PropTypes.arrayOf(
|
|
|
|
PropTypes.shape( {
|
|
|
|
display: PropTypes.node,
|
|
|
|
value: PropTypes.oneOfType( [ PropTypes.string, PropTypes.number, PropTypes.bool ] ),
|
|
|
|
} )
|
|
|
|
)
|
|
|
|
).isRequired,
|
2018-09-18 14:12:13 +00:00
|
|
|
/**
|
|
|
|
* The total number of rows to display per page.
|
|
|
|
*/
|
|
|
|
rowsPerPage: PropTypes.number.isRequired,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* An array of objects with `label` & `value` properties, which display in a line under the table.
|
|
|
|
* Optional, can be left off to show no summary.
|
|
|
|
*/
|
2018-08-01 16:00:45 +00:00
|
|
|
summary: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape( {
|
|
|
|
label: PropTypes.node,
|
|
|
|
value: PropTypes.oneOfType( [ PropTypes.string, PropTypes.number ] ),
|
|
|
|
} )
|
|
|
|
),
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* The title used in the card header, also used as the caption for the content in this table.
|
|
|
|
*/
|
2018-08-01 16:00:45 +00:00
|
|
|
title: PropTypes.string.isRequired,
|
2018-09-18 14:12:13 +00:00
|
|
|
/**
|
|
|
|
* The total number of rows (across all pages).
|
|
|
|
*/
|
2018-09-03 15:25:38 +00:00
|
|
|
totalRows: PropTypes.number.isRequired,
|
2018-06-20 15:09:37 +00:00
|
|
|
};
|
|
|
|
|
2018-08-01 16:00:45 +00:00
|
|
|
TableCard.defaultProps = {
|
2018-10-24 07:48:20 +00:00
|
|
|
downloadable: false,
|
2018-08-01 16:00:45 +00:00
|
|
|
onQueryChange: noop,
|
|
|
|
query: {},
|
2018-06-20 15:09:37 +00:00
|
|
|
rowHeader: 0,
|
2018-08-01 16:00:45 +00:00
|
|
|
rows: [],
|
2018-06-20 15:09:37 +00:00
|
|
|
};
|
|
|
|
|
2018-08-31 17:19:13 +00:00
|
|
|
export default TableCard;
|