@@ -12,10 +12,10 @@ import {
1212import { Button } from "@progress/kendo-react-buttons" ;
1313import { GridPDFExport } from "@progress/kendo-react-pdf" ;
1414import { ExcelExport } from "@progress/kendo-react-excel-export" ;
15- import { process } from "@progress/kendo-data-query" ;
1615import { Input } from "@progress/kendo-react-inputs" ;
1716import { useLocalization } from "@progress/kendo-react-intl" ;
1817import { filePdfIcon , fileExcelIcon } from "@progress/kendo-svg-icons" ;
18+ import { filterBy } from '@progress/kendo-data-query' ;
1919const noMessage = "message not defined" ;
2020
2121export const Column = GridColumn ;
@@ -30,40 +30,17 @@ export const ColumnMenu = (props) => {
3030} ;
3131
3232export const Grid = ( props ) => {
33- const { data, onDataChange, ...others } = props ;
33+ const { data, onDataChange, defaultFilter , ...others } = props ;
3434
3535 const excelExportRef = React . useRef ( null ) ;
3636 const pdfExportRef = React . useRef ( null ) ;
3737
3838 const [ isPdfExporting , setIsPdfExporting ] = React . useState ( false ) ;
39- const [ take , setTake ] = React . useState ( 10 ) ;
40- const [ skip , setSkip ] = React . useState ( 0 ) ;
41- const [ sort , setSort ] = React . useState ( [ ] ) ;
42- const [ group , setGroup ] = React . useState ( [ ] ) ;
43- const [ filter , setFilter ] = React . useState ( null ) ;
44- const lastSelectedIndexRef = React . useRef ( 0 ) ;
45- const [ allColumnFilter , setAllColumnFilter ] = React . useState ( "" ) ;
39+ const [ filter , setFilter ] = React . useState ( defaultFilter ) ;
40+ const filteredData = filterBy ( data , filter ) ;
41+ const [ select , setSelect ] = React . useState ( { } ) ;
4642 const localizationService = useLocalization ( ) ;
4743
48- const dataState = {
49- take,
50- skip,
51- sort,
52- group,
53- filter,
54- } ;
55-
56- const onDataStateChange = React . useCallback (
57- ( event ) => {
58- setTake ( event . dataState . take ) ;
59- setSkip ( event . dataState . skip ) ;
60- setSort ( event . dataState . sort ) ;
61- setGroup ( event . dataState . group ) ;
62- setFilter ( event . dataState . filter ) ;
63- } ,
64- [ setTake , setSkip , setSort , setGroup ]
65- ) ;
66-
6744 const onExcelExport = React . useCallback ( ( ) => {
6845 if ( excelExportRef . current ) {
6946 excelExportRef . current . save ( ) ;
@@ -74,116 +51,64 @@ export const Grid = (props) => {
7451 setIsPdfExporting ( false ) ;
7552 } , [ ] ) ;
7653
77- const onAllColumnFilterChange = React . useCallback (
78- ( event ) => {
79- setAllColumnFilter ( event . value ) ;
80- } ,
81- [ setAllColumnFilter ]
82- ) ;
83-
84- const onSelectionChange = React . useCallback (
85- ( event ) => {
86- let last = lastSelectedIndexRef . current ;
87- const updatedData = data . map ( ( dataItem ) => {
88- return { ...dataItem } ;
89- } ) ;
90- const current = data . findIndex ( ( dataItem ) => dataItem === event . dataItem ) ;
91-
92- if ( ! event . nativeEvent . shiftKey ) {
93- lastSelectedIndexRef . current = last = current ;
94- }
95-
96- if ( ! event . nativeEvent . ctrlKey ) {
97- updatedData . forEach ( ( item ) => ( item . selected = false ) ) ;
98- }
99- const select = ! event . dataItem . selected ;
100- for ( let i = Math . min ( last , current ) ; i <= Math . max ( last , current ) ; i ++ ) {
101- updatedData [ i ] . selected = select ;
102- }
103-
104- onDataChange ( updatedData ) ;
105- } ,
106- [ data , onDataChange ]
107- ) ;
54+ const onPdfExport = React . useCallback ( ( ) => {
55+ if ( pdfExportRef . current ) {
56+ setIsPdfExporting ( true ) ;
57+ pdfExportRef . current . save ( data , onPdfExportDone ) ;
58+ }
59+ } , [ data , onPdfExportDone ] ) ;
10860
109- const onHeaderSelectionChange = React . useCallback (
61+ const handleFilterChange = React . useCallback (
11062 ( event ) => {
111- const checked = event . syntheticEvent . target . checked ;
112- const updatedData = data . map ( ( item ) => {
113- return {
114- ...item ,
115- selected : checked ,
63+ let filter = {
64+ logic : 'or' ,
65+ filters : [
66+ { field : 'contactName' , operator : 'contains' , value : event . value } ,
67+ { field : 'jobTitle' , operator : 'contains' , value : event . value } ,
68+ { field : 'budget' , operator : 'contains' , value : event . value } ,
69+ { field : 'phone' , operator : 'contains' , value : event . value } ,
70+ { field : 'address' , operator : 'contains' , value : event . value }
71+ ]
11672 } ;
117- } ) ;
118-
119- onDataChange ( updatedData ) ;
73+ setFilter ( filter ) ;
12074 } ,
121- [ data , onDataChange ]
75+ [ setFilter ]
12276 ) ;
12377
124- const textColumns = props . children
125- . map ( ( col ) => {
126- if ( col . props . children ) {
127- return col . props . children . map ( ( child ) => {
128- if ( ! child . props . filter || child . props . filter === "text" ) {
129- return child . props . field ;
130- }
131- return null ;
132- } ) ;
133- } else if ( col . props . field ) {
134- if ( ! col . props . filter || col . props . filter === "text" ) {
135- return col . props . field ;
136- }
137- }
138- return null ;
139- } )
140- . flat ( )
141- . filter ( ( field ) => field ) ;
142-
143- const allColumnsFilters = textColumns . map ( ( column ) => ( {
144- field : column ,
145- operator : "contains" ,
146- value : allColumnFilter ,
147- } ) ) ;
148-
149- const allColumnFilteredData = allColumnFilter
150- ? process ( data , { filter : { logic : "or" , filters : allColumnsFilters } } )
151- . data
152- : data ;
153-
154- const processedData = process ( allColumnFilteredData , dataState ) ;
155-
156- React . useEffect ( ( ) => {
157- if ( ! processedData . data . length ) {
158- setSkip ( 0 ) ;
159- }
160- } , [ processedData ] ) ;
78+ const onSelectionChange = React . useCallback ( ( event ) => {
79+ setSelect ( event . select ) ;
80+ } , [ ] ) ;
16181
162- const onPdfExport = React . useCallback ( ( ) => {
163- if ( pdfExportRef . current ) {
164- setIsPdfExporting ( true ) ;
165- pdfExportRef . current . save ( processedData . data , onPdfExportDone ) ;
166- }
167- } , [ processedData , onPdfExportDone ] ) ;
82+ const onHeaderSelectionChange = React . useCallback ( ( event ) => {
83+ setSelect ( event . select ) ;
84+ } , [ ] ) ;
16885
16986 const GridElement = (
17087 < KendoGrid
171- { ...dataState }
17288 { ...others }
17389 rowHeight = { 40 }
17490 pageable = { true }
91+ defaultSkip = { 0 }
92+ defaultTake = { 10 }
17593 sortable = { true }
17694 groupable = { true }
177- selectedField = { "selected" }
178- data = { processedData }
179- onDataStateChange = { onDataStateChange }
95+ filter = { filter }
96+ data = { filteredData }
97+ dataItemKey = { 'id' }
98+ autoProcessData = { true }
99+ selectable = { {
100+ enabled : true ,
101+ drag : false ,
102+ cell : false ,
103+ mode : 'single'
104+ } }
105+ select = { select }
180106 onSelectionChange = { onSelectionChange }
181107 onHeaderSelectionChange = { onHeaderSelectionChange }
182108 >
183109 < GridToolbar >
184110 < Input
185- value = { allColumnFilter }
186- onChange = { onAllColumnFilterChange }
111+ onChange = { handleFilterChange }
187112 placeholder = { localizationService . toLanguageString (
188113 "custom.gridSearch" ,
189114 noMessage
@@ -203,14 +128,7 @@ export const Grid = (props) => {
203128 { localizationService . toLanguageString ( "custom.exportPdf" , noMessage ) }
204129 </ Button >
205130 </ GridToolbar >
206- < Column
207- field = { "selected" }
208- width = { 50 }
209- title = { " " }
210- headerSelectionValue = {
211- data . findIndex ( ( dataItem ) => dataItem . selected === false ) === - 1
212- }
213- />
131+ < Column columnType = "checkbox" />
214132 { props . children }
215133 </ KendoGrid >
216134 ) ;
0 commit comments