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

[Discover] Allow to select/deselect all rows in the grid at once #184241

Merged
merged 50 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
2f2adcf
[Discover] Allow to select all rows in the grid
jughosta May 24, 2024
5139871
[Discover] Update tests
jughosta May 24, 2024
d296dc7
Merge remote-tracking branch 'upstream/main' into 175943-select-all-rows
jughosta Jul 3, 2024
17801e7
[Discover] Select only docs from the current page
jughosta Jul 4, 2024
bff44b2
[Discover] Deselect docs only from the current page
jughosta Jul 4, 2024
2fd3e8f
[Discover] Allow to select all rows via menu
jughosta Jul 4, 2024
457bec1
[Discover] Update the message
jughosta Jul 4, 2024
902682a
Merge branch 'main' into 175943-select-all-rows
jughosta Jul 5, 2024
99e8e48
[Discover] Update message
jughosta Jul 5, 2024
e15ef05
[Discover] Fix styles
jughosta Jul 5, 2024
77b5a9d
[Discover] Update tests
jughosta Jul 5, 2024
621becc
[Discover] Add a title
jughosta Jul 5, 2024
407dd73
[Discover] Disable "Compare" button if limit is reached
jughosta Jul 5, 2024
3bd2c58
[Discover] Update tests
jughosta Jul 5, 2024
8f83722
[Discover] Clean up
jughosta Jul 6, 2024
51e340d
Merge branch 'main' into 175943-select-all-rows
jughosta Jul 8, 2024
ff2165d
[Discover] Add the header label back
jughosta Jul 8, 2024
abde706
Revert "[Discover] Clean up"
jughosta Jul 8, 2024
6de42a0
[Discover] Add tests
jughosta Jul 8, 2024
785b522
[Discover] Add tests
jughosta Jul 8, 2024
aa0c312
[Discover] Add tests
jughosta Jul 8, 2024
1799614
[Discover] Rename
jughosta Jul 8, 2024
ec8356c
Merge remote-tracking branch 'upstream/main' into 175943-select-all-rows
jughosta Jul 17, 2024
e617949
[Discover] Rearrange items
jughosta Jul 17, 2024
124fb49
[Discover] Reposition "Select all" button
jughosta Jul 18, 2024
4c4bb84
[Discover] Fix Security Timeline styles
jughosta Jul 18, 2024
048e33e
[Discover] Simplify styles
jughosta Jul 18, 2024
81679d2
[Discover] Fix tests
jughosta Jul 18, 2024
e38eff8
[Discover] Add formatting
jughosta Jul 18, 2024
4392b3f
Merge branch 'main' into 175943-select-all-rows
jughosta Jul 18, 2024
0727579
Merge branch 'main' into 175943-select-all-rows
jughosta Jul 18, 2024
c93dfda
Merge branch 'main' into 175943-select-all-rows
elasticmachine Jul 18, 2024
2038d14
Merge branch 'main' into 175943-select-all-rows
jughosta Jul 19, 2024
b57139c
Merge branch 'main' into 175943-select-all-rows
jughosta Jul 22, 2024
2c8a930
Merge branch 'main' into 175943-select-all-rows
jughosta Jul 23, 2024
69ae73f
Update packages/kbn-unified-data-table/src/components/data_table_docu…
jughosta Jul 24, 2024
831d34a
Merge branch 'main' into 175943-select-all-rows
jughosta Jul 24, 2024
2d61598
Merge branch 'main' into 175943-select-all-rows
jughosta Jul 24, 2024
66c6ddf
Merge branch 'main' into 175943-select-all-rows
jughosta Jul 25, 2024
05bd861
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Jul 25, 2024
22a4b03
Merge branch 'main' into 175943-select-all-rows
jughosta Jul 29, 2024
12455a5
Merge branch 'main' into 175943-select-all-rows
jughosta Jul 30, 2024
f123193
[Discover] Show "Select all X" conditionally
jughosta Jul 30, 2024
de39fc4
[Discover] Improve a little
jughosta Jul 30, 2024
291bb8c
[Discover] Add functional tests
jughosta Jul 30, 2024
d490ef4
Merge branch 'main' into 175943-select-all-rows
jughosta Jul 30, 2024
3a7f3f3
[Discover] Keep the button when switching between pages
jughosta Jul 30, 2024
b9c1a7c
Merge branch 'main' into 175943-select-all-rows
jughosta Jul 30, 2024
78b885f
[Discover] Extend the test
jughosta Jul 30, 2024
d5017b9
Merge branch 'main' into 175943-select-all-rows
jughosta Jul 30, 2024
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
24 changes: 22 additions & 2 deletions packages/kbn-unified-data-table/__mocks__/table_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { DataTableContext } from '../src/table_context';
import { convertValueToString } from '../src/utils/convert_value_to_string';
import { buildDataTableRecord } from '@kbn/discover-utils';
import type { EsHitRecord } from '@kbn/discover-utils/types';
import type { UseSelectedDocsState } from '../src/hooks/use_selected_docs';

