@@ -5,8 +5,9 @@ import { useDataViewFilters, useDataViewPagination } from '@patternfly/react-dat
55import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView' ;
66import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable' ;
77import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar' ;
8- import { DataViewFilters } from '@patternfly/react-data-view/dist/dynamic/DataViewFilters' ;
8+ import { DataViewFilterOption , DataViewFilters } from '@patternfly/react-data-view/dist/dynamic/DataViewFilters' ;
99import { DataViewTextFilter } from '@patternfly/react-data-view/dist/dynamic/DataViewTextFilter' ;
10+ import { DataViewCheckboxFilter } from '@patternfly/react-data-view/dist/dynamic/DataViewCheckboxFilter' ;
1011
1112const perPageOptions = [
1213 { title : '5' , value : 5 } ,
@@ -17,38 +18,51 @@ interface Repository {
1718 name : string ;
1819 branch : string | null ;
1920 prs : string | null ;
20- workspaces : string ;
21+ workspace : string ;
2122 lastCommit : string ;
2223}
2324
2425interface RepositoryFilters {
2526 name : string ,
26- branch : string
27+ branch : string ,
28+ workspace : string [ ]
2729}
2830
2931const repositories : Repository [ ] = [
30- { name : 'Repository one' , branch : 'Branch one' , prs : 'Pull request one' , workspaces : 'Workspace one' , lastCommit : 'Timestamp one' } ,
31- { name : 'Repository two' , branch : 'Branch two' , prs : 'Pull request two' , workspaces : 'Workspace two' , lastCommit : 'Timestamp two' } ,
32- { name : 'Repository three' , branch : 'Branch three' , prs : 'Pull request three' , workspaces : 'Workspace three ' , lastCommit : 'Timestamp three' } ,
33- { name : 'Repository four' , branch : 'Branch four' , prs : 'Pull request four' , workspaces : 'Workspace four ' , lastCommit : 'Timestamp four' } ,
34- { name : 'Repository five' , branch : 'Branch five' , prs : 'Pull request five' , workspaces : 'Workspace five ' , lastCommit : 'Timestamp five' } ,
35- { name : 'Repository six' , branch : 'Branch six' , prs : 'Pull request six' , workspaces : 'Workspace six ' , lastCommit : 'Timestamp six' }
32+ { name : 'Repository one' , branch : 'Branch one' , prs : 'Pull request one' , workspace : 'Workspace one' , lastCommit : 'Timestamp one' } ,
33+ { name : 'Repository two' , branch : 'Branch two' , prs : 'Pull request two' , workspace : 'Workspace two' , lastCommit : 'Timestamp two' } ,
34+ { name : 'Repository three' , branch : 'Branch three' , prs : 'Pull request three' , workspace : 'Workspace one ' , lastCommit : 'Timestamp three' } ,
35+ { name : 'Repository four' , branch : 'Branch four' , prs : 'Pull request four' , workspace : 'Workspace one ' , lastCommit : 'Timestamp four' } ,
36+ { name : 'Repository five' , branch : 'Branch five' , prs : 'Pull request five' , workspace : 'Workspace two ' , lastCommit : 'Timestamp five' } ,
37+ { name : 'Repository six' , branch : 'Branch six' , prs : 'Pull request six' , workspace : 'Workspace three ' , lastCommit : 'Timestamp six' }
3638] ;
3739
38- const columns = [ 'Name' , 'Branch' , 'Pull requests' , 'Workspaces' , 'Last commit' ] ;
40+ const filterOptions : DataViewFilterOption [ ] = [
41+ { label : 'Workspace one' , value : 'workspace-one' } ,
42+ { label : 'Workspace two' , value : 'workspace-two' } ,
43+ { label : 'Workspace three' , value : 'workspace-three' }
44+ ] ;
45+
46+ const columns = [ 'Name' , 'Branch' , 'Pull requests' , 'Workspace' , 'Last commit' ] ;
3947
4048const ouiaId = 'LayoutExample' ;
4149
4250const MyTable : React . FunctionComponent = ( ) => {
4351 const [ searchParams , setSearchParams ] = useSearchParams ( ) ;
52+ const { filters, onSetFilters, clearAllFilters } = useDataViewFilters < RepositoryFilters > ( { initialFilters : { name : '' , branch : '' , workspace : [ ] } , searchParams, setSearchParams } ) ;
4453 const pagination = useDataViewPagination ( { perPage : 5 } ) ;
4554 const { page, perPage } = pagination ;
46- const { filters, onSetFilters, clearAllFilters } = useDataViewFilters < RepositoryFilters > ( { initialFilters : { name : '' , branch : '' } , searchParams, setSearchParams } ) ;
4755
48- const pageRows = useMemo ( ( ) => repositories
49- . filter ( item => ( ! filters . name || item . name ?. toLocaleLowerCase ( ) . includes ( filters . name ?. toLocaleLowerCase ( ) ) ) && ( ! filters . branch || item . branch ?. toLocaleLowerCase ( ) . includes ( filters . branch ?. toLocaleLowerCase ( ) ) ) )
56+ const filteredData = useMemo ( ( ) => repositories . filter ( item =>
57+ ( ! filters . name || item . name ?. toLocaleLowerCase ( ) . includes ( filters . name ?. toLocaleLowerCase ( ) ) ) &&
58+ ( ! filters . branch || item . branch ?. toLocaleLowerCase ( ) . includes ( filters . branch ?. toLocaleLowerCase ( ) ) ) &&
59+ ( ! filters . workspace || filters . workspace . length === 0 || filters . workspace . includes ( String ( filterOptions . find ( option => option . label === item . workspace ) ?. value ) ) )
60+ ) , [ filters ] ) ;
61+
62+ const pageRows = useMemo ( ( ) => filteredData
5063 . slice ( ( page - 1 ) * perPage , ( ( page - 1 ) * perPage ) + perPage )
51- . map ( item => Object . values ( item ) ) , [ page , perPage , filters ] ) ;
64+ . map ( item => Object . values ( item ) ) ,
65+ [ page , perPage , filteredData ] ) ;
5266
5367 return (
5468 < DataView >
@@ -66,6 +80,7 @@ const MyTable: React.FunctionComponent = () => {
6680 < DataViewFilters onChange = { ( _e , values ) => onSetFilters ( values ) } values = { filters } >
6781 < DataViewTextFilter filterId = "name" title = 'Name' placeholder = 'Filter by name' />
6882 < DataViewTextFilter filterId = "branch" title = 'Branch' placeholder = 'Filter by branch' />
83+ < DataViewCheckboxFilter filterId = "workspace" title = 'Workspace' placeholder = 'Filter by workspace' options = { filterOptions } />
6984 </ DataViewFilters >
7085 }
7186 />
@@ -76,7 +91,7 @@ const MyTable: React.FunctionComponent = () => {
7691 < Pagination
7792 isCompact
7893 perPageOptions = { perPageOptions }
79- itemCount = { repositories . length }
94+ itemCount = { filteredData . length }
8095 { ...pagination }
8196 />
8297 }
0 commit comments