From 1d123da5be15d904c84f44f43803497edac4c459 Mon Sep 17 00:00:00 2001 From: Julia Rechkunova Date: Tue, 26 Apr 2022 21:11:42 +0200 Subject: [PATCH] [8.2] [Discover] Fix adding/removing a filter for a scripted field in Document Explorer view (#130895) (#130996) * [Discover] Fix adding/removing a filter for a scripted field in Document Explorer view (#130895) (cherry picked from commit 60049fd085b38d608321a5802d7039034596146d) # Conflicts: # src/plugins/discover/public/components/discover_grid/discover_grid_cell_actions.tsx * [Discover] Revert paths for backporting --- .../discover_grid_cell_actions.test.tsx | 12 +++++-- .../discover_grid_cell_actions.tsx | 31 +++++++++++-------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/plugins/discover/public/components/discover_grid/discover_grid_cell_actions.test.tsx b/src/plugins/discover/public/components/discover_grid/discover_grid_cell_actions.test.tsx index 0c6783fabf189..73b49f3e19954 100644 --- a/src/plugins/discover/public/components/discover_grid/discover_grid_cell_actions.test.tsx +++ b/src/plugins/discover/public/components/discover_grid/discover_grid_cell_actions.test.tsx @@ -49,7 +49,11 @@ describe('Discover cell actions ', function () { ); const button = findTestSubject(component, 'filterForButton'); await button.simulate('click'); - expect(contextMock.onFilter).toHaveBeenCalledWith('extension', 'jpg', '+'); + expect(contextMock.onFilter).toHaveBeenCalledWith( + indexPatternMock.fields.getByName('extension'), + 'jpg', + '+' + ); }); it('triggers filter function when FilterOutBtn is clicked', async () => { const contextMock = { @@ -78,6 +82,10 @@ describe('Discover cell actions ', function () { ); const button = findTestSubject(component, 'filterOutButton'); await button.simulate('click'); - expect(contextMock.onFilter).toHaveBeenCalledWith('extension', 'jpg', '-'); + expect(contextMock.onFilter).toHaveBeenCalledWith( + indexPatternMock.fields.getByName('extension'), + 'jpg', + '-' + ); }); }); diff --git a/src/plugins/discover/public/components/discover_grid/discover_grid_cell_actions.tsx b/src/plugins/discover/public/components/discover_grid/discover_grid_cell_actions.tsx index dac15af537563..aab482641f342 100644 --- a/src/plugins/discover/public/components/discover_grid/discover_grid_cell_actions.tsx +++ b/src/plugins/discover/public/components/discover_grid/discover_grid_cell_actions.tsx @@ -11,7 +11,22 @@ import { EuiDataGridColumnCellActionProps } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import { DataViewField } from '../../../../data_views/public'; import { flattenHit } from '../../../../data/public'; -import { DiscoverGridContext } from './discover_grid_context'; +import { DiscoverGridContext, GridContext } from './discover_grid_context'; + +function onFilterCell( + context: GridContext, + rowIndex: EuiDataGridColumnCellActionProps['rowIndex'], + columnId: EuiDataGridColumnCellActionProps['columnId'], + mode: '+' | '-' +) { + const row = context.rows[rowIndex]; + const flattened = flattenHit(row, context.indexPattern); + const field = context.indexPattern.fields.getByName(columnId); + + if (flattened && field) { + context.onFilter(field, flattened[columnId], mode); + } +} export const FilterInBtn = ({ Component, @@ -27,12 +42,7 @@ export const FilterInBtn = ({ return ( { - const row = context.rows[rowIndex]; - const flattened = flattenHit(row, context.indexPattern); - - if (flattened) { - context.onFilter(columnId, flattened[columnId], '+'); - } + onFilterCell(context, rowIndex, columnId, '+'); }} iconType="plusInCircle" aria-label={buttonTitle} @@ -60,12 +70,7 @@ export const FilterOutBtn = ({ return ( { - const row = context.rows[rowIndex]; - const flattened = flattenHit(row, context.indexPattern); - - if (flattened) { - context.onFilter(columnId, flattened[columnId], '-'); - } + onFilterCell(context, rowIndex, columnId, '-'); }} iconType="minusInCircle" aria-label={buttonTitle}