const buildTableContext = (dataView: DataView, rows: EsHitRecord[]): DataTableContext => {
const usedRows = rows.map((row) => {
Expand All @@ -28,8 +29,9 @@ const buildTableContext = (dataView: DataView, rows: EsHitRecord[]): DataTableCo
onFilter: jest.fn(),
dataView,
isDarkMode: false,
selectedDocs: [],
setSelectedDocs: jest.fn(),
selectedDocsState: buildSelectedDocsState([]),
pageIndex: 0,
pageSize: 10,
valueToStringConverter: (rowIndex, columnId, options) =>
convertValueToString({
rowIndex,
Expand All @@ -45,3 +47,21 @@ const buildTableContext = (dataView: DataView, rows: EsHitRecord[]): DataTableCo
export const dataTableContextMock = buildTableContext(dataViewMock, esHitsMock);

export const dataTableContextComplexMock = buildTableContext(dataViewComplexMock, esHitsComplex);

export function buildSelectedDocsState(selectedDocIds: string[]): UseSelectedDocsState {
const selectedDocsSet = new Set(selectedDocIds);

return {
isDocSelected: (docId: string) => selectedDocsSet.has(docId),
getCountOfSelectedDocs: (docIds: string[]) =>
docIds.reduce((acc, docId) => (selectedDocsSet.has(docId) ? acc + 1 : acc), 0),
hasSelectedDocs: selectedDocsSet.size > 0,
selectedDocIds,
toggleDocSelection: jest.fn(),
selectAllDocs: jest.fn(),
selectMoreDocs: jest.fn(),
deselectSomeDocs: jest.fn(),
replaceSelectedDocs: jest.fn(),
clearAllSelectedDocs: jest.fn(),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const getDocById = (id: string) => docs.find((doc) => doc.raw._id === id);
const renderCompareDocuments = ({
forceShowAllFields = false,
}: { forceShowAllFields?: boolean } = {}) => {
const setSelectedDocs = jest.fn();
const replaceSelectedDocs = jest.fn();
const getCompareDocuments = (props?: Partial<CompareDocumentsProps>) => (
<CompareDocuments
id="test"
Expand All @@ -63,20 +63,20 @@ const renderCompareDocuments = ({
dataView={dataViewWithTimefieldMock}
isPlainRecord={false}
selectedFieldNames={['message', 'extension', 'bytes']}
selectedDocs={['0', '1', '2']}
selectedDocIds={['0', '1', '2']}
schemaDetectors={[]}
forceShowAllFields={forceShowAllFields}
showFullScreenButton={true}
fieldFormats={{} as any}
getDocById={getDocById}
setSelectedDocs={setSelectedDocs}
replaceSelectedDocs={replaceSelectedDocs}
setIsCompareActive={jest.fn()}
{...props}
/>
);
const { rerender } = render(getCompareDocuments());
return {
setSelectedDocs,
replaceSelectedDocs,
rerender: (props?: Partial<CompareDocumentsProps>) => rerender(getCompareDocuments(props)),
};
};
Expand Down Expand Up @@ -146,10 +146,10 @@ describe('CompareDocuments', () => {
});

it('should set selected docs when columns change', () => {
const { setSelectedDocs } = renderCompareDocuments();
const { replaceSelectedDocs } = renderCompareDocuments();
const visibleColumns = ['fields_generated-id', '0', '1', '2'];
mockDataGridProps?.columnVisibility.setVisibleColumns(visibleColumns);
expect(setSelectedDocs).toHaveBeenCalledWith(visibleColumns.slice(1));
expect(replaceSelectedDocs).toHaveBeenCalledWith(visibleColumns.slice(1));
});

it('should force show all fields when prop is true', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ export interface CompareDocumentsProps {
dataView: DataView;
isPlainRecord: boolean;
selectedFieldNames: string[];
selectedDocs: string[];
selectedDocIds: string[];
schemaDetectors: EuiDataGridSchemaDetector[];
forceShowAllFields: boolean;
showFullScreenButton?: boolean;
fieldFormats: FieldFormatsStart;
getDocById: (id: string) => DataTableRecord | undefined;
setSelectedDocs: (selectedDocs: string[]) => void;
replaceSelectedDocs: (docIds: string[]) => void;
setIsCompareActive: (isCompareActive: boolean) => void;
additionalFieldGroups?: AdditionalFieldGroups;
}
Expand All @@ -69,13 +69,13 @@ const CompareDocuments = ({
isPlainRecord,
selectedFieldNames,
additionalFieldGroups,
selectedDocs,
selectedDocIds,
schemaDetectors,
forceShowAllFields,
showFullScreenButton,
fieldFormats,
getDocById,
setSelectedDocs,
replaceSelectedDocs,
setIsCompareActive,
}: CompareDocumentsProps) => {
// Memoize getDocById to ensure we don't lose access to the comparison docs if, for example,
Expand Down Expand Up @@ -104,7 +104,7 @@ const CompareDocuments = ({
dataView,
selectedFieldNames,
additionalFieldGroups,
selectedDocs,
selectedDocIds,
showAllFields: Boolean(forceShowAllFields || showAllFields),
showMatchingValues: Boolean(showMatchingValues),
getDocById: memoizedGetDocById,
Expand All @@ -113,25 +113,25 @@ const CompareDocuments = ({
wrapper,
isPlainRecord,
fieldColumnId,
selectedDocs,
selectedDocIds,
getDocById: memoizedGetDocById,
setSelectedDocs,
replaceSelectedDocs,
});
const comparisonColumnVisibility = useMemo<EuiDataGridColumnVisibility>(
() => ({
visibleColumns: comparisonColumns.map(({ id: columnId }) => columnId),
setVisibleColumns: (visibleColumns) => {
const [_fieldColumnId, ...newSelectedDocs] = visibleColumns;
setSelectedDocs(newSelectedDocs);
replaceSelectedDocs(newSelectedDocs);
},
}),
[comparisonColumns, setSelectedDocs]
[comparisonColumns, replaceSelectedDocs]
);
const additionalControls = useMemo(
() => (
<ComparisonControls
isPlainRecord={isPlainRecord}
selectedDocs={selectedDocs}
selectedDocIds={selectedDocIds}
showDiff={showDiff}
diffMode={diffMode}
showDiffDecorations={showDiffDecorations}
Expand All @@ -150,7 +150,7 @@ const CompareDocuments = ({
diffMode,
forceShowAllFields,
isPlainRecord,
selectedDocs,
selectedDocIds,
setDiffMode,
setIsCompareActive,
setShowAllFields,
Expand Down Expand Up @@ -184,7 +184,7 @@ const CompareDocuments = ({
dataView,
comparisonFields,
fieldColumnId,
selectedDocs,
selectedDocIds,
diffMode: showDiff ? diffMode : undefined,
fieldFormats,
getDocById: memoizedGetDocById,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const renderComparisonControls = ({
isPlainRecord?: ComparisonControlsProps['isPlainRecord'];
forceShowAllFields?: ComparisonControlsProps['forceShowAllFields'];
} = {}) => {
const selectedDocs = ['0', '1', '2'];
const selectedDocIds = ['0', '1', '2'];
const Wrapper = () => {
const [showDiff, setShowDiff] = useState(true);
const [diffMode, setDiffMode] = useState<DocumentDiffMode>('basic');
Expand All @@ -34,7 +34,7 @@ const renderComparisonControls = ({
<IntlProvider locale="en">
<ComparisonControls
isPlainRecord={isPlainRecord}
selectedDocs={selectedDocs}
selectedDocIds={selectedDocIds}
showDiff={showDiff}
diffMode={diffMode}
showDiffDecorations={showDiffDecorations}
Expand Down Expand Up @@ -67,7 +67,7 @@ const renderComparisonControls = ({
return {
getComparisonCountDisplay: () =>
screen.getByText(
`Comparing ${selectedDocs.length} ${isPlainRecord ? 'results' : 'documents'}`
`Comparing ${selectedDocIds.length} ${isPlainRecord ? 'results' : 'documents'}`
),
getComparisonSettingsButton,
clickComparisonSettingsButton: () => userEvent.click(getComparisonSettingsButton()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import type { DocumentDiffMode } from './types';

export interface ComparisonControlsProps {
isPlainRecord?: boolean;
selectedDocs: string[];
selectedDocIds: string[];
showDiff: boolean | undefined;
diffMode: DocumentDiffMode | undefined;
showDiffDecorations: boolean | undefined;
Expand All @@ -47,7 +47,7 @@ export interface ComparisonControlsProps {

export const ComparisonControls = ({
isPlainRecord,
selectedDocs,
selectedDocIds,
showDiff,
diffMode,
showDiffDecorations,
Expand All @@ -72,13 +72,13 @@ export const ComparisonControls = ({
<FormattedMessage
id="unifiedDataTable.comparingResults"
defaultMessage="Comparing {documentCount} results"
values={{ documentCount: selectedDocs.length }}
values={{ documentCount: selectedDocIds.length }}
/>
) : (
<FormattedMessage
id="unifiedDataTable.comparingDocuments"
defaultMessage="Comparing {documentCount} documents"
values={{ documentCount: selectedDocs.length }}
values={{ documentCount: selectedDocIds.length }}
/>
)}
</strong>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const renderComparisonCellValue = (props: Partial<UseComparisonCellValueProps> =
dataView: dataViewWithTimefieldMock,
comparisonFields: ['message', 'extension', 'bytes'],
fieldColumnId,
selectedDocs: ['0', '1', '2'],
selectedDocIds: ['0', '1', '2'],
diffMode: undefined,
fieldFormats: fieldFormatsMock,
getDocById,
Expand Down Expand Up @@ -408,7 +408,7 @@ describe('useComparisonCellValue', () => {
expect(calculateDiff).toHaveBeenCalledTimes(2);
renderComparisonCell(cellProps2);
expect(calculateDiff).toHaveBeenCalledTimes(2);
rerender({ diffMode: 'words', selectedDocs: ['1', '2', '0'] });
rerender({ diffMode: 'words', selectedDocIds: ['1', '2', '0'] });
const cellProps3 = {
...cellProps1,
columnId: '2',
Expand All @@ -425,7 +425,7 @@ describe('useComparisonCellValue', () => {
expect(calculateDiff).toHaveBeenCalledTimes(4);
renderComparisonCell(cellProps4);
expect(calculateDiff).toHaveBeenCalledTimes(4);
rerender({ diffMode: 'lines', selectedDocs: ['2', '0', '1'] });
rerender({ diffMode: 'lines', selectedDocIds: ['2', '0', '1'] });
const cellProps5 = {
...cellProps1,
columnId: '0',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface UseComparisonCellValueProps {
dataView: DataView;
comparisonFields: string[];
fieldColumnId: string;
selectedDocs: string[];
selectedDocIds: string[];
diffMode: DocumentDiffMode | undefined;
fieldFormats: FieldFormatsStart;
getDocById: (id: string) => DataTableRecord | undefined;
Expand All @@ -50,13 +50,13 @@ export const useComparisonCellValue = ({
dataView,
comparisonFields,
fieldColumnId,
selectedDocs,
selectedDocIds,
diffMode,
fieldFormats,
getDocById,
additionalFieldGroups,
}: UseComparisonCellValueProps) => {
const baseDocId = selectedDocs[0];
const baseDocId = selectedDocIds[0];
const baseDoc = useMemo(() => getDocById(baseDocId)?.flattened, [baseDocId, getDocById]);
const [calculateDiffMemoized] = useState(() => createCalculateDiffMemoized());

Expand Down Expand Up @@ -92,7 +92,7 @@ export const useComparisonCellValue = ({
);
};

type CellValueProps = Omit<UseComparisonCellValueProps, 'selectedDocs'> &
type CellValueProps = Omit<UseComparisonCellValueProps, 'selectedDocIds'> &
EuiDataGridCellValueElementProps & {
baseDocId: string;
baseDoc: DataTableRecord['flattened'] | undefined;
Expand Down
Loading