Skip to content

Commit

Permalink
[DataTable] Added ability to control location of Totals row (#2200)
Browse files Browse the repository at this point in the history
* Adjustments [DEV]

* Introduced new logic for creating the footer markup

* Refactored the rendering logic for header vs footer totals

* Edited placement

* Addded entry to unreleased

* Update UNRELEASED.md

Co-Authored-By: Dan Rosenthal <[email protected]>

* Renamed prop

* Changed prop name to be more action oriented

* Updated unreleased

* added test for getting codecov >90%

* Renamed test for the new prop

* renamed prop to the final showTotalsInFooter

* Edited the markup to use a <tfoot> for the footer totals

* Edited rendering logic for footer totals

* Added example for the new totals prop in the readme

* Added correct top border for a totals row in the footer
  • Loading branch information
mitchmaps authored Oct 7, 2019
1 parent aed5728 commit befe767
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 2 deletions.
2 changes: 2 additions & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f

### Enhancements

- Added showTotalsFooter prop to `DataTable` for control over placement of Totals row ([#2200](https://github.com/Shopify/polaris-react/pull/2200))
- Removed the need for z-indexes in `Icon` ([#2207](https://github.com/Shopify/polaris-react/pull/2207))
- Added `hasFocusableParent` to `Spinner` ([#2176](https://github.com/Shopify/polaris-react/pull/2176))

### Bug fixes
Expand Down
5 changes: 5 additions & 0 deletions src/components/DataTable/DataTable.scss
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ $breakpoint: 768px;
border-bottom: border();
}

.Cell-total-footer {
border-top: border();
border-bottom: none;
}

.Footer {
padding: spacing();
background: color('sky', 'light');
Expand Down
21 changes: 19 additions & 2 deletions src/components/DataTable/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface DataTableProps {
headings: string[];
/** List of numeric column totals, highlighted in the table’s header below column headings. Use empty strings as placeholders for columns with no total. */
totals?: TableData[];
/** Placement of totals row within table, true for t */
showTotalsInFooter?: boolean;
/** Lists of data points which map to table body rows. */
rows: TableData[][];
/** Truncate content in first column instead of wrapping.
Expand Down Expand Up @@ -120,7 +122,13 @@ class DataTable extends React.PureComponent<CombinedProps, DataTableState> {
}

render() {
const {headings, totals, rows, footerContent} = this.props;
const {
headings,
totals,
showTotalsInFooter,
rows,
footerContent,
} = this.props;
const {
condensed,
columnVisibilityData,
Expand Down Expand Up @@ -150,6 +158,11 @@ class DataTable extends React.PureComponent<CombinedProps, DataTableState> {
<div className={styles.Footer}>{footerContent}</div>
) : null;

const headerTotalsMarkup = !showTotalsInFooter ? totalsMarkup : null;
const footerTotalsMarkup = showTotalsInFooter ? (
<tfoot>{totalsMarkup}</tfoot>
) : null;

return (
<div className={wrapperClassName}>
<Navigation
Expand All @@ -170,9 +183,10 @@ class DataTable extends React.PureComponent<CombinedProps, DataTableState> {
<table className={styles.Table} ref={this.table}>
<thead>
{headingMarkup}
{totalsMarkup}
{headerTotalsMarkup}
</thead>
<tbody>{bodyMarkup}</tbody>
{footerTotalsMarkup}
</table>
</div>
{footerMarkup}
Expand Down Expand Up @@ -320,9 +334,12 @@ class DataTable extends React.PureComponent<CombinedProps, DataTableState> {
content = total;
}

const totalInFooter = this.props.showTotalsInFooter;

return (
<Cell
total
totalInFooter={totalInFooter}
firstColumn={index === 0}
key={id}
content={content}
Expand Down
47 changes: 47 additions & 0 deletions src/components/DataTable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,53 @@ function DataTableFooterExample() {
}
```

### Data table with totals in footer

Use to reposition the totals row in a more appropriate location based on the data stored in the
table for merchants to better understand its meaning.

```jsx
function DataTableExample() {
const rows = [
['Emerald Silk Gown', '$875.00', 124689, 140, '$122,500.00'],
['Mauve Cashmere Scarf', '$230.00', 124533, 83, '$19,090.00'],
[
'Navy Merino Wool Blazer with khaki chinos and yellow belt',
'$445.00',
124518,
32,
'$14,240.00',
],
];

return (
<Page title="Sales by product">
<Card>
<DataTable
columnContentTypes={[
'text',
'numeric',
'numeric',
'numeric',
'numeric',
]}
headings={[
'Product',
'Price',
'SKU Number',
'Net quantity',
'Net sales',
]}
rows={rows}
totals={['', '', '', 255, '$155,830.00']}
showTotalsInFooter
/>
</Card>
</Page>
);
}
```

### Data table with row heading links

Use to help merchants find relevant, finer grained data sets.
Expand Down
3 changes: 3 additions & 0 deletions src/components/DataTable/components/Cell/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface CellProps {
truncate?: boolean;
header?: boolean;
total?: boolean;
totalInFooter?: boolean;
sorted?: boolean;
sortable?: boolean;
sortDirection?: SortDirection;
Expand All @@ -31,6 +32,7 @@ export function Cell({
truncate,
header,
total,
totalInFooter,
sorted,
sortable,
sortDirection,
Expand All @@ -47,6 +49,7 @@ export function Cell({
firstColumn && truncate && styles['Cell-truncated'],
header && styles['Cell-header'],
total && styles['Cell-total'],
totalInFooter && styles['Cell-total-footer'],
numeric && styles['Cell-numeric'],
sortable && styles['Cell-sortable'],
sorted && styles['Cell-sorted'],
Expand Down
9 changes: 9 additions & 0 deletions src/components/DataTable/tests/DataTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ describe('<DataTable />', () => {

totalsCells.forEach((total) => expect(total.text()).toBe(''));
});

it('renders totals in the footer with a showTotalsInFooter prop', () => {
const totals = ['', '', ''];
const dataTable = mountWithAppProvider(
<DataTable {...defaultProps} totals={totals} showTotalsInFooter />,
);

expect(dataTable.find('tfoot')).toHaveLength(1);
});
});

describe('rows', () => {
Expand Down

0 comments on commit befe767

Please sign in to comment.