-
Notifications
You must be signed in to change notification settings - Fork 16
[RHCLOUD-43251] Adding treeview filter to DataView #572
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
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5c863e2
feat: adding data view tree filter
Jakub007d 536676f
fix: fixing lint errors
Jakub007d 4b1ed54
fix: extracting uncheckRecursive to avoid duplicate code
Jakub007d ab342ac
fix: adding new requested tests and implementing requested changes
Jakub007d 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
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
248 changes: 248 additions & 0 deletions
248
...odule/patternfly-docs/content/extensions/data-view/examples/Toolbar/TreeFilterExample.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,248 @@ | ||
| import React, { useMemo } from 'react'; | ||
| import { Pagination } from '@patternfly/react-core'; | ||
| import { BrowserRouter, useSearchParams } from 'react-router-dom'; | ||
| import { TreeViewDataItem } from '@patternfly/react-core'; | ||
| import { useDataViewFilters, useDataViewPagination } from '@patternfly/react-data-view/dist/dynamic/Hooks'; | ||
| import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView'; | ||
| import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable'; | ||
| import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar'; | ||
| import { DataViewFilters } from '@patternfly/react-data-view/dist/dynamic/DataViewFilters'; | ||
| import { DataViewTreeFilter } from '@patternfly/react-data-view/dist/dynamic/DataViewTreeFilter'; | ||
|
|
||
| const perPageOptions = [ | ||
| { title: '5', value: 5 }, | ||
| { title: '10', value: 10 } | ||
| ]; | ||
|
|
||
| interface Repository { | ||
| name: string; | ||
| workspace: string; | ||
| tags: string[]; | ||
| os: string; | ||
| lastSeen: string; | ||
| } | ||
|
|
||
| interface RepositoryFilters { | ||
| name: string; | ||
| workspace: string[]; | ||
| os: string[]; | ||
| tags: string[]; | ||
| } | ||
|
|
||
| const repositories: Repository[] = [ | ||
| { name: 'Server-001', workspace: 'Development Workspace', tags: ['web', 'frontend'], os: 'Ubuntu 22.04', lastSeen: '2 hours ago' }, | ||
| { name: 'Server-002', workspace: 'Development Workspace', tags: ['api', 'backend'], os: 'RHEL 9', lastSeen: '5 hours ago' }, | ||
| { name: 'Server-003', workspace: 'Development Workspace', tags: ['database'], os: 'Windows Server 2022', lastSeen: '1 day ago' }, | ||
| { name: 'Server-004', workspace: 'Production Workspace', tags: ['web', 'frontend'], os: 'Ubuntu 22.04', lastSeen: '30 minutes ago' }, | ||
| { name: 'Server-005', workspace: 'Production Workspace', tags: ['api', 'backend'], os: 'Debian 12', lastSeen: '1 hour ago' }, | ||
| { name: 'Server-006', workspace: 'Production Workspace', tags: ['monitoring'], os: 'macOS Ventura', lastSeen: '3 hours ago' }, | ||
| { name: 'Server-007', workspace: 'Production Workspace', tags: ['cache'], os: 'macOS Sonoma', lastSeen: '2 days ago' }, | ||
| { name: 'Server-008', workspace: 'Testing Workspace', tags: ['test', 'frontend'], os: 'CentOS 8', lastSeen: '6 hours ago' }, | ||
| { name: 'Server-009', workspace: 'Testing Workspace', tags: ['test', 'backend'], os: 'Fedora 38', lastSeen: '4 hours ago' } | ||
| ]; | ||
|
|
||
|
|
||
| const osOptions: TreeViewDataItem[] = [ | ||
| { | ||
| name: 'Linux', | ||
| id: 'os-linux', | ||
| checkProps: { 'aria-label': 'linux-check', checked: false }, | ||
| children: [ | ||
| { | ||
| name: 'Ubuntu 22.04', | ||
| id: 'os-ubuntu', | ||
| checkProps: { checked: false } | ||
| }, | ||
| { | ||
| name: 'RHEL 9', | ||
| id: 'os-rhel', | ||
| checkProps: { checked: false } | ||
| }, | ||
| { | ||
| name: 'Debian 12', | ||
| id: 'os-debian', | ||
| checkProps: { checked: false } | ||
| }, | ||
| { | ||
| name: 'CentOS 8', | ||
| id: 'os-centos', | ||
| checkProps: { checked: false } | ||
| }, | ||
| { | ||
| name: 'Fedora 38', | ||
| id: 'os-fedora', | ||
| checkProps: { checked: false } | ||
| } | ||
| ], | ||
| defaultExpanded: true | ||
| }, | ||
| { | ||
| name: 'Windows', | ||
| id: 'os-windows', | ||
| checkProps: { 'aria-label': 'windows-check', checked: false }, | ||
| children: [ | ||
| { | ||
| name: 'Windows Server 2022', | ||
| id: 'os-windows-2022', | ||
| checkProps: { checked: false } | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| name: 'macOS', | ||
| id: 'os-macos', | ||
| checkProps: { 'aria-label': 'macos-check', checked: false }, | ||
| children: [ | ||
| { | ||
| name: 'macOS Ventura', | ||
| id: 'os-macos-ventura', | ||
| checkProps: { checked: false } | ||
| }, | ||
| { | ||
| name: 'macOS Sonoma', | ||
| id: 'os-macos-sonoma', | ||
| checkProps: { checked: false } | ||
| } | ||
| ] | ||
| } | ||
| ]; | ||
|
|
||
| const tagOptions: TreeViewDataItem[] = [ | ||
| { | ||
| name: 'Environment', | ||
| id: 'tags-env', | ||
| checkProps: { 'aria-label': 'env-check', checked: false }, | ||
| children: [ | ||
| { | ||
| name: 'web', | ||
| id: 'tag-web', | ||
| checkProps: { checked: false } | ||
| }, | ||
| { | ||
| name: 'api', | ||
| id: 'tag-api', | ||
| checkProps: { checked: false } | ||
| }, | ||
| { | ||
| name: 'database', | ||
| id: 'tag-database', | ||
| checkProps: { checked: false } | ||
| } | ||
| ], | ||
| defaultExpanded: true | ||
| }, | ||
| { | ||
| name: 'Layer', | ||
| id: 'tags-layer', | ||
| checkProps: { 'aria-label': 'layer-check', checked: false }, | ||
| children: [ | ||
| { | ||
| name: 'frontend', | ||
| id: 'tag-frontend', | ||
| checkProps: { checked: false } | ||
| }, | ||
| { | ||
| name: 'backend', | ||
| id: 'tag-backend', | ||
| checkProps: { checked: false } | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| name: 'Other', | ||
| id: 'tags-other', | ||
| checkProps: { 'aria-label': 'other-check', checked: false }, | ||
| children: [ | ||
| { | ||
| name: 'monitoring', | ||
| id: 'tag-monitoring', | ||
| checkProps: { checked: false } | ||
| }, | ||
| { | ||
| name: 'cache', | ||
| id: 'tag-cache', | ||
| checkProps: { checked: false } | ||
| }, | ||
| { | ||
| name: 'test', | ||
| id: 'tag-test', | ||
| checkProps: { checked: false } | ||
| } | ||
| ] | ||
| } | ||
| ]; | ||
|
|
||
| const columns = ['Name', 'Workspace', 'Tags', 'OS', 'Last seen']; | ||
|
|
||
| const ouiaId = 'TreeFilterExample'; | ||
|
|
||
| const MyTable: React.FunctionComponent = () => { | ||
| const [searchParams, setSearchParams] = useSearchParams(); | ||
| const { filters, onSetFilters, clearAllFilters } = useDataViewFilters<RepositoryFilters>({ | ||
| initialFilters: { name: '', workspace: [], os: [], tags: [] }, | ||
| searchParams, | ||
| setSearchParams, | ||
| }); | ||
| const pagination = useDataViewPagination({ perPage: 5 }); | ||
| const { page, perPage } = pagination; | ||
|
|
||
| const filteredData = useMemo( | ||
| () => | ||
| repositories.filter( | ||
| (item) => | ||
| (!filters.name || item.name?.toLocaleLowerCase().includes(filters.name?.toLocaleLowerCase())) && | ||
| (!filters.workspace || filters.workspace.length === 0 || filters.workspace.includes(item.workspace)) && | ||
| (!filters.os || filters.os.length === 0 || filters.os.includes(item.os)) && | ||
| (!filters.tags || filters.tags.length === 0 || filters.tags.some(tag => item.tags.includes(tag))) | ||
| ), | ||
| [filters] | ||
| ); | ||
|
|
||
| const pageRows = useMemo( | ||
| () => | ||
| filteredData | ||
| .slice((page - 1) * perPage, (page - 1) * perPage + perPage) | ||
| .map((item) => [item.name, item.workspace, item.tags.join(', '), item.os, item.lastSeen]), | ||
| [page, perPage, filteredData] | ||
| ); | ||
|
|
||
| return ( | ||
| <DataView> | ||
| <DataViewToolbar | ||
| ouiaId="TreeFilterExampleHeader" | ||
| clearAllFilters={clearAllFilters} | ||
| pagination={<Pagination perPageOptions={perPageOptions} itemCount={filteredData.length} {...pagination} />} | ||
| filters={ | ||
| <DataViewFilters onChange={(_e, values) => onSetFilters(values)} values={filters}> | ||
| <DataViewTreeFilter | ||
| filterId="os" | ||
| title="Operating System" | ||
| items={osOptions} | ||
| defaultExpanded={true} | ||
| /> | ||
| <DataViewTreeFilter | ||
| filterId="tags" | ||
| title="Tags" | ||
| items={tagOptions} | ||
| defaultExpanded={false} | ||
| defaultSelected={['tag-web', 'tag-api']} | ||
| /> | ||
| </DataViewFilters> | ||
| } | ||
| /> | ||
| <DataViewTable aria-label="Repositories table" ouiaId={ouiaId} columns={columns} rows={pageRows} /> | ||
| <DataViewToolbar | ||
| ouiaId="TreeFilterExampleFooter" | ||
| pagination={ | ||
| <Pagination isCompact perPageOptions={perPageOptions} itemCount={filteredData.length} {...pagination} /> | ||
| } | ||
| /> | ||
| </DataView> | ||
| ); | ||
| }; | ||
|
|
||
| export const TreeFilterExample: React.FunctionComponent = () => ( | ||
| <BrowserRouter> | ||
| <MyTable /> | ||
| </BrowserRouter> | ||
| ); |
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
50 changes: 50 additions & 0 deletions
50
packages/module/src/DataViewTreeFilter/DataViewTreeFilter.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,50 @@ | ||
| import { render } from '@testing-library/react'; | ||
| import DataViewTreeFilter, { DataViewTreeFilterProps } from './DataViewTreeFilter'; | ||
| import DataViewToolbar from '../DataViewToolbar'; | ||
| import { TreeViewDataItem } from '@patternfly/react-core'; | ||
|
|
||
| describe('DataViewTreeFilter component', () => { | ||
| const treeItems: TreeViewDataItem[] = [ | ||
| { | ||
| name: 'Development Workspace', | ||
| id: 'workspace-dev', | ||
| checkProps: { 'aria-label': 'dev-workspace-check', checked: false } | ||
| }, | ||
| { | ||
| name: 'Production Workspace', | ||
| id: 'workspace-prod', | ||
| checkProps: { 'aria-label': 'prod-workspace-check', checked: false } | ||
| }, | ||
| { | ||
| name: 'Operating Systems', | ||
| id: 'os-parent', | ||
| checkProps: { 'aria-label': 'os-check', checked: false }, | ||
| children: [ | ||
| { | ||
| name: 'Linux', | ||
| id: 'os-linux', | ||
| checkProps: { checked: false } | ||
| }, | ||
| { | ||
| name: 'Windows', | ||
| id: 'os-windows', | ||
| checkProps: { checked: false } | ||
| } | ||
| ] | ||
| } | ||
| ]; | ||
|
|
||
| const defaultProps: DataViewTreeFilterProps = { | ||
| filterId: 'test-tree-filter', | ||
| title: 'Test Tree Filter', | ||
| value: ['Linux'], | ||
| items: treeItems | ||
| }; | ||
|
|
||
| it('should render correctly', () => { | ||
| const { container } = render( | ||
| <DataViewToolbar filters={<DataViewTreeFilter {...defaultProps} />} /> | ||
| ); | ||
| expect(container).toMatchSnapshot(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary to change? Should it also be checking the titles of the filter items as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right i have reverted the change