This repository has been archived by the owner on Jan 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
67 lines (61 loc) · 1.88 KB
/
index.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
// @flow
import React from "react";
import convert, { getKind ,reduceToObj } from "kind2string";
import Prop from "../Prop";
import allComponents, { type Components } from "../components";
const renderPropType = (
propType: any,
{ overrides = {}, shouldCollapseProps, components }: any
) => {
if (!components) {
components = allComponents;
} else {
components = { ...allComponents, ...components };
}
if (propType.kind === "spread") {
const furtherProps = reduceToObj(propType.value);
if (Array.isArray(furtherProps) && furtherProps.length > 0) {
/* Only render the spread contents if they are a non-empty value, otherwise render the
* spread itself so we can see the spread of generics and other types that have not been
* converted into an object */
return furtherProps.map(p =>
renderPropType(p, { overrides, shouldCollapseProps, components })
);
}
}
let description;
if (propType.leadingComments) {
description = propType.leadingComments.reduce(
(acc, { value }) => acc.concat(`\n${value}`),
""
);
}
if (!propType.value) {
// eslint-disable-next-line no-console
console.error(
`Prop ${
propType.key
} has no type; this usually indicates invalid propType or defaultProps config`
);
return null;
}
const name = propType.kind === 'spread' ? '...' : convert(propType.key);
const OverrideComponent = overrides[name];
const commonProps = {
components,
name,
key: name,
required: !propType.optional,
type: getKind(propType.value),
defaultValue: propType.default && convert(propType.default),
description: description,
shouldCollapse: shouldCollapseProps,
typeValue: propType.value
};
return overrides[name] ? (
<OverrideComponent {...commonProps} />
) : (
<Prop {...commonProps} />
);
};
export default renderPropType;