Skip to content

Commit 25ddc33

Browse files
Merge pull request #2 from MuhammadRafeh/component-update-branch
Component update branch
2 parents 8e8f173 + 9ac27b6 commit 25ddc33

File tree

4 files changed

+44
-63
lines changed

4 files changed

+44
-63
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Name | Type| Default | Is Require
8686
data | [] of {} | - | Yes
8787
colNames | [] of Strings | - | Yes
8888
colSettings | [] of {} | - | No
89-
noOfPages | Number | 3 | No
89+
noOfPages | Number | 2 | No
9090
9191
## Constants
9292

functions/showCurrentProgress.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const showCurrentProgress = (numOfPages = 3, fullLengthOfData) => { //default is 3
1+
const showCurrentProgress = (numOfPages = 2, fullLengthOfData) => { //default is 3
22
if (+numOfPages < 1) {
33
throw new Error('No Of Pages Should be equal or greater Than 1')
44
}

src/DataTable.js

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import DataTableHeader from './DataTableHeader';
77
import Line from './Line';
88
import sortData from '../functions/sort';
99
import showCurrentProgress from '../functions/showCurrentProgress';
10-
// import 'react-native-gesture-handlerasd'
11-
12-
const { width, height } = Dimensions.get('window');
1310

1411
export 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-
3018
const TOTAL_WIDTH = 100; //'100%'
3119

32-
const defaultShowRows = 3; //means 3 percent
33-
3420
class 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
/>

src/DataTableRow.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,11 @@ const styles = StyleSheet.create({
6262
},
6363
rowCellText: {
6464
color: 'black',
65-
fontSize: 14.5,
66-
// backgroundColor: 'green',
67-
// flex: 1
68-
// flexWrap: 'wrap'
65+
fontSize: 14.5
6966
},
7067
rowCellContainer: {
7168
paddingTop: 10,
7269
paddingBottom: 10,
73-
// backgroundColor: 'green',
74-
},
75-
// line: {
76-
// height: 1,
77-
// backgroundColor: '#e3e3e3',
78-
// alignSelf: 'center',
79-
// width
80-
// }
70+
}
8171
});
8272

0 commit comments

Comments
 (0)