forked from newrelic/docs-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionDefinition.js
More file actions
69 lines (62 loc) · 1.92 KB
/
Copy pathFunctionDefinition.js
File metadata and controls
69 lines (62 loc) · 1.92 KB
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
import React from 'react';
import PropTypes from 'prop-types';
import CodeDef from './CodeDef';
const FunctionDefinition = ({ className, arguments: params, returnValue }) => {
const hasParams = params.length;
const isReturnArray = Array.isArray(returnValue);
const hasReturn =
(isReturnArray && returnValue.length) || (!isReturnArray && returnValue);
if (!hasParams && !hasReturn) {
return null;
}
if (!Array.isArray(returnValue)) {
returnValue = [returnValue];
}
return (
<CodeDef className={className}>
<CodeDef.Keyword>function</CodeDef.Keyword>{' '}
<CodeDef.Bracket>{params.length > 0 ? '(' : '()'}</CodeDef.Bracket>
{params.length > 0 && (
<CodeDef.Block>
{params.map((param, i) => (
<div key={i}>
<CodeDef.Identifier>
{param.type.startsWith('...') ? `...${param.name}` : param.name}
:{' '}
</CodeDef.Identifier>
<CodeDef.Type>{param.type}</CodeDef.Type>
{i !== params.length - 1 ? ', ' : ' '}
<CodeDef.Comment text={param.description} />
</div>
))}
</CodeDef.Block>
)}
{params.length > 0 && <CodeDef.Bracket>)</CodeDef.Bracket>}
{returnValue.length > 0 && (
<>
<CodeDef.Operator> => </CodeDef.Operator>
<CodeDef.Type>{returnValue[0].type}</CodeDef.Type>
</>
)}
</CodeDef>
);
};
const ReturnValueType = PropTypes.shape({
type: PropTypes.string,
description: PropTypes.string,
});
FunctionDefinition.propTypes = {
className: PropTypes.string,
arguments: PropTypes.arrayOf(
PropTypes.shape({
name: PropTypes.string,
type: PropTypes.string,
description: PropTypes.string,
})
).isRequired,
returnValue: PropTypes.oneOfType([
ReturnValueType,
PropTypes.arrayOf(ReturnValueType),
]).isRequired,
};
export default FunctionDefinition;