-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Inventory][ECO] filter by type on grid #193875
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
Merged
cauemarcondes
merged 2 commits into
elastic:main
from
cauemarcondes:eco-filter-by-entity-type-on-table
Sep 25, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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', | ||
|
kpatticha marked this conversation as resolved.
|
||
| })} | ||
| </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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.