@@ -12,13 +12,14 @@ export const COL_TYPES = {
1212 // RADIO: 'RADIO',
1313 INT : 'INT' ,
1414 STRING : 'STRING' ,
15- // ICON : 'ICON '
15+ CHECK_BOX : 'CHECK_BOX '
1616}
1717
1818const TOTAL_WIDTH = 100 ; //'100%'
1919
20- class DataTable extends React . PureComponent {
20+ class DataTable extends React . Component {
2121 state = {
22+ dataPropSnap : null ,
2223 data : [ ] , //[{...}, {...}, ....]
2324 displayData : [ ] , //currentlyDisplayData
2425 colNames : [ ] , //['ad', 'asd', ...]
@@ -64,6 +65,24 @@ class DataTable extends React.PureComponent {
6465 }
6566 }
6667
68+ handleOnRowSelect = ( isChecked , id , colName ) => {
69+ const data = this . state . data . map ( row => {
70+ if ( row . id != id ) return row ;
71+ if ( 'onRowSelect' in this . props ) this . props ?. onRowSelect ( { ...row , [ colName ] : isChecked } ) // Sending props
72+ return { ...row , [ colName ] : isChecked }
73+ } )
74+
75+ const displayData = this . state . displayData . map ( row => {
76+ if ( row . id != id ) return row ;
77+ return { ...row , [ colName ] : isChecked }
78+ } )
79+
80+ this . setState ( {
81+ data,
82+ displayData
83+ } )
84+ }
85+
6786 handleNextPreviousPagePress = ( type ) => { //next | back
6887 if ( type == 'next' ) {
6988 // this.state.activeDisplayDataId
@@ -89,16 +108,16 @@ class DataTable extends React.PureComponent {
89108 }
90109
91110 static getDerivedStateFromProps ( props , currentState ) {
92- // console.log(props)
93- if ( JSON . stringify ( props . data ) === JSON . stringify ( currentState . data ) ) return null ;
94-
111+ //this called on every setState() & on mount & on prop changes
112+ if ( JSON . stringify ( props . data ) === JSON . stringify ( currentState . dataPropSnap ) ) return null ;
113+ //Here below code means that data prop is changed
95114 let data = props ?. data
96115 let colNames = props ?. colNames ;
97116
98- if ( typeof ( data ) != 'object' ) {
117+ if ( typeof ( data ) != 'object' ) {
99118 data = [ ] ;
100119 }
101- if ( typeof ( colNames ) != 'object' ) {
120+ if ( typeof ( colNames ) != 'object' ) {
102121 colNames = [ 'No Columns' ] ;
103122 }
104123
@@ -123,11 +142,16 @@ class DataTable extends React.PureComponent {
123142 isSortedAssending [ name ] = false ;
124143 } )
125144
126- const cloneData = [ ...data ] ;
127-
145+ // const modifiedData = [...data];
146+ const modifiedData = data . map ( ( row , index ) => {
147+ if ( ! row . id ) return { ...row , id : index }
148+ return row ;
149+ } )
150+ // console.log(modifiedData)
128151 return {
129- data : cloneData ,
130- displayData : cloneData . slice ( 0 , end [ 0 ] ?. endData ) ,
152+ dataPropSnap : props ?. data ,
153+ data : modifiedData ,
154+ displayData : modifiedData . slice ( 0 , end [ 0 ] ?. endData ) ,
131155 colNames : [ ...colNames ] ,
132156 defaultEachColumnWidth : TOTAL_WIDTH / noOfCols + '%' ,
133157 isSortedAssending : { ...currentState . isSortedAssending , ...isSortedAssending } ,
@@ -158,8 +182,10 @@ class DataTable extends React.PureComponent {
158182 {
159183 this . state . displayData . map ( ( item , index ) => (
160184 < DataTableRow
185+ handleOnRowSelect = { this . handleOnRowSelect }
161186 widthOfLine = { this . state . widthOfContainer }
162187 key = { index }
188+ index = { index }
163189 data = { item }
164190 mapColNameToType = { this . state . mapColNameToType }
165191 colNames = { this . state . colNames }
@@ -203,5 +229,6 @@ DataTable.propTypes = {
203229 } )
204230 ) ,
205231 noOfPages : PropTypes . number ,
232+ onRowSelect : PropTypes . func
206233 // showNoOfRowsPerDisplay: PropTypes.number //default all
207234}
0 commit comments