-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from newrelic/jerel/method-defs
Method definitions
- Loading branch information
Showing
8 changed files
with
233 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import Markdown from 'react-markdown'; | ||
import styles from './FunctionDefinition.module.scss'; | ||
|
||
const ParamDescription = ({ children, ...props }) => ( | ||
<span {...props} className={styles.paramDescription}> | ||
{'//'} {children} | ||
</span> | ||
); | ||
|
||
ParamDescription.propTypes = { | ||
children: PropTypes.node, | ||
}; | ||
|
||
const FunctionDefinition = ({ params, returnValue }) => { | ||
return ( | ||
<div className={styles.container}> | ||
<span className={styles.keyword}> | ||
{params.length > 0 ? 'function (' : 'function ()'} | ||
</span> | ||
{params.map((param, i) => ( | ||
<div key={i} className={styles.param}> | ||
<span className={styles.paramName}>{param.name}: </span> | ||
<span className={styles.type}>{param.type} </span> | ||
<Markdown | ||
source={param.description} | ||
renderers={{ | ||
root: ParamDescription, | ||
}} | ||
/> | ||
</div> | ||
))} | ||
{params.length > 0 && <span className={styles.keyword}>) </span>} | ||
<span className={styles.keyword}>=> </span> | ||
<span className={styles.type}>{returnValue.type}</span> | ||
</div> | ||
); | ||
}; | ||
|
||
FunctionDefinition.propTypes = { | ||
params: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
name: PropTypes.string, | ||
type: PropTypes.string, | ||
description: PropTypes.string, | ||
}) | ||
), | ||
returnValue: PropTypes.shape({ | ||
type: PropTypes.string.isRequired, | ||
}), | ||
}; | ||
|
||
export default FunctionDefinition; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
.container { | ||
font-family: 'Menlo', 'Consolas', monospace; | ||
font-size: 0.875rem; | ||
line-height: 1.5; | ||
} | ||
|
||
.line:not(:last-child) { | ||
margin-bottom: 0.5rem; | ||
} | ||
|
||
.param { | ||
margin-left: 1rem; | ||
} | ||
|
||
.paramName { | ||
color: var(--color-neutrals-700); | ||
} | ||
|
||
.paramDescription { | ||
color: var(--color-neutrals-500); | ||
|
||
> p { | ||
color: inherit; | ||
display: inline; | ||
} | ||
} | ||
|
||
.keyword { | ||
color: var(--color-neutrals-600); | ||
} | ||
|
||
.type { | ||
color: var(--color-green-500); | ||
background: var(--color-green-050); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useMemo } from 'react'; | ||
|
||
const IGNORED_METHODS = [ | ||
'prototype', | ||
'length', | ||
'name', | ||
'propTypes', | ||
'getDerivedStateFromProps', | ||
'defaultProps', | ||
]; | ||
|
||
const useComponentDoc = (componentName) => { | ||
if (typeof window === 'undefined') global.window = {}; | ||
|
||
return useMemo(() => { | ||
const sdk = window.__NR1_SDK__?.default ?? {}; | ||
const component = sdk[componentName]; | ||
|
||
if (!component) { | ||
return null; | ||
} | ||
|
||
const componentDocs = component?.__docs__; | ||
|
||
return { | ||
description: componentDocs?.text, | ||
examples: componentDocs?.tags.examples ?? [], | ||
usage: `import { ${componentName} } from 'nr1'`, | ||
methods: Object.getOwnPropertyNames(component) | ||
.filter( | ||
(member) => | ||
!IGNORED_METHODS.includes(member) && | ||
typeof component[member] === 'function' | ||
) | ||
.map((member) => { | ||
const methodDocs = component[member].__docs__; | ||
|
||
return { | ||
name: `${componentName}.${member}`, | ||
description: methodDocs?.text, | ||
returnValue: methodDocs?.tags.return?.[0] ?? { type: 'undefined' }, | ||
params: methodDocs?.tags.param, | ||
}; | ||
}), | ||
}; | ||
}, [componentName, window?.__NR1_SDK__]); | ||
}; | ||
|
||
export default useComponentDoc; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
path: '/components/accounts-query' | ||
title: 'AccountsQuery' | ||
description: 'A AccountsQuery component!' | ||
component: 'AccountsQuery' | ||
template: 'ReferenceTemplate' | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters