forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Inventory][ECO] filter by type on grid (elastic#193875)
closes elastic#192669 https://github.com/user-attachments/assets/d8c51a63-6783-4a9c-a785-83004489cd5d (cherry picked from commit 475fd6c)
- Loading branch information
1 parent
f1a6bd5
commit 251c3d1
Showing
5 changed files
with
229 additions
and
22 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
.../inventory/public/components/badge_filter_with_popover/badge_filter_with_popover.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { render, fireEvent, screen } from '@testing-library/react'; | ||
import { BadgeFilterWithPopover } from '.'; | ||
import { EuiThemeProvider, copyToClipboard } from '@elastic/eui'; | ||
import { ENTITY_TYPE } from '../../../common/es_fields/entities'; | ||
|
||
jest.mock('@elastic/eui', () => ({ | ||
...jest.requireActual('@elastic/eui'), | ||
copyToClipboard: jest.fn(), | ||
})); | ||
|
||
describe('BadgeFilterWithPopover', () => { | ||
const mockOnFilter = jest.fn(); | ||
const field = ENTITY_TYPE; | ||
const value = 'host'; | ||
const label = 'Host'; | ||
const popoverContentDataTestId = 'inventoryBadgeFilterWithPopoverContent'; | ||
const popoverContentTitleTestId = 'inventoryBadgeFilterWithPopoverTitle'; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('renders the badge with the correct label', () => { | ||
render( | ||
<BadgeFilterWithPopover field={field} value={value} onFilter={mockOnFilter} label={label} />, | ||
{ wrapper: EuiThemeProvider } | ||
); | ||
expect(screen.queryByText(label)).toBeInTheDocument(); | ||
expect(screen.getByText(label).textContent).toBe(label); | ||
}); | ||
|
||
it('opens the popover when the badge is clicked', () => { | ||
render(<BadgeFilterWithPopover field={field} value={value} onFilter={mockOnFilter} />); | ||
expect(screen.queryByTestId(popoverContentDataTestId)).not.toBeInTheDocument(); | ||
fireEvent.click(screen.getByText(value)); | ||
expect(screen.queryByTestId(popoverContentDataTestId)).toBeInTheDocument(); | ||
expect(screen.queryByTestId(popoverContentTitleTestId)?.textContent).toBe(`${field}:${value}`); | ||
}); | ||
|
||
it('calls onFilter when the "Filter for" button is clicked', () => { | ||
render(<BadgeFilterWithPopover field={field} value={value} onFilter={mockOnFilter} />); | ||
fireEvent.click(screen.getByText(value)); | ||
fireEvent.click(screen.getByTestId('inventoryBadgeFilterWithPopoverFilterForButton')); | ||
expect(mockOnFilter).toHaveBeenCalled(); | ||
}); | ||
|
||
it('copies value to clipboard when the "Copy value" button is clicked', () => { | ||
render(<BadgeFilterWithPopover field={field} value={value} onFilter={mockOnFilter} />); | ||
fireEvent.click(screen.getByText(value)); | ||
fireEvent.click(screen.getByTestId('inventoryBadgeFilterWithPopoverCopyValueButton')); | ||
expect(copyToClipboard).toHaveBeenCalledWith(value); | ||
}); | ||
}); |
103 changes: 103 additions & 0 deletions
103
...ns/observability_solution/inventory/public/components/badge_filter_with_popover/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { | ||
EuiBadge, | ||
EuiButtonEmpty, | ||
EuiFlexGrid, | ||
EuiFlexGroup, | ||
EuiFlexItem, | ||
EuiPopover, | ||
EuiPopoverFooter, | ||
copyToClipboard, | ||
useEuiTheme, | ||
} from '@elastic/eui'; | ||
import { css } from '@emotion/react'; | ||
import { i18n } from '@kbn/i18n'; | ||
import React, { useState } from 'react'; | ||
|
||
interface Props { | ||
field: string; | ||
value: string; | ||
label?: string; | ||
onFilter: () => void; | ||
} | ||
|
||
export function BadgeFilterWithPopover({ field, value, onFilter, label }: Props) { | ||
const [isOpen, setIsOpen] = useState(false); | ||
const theme = useEuiTheme(); | ||
|
||
return ( | ||
<EuiPopover | ||
button={ | ||
<EuiBadge | ||
data-test-subj="inventoryBadgeFilterWithPopoverButton" | ||
color="hollow" | ||
onClick={() => setIsOpen((state) => !state)} | ||
onClickAriaLabel={i18n.translate( | ||
'xpack.inventory.badgeFilterWithPopover.openPopoverBadgeLabel', | ||
{ defaultMessage: 'Open popover' } | ||
)} | ||
> | ||
{label || value} | ||
</EuiBadge> | ||
} | ||
isOpen={isOpen} | ||
closePopover={() => setIsOpen(false)} | ||
> | ||
<span data-test-subj="inventoryBadgeFilterWithPopoverTitle"> | ||
<EuiFlexGroup | ||
data-test-subj="inventoryBadgeFilterWithPopoverContent" | ||
responsive={false} | ||
gutterSize="xs" | ||
css={css` | ||
font-family: ${theme.euiTheme.font.familyCode}; | ||
`} | ||
> | ||
<EuiFlexItem grow={false}> | ||
<span | ||
css={css` | ||
font-weight: bold; | ||
`} | ||
> | ||
{field}: | ||
</span> | ||
</EuiFlexItem> | ||
<EuiFlexItem grow={false}> | ||
<span className="eui-textBreakWord">{value}</span> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
</span> | ||
<EuiPopoverFooter> | ||
<EuiFlexGrid responsive={false} columns={2}> | ||
<EuiFlexItem> | ||
<EuiButtonEmpty | ||
data-test-subj="inventoryBadgeFilterWithPopoverFilterForButton" | ||
iconType="plusInCircle" | ||
onClick={onFilter} | ||
> | ||
{i18n.translate('xpack.inventory.badgeFilterWithPopover.filterForButtonEmptyLabel', { | ||
defaultMessage: 'Filter for', | ||
})} | ||
</EuiButtonEmpty> | ||
</EuiFlexItem> | ||
<EuiFlexItem> | ||
<EuiButtonEmpty | ||
data-test-subj="inventoryBadgeFilterWithPopoverCopyValueButton" | ||
iconType="copyClipboard" | ||
onClick={() => copyToClipboard(value)} | ||
> | ||
{i18n.translate('xpack.inventory.badgeFilterWithPopover.copyValueButtonEmptyLabel', { | ||
defaultMessage: 'Copy value', | ||
})} | ||
</EuiButtonEmpty> | ||
</EuiFlexItem> | ||
</EuiFlexGrid> | ||
</EuiPopoverFooter> | ||
</EuiPopover> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters