2018-06-20 15:09:37 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-08-01 16:00:45 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2018-08-02 22:21:37 +00:00
|
|
|
import { Component } from '@wordpress/element';
|
2018-08-01 16:00:45 +00:00
|
|
|
import { IconButton, ToggleControl } from '@wordpress/components';
|
2018-08-06 17:01:41 +00:00
|
|
|
import { fill, find, findIndex, first, isArray, noop } from 'lodash';
|
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';
|
|
|
|
import { EllipsisMenu, MenuItem, MenuTitle } from 'components/ellipsis-menu';
|
|
|
|
import Pagination from 'components/pagination';
|
|
|
|
import Table from './table';
|
|
|
|
import TableSummary from './summary';
|
2018-06-20 15:09:37 +00:00
|
|
|
|
2018-08-02 22:21:37 +00:00
|
|
|
class TableCard extends Component {
|
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
|
|
|
this.state = {
|
|
|
|
showCols: fill( Array( props.headers.length ), true ),
|
|
|
|
};
|
|
|
|
this.toggleCols = this.toggleCols.bind( this );
|
|
|
|
}
|
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-08-02 22:21:37 +00:00
|
|
|
filterCols( rows = [] ) {
|
|
|
|
const { showCols } = this.state;
|
|
|
|
// Header is a 1d array
|
|
|
|
if ( ! isArray( first( rows ) ) ) {
|
|
|
|
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-08-02 22:21:37 +00:00
|
|
|
render() {
|
|
|
|
const { onClickDownload, onQueryChange, query, rowHeader, summary, title } = this.props;
|
|
|
|
const { showCols } = this.state;
|
|
|
|
const allHeaders = this.props.headers;
|
|
|
|
const headers = this.filterCols( this.props.headers );
|
|
|
|
const rows = this.filterCols( this.props.rows );
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Card
|
|
|
|
className="woocommerce-table"
|
|
|
|
title={ title }
|
|
|
|
action={
|
|
|
|
onClickDownload && (
|
|
|
|
<IconButton onClick={ onClickDownload } icon="arrow-down" size={ 18 } isDefault>
|
|
|
|
{ __( 'Download', 'wc-admin' ) }
|
|
|
|
</IconButton>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
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>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{ /* @todo Switch a placeholder view if we don't have rows */ }
|
|
|
|
<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 }
|
|
|
|
perPage={ parseInt( query.per_page ) || 25 }
|
|
|
|
total={ 5000 }
|
|
|
|
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-08-06 17:01:41 +00:00
|
|
|
headers: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape( {
|
|
|
|
defaultSort: PropTypes.bool,
|
|
|
|
isSortable: PropTypes.bool,
|
|
|
|
key: PropTypes.string,
|
|
|
|
label: PropTypes.string,
|
|
|
|
required: PropTypes.bool,
|
|
|
|
} )
|
|
|
|
),
|
2018-08-01 16:00:45 +00:00
|
|
|
onQueryChange: PropTypes.func,
|
|
|
|
onClickDownload: PropTypes.func,
|
|
|
|
query: PropTypes.object,
|
2018-06-20 15:09:37 +00:00
|
|
|
rowHeader: PropTypes.oneOfType( [ PropTypes.number, PropTypes.bool ] ),
|
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,
|
|
|
|
summary: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape( {
|
|
|
|
label: PropTypes.node,
|
|
|
|
value: PropTypes.oneOfType( [ PropTypes.string, PropTypes.number ] ),
|
|
|
|
} )
|
|
|
|
),
|
|
|
|
title: PropTypes.string.isRequired,
|
2018-06-20 15:09:37 +00:00
|
|
|
};
|
|
|
|
|
2018-08-01 16:00:45 +00:00
|
|
|
TableCard.defaultProps = {
|
|
|
|
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-01 16:00:45 +00:00
|
|
|
export { TableCard, Table, TableSummary };
|
2018-08-31 14:43:25 +00:00
|
|
|
export { default as TablePlaceholder } from './placeholder';
|