@@ -5,17 +5,22 @@ import {
55 useStore ,
66 DataTableColumnRankContext ,
77 useDataTableStoreContext ,
8+ useTranslate ,
9+ DataTableColumnFilterContext ,
810} from 'ra-core' ;
9- import { Box } from '@mui/material' ;
11+ import { Box , InputAdornment } from '@mui/material' ;
12+ import SearchIcon from '@mui/icons-material/Search' ;
1013
1114import { Button } from '../../button' ;
15+ import { ResettableTextField } from '../../input/ResettableTextField' ;
1216
1317/**
1418 * Render DataTable.Col elements in the ColumnsButton selector using a React Portal.
1519 *
1620 * @see ColumnsButton
1721 */
1822export const ColumnsSelector = ( { children } : ColumnsSelectorProps ) => {
23+ const translate = useTranslate ( ) ;
1924 const { storeKey, defaultHiddenColumns } = useDataTableStoreContext ( ) ;
2025 const [ columnRanks , setColumnRanks ] = useStore < number [ ] | undefined > (
2126 `${ storeKey } _columnRanks`
@@ -53,19 +58,55 @@ export const ColumnsSelector = ({ children }: ColumnsSelectorProps) => {
5358 } ;
5459 } , [ elementId , container ] ) ;
5560
61+ const [ columnFilter , setColumnFilter ] = React . useState < string > ( '' ) ;
62+
5663 if ( ! container ) return null ;
5764
5865 const childrenArray = Children . toArray ( children ) ;
5966 const paddedColumnRanks = padRanks ( columnRanks ?? [ ] , childrenArray . length ) ;
67+ const shouldDisplaySearchInput = childrenArray . length > 5 ;
6068
6169 return createPortal (
6270 < >
71+ { shouldDisplaySearchInput ? (
72+ < ResettableTextField
73+ hiddenLabel
74+ label = ""
75+ value = { columnFilter }
76+ onChange = { e => {
77+ if ( typeof e === 'string' ) {
78+ setColumnFilter ( e ) ;
79+ return ;
80+ }
81+ setColumnFilter ( e . target . value ) ;
82+ } }
83+ placeholder = { translate ( 'ra.action.search_columns' , {
84+ _ : 'Search columns' ,
85+ } ) }
86+ InputProps = { {
87+ endAdornment : (
88+ < InputAdornment position = "end" >
89+ < SearchIcon color = "disabled" />
90+ </ InputAdornment >
91+ ) ,
92+ } }
93+ resettable
94+ autoFocus
95+ size = "small"
96+ sx = { { mb : 1 } }
97+ />
98+ ) : null }
6399 { paddedColumnRanks . map ( ( position , index ) => (
64100 < DataTableColumnRankContext . Provider
65101 value = { position }
66102 key = { index }
67103 >
68- { childrenArray [ position ] }
104+ < DataTableColumnFilterContext . Provider
105+ value = { columnFilter }
106+ key = { index }
107+ >
108+ { childrenArray [ position ] }
109+ </ DataTableColumnFilterContext . Provider >
69110 </ DataTableColumnRankContext . Provider >
70111 ) ) }
71112 < Box
0 commit comments