@@ -7,9 +7,6 @@ import DataTableHeader from './DataTableHeader';
7
7
import Line from './Line' ;
8
8
import sortData from '../functions/sort' ;
9
9
import showCurrentProgress from '../functions/showCurrentProgress' ;
10
- // import 'react-native-gesture-handlerasd'
11
-
12
- const { width, height } = Dimensions . get ( 'window' ) ;
13
10
14
11
export const COL_TYPES = {
15
12
// RADIO: 'RADIO',
@@ -18,34 +15,25 @@ export const COL_TYPES = {
18
15
// ICON: 'ICON'
19
16
}
20
17
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
-
30
18
const TOTAL_WIDTH = 100 ; //'100%'
31
19
32
- const defaultShowRows = 3 ; //means 3 percent
33
-
34
20
class DataTable extends React . PureComponent {
35
21
state = {
36
22
data : [ ] , //[{...}, {...}, ....]
37
23
displayData : [ ] , //currentlyDisplayData
38
24
colNames : [ ] , //['ad', 'asd', ...]
39
25
defaultEachColumnWidth : '50%' ,
40
26
// noOfCols: 0, //default 2, set 0 because of fast rendering at start
41
- widthOfContainer : width ,
27
+ widthOfContainer : 0 ,
42
28
isSortedAssending : { recentlySortedCol : null } , //ColName: true||false
43
29
startDataArray : [ ] , //[{id: startData}]
44
30
endDataArray : [ ] , //[{id, endData}]
45
31
noOfPages : 3 , //default
46
- activeDisplayDataId : 0
32
+ activeDisplayDataId : 0 ,
33
+ mapColNameToType : { }
47
34
}
48
35
36
+
49
37
handleColPress = name => {
50
38
const newData = [ ...this . state . displayData ] ;
51
39
@@ -100,51 +88,54 @@ class DataTable extends React.PureComponent {
100
88
}
101
89
}
102
90
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 ;
106
94
107
- if ( typeof ( data ) != 'object' ) {
95
+ let data = props ?. data
96
+ let colNames = props ?. colNames ;
97
+
98
+ if ( typeof ( data ) != 'object' ) {
108
99
data = [ ] ;
109
- }
110
- if ( typeof ( colNames ) != 'object' ) {
100
+ }
101
+ if ( typeof ( colNames ) != 'object' ) {
111
102
colNames = [ 'No Columns' ] ;
112
103
}
113
104
114
- this . mapColNameToType = { }
115
- this . props . colSettings ?. forEach ( setting => {
105
+ const mapColNameToType = { }
106
+ props . colSettings ?. forEach ( setting => {
116
107
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 ;
118
109
} )
119
110
let start = [ ] ;
120
111
let end = [ ]
121
112
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}]
123
114
if ( progress ) {
124
115
start = progress . start ;
125
116
end = progress . end ;
126
117
}
127
118
}
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
+ } ;
148
139
}
149
140
150
141
render ( ) {
@@ -157,20 +148,20 @@ class DataTable extends React.PureComponent {
157
148
158
149
< DataTableHeader
159
150
colNames = { this . state . colNames }
160
- mapColNameToType = { this . mapColNameToType }
151
+ mapColNameToType = { this . state . mapColNameToType }
161
152
defaultEachColumnWidth = { this . state . defaultEachColumnWidth }
162
153
handleColPress = { this . handleColPress }
163
154
/>
164
155
165
156
< Line width = { this . state . widthOfContainer } header />
166
157
167
- { this . state . data &&
158
+ {
168
159
this . state . displayData . map ( ( item , index ) => (
169
160
< DataTableRow
170
161
widthOfLine = { this . state . widthOfContainer }
171
162
key = { index }
172
163
data = { item }
173
- mapColNameToType = { this . mapColNameToType }
164
+ mapColNameToType = { this . state . mapColNameToType }
174
165
colNames = { this . state . colNames }
175
166
style = { { defaultEachColumnWidth : this . state . defaultEachColumnWidth } }
176
167
/>
0 commit comments