From d4d0180b24267465647a5598750a6ca5f2fda796 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Wed, 3 Jun 2020 16:26:28 -0700 Subject: [PATCH] feat: Add method definitions and data to useComponentDoc --- src/hooks/useComponentDoc.js | 34 +++++++++++++++++++++++------- src/templates/ReferenceTemplate.js | 16 +++++++++++--- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/hooks/useComponentDoc.js b/src/hooks/useComponentDoc.js index 45369a939..07907e50d 100644 --- a/src/hooks/useComponentDoc.js +++ b/src/hooks/useComponentDoc.js @@ -13,16 +13,34 @@ const useComponentDoc = (componentName) => { if (typeof window === 'undefined') global.window = {}; return useMemo(() => { - const component = window?.__NR1_SDK__?.default?.[componentName]; + const sdk = window.__NR1_SDK__?.default ?? {}; + const component = sdk[componentName]; + + if (!component) { + return null; + } + + const componentDocs = component?.__docs__; return { - description: component?.__docs__.text, - examples: component?.__docs__.tags.examples ?? [], - methods: Object.getOwnPropertyNames(component).filter( - (member) => - !IGNORED_METHODS.includes(member) && - typeof component[member] === 'function' - ), + description: componentDocs?.text, + examples: componentDocs?.tags.examples ?? [], + 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, + params: methodDocs?.tags.param, + }; + }), }; }, [componentName, window?.__NR1_SDK__]); }; diff --git a/src/templates/ReferenceTemplate.js b/src/templates/ReferenceTemplate.js index 4f85c8aec..7b9b9500d 100644 --- a/src/templates/ReferenceTemplate.js +++ b/src/templates/ReferenceTemplate.js @@ -23,9 +23,11 @@ const ReferenceTemplate = ({ data }) => { const { mdx } = data; const { frontmatter } = mdx; const { title, description, component } = frontmatter; - const { examples, description: componentDescription } = useComponentDoc( - component - ); + const { + examples, + description: componentDescription, + methods, + } = useComponentDoc(component); return ( @@ -59,6 +61,14 @@ const ReferenceTemplate = ({ data }) => { )} + {methods.length > 0 && ( +
+

Methods

+ {methods.map((method, i) => ( +

{method.name}

+ ))} +
+ )}