@@ -5,7 +5,7 @@ import CPagination from '../pagination/CPagination'
5
5
import CElementCover from '../element-cover/CElementCover'
6
6
import style from './CDataTable.module.css'
7
7
import CIcon from '@coreui/icons-react'
8
- import { cilArrowTop , cilBan } from '@coreui/icons'
8
+ import { cilArrowTop , cilBan , cilFilterX } from '@coreui/icons'
9
9
10
10
//component - CoreUI / CTable
11
11
const CDataTable = props => {
@@ -37,6 +37,7 @@ const CDataTable = props => {
37
37
columnFilter,
38
38
tableFilterValue,
39
39
tableFilter,
40
+ cleaner,
40
41
addTableClasses,
41
42
size,
42
43
dark,
@@ -185,6 +186,15 @@ const CDataTable = props => {
185
186
}
186
187
}
187
188
189
+ const clean = ( ) => {
190
+ setTableFilterState ( '' )
191
+ setColumnFilterState ( { } )
192
+ setSorterState ( {
193
+ column : "" ,
194
+ asc : true
195
+ } )
196
+ }
197
+
188
198
// computed
189
199
190
200
const genCols = Object . keys ( passedItems [ 0 ] || { } ) . filter ( el => el . charAt ( 0 ) !== '_' )
@@ -322,6 +332,16 @@ const CDataTable = props => {
322
332
return customValues . noItems || 'No items'
323
333
} ) ( )
324
334
335
+ const isFiltered = tableFilterState || sorterState . column ||
336
+ Object . values ( columnFilterState ) . join ( '' )
337
+
338
+ const cleanerProps = {
339
+ content : cilFilterX ,
340
+ className : `ml-2 ${ isFiltered ? 'text-danger' : 'transparent' } ` ,
341
+ role : isFiltered ? 'button' : null ,
342
+ tabIndex : isFiltered ? 0 : null ,
343
+ }
344
+
325
345
// watch
326
346
useMemo ( ( ) => setPerPageItems ( itemsPerPage ) , [ itemsPerPage ] )
327
347
@@ -384,26 +404,41 @@ const CDataTable = props => {
384
404
< React . Fragment >
385
405
< div ref = { innerRef } >
386
406
{
387
- ( itemsPerPageSelect || tableFilter ) &&
407
+ ( itemsPerPageSelect || tableFilter || cleaner ) &&
388
408
< div className = "row my-2 mx-0" >
389
409
{
390
- tableFilter &&
410
+ ( tableFilter || cleaner ) &&
391
411
< div className = "col-sm-6 form-inline p-0" >
392
- < label className = "mr-2" > { tableFilterData . label } </ label >
393
- < input
394
- className = "form-control"
395
- type = "text"
396
- placeholder = { tableFilterData . placeholder }
397
- onInput = { ( e ) => { tableFilterChange ( e . target . value , 'input' ) } }
398
- onChange = { ( e ) => { tableFilterChange ( e . target . value , 'change' ) } }
399
- value = { tableFilterState || '' }
400
- aria-label = "table filter input"
401
- />
412
+ {
413
+ tableFilter &&
414
+ < >
415
+ < label className = "mr-2" > { tableFilterData . label } </ label >
416
+ < input
417
+ className = "form-control"
418
+ type = "text"
419
+ placeholder = { tableFilterData . placeholder }
420
+ onInput = { ( e ) => { tableFilterChange ( e . target . value , 'input' ) } }
421
+ onChange = { ( e ) => { tableFilterChange ( e . target . value , 'change' ) } }
422
+ value = { tableFilterState || '' }
423
+ aria-label = "table filter input"
424
+ />
425
+ </ >
426
+ }
427
+ {
428
+ cleaner && (
429
+ typeof cleaner === 'function' ? cleaner ( clean , isFiltered , cleanerProps ) :
430
+ < CIcon
431
+ { ...cleanerProps }
432
+ onClick = { clean }
433
+ />
434
+ )
435
+ }
436
+
402
437
</ div >
403
438
}
404
439
{
405
440
itemsPerPageSelect &&
406
- < div className = { 'col-sm-6 p-0' + ( ! tableFilter && 'offset-sm-6' ) } >
441
+ < div className = { 'col-sm-6 p-0' + ( ! ( tableFilter || cleaner ) && 'offset-sm-6' ) } >
407
442
< div className = "form-inline justify-content-sm-end" >
408
443
< label className = "mr-2" > { paginationSelect . label } </ label >
409
444
< select
@@ -585,6 +620,7 @@ CDataTable.propTypes = {
585
620
columnFilter : PropTypes . oneOfType ( [ PropTypes . bool , PropTypes . object ] ) ,
586
621
tableFilterValue : PropTypes . string ,
587
622
tableFilter : PropTypes . oneOfType ( [ PropTypes . bool , PropTypes . object ] ) ,
623
+ cleaner : PropTypes . oneOfType ( [ PropTypes . bool , PropTypes . func ] ) ,
588
624
addTableClasses : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . array , PropTypes . object ] ) ,
589
625
size : PropTypes . string ,
590
626
dark : PropTypes . bool ,
0 commit comments