@@ -7,9 +7,6 @@ import DataTableHeader from './DataTableHeader';
77import Line from './Line' ;
88import sortData from '../functions/sort' ;
99import showCurrentProgress from '../functions/showCurrentProgress' ;
10- // import 'react-native-gesture-handlerasd'
11-
12- const { width, height } = Dimensions . get ( 'window' ) ;
1310
1411export const COL_TYPES = {
1512 // RADIO: 'RADIO',
@@ -18,34 +15,25 @@ export const COL_TYPES = {
1815 // ICON: 'ICON'
1916}
2017
21- // let TouchableComponent = TouchableOpacity
22- // export const PADDING_HORIZONTAL = 10;
23-
24- // if (Platform.OS == 'android' && Platform.Version >= 21) {
25- // TouchableComponent = TouchableNativeFeedback
26- // }
27-
28- const PADDING_HORIZONTAL = 10 ;
29-
3018const TOTAL_WIDTH = 100 ; //'100%'
3119
32- const defaultShowRows = 3 ; //means 3 percent
33-
3420class DataTable extends React . PureComponent {
3521 state = {
3622 data : [ ] , //[{...}, {...}, ....]
3723 displayData : [ ] , //currentlyDisplayData
3824 colNames : [ ] , //['ad', 'asd', ...]
3925 defaultEachColumnWidth : '50%' ,
4026 // noOfCols: 0, //default 2, set 0 because of fast rendering at start
41- widthOfContainer : width ,
27+ widthOfContainer : 0 ,
4228 isSortedAssending : { recentlySortedCol : null } , //ColName: true||false
4329 startDataArray : [ ] , //[{id: startData}]
4430 endDataArray : [ ] , //[{id, endData}]
4531 noOfPages : 3 , //default
46- activeDisplayDataId : 0
32+ activeDisplayDataId : 0 ,
33+ mapColNameToType : { }
4734 }
4835
36+
4937 handleColPress = name => {
5038 const newData = [ ...this . state . displayData ] ;
5139
@@ -100,51 +88,54 @@ class DataTable extends React.PureComponent {
10088 }
10189 }
10290
103- componentDidMount ( ) {
104- let data = this . props ?. data
105- let colNames = this . props ?. colNames ;
91+ static getDerivedStateFromProps ( props , currentState ) {
92+ // console.log( props)
93+ if ( JSON . stringify ( props . data ) === JSON . stringify ( currentState . data ) ) return null ;
10694
107- if ( typeof ( data ) != 'object' ) {
95+ let data = props ?. data
96+ let colNames = props ?. colNames ;
97+
98+ if ( typeof ( data ) != 'object' ) {
10899 data = [ ] ;
109- }
110- if ( typeof ( colNames ) != 'object' ) {
100+ }
101+ if ( typeof ( colNames ) != 'object' ) {
111102 colNames = [ 'No Columns' ] ;
112103 }
113104
114- this . mapColNameToType = { }
115- this . props . colSettings ?. forEach ( setting => {
105+ const mapColNameToType = { }
106+ props . colSettings ?. forEach ( setting => {
116107 if ( ! colNames . includes ( setting . name ) ) throw new Error ( 'No Column exists which mentioned in provided colSettings prop Name!' )
117- this . mapColNameToType [ setting . name ] = setting . type ;
108+ mapColNameToType [ setting . name ] = setting . type ;
118109 } )
119110 let start = [ ] ;
120111 let end = [ ]
121112 if ( data . length != 0 ) {
122- const progress = showCurrentProgress ( this . props ?. noOfPages , data ?. length ) //[{id, endData}]
113+ const progress = showCurrentProgress ( props ?. noOfPages , data ?. length ) //[{id, endData}]
123114 if ( progress ) {
124115 start = progress . start ;
125116 end = progress . end ;
126117 }
127118 }
128- this . setState ( ( state ) => {
129- const noOfCols = colNames . length ;
130- const isSortedAssending = { } ;
131- colNames . forEach ( name => {
132- isSortedAssending [ name ] = false ;
133- } )
134-
135- const cloneData = [ ...data ] ;
136-
137- return {
138- data : cloneData ,
139- displayData : cloneData . slice ( 0 , end [ 0 ] ?. endData ) ,
140- colNames : [ ...colNames ] ,
141- defaultEachColumnWidth : TOTAL_WIDTH / noOfCols + '%' ,
142- isSortedAssending : { ...state . isSortedAssending , ...isSortedAssending } ,
143- activeDisplayDataId : 0 , //by default it's zero
144- startDataArray : start ,
145- endDataArray : end
146- }
147- } ) ;
119+
120+ const noOfCols = colNames . length ;
121+ const isSortedAssending = { } ;
122+ colNames . forEach ( name => {
123+ isSortedAssending [ name ] = false ;
124+ } )
125+
126+ const cloneData = [ ...data ] ;
127+
128+ return {
129+ data : cloneData ,
130+ displayData : cloneData . slice ( 0 , end [ 0 ] ?. endData ) ,
131+ colNames : [ ...colNames ] ,
132+ defaultEachColumnWidth : TOTAL_WIDTH / noOfCols + '%' ,
133+ isSortedAssending : { ...currentState . isSortedAssending , ...isSortedAssending } ,
134+ activeDisplayDataId : 0 , //by default it's zero
135+ startDataArray : start ,
136+ endDataArray : end ,
137+ mapColNameToType
138+ } ;
148139 }
149140
150141 render ( ) {
@@ -157,20 +148,20 @@ class DataTable extends React.PureComponent {
157148
158149 < DataTableHeader
159150 colNames = { this . state . colNames }
160- mapColNameToType = { this . mapColNameToType }
151+ mapColNameToType = { this . state . mapColNameToType }
161152 defaultEachColumnWidth = { this . state . defaultEachColumnWidth }
162153 handleColPress = { this . handleColPress }
163154 />
164155
165156 < Line width = { this . state . widthOfContainer } header />
166157
167- { this . state . data &&
158+ {
168159 this . state . displayData . map ( ( item , index ) => (
169160 < DataTableRow
170161 widthOfLine = { this . state . widthOfContainer }
171162 key = { index }
172163 data = { item }
173- mapColNameToType = { this . mapColNameToType }
164+ mapColNameToType = { this . state . mapColNameToType }
174165 colNames = { this . state . colNames }
175166 style = { { defaultEachColumnWidth : this . state . defaultEachColumnWidth } }
176167 />
0 commit comments