@@ -38,8 +38,6 @@ export default function ListViewAdvanced(props) {
38
38
39
39
const apiRef = useGridApiRef ( ) ;
40
40
41
- const [ rows , setRows ] = React . useState ( INITIAL_ROWS ) ;
42
-
43
41
const [ loading , setLoading ] = React . useState ( false ) ;
44
42
45
43
const [ overlayState , setOverlayState ] = React . useState ( {
@@ -51,65 +49,67 @@ export default function ListViewAdvanced(props) {
51
49
setOverlayState ( { overlay : null , params : null } ) ;
52
50
} ;
53
51
54
- const handleDelete = React . useCallback ( ( ids ) => {
55
- setRows ( ( prevRows ) => prevRows . filter ( ( row ) => ! ids . includes ( row . id ) ) ) ;
56
- } , [ ] ) ;
52
+ const handleDelete = React . useCallback (
53
+ ( ids ) => {
54
+ apiRef . current . updateRows ( ids . map ( ( id ) => ( { id, _action : 'delete' } ) ) ) ;
55
+ } ,
56
+ [ apiRef ] ,
57
+ ) ;
57
58
58
- const handleUpdate = React . useCallback ( ( id , field , value ) => {
59
- setRows ( ( prevRows ) =>
60
- prevRows . map ( ( row ) =>
61
- row . id === id
62
- ? { ...row , [ field ] : value , updatedAt : new Date ( ) . toISOString ( ) }
63
- : row ,
64
- ) ,
65
- ) ;
66
- } , [ ] ) ;
59
+ const handleUpdate = React . useCallback (
60
+ ( id , field , value ) => {
61
+ const updatedAt = new Date ( ) . toISOString ( ) ;
62
+ apiRef . current . updateRows ( [ { id, [ field ] : value , updatedAt } ] ) ;
63
+ } ,
64
+ [ apiRef ] ,
65
+ ) ;
67
66
68
- const handleUpload = React . useCallback ( ( event ) => {
69
- if ( ! event . target . files ) {
70
- return ;
71
- }
67
+ const handleUpload = React . useCallback (
68
+ ( event ) => {
69
+ if ( ! event . target . files ) {
70
+ return ;
71
+ }
72
72
73
- const file = event . target . files [ 0 ] ;
74
- const createdAt = new Date ( ) . toISOString ( ) ;
73
+ const file = event . target . files [ 0 ] ;
74
+ const createdAt = new Date ( ) . toISOString ( ) ;
75
75
76
- const fileType = file . type . split ( '/' ) [ 1 ] ;
76
+ const fileType = file . type . split ( '/' ) [ 1 ] ;
77
77
78
- // validate file type
79
- if ( ! FILE_TYPES . includes ( fileType ) ) {
80
- alert ( 'Invalid file type' ) ;
81
- return ;
82
- }
78
+ // validate file type
79
+ if ( ! FILE_TYPES . includes ( fileType ) ) {
80
+ alert ( 'Invalid file type' ) ;
81
+ return ;
82
+ }
83
83
84
- const row = {
85
- id : randomId ( ) ,
86
- name : file . name ,
87
- description : '' ,
88
- type : fileType ,
89
- size : file . size ,
90
- createdBy : 'Kenan Yusuf' ,
91
- createdAt,
92
- updatedAt : createdAt ,
93
- state : 'pending' ,
94
- } ;
84
+ const row = {
85
+ id : randomId ( ) ,
86
+ name : file . name ,
87
+ description : '' ,
88
+ type : fileType ,
89
+ size : file . size ,
90
+ createdBy : 'Kenan Yusuf' ,
91
+ createdAt,
92
+ updatedAt : createdAt ,
93
+ state : 'pending' ,
94
+ } ;
95
95
96
- event . target . value = '' ;
96
+ event . target . value = '' ;
97
97
98
- // Add temporary row
99
- setLoading ( true ) ;
100
- setRows ( ( prevRows ) => [ ... prevRows , row ] ) ;
98
+ // Add temporary row
99
+ setLoading ( true ) ;
100
+ apiRef . current . updateRows ( [ row ] ) ;
101
101
102
- // Simulate server response time
103
- const timeout = Math . floor ( Math . random ( ) * 3000 ) + 2000 ;
104
- setTimeout ( ( ) => {
105
- const uploadedRow = { ...row , state : 'uploaded' } ;
106
- setRows ( ( prevRows ) =>
107
- prevRows . map ( ( r ) => ( r . id === row . id ? uploadedRow : r ) ) ,
108
- ) ;
109
- setOverlayState ( { overlay : 'actions' , params : { row } } ) ;
110
- setLoading ( false ) ;
111
- } , timeout ) ;
112
- } , [ ] ) ;
102
+ // Simulate server response time
103
+ const timeout = Math . floor ( Math . random ( ) * 3000 ) + 2000 ;
104
+ setTimeout ( ( ) => {
105
+ const uploadedRow = { ...row , state : 'uploaded' } ;
106
+ apiRef . current . updateRows ( [ uploadedRow ] ) ;
107
+ setOverlayState ( { overlay : 'actions' , params : { row } } ) ;
108
+ setLoading ( false ) ;
109
+ } , timeout ) ;
110
+ } ,
111
+ [ apiRef ] ,
112
+ ) ;
113
113
114
114
const columns = React . useMemo (
115
115
( ) => [
@@ -266,7 +266,7 @@ export default function ListViewAdvanced(props) {
266
266
< div ref = { containerRef } style = { { maxWidth : '100%' } } >
267
267
< DataGridPremium
268
268
apiRef = { apiRef }
269
- rows = { rows }
269
+ rows = { INITIAL_ROWS }
270
270
columns = { columns }
271
271
loading = { loading }
272
272
slots = { { toolbar : Toolbar } }
0 commit comments