-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPropsTable.jsx
180 lines (167 loc) · 5.23 KB
/
PropsTable.jsx
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import React, { Component, Fragment } from 'react';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableContainer from '@material-ui/core/TableContainer';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import Paper from '@material-ui/core/Paper';
import { withStyles } from '@material-ui/core/styles';
import ExpandMore from '@material-ui/icons/ExpandMore';
import ExpandLess from '@material-ui/icons/ExpandLess';
import IconButton from '@material-ui/core/IconButton';
const styles = (theme) => ({
tableName: {
color: '#adc285',
},
tableType: {
color: '#a7577f',
},
tableRequired: {
color: '#abaaab',
},
expandText: {
display: 'flex',
alignItems: 'center',
},
});
class PropsTable extends Component {
constructor(props) {
super(props);
this.state = {
propsExpand: {},
};
this.handleExpand = this.handleExpand.bind(this);
}
handleExpand = (key) => {
const { propsExpand } = this.state;
if (key in propsExpand) {
this.setState({
propsExpand: { ...propsExpand, [key]: !propsExpand[key] },
});
} else {
this.setState({
propsExpand: { ...propsExpand, [key]: true },
});
}
};
generateTable(props) {
const { classes } = this.props;
const { propsExpand } = this.state;
let table = (
<TableContainer component={Paper}>
<Table aria-label="simple table">
<TableHead>
<TableRow>{this.genereateTableHead()}</TableRow>
</TableHead>
<TableBody>
{Object.keys(props).map((key) => (
<Fragment key={key}>
<TableRow key={key}>
<TableCell
className={classes.tableName}
component="th"
scope="row"
>
{props[key].type.value ? (
<span>
{key}
<IconButton onClick={() => this.handleExpand(key)}>
{propsExpand[key] ? <ExpandLess /> : <ExpandMore />}
</IconButton>
</span>
) : (
key
)}
</TableCell>
<TableCell className={classes.tableType}>
{props[key].type.name}
</TableCell>
<TableCell className={classes.tableRequired}>
{props[key].required ? 'required' : 'optional'}
</TableCell>
<TableCell>{props[key].description}</TableCell>
</TableRow>
{propsExpand[key]
? this.generateInnerTable(props[key].type.value, 1)
: null}
</Fragment>
))}
</TableBody>
</Table>
</TableContainer>
);
return table;
}
generateInnerTable(props, level = 1) {
const { classes } = this.props;
const { propsExpand } = this.state;
const indent = 40 * level;
let table = (
<Fragment>
<TableRow>{this.genereateTableHead(indent)}</TableRow>
<Fragment>
{Object.keys(props).map((key) => (
<Fragment key={key}>
<TableRow key={key}>
<TableCell
style={{ paddingLeft: indent }}
className={classes.tableName}
component="th"
scope="row"
>
{props[key].value && props[key].value.value ? (
<span className={classes.expandText}>
{key}
<IconButton onClick={() => this.handleExpand(key)}>
{propsExpand[key] ? <ExpandLess /> : <ExpandMore />}
</IconButton>
</span>
) : (
key
)}
</TableCell>
<TableCell className={classes.tableType}>
{props[key].name}
</TableCell>
<TableCell className={classes.tableRequired}>
{props[key].required ? 'required' : 'optional'}
</TableCell>
<TableCell>{props[key].description}</TableCell>
</TableRow>
{propsExpand[key]
? this.generateInnerTable(props[key].value.value, ++level)
: null}
</Fragment>
))}
</Fragment>
</Fragment>
);
return table;
}
genereateTableHead(indent) {
const style = indent ? { paddingLeft: indent } : {};
return (
<Fragment>
<TableCell style={style}>
<b>Name</b>
</TableCell>
<TableCell>
<b>Type</b>
</TableCell>
<TableCell>
<b>Required</b>
</TableCell>
<TableCell>
<b>Description</b>
</TableCell>
</Fragment>
);
}
render() {
const { propsConfigs } = this.props;
const table = this.generateTable(propsConfigs);
return table;
}
}
export default withStyles(styles, { withTheme: true })(PropsTable);