forked from storybookjs/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Props.js
54 lines (46 loc) 路 1.39 KB
/
Props.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
import React from 'react';
import PropVal from './PropVal';
const stylesheet = {
propStyle: {},
propNameStyle: {},
propValueStyle: {},
};
export default class Props extends React.Component {
render() {
const props = this.props.node.props;
const defaultProps = this.props.node.type.defaultProps;
if (!props || typeof props !== 'object') {
return <span />;
}
const { propStyle, propValueStyle, propNameStyle } = stylesheet;
const names = Object.keys(props).filter(
name =>
name[0] !== '_' &&
name !== 'children' &&
(!defaultProps || props[name] != defaultProps[name]),
);
const breakIntoNewLines = names.length > 3;
const endingSpace = this.props.singleLine ? ' ' : '';
const items = [];
names.forEach((name, i) => {
items.push(
<span key={name}>
{breakIntoNewLines
? <span>
<br />
</span>
: ' '}
<span style={propNameStyle}>{name}</span>
{/* Use implicit true: */}
{(!props[name] || typeof props[name] !== 'boolean') &&
<span>
=
<span style={propValueStyle}><PropVal val={props[name]} /></span>
</span>}
{i === names.length - 1 && (breakIntoNewLines ? <br /> : endingSpace)}
</span>,
);
});
return <span>{items}</span>;
}
}