-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
141 lines (130 loc) · 4.74 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Created by appBakerz - 05 on 07-Jun-17.
*/
class Table extends React.Component{
constructor(props){
super(props);
}
render(){
//generate table headings.
const generateHeadings = (arr) => {
return arr.map((obj, i) => {
return (
<th key={i}>
{obj.label}
{obj.icon}
</th>
)
})
};
//generate table data.
const generateTd = (data, tableIndex, rowIndex) => {
return this.props.tables[tableIndex].config.map((obj, i) => {
//nested key.
if(obj.nestedKey){
//nested key && customize function.
if(obj.modification){
return (
(data.hasOwnProperty([obj.key]) && typeof data[obj.key] === 'object' && !(data[obj.key] instanceof Array)) ?
<td key={i}>{obj.modification(data[obj.key][obj.nestedKey], data, rowIndex)}</td> :
<td key={i}>Not an object</td>
)
}
return(
(data.hasOwnProperty([obj.key]) && typeof data[obj.key] === 'object' && !(data[obj.key] instanceof Array)) ?
<td key={i}>{data[obj.key][obj.nestedKey]}</td> :
<td key={i}>Not an object</td>
)
}
//customize function.
else if(obj.modification){
return (
<td key={i}>{obj.modification(data[obj.key], data, rowIndex)}</td>
)
}
//default node.
return (
data.hasOwnProperty([obj.key]) ? <td key={i}>{data[obj.key]}</td> : <td key={i}>Not define</td>
)
})
};
const generateRows = (index) => {
return this.props.data.map((obj, i) => {
return (
<tr key={i}>{generateTd(obj, index, i)}</tr>
)
})
};
return (
<div className="react-table-container">
{/*Tables map*/}
{this.props.tables.map((obj, i) => {
return (
<table className={obj.cssClass} key={i}>
<thead>
<tr>
{generateHeadings(obj.config)}
</tr>
</thead>
<tbody>
{generateRows(i)}
</tbody>
</table>
)
})}
</div>
)
}
}
Table.propTypes = {
//example: [{config: [{key: '', label: '', isSortable: boolean}], isFixed: boolean, cssClass: string}]
/*Note: In tables props each object generate it's own table according to it's configuration.
1)key is the name of your object properties,
2)modification have three arguments first key value, second full data, third rowIndex.
for now.*/
//tables is an array of objects.
tables: PropTypes.arrayOf( React.PropTypes.shape({
config: PropTypes.arrayOf(React.PropTypes.shape({
key: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
icon: PropTypes.node,
nestedKey: PropTypes.string,
modification: PropTypes.func
})).isRequired,
cssClass: PropTypes.string
}) ).isRequired,
//array of objects.
data: PropTypes.arrayOf(React.PropTypes.shape({})).isRequired
};
//Bootstrap App
ReactDOM.render(
<Table tables={
[
{
config: [
{
key: 'name',
label: 'NAME',
icon: <i className="fa fa-user" aria-hidden="true"></i>,
modification: (name, obj, index) => `Mr: ${name}`
}
]
}
]
}
data={
[
{
name: 'Jhon'
},
{
name: 'Sara'
},
{
name: 'Domnic'
}
]
}
/>,
document.getElementById('app')
);