This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
PropTable.js
155 lines (136 loc) · 3.76 KB
/
PropTable.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import React from 'react';
import PropVal from './PropVal';
const stylesheet = {
propTable: {
marginLeft: -10,
borderSpacing: '10px 5px',
borderCollapse: 'separate',
},
};
function isNotEmpty(obj) {
return obj && Object.keys(obj).length > 0;
}
const PropTypesMap = new Map();
for (const typeName in React.PropTypes) {
if (!React.PropTypes.hasOwnProperty(typeName)) {
continue;
}
const type = React.PropTypes[typeName];
PropTypesMap.set(type, typeName);
PropTypesMap.set(type.isRequired, typeName);
}
function renderDocgenPropType(propType) {
if (!propType) {
return 'unknown';
}
const name = propType.name;
switch (name) {
case 'arrayOf':
return `${propType.value.name}[]`;
case 'instanceOf':
return propType.value;
case 'union':
return propType.raw;
default:
return name;
}
}
function hasDocgen(type) {
return isNotEmpty(type.__docgenInfo);
}
function propsFromDocgen(type) {
let props = null;
const docgenInfo = type.__docgenInfo || {};
const docgenInfoProps = docgenInfo.props;
if (docgenInfoProps) {
props = {};
for (const propName in docgenInfoProps) {
if (!docgenInfoProps.hasOwnProperty(propName)) {
continue;
}
const docgenInfoProp = docgenInfoProps[propName];
const defaultValueDesc = docgenInfoProp.defaultValue || {};
const propType = docgenInfoProp.flowType || docgenInfoProp.type || 'other';
props[propName] = {
property: propName,
propType: renderDocgenPropType(propType),
required: docgenInfoProp.required,
description: docgenInfoProp.description,
defaultValue: defaultValueDesc.value,
};
}
}
return props;
}
function propsFromPropTypes(type) {
let props = null;
if (type.propTypes) {
props = {};
for (const property in type.propTypes) {
if (!type.propTypes.hasOwnProperty(property)) {
continue;
}
const typeInfo = type.propTypes[property];
const propType = PropTypesMap.get(typeInfo) || 'other';
const required = typeInfo.isRequired === undefined ? 'yes' : 'no';
props[property] = { property, propType, required };
}
}
if (type.defaultProps) {
for (const property in type.defaultProps) {
if (!type.defaultProps.hasOwnProperty(property)) {
continue;
}
const value = type.defaultProps[property];
if (value === undefined) {
continue;
}
if (!props[property]) {
props[property] = { property };
}
props[property].defaultValue = value;
}
}
return props;
}
export default class PropTable extends React.Component {
render() {
const type = this.props.type;
if (!type) {
return null;
}
const props = hasDocgen(type) ? propsFromDocgen(type) : propsFromPropTypes(type);
if (isNotEmpty(props)) {
return (
<table style={stylesheet.propTable}>
<thead>
<tr>
<th>property</th>
<th>propType</th>
<th>required</th>
<th>default</th>
<th>description</th>
</tr>
</thead>
<tbody>
{Object.values(props).sort((a, b) => a.property > b.property).map(row => (
<tr key={row.property}>
<td>{row.property}</td>
<td>{row.propType}</td>
<td>{row.required}</td>
<td>{row.defaultValue === undefined ? '-' : <PropVal val={row.defaultValue} />}</td>
<td>{row.description}</td>
</tr>
))}
</tbody>
</table>
);
} else {
return <small>No propTypes defined!</small>;
}
}
}
PropTable.displayName = 'PropTable';
PropTable.propTypes = {
type: React.PropTypes.func,
};