From 2653abaa980da29e7acfd2b790a9bbd2fbb32677 Mon Sep 17 00:00:00 2001 From: Stratoula Kalafateli Date: Tue, 8 Dec 2020 18:04:19 +0200 Subject: [PATCH] [EuiDataGrid] Add sorting icon if the column is sorted (#4343) * [EuiDataGrid] Add sorting direction icon indicator if the column is sorted * Show the sorting direction arrows even if the column actions are disabled * minor styling updates * Update changelog with the change Co-authored-by: Dave Snider --- CHANGELOG.md | 1 + .../datagrid/_data_grid_header_row.scss | 4 + src/components/datagrid/data_grid.test.tsx | 105 ++++++++++++++++++ .../datagrid/data_grid_header_cell.tsx | 19 +++- 4 files changed, 126 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9892877720d..afb055cb2a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Adjusted the shadow in `EuiComment` ([#4321](https://github.com/elastic/eui/pull/4321)) - Added `success` and `warning` colors to `EuiButtonEmpty` ([#4325](https://github.com/elastic/eui/pull/4325)) +- Added a sorting indicator on the `EuiDataGridColumn` which indicates the sorting direction and is being displayed only when the column is sorted ([#4343](https://github.com/elastic/eui/pull/4343)) - Added `disabled` and `loading` `status` to `EuiStep` ([#4338](https://github.com/elastic/eui/pull/4338)) **Bug fixes** diff --git a/src/components/datagrid/_data_grid_header_row.scss b/src/components/datagrid/_data_grid_header_row.scss index 2747e40ac72..53956758926 100644 --- a/src/components/datagrid/_data_grid_header_row.scss +++ b/src/components/datagrid/_data_grid_header_row.scss @@ -53,6 +53,10 @@ outline: none; } + .euiDataGridHeaderCell__sortingArrow { + margin-right: $euiSizeXS; + } + .euiDataGridHeaderCell__content { @include euiTextTruncate; overflow: hidden; diff --git a/src/components/datagrid/data_grid.test.tsx b/src/components/datagrid/data_grid.test.tsx index 004e555ede3..c75ecd7fd52 100644 --- a/src/components/datagrid/data_grid.test.tsx +++ b/src/components/datagrid/data_grid.test.tsx @@ -1888,6 +1888,111 @@ Array [ }); }); + describe('render sorting arrows', () => { + it('renders sorting arrows when direction is given', () => { + const component = mount( + {}, + }} + columns={[ + { id: 'A', isSortable: true }, + { id: 'B', isSortable: true }, + ]} + columnVisibility={{ + visibleColumns: ['A', 'B'], + setVisibleColumns: () => {}, + }} + rowCount={2} + renderCellValue={({ rowIndex, columnId }) => + `${rowIndex}-${columnId}` + } + /> + ); + const arrowA = findTestSubject( + component, + 'dataGridHeaderCellSortingIcon-A' + ); + expect(arrowA.length).toBe(1); + + const arrowB = findTestSubject( + component, + 'dataGridHeaderCellSortingIcon-B' + ); + expect(arrowB.length).toBe(1); + }); + + it('does not render the arrows if the column is not sorted', () => { + const component = mount( + {}, + }} + columns={[ + { + id: 'C', + isSortable: true, + actions: { + showHide: false, + showMoveRight: false, + showMoveLeft: false, + showSortAsc: false, + showSortDesc: false, + additional: [{ label: 'test' }], + }, + }, + ]} + columnVisibility={{ + visibleColumns: ['C'], + setVisibleColumns: () => {}, + }} + rowCount={2} + renderCellValue={({ rowIndex, columnId }) => + `${rowIndex}-${columnId}` + } + /> + ); + const arrowC = findTestSubject( + component, + 'dataGridHeaderCellSortingIcon-C' + ); + expect(arrowC.length).toBe(0); + }); + + it('renders the icons if they are sorted but user is not allowed to perform any action', () => { + const component = mount( + {}, + }} + columns={[{ id: 'D', actions: false }]} + columnVisibility={{ + visibleColumns: ['D'], + setVisibleColumns: () => {}, + }} + rowCount={2} + renderCellValue={({ rowIndex, columnId }) => + `${rowIndex}-${columnId}` + } + /> + ); + const arrowD = findTestSubject( + component, + 'dataGridHeaderCellSortingIcon-D' + ); + expect(arrowD.length).toBe(1); + }); + }); + describe('render column cell actions', () => { it('renders various column cell actions configurations', () => { const alertFn = jest.fn(); diff --git a/src/components/datagrid/data_grid_header_cell.tsx b/src/components/datagrid/data_grid_header_cell.tsx index 23977ed6e24..608d7f4cbe4 100644 --- a/src/components/datagrid/data_grid_header_cell.tsx +++ b/src/components/datagrid/data_grid_header_cell.tsx @@ -284,6 +284,15 @@ export const EuiDataGridHeaderCell: FunctionComponent 0; + const sortedColumn = sorting?.columns.find((col) => col.id === id); + const sortingArrow = sortedColumn ? ( + + ) : null; return (
)} {!showColumnActions ? ( -
- {display || displayAsText || id} -
+ <> + {sortingArrow} +
+ {display || displayAsText || id} +
+ ) : (