Skip to content

Commit

Permalink
[Discover] Fix adding/removing a filter for a scripted field in Docum…
Browse files Browse the repository at this point in the history
…ent Explorer view (elastic#130895)

(cherry picked from commit 60049fd)

# Conflicts:
#	src/plugins/discover/public/components/discover_grid/discover_grid_cell_actions.tsx
  • Loading branch information
jughosta committed Apr 26, 2022
1 parent 4dd7ba0 commit 1971daa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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',
'-'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,24 @@
import React, { useContext } from 'react';
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 { DataViewField } from '@kbn/data-views-plugin/public';
import { flattenHit } from '@kbn/data-plugin/public';
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,
Expand All @@ -27,12 +42,7 @@ export const FilterInBtn = ({
return (
<Component
onClick={() => {
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}
Expand Down Expand Up @@ -60,12 +70,7 @@ export const FilterOutBtn = ({
return (
<Component
onClick={() => {
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}
Expand Down

0 comments on commit 1971daa

Please sign in to comment.