-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathPropsTable.react.js
66 lines (63 loc) · 1.8 KB
/
PropsTable.react.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
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import React from 'react';
import styles from 'parse-interface-guide/PIG.scss';
function typeString(prop) {
switch (prop._id) {
case 'Enum':
return 'Enum { ' + prop._values.join(', ') + ' }';
case 'Union':
return prop._classes.map(c => c._id).join(' | ');
default:
return prop._id;
}
}
const PropsRow = p => (
<div className={styles.row}>
<div>
<span className={styles.prop_name}>{p.name}</span>
{p.required ? <span className={styles.prop_required}>[Required]</span> : null}
<span className={styles.prop_type}>{p.type}</span>
</div>
<p>{p.description}</p>
</div>
);
export default class PropsTable extends React.Component {
render() {
const component = this.props.component;
const requiredProps = [];
const optionalProps = [];
if (component.propTypes) {
for (const p in component.propTypes) {
const info = {
name: p,
type: typeString(component.propTypes[p]),
required: component.propTypes[p]._required,
description: component.propTypes[p]._description,
};
if (component.defaultProps && component.defaultProps[p]) {
info.default = component.defaultProps[p];
}
if (info.required) {
requiredProps.push(info);
} else {
optionalProps.push(info);
}
}
}
const propInfo = requiredProps.concat(optionalProps);
return (
<div className={styles.table}>
<div className={styles.header}>Props</div>
{propInfo.map(p => (
<PropsRow key={p.name} {...p} />
))}
</div>
);
}
}