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

[Security Solution][Investigations] - Add pagination to alert details table #131358

Merged
merged 5 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { login, visitWithoutDateRange } from '../../tasks/login';
import { getUnmappedRule } from '../../objects/rule';

import { ALERTS_URL } from '../../urls/navigation';
import { pageSelector } from '../../screens/alerts_detection_rules';

describe('Alert details with unmapped fields', () => {
before(() => {
Expand Down Expand Up @@ -57,6 +58,7 @@ describe('Alert details with unmapped fields', () => {
};

openTable();
cy.get(ALERT_FLYOUT).find(pageSelector(5)).click({ force: true });
cy.get(ALERT_FLYOUT)
.find(TABLE_ROWS)
.within(() => {
Expand All @@ -74,10 +76,12 @@ describe('Alert details with unmapped fields', () => {
.within(($tableContainer) => {
expect($tableContainer[0].scrollLeft).to.equal(0);

// Try to scroll left and make sure that the table hasn't actually scrolled
// Due to the introduction of pagination on the table, a slight horizontal overflow has been introduced.
// scroll ignores the `overflow-x:hidden` attribute and will still scroll the element if there is a hidden overflow
// Updated the below to equal 4 to account for this and keep a test to make sure it doesn't grow
$tableContainer[0].scroll({ left: 1000 });

expect($tableContainer[0].scrollLeft).to.equal(0);
expect($tableContainer[0].scrollLeft).to.equal(4);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { getOr, noop, sortBy } from 'lodash/fp';
import { EuiInMemoryTable } from '@elastic/eui';
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useDispatch } from 'react-redux';
import { rgba } from 'polished';
import styled from 'styled-components';
Expand Down Expand Up @@ -59,6 +59,8 @@ const TableWrapper = styled.div`
const StyledEuiInMemoryTable = styled(EuiInMemoryTable as any)`
flex: 1;
overflow: auto;
overflow-x: hidden;

&::-webkit-scrollbar {
height: ${({ theme }) => theme.eui.euiScrollBar};
width: ${({ theme }) => theme.eui.euiScrollBar};
Expand Down Expand Up @@ -131,6 +133,32 @@ const StyledEuiInMemoryTable = styled(EuiInMemoryTable as any)`
}
`;

// Match structure in discover
const COUNT_PER_PAGE_OPTIONS = [25, 50, 100];

// Encapsulating the pagination logic for the table.
const useFieldBrowserPagination = () => {
const [pagination, setPagination] = useState<{ pageIndex: number }>({
pageIndex: 0,
});

const onTableChange = useCallback(({ page: { index } }: { page: { index: number } }) => {
setPagination({ pageIndex: index });
}, []);
const paginationTableProp = useMemo(
() => ({
...pagination,
pageSizeOptions: COUNT_PER_PAGE_OPTIONS,
}),
[pagination]
);

return {
onTableChange,
paginationTableProp,
};
};

/**
* This callback, invoked via `EuiInMemoryTable`'s `rowProps, assigns
* attributes to every `<tr>`.
Expand Down Expand Up @@ -274,14 +302,18 @@ export const EventFieldsBrowser = React.memo<Props>(
focusSearchInput();
}, [focusSearchInput]);

// Pagination
const { onTableChange, paginationTableProp } = useFieldBrowserPagination();

return (
<TableWrapper onKeyDown={onKeyDown} ref={containerElement}>
<StyledEuiInMemoryTable
className={EVENT_FIELDS_TABLE_CLASS_NAME}
items={items}
itemId="field"
columns={columns}
pagination={false}
onTableChange={onTableChange}
pagination={paginationTableProp}
rowProps={onSetRowProps}
search={search}
sorting={false}
Expand Down