Skip to content

Commit 728e013

Browse files
authored
feat(AnalyticalTable): allow multi-sorting (#4550)
1 parent b7dbe2b commit 728e013

File tree

5 files changed

+76
-2
lines changed

5 files changed

+76
-2
lines changed

packages/main/src/components/AnalyticalTable/AnalyticalTable.cy.tsx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,72 @@ describe('AnalyticalTable', () => {
17251725
cy.get('[data-component-name="AnalyticalTableBody"]').invoke('scrollTop').should('not.equal', 0);
17261726
});
17271727

1728+
it('multi-sort', () => {
1729+
const columns = [
1730+
{
1731+
Header: 'Name',
1732+
accessor: 'name',
1733+
enableMultiSort: true
1734+
},
1735+
{
1736+
Header: 'Age',
1737+
accessor: 'age',
1738+
enableMultiSort: true
1739+
},
1740+
{
1741+
Header: 'Name 2',
1742+
accessor: 'name2'
1743+
}
1744+
];
1745+
const data = [
1746+
{ name: 'A', age: 40, name2: 'Y', age2: 18 },
1747+
{ name: 'B', age: 40, name2: 'X', age2: 21 },
1748+
{ name: 'A', age: 30, name2: 'Z', age2: 90 },
1749+
{ name: 'A', age: 70, name2: 'Z', age2: 15 },
1750+
{ name: 'B', age: 60, name2: 'Q', age2: 80 },
1751+
{ name: 'B', age: 20, name2: 'Y', age2: 80 },
1752+
{ name: 'C', age: 40, name2: 'Y', age2: 80 }
1753+
];
1754+
cy.mount(<AnalyticalTable columns={columns} data={data} sortable />);
1755+
1756+
//sort both Name and Age (multi-sort enabled)
1757+
cy.findByText('Name').click();
1758+
cy.findByText('Sort Ascending').shadow().findByRole('listitem').click({ force: true });
1759+
cy.findByText('Age').click();
1760+
cy.findByText('Sort Ascending').shadow().findByRole('listitem').click({ force: true });
1761+
1762+
cy.get('[data-column-index="0"][data-row-index="1"]').children().should('have.text', 'A');
1763+
cy.get('[data-column-index="1"][data-row-index="1"]').children().should('have.text', '30');
1764+
cy.get('[data-column-index="0"][data-row-index="2"]').children().should('have.text', 'A');
1765+
cy.get('[data-column-index="1"][data-row-index="2"]').children().should('have.text', '40');
1766+
cy.get('[data-column-index="0"][data-row-index="3"]').children().should('have.text', 'A');
1767+
cy.get('[data-column-index="1"][data-row-index="3"]').children().should('have.text', '70');
1768+
1769+
cy.get('[data-column-index="0"][data-row-index="4"]').children().should('have.text', 'B');
1770+
cy.get('[data-column-index="1"][data-row-index="4"]').children().should('have.text', '20');
1771+
cy.get('[data-column-index="0"][data-row-index="5"]').children().should('have.text', 'B');
1772+
cy.get('[data-column-index="1"][data-row-index="5"]').children().should('have.text', '40');
1773+
cy.get('[data-column-index="0"][data-row-index="6"]').children().should('have.text', 'B');
1774+
cy.get('[data-column-index="1"][data-row-index="6"]').children().should('have.text', '60');
1775+
1776+
cy.get('[data-column-index="0"][data-row-index="7"]').children().should('have.text', 'C');
1777+
cy.get('[data-column-index="1"][data-row-index="7"]').children().should('have.text', '40');
1778+
1779+
//only sort Name2
1780+
cy.findByText('Name 2').click();
1781+
cy.findByText('Sort Ascending').shadow().findByRole('listitem').click({ force: true });
1782+
1783+
cy.get('[data-column-index="0"][data-row-index="1"]').children().should('have.text', 'B');
1784+
cy.get('[data-column-index="1"][data-row-index="1"]').children().should('have.text', '60');
1785+
cy.get('[data-column-index="2"][data-row-index="1"]').children().should('have.text', 'Q');
1786+
cy.get('[data-column-index="0"][data-row-index="2"]').children().should('have.text', 'B');
1787+
cy.get('[data-column-index="1"][data-row-index="2"]').children().should('have.text', '40');
1788+
cy.get('[data-column-index="2"][data-row-index="2"]').children().should('have.text', 'X');
1789+
cy.get('[data-column-index="0"][data-row-index="3"]').children().should('have.text', 'A');
1790+
cy.get('[data-column-index="1"][data-row-index="3"]').children().should('have.text', '40');
1791+
cy.get('[data-column-index="2"][data-row-index="3"]').children().should('have.text', 'Y');
1792+
});
1793+
17281794
cypressPassThroughTestsFactory(AnalyticalTable, { data, columns });
17291795
});
17301796

packages/main/src/components/AnalyticalTable/AnalyticalTable.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ import * as ComponentStories from './AnalyticalTable.stories';
185185
| `PopInHeader` | `string OR ComponentType` | Custom pop-in header renderer. If set, the table will call that component for every column that is "popped-in" and pass the table instance as prop. |
186186
| `disableDragAndDrop` | `boolean` | Defines if the column is reorderable by dragging and dropping columns. |
187187
| `canReorder` | `boolean` | **deprecated** Defines if the column is reorderable by dragging and dropping columns. Please use `disableDragAndDrop` instead. Defaults to: `true`. |
188+
| `enableMultiSort` | `boolean` | Defines whether this column should allow multi-sort.<br /><br /> **Note:** When sorting by a column that does not allow multiple sorting, only the current column is sorted and all other sorted columns are reset. |
188189

189190
<br />
190191

packages/main/src/components/AnalyticalTable/ColumnHeader/ColumnHeaderModal.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export const ColumnHeaderModal = (props: ColumnHeaderModalProperties) => {
8282

8383
switch (sortType) {
8484
case 'asc':
85-
column.toggleSortBy(false);
85+
column.toggleSortBy(false, !!column.enableMultiSort);
8686
if (typeof onSort === 'function') {
8787
onSort(
8888
enrichEventWithDetails(e, {
@@ -93,7 +93,7 @@ export const ColumnHeaderModal = (props: ColumnHeaderModalProperties) => {
9393
}
9494
break;
9595
case 'desc':
96-
column.toggleSortBy(true);
96+
column.toggleSortBy(true, !!column.enableMultiSort);
9797
if (typeof onSort === 'function') {
9898
onSort(
9999
enrichEventWithDetails(e, {

packages/main/src/components/AnalyticalTable/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@ export interface AnalyticalTableColumnDefinition {
254254
* Defines if the column is reorderable by dragging and dropping columns.
255255
*/
256256
disableDragAndDrop?: boolean;
257+
/**
258+
* Defines whether this column should allow multi-sort.
259+
*
260+
* __Note:__ When sorting by a column that does not allow multiple sorting, only the current column is sorted and all other sorted columns are reset.
261+
*/
262+
enableMultiSort?: boolean;
257263

258264
// all other custom properties of [React Table](https://react-table-v7.tanstack.com/) column options
259265
[key: string]: any;

packages/main/src/components/AnalyticalTable/types/ColumnType.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ export interface ColumnType extends Column {
2323
hAlign: string;
2424
totalLeft: number;
2525
totalFlexWidth: number;
26+
enableMultiSort?: boolean;
2627
}

0 commit comments

Comments
 (0)