1
- import React , { useMemo , useEffect , useState } from 'react' ;
1
+ import React , { useMemo , useEffect , useState , useCallback } from 'react' ;
2
2
import DataTable , {
3
3
TableContainer ,
4
4
Table ,
@@ -74,11 +74,24 @@ const ActiveVisitsTable = (props) => {
74
74
const pageSizes = config ?. activeVisits ?. pageSizes ?? [ 10 , 20 , 50 ] ;
75
75
const [ loading , setLoading ] = useState ( true ) ;
76
76
const [ activeVisits , setActiveVisits ] = useState < ActiveVisitRow [ ] > ( [ ] ) ;
77
- const { goTo, currentPage } = usePagination ( activeVisits , currentPageSize ) ;
78
- const [ lowerBound , upperBound ] = useMemo (
79
- ( ) => [ ( currentPage - 1 ) * currentPageSize , ( currentPage + 0 ) * currentPageSize ] ,
80
- [ currentPage , currentPageSize ] ,
81
- ) ;
77
+ const [ searchString , setSearchString ] = useState ( '' ) ;
78
+
79
+ const searchResults = useMemo ( ( ) => {
80
+ if ( searchString && searchString . trim ( ) !== '' ) {
81
+ const search = searchString . toLowerCase ( ) ;
82
+ return activeVisits . filter ( ( activeVisitRow ) =>
83
+ Object . keys ( activeVisitRow ) . some ( ( header ) => {
84
+ if ( header === 'patientUuid' ) {
85
+ return false ;
86
+ }
87
+ return `${ activeVisitRow [ header ] } ` . toLowerCase ( ) . includes ( search ) ;
88
+ } ) ,
89
+ ) ;
90
+ } else {
91
+ return activeVisits ;
92
+ }
93
+ } , [ searchString , activeVisits ] ) ;
94
+ const { goTo, currentPage, results } = usePagination ( searchResults , currentPageSize ) ;
82
95
83
96
useEffect ( ( ) => {
84
97
const activeVisits = fetchActiveVisits ( ) . subscribe ( ( data ) => {
@@ -98,20 +111,22 @@ const ActiveVisitsTable = (props) => {
98
111
return ( ) => activeVisits . unsubscribe ( ) ;
99
112
} , [ ] ) ;
100
113
114
+ const handleSearch = useCallback ( ( e ) => setSearchString ( e . target . value ) , [ ] ) ;
115
+
101
116
return ! loading ? (
102
117
< div className = { styles . activeVisitsContainer } >
103
118
< div className = { styles . activeVisitsDetailHeaderContainer } >
104
119
< h4 className = { styles . productiveHeading02 } > { t ( 'activeVisits' , 'Active Visits' ) } </ h4 >
105
120
</ div >
106
- < DataTable rows = { activeVisits } headers = { headerData } isSortable >
107
- { ( { rows, headers, getHeaderProps, onInputChange , getTableProps, getBatchActionProps } ) => (
121
+ < DataTable rows = { results } headers = { headerData } isSortable >
122
+ { ( { rows, headers, getHeaderProps, getTableProps, getBatchActionProps } ) => (
108
123
< TableContainer title = "" className = { styles . tableContainer } >
109
124
< TableToolbar >
110
125
< TableToolbarContent >
111
126
< Search
112
127
tabIndex = { getBatchActionProps ( ) . shouldShowBatchActions ? - 1 : 0 }
113
- placeHolderText = "Filter table"
114
- onChange = { onInputChange }
128
+ placeholder = "Filter table"
129
+ onChange = { handleSearch }
115
130
/>
116
131
</ TableToolbarContent >
117
132
</ TableToolbar >
@@ -124,12 +139,12 @@ const ActiveVisitsTable = (props) => {
124
139
</ TableRow >
125
140
</ TableHead >
126
141
< TableBody >
127
- { rows . slice ( lowerBound , upperBound ) . map ( ( row ) => (
142
+ { rows . map ( ( row , ind ) => (
128
143
< TableRow key = { row . id } style = { { height : desktopView ? '2rem' : '3rem' } } >
129
- { row . cells . map ( ( cell , ind ) => (
144
+ { row . cells . map ( ( cell ) => (
130
145
< TableCell key = { cell . id } >
131
146
{ cell . info . header === 'name' ? (
132
- < ConfigurableLink to = { `\${openmrsSpaBase}/patient/${ activeVisits [ ind ] . patientUuid } /chart/` } >
147
+ < ConfigurableLink to = { `\${openmrsSpaBase}/patient/${ results [ ind ] ? .patientUuid } /chart/` } >
133
148
{ cell . value }
134
149
</ ConfigurableLink >
135
150
) : (
@@ -149,10 +164,13 @@ const ActiveVisitsTable = (props) => {
149
164
</ p >
150
165
) }
151
166
< Pagination
167
+ forwardText = ""
168
+ backwardText = ""
152
169
page = { currentPage }
153
170
pageSize = { currentPageSize }
154
171
pageSizes = { pageSizes }
155
- totalItems = { rows . length }
172
+ totalItems = { searchResults . length }
173
+ className = { styles . pagination }
156
174
onChange = { ( { pageSize, page } ) => {
157
175
if ( pageSize !== currentPageSize ) {
158
176
setPageSize ( pageSize ) ;
@@ -167,7 +185,7 @@ const ActiveVisitsTable = (props) => {
167
185
</ DataTable >
168
186
</ div >
169
187
) : (
170
- < DataTableSkeleton className = { styles . tableSkeleton } />
188
+ < DataTableSkeleton />
171
189
) ;
172
190
} ;
173
191
0 commit comments