@@ -22,17 +22,18 @@ export interface ResponsiveDataTableProps {
2222 updateCols ?: ( ( columns : Column [ ] ) => void ) | undefined ;
2323 columnVisibility : Record < string , boolean > | undefined ;
2424 theme ?: object ;
25+ colViews ?: Record < string , boolean > | undefined ;
2526}
2627
2728const ResponsiveDataTable = ( {
2829 data,
2930 columns,
3031 options = { } ,
32+ tableCols,
33+ updateCols,
34+ columnVisibility,
3135 ...props
3236} : ResponsiveDataTableProps ) : JSX . Element => {
33- const [ tableCols , updateCols ] = React . useState < Column [ ] > ( columns ) ;
34- const [ columnVisibility , , ] = React . useState < Record < string , boolean > > ( { } ) ;
35-
3637 const formatDate = ( date : Date ) : string => {
3738 const dateOptions : Intl . DateTimeFormatOptions = {
3839 weekday : 'short' ,
@@ -51,16 +52,20 @@ const ResponsiveDataTable = ({
5152 case 'add' : {
5253 const colToAdd = columns . find ( ( obj ) => obj . name === column ) ;
5354 if ( colToAdd ) {
54- colToAdd . options ! . display = true ;
55- updateCols ( [ ...columns ] ) ;
55+ if ( colToAdd . options ) {
56+ colToAdd . options . display = true ;
57+ updateCols && updateCols ( [ ...columns ] ) ;
58+ }
5659 }
5760 break ;
5861 }
5962 case 'remove' : {
6063 const colToRemove = columns . find ( ( obj ) => obj . name === column ) ;
6164 if ( colToRemove ) {
62- colToRemove . options ! . display = false ;
63- updateCols ( [ ...columns ] ) ;
65+ if ( colToRemove . options ) {
66+ colToRemove . options . display = false ;
67+ updateCols && updateCols ( [ ...columns ] ) ;
68+ }
6469 }
6570 break ;
6671 }
@@ -75,7 +80,7 @@ const ResponsiveDataTable = ({
7580 if ( ! col . options ) {
7681 col . options = { } ;
7782 }
78- col . options . display = columnVisibility [ col . name ] ;
83+ col . options . display = columnVisibility && columnVisibility [ col . name ] ;
7984
8085 if (
8186 [ 'updated_at' , 'created_at' , 'deleted_at' , 'last_login_time' , 'joined_at' ] . includes (
@@ -103,7 +108,7 @@ const ResponsiveDataTable = ({
103108 }
104109 }
105110 } ) ;
106- updateCols ( [ ...columns ] ) ;
111+ updateCols && updateCols ( [ ...columns ] ) ;
107112 } , [ columnVisibility ] ) ;
108113
109114 const components = {
@@ -112,13 +117,11 @@ const ResponsiveDataTable = ({
112117
113118 return (
114119 < MUIDataTable
115- columns = { columns || [ ] }
120+ columns = { tableCols ?? [ ] }
116121 data = { data || [ ] }
117122 title = { undefined }
118123 components = { components }
119124 options = { updatedOptions }
120- tableCols = { tableCols }
121- updateCols = { updateCols }
122125 { ...props }
123126 />
124127 ) ;
0 commit comments