Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EuiDataGrid] Add sorting icon if the column is sorted #4343

Merged
merged 6 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/components/datagrid/_data_grid_header_row.scss
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
outline: none;
}

.euiDataGridHeaderCell__sortingArrow {
margin-right: $euiSizeXS;
}

.euiDataGridHeaderCell__content {
@include euiTextTruncate;
overflow: hidden;
Expand Down
105 changes: 105 additions & 0 deletions src/components/datagrid/data_grid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,111 @@ Array [
});
});

describe('render sorting arrows', () => {
it('renders sorting arrows when direction is given', () => {
const component = mount(
<EuiDataGrid
aria-labelledby="#test"
sorting={{
columns: [
{ id: 'A', direction: 'asc' },
{ id: 'B', direction: 'desc' },
],
onSort: () => {},
}}
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(
<EuiDataGrid
aria-labelledby="#test"
sorting={{
columns: [],
onSort: () => {},
}}
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(
<EuiDataGrid
aria-labelledby="#test"
sorting={{
columns: [{ id: 'D', direction: 'asc' }],
onSort: () => {},
}}
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();
Expand Down
15 changes: 14 additions & 1 deletion src/components/datagrid/data_grid_header_cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,15 @@ export const EuiDataGridHeaderCell: FunctionComponent<EuiDataGridHeaderCellProps
);

const showColumnActions = columnActions && columnActions.length > 0;
const sortedColumn = sorting?.columns.find((col) => col.id === id);
const sortingArrow = sortedColumn ? (
<EuiIcon
type={sortedColumn.direction === 'asc' ? 'sortUp' : 'sortDown'}
color="text"
className="euiDataGridHeaderCell__sortingArrow"
data-test-subj={`dataGridHeaderCellSortingIcon-${id}`}
/>
) : null;

return (
<div
Expand All @@ -308,11 +317,15 @@ export const EuiDataGridHeaderCell: FunctionComponent<EuiDataGridHeaderCellProps
</EuiScreenReaderOnly>
)}
{!showColumnActions ? (
<div className="euiDataGridHeaderCell__content">{display || id}</div>
<>
{sortingArrow}
<div className="euiDataGridHeaderCell__content">{display || id}</div>
</>
) : (
<button
className="euiDataGridHeaderCell__button"
onClick={() => setIsPopoverOpen(true)}>
{sortingArrow}
<div className="euiDataGridHeaderCell__content">{display || id}</div>
<EuiPopover
className="euiDataGridHeaderCell__popover"
Expand Down