diff --git a/UNRELEASED.md b/UNRELEASED.md index b9eac844f39..aab2f6f7e69 100644 --- a/UNRELEASED.md +++ b/UNRELEASED.md @@ -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 diff --git a/src/components/DataTable/DataTable.scss b/src/components/DataTable/DataTable.scss index b83a3cf95f9..c14464b5599 100644 --- a/src/components/DataTable/DataTable.scss +++ b/src/components/DataTable/DataTable.scss @@ -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'); diff --git a/src/components/DataTable/DataTable.tsx b/src/components/DataTable/DataTable.tsx index 3b1d1f76cff..06ad436d1e8 100644 --- a/src/components/DataTable/DataTable.tsx +++ b/src/components/DataTable/DataTable.tsx @@ -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. @@ -120,7 +122,13 @@ class DataTable extends React.PureComponent { } render() { - const {headings, totals, rows, footerContent} = this.props; + const { + headings, + totals, + showTotalsInFooter, + rows, + footerContent, + } = this.props; const { condensed, columnVisibilityData, @@ -150,6 +158,11 @@ class DataTable extends React.PureComponent {
{footerContent}
) : null; + const headerTotalsMarkup = !showTotalsInFooter ? totalsMarkup : null; + const footerTotalsMarkup = showTotalsInFooter ? ( + {totalsMarkup} + ) : null; + return (
{ {headingMarkup} - {totalsMarkup} + {headerTotalsMarkup} {bodyMarkup} + {footerTotalsMarkup}
{footerMarkup} @@ -320,9 +334,12 @@ class DataTable extends React.PureComponent { content = total; } + const totalInFooter = this.props.showTotalsInFooter; + return ( + + + + + ); +} +``` + ### Data table with row heading links Use to help merchants find relevant, finer grained data sets. diff --git a/src/components/DataTable/components/Cell/Cell.tsx b/src/components/DataTable/components/Cell/Cell.tsx index fec47138550..48ee2b999ba 100644 --- a/src/components/DataTable/components/Cell/Cell.tsx +++ b/src/components/DataTable/components/Cell/Cell.tsx @@ -16,6 +16,7 @@ export interface CellProps { truncate?: boolean; header?: boolean; total?: boolean; + totalInFooter?: boolean; sorted?: boolean; sortable?: boolean; sortDirection?: SortDirection; @@ -31,6 +32,7 @@ export function Cell({ truncate, header, total, + totalInFooter, sorted, sortable, sortDirection, @@ -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'], diff --git a/src/components/DataTable/tests/DataTable.test.tsx b/src/components/DataTable/tests/DataTable.test.tsx index f405a5a5f42..90683a67393 100644 --- a/src/components/DataTable/tests/DataTable.test.tsx +++ b/src/components/DataTable/tests/DataTable.test.tsx @@ -155,6 +155,15 @@ describe('', () => { totalsCells.forEach((total) => expect(total.text()).toBe('')); }); + + it('renders totals in the footer with a showTotalsInFooter prop', () => { + const totals = ['', '', '']; + const dataTable = mountWithAppProvider( + , + ); + + expect(dataTable.find('tfoot')).toHaveLength(1); + }); }); describe('rows', () => {