@@ -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' ;
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,57 @@ export const ColumnsSelector = ({ children }: ColumnsSelectorProps) => {
5358 } ;
5459 } , [ elementId , container ] ) ;
5560
61+ const [ columnFilter , setColumnFilter ] = React . useState <
62+ string | undefined
63+ > ( ) ;
64+
5665 if ( ! container ) return null ;
5766
5867 const childrenArray = Children . toArray ( children ) ;
5968 const paddedColumnRanks = padRanks ( columnRanks ?? [ ] , childrenArray . length ) ;
69+ const shouldDisplaySearchInput = childrenArray . length > 5 ;
6070
6171 return createPortal (
6272 < >
73+ { shouldDisplaySearchInput ? (
74+ < ResettableTextField
75+ hiddenLabel
76+ label = ""
77+ value = { columnFilter }
78+ onChange = { e => {
79+ if ( typeof e === 'string' ) {
80+ setColumnFilter ( e ) ;
81+ return ;
82+ }
83+ setColumnFilter ( e . target . value ) ;
84+ } }
85+ placeholder = { translate ( 'ra.action.search_columns' , {
86+ _ : 'Search columns' ,
87+ } ) }
88+ InputProps = { {
89+ endAdornment : (
90+ < InputAdornment position = "end" >
91+ < SearchIcon color = "disabled" />
92+ </ InputAdornment >
93+ ) ,
94+ } }
95+ resettable
96+ autoFocus
97+ size = "small"
98+ sx = { { mb : 1 } }
99+ />
100+ ) : null }
63101 { paddedColumnRanks . map ( ( position , index ) => (
64102 < DataTableColumnRankContext . Provider
65103 value = { position }
66104 key = { index }
67105 >
68- { childrenArray [ position ] }
106+ < DataTableColumnFilterContext . Provider
107+ value = { columnFilter }
108+ key = { index }
109+ >
110+ { childrenArray [ position ] }
111+ </ DataTableColumnFilterContext . Provider >
69112 </ DataTableColumnRankContext . Provider >
70113 ) ) }
71114 < Box
0 commit comments