@@ -99,6 +99,31 @@ export function filterData(
9999 return resultData ;
100100}
101101
102+ export function applyHeaderFilters (
103+ data : Array < RecordType > ,
104+ headerFilters : Record < string , any [ ] >
105+ ) {
106+ if ( ! headerFilters || Object . keys ( headerFilters ) . length === 0 ) {
107+ return data ;
108+ }
109+
110+ return data . filter ( ( row ) =>
111+ Object . entries ( headerFilters ) . every ( ( [ columnKey , filterValues ] ) => {
112+ if ( ! Array . isArray ( filterValues ) || filterValues . length === 0 ) {
113+ return true ;
114+ }
115+
116+ const cellValue = row [ columnKey ] ;
117+ return filterValues . some ( ( filterValue ) => {
118+ if ( cellValue == null ) {
119+ return filterValue == null ;
120+ }
121+ return String ( cellValue ) === String ( filterValue ) ;
122+ } ) ;
123+ } )
124+ ) ;
125+ }
126+
102127export function sortData (
103128 data : Array < JSONObject > ,
104129 columns : Record < string , { sortable : boolean } > , // key: dataIndex
@@ -291,6 +316,14 @@ export function getColumnsAggr(
291316 . uniqBy ( "text" )
292317 . value ( ) ;
293318 }
319+
320+ res . uniqueValues = _ ( oriDisplayData )
321+ . map ( ( row ) => row [ dataIndex ] )
322+ . filter ( ( value ) : value is JSONValue => value !== undefined && value !== null && value !== "" )
323+ . uniqWith ( _ . isEqual )
324+ . slice ( 0 , 100 )
325+ . value ( ) ;
326+
294327 return res ;
295328 } ) ;
296329}
@@ -338,6 +371,27 @@ export type CustomColumnType<RecordType> = ColumnType<RecordType> & {
338371 columnDataTestId ?: string ;
339372} ;
340373
374+ function buildHeaderFilterProps (
375+ dataIndex : string ,
376+ filterable : boolean ,
377+ uniqueValues : any [ ] ,
378+ headerFilters : Record < string , any [ ] > = { }
379+ ) {
380+ if ( ! filterable || uniqueValues . length === 0 ) {
381+ return { } ;
382+ }
383+
384+ return {
385+ filters : uniqueValues . map ( ( value ) => ( {
386+ text : String ( value ) ,
387+ value,
388+ } ) ) ,
389+ filteredValue : headerFilters [ dataIndex ] ?? null ,
390+ filterSearch : true ,
391+ filterMultiple : true ,
392+ } as const ;
393+ }
394+
341395/**
342396 * convert column in raw format into antd format
343397 */
@@ -351,6 +405,7 @@ export function columnsToAntdFormat(
351405 columnsAggrData : ColumnsAggrData ,
352406 editMode : string ,
353407 onTableEvent : ( eventName : any ) => void ,
408+ headerFilters : Record < string , any [ ] > = { } ,
354409) : Array < CustomColumnType < RecordType > > {
355410 const customColumns = columns . filter ( col => col . isCustom ) . map ( col => col . dataIndex ) ;
356411 const initialColumns = getInitialColumns ( columnsAggrData , customColumns ) ;
@@ -393,10 +448,18 @@ export function columnsToAntdFormat(
393448 text : string ;
394449 status : StatusType ;
395450 } [ ] ;
451+ const uniqueValues = ( ( columnsAggrData [ column . dataIndex ] ?? { } ) . uniqueValues ?? [ ] ) as any [ ] ;
452+ const columnKey = column . dataIndex || `custom-${ mIndex } ` ;
396453 const title = renderTitle ( { title : column . title , tooltip : column . titleTooltip , editable : column . editable } ) ;
454+ const filterProps = buildHeaderFilterProps (
455+ column . dataIndex ,
456+ column . filterable ,
457+ uniqueValues ,
458+ headerFilters
459+ ) ;
397460
398461 return {
399- key : ` ${ column . dataIndex } - ${ mIndex } ` ,
462+ key : columnKey ,
400463 title : column . showTitle ? title : '' ,
401464 titleText : column . title ,
402465 dataIndex : column . dataIndex ,
@@ -468,6 +531,7 @@ export function columnsToAntdFormat(
468531 showSorterTooltip : false ,
469532 }
470533 : { } ) ,
534+ ...filterProps ,
471535 } ;
472536 } ) ;
473537}
@@ -504,6 +568,16 @@ export function onTableChange(
504568 dispatch ( changeChildAction ( "sort" , sortValues , true ) ) ;
505569 onEvent ( "sortChange" ) ;
506570 }
571+
572+ if ( extra . action === "filter" ) {
573+ const headerFilters = _ ( filters )
574+ . pickBy ( ( filterValues ) => Array . isArray ( filterValues ) && filterValues . length > 0 )
575+ . mapValues ( ( filterValues ) => filterValues as any [ ] )
576+ . value ( ) ;
577+
578+ dispatch ( changeChildAction ( "headerFilters" , headerFilters , true ) ) ;
579+ onEvent ( "filterChange" ) ;
580+ }
507581}
508582
509583export function calcColumnWidth ( columnKey : string , data : Array < JSONObject > ) {
0 commit comments