-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hook to find docgen props (#1371)
* feat: setup test case * feat: add component props hook * feat: remove test example
- Loading branch information
1 parent
44edc68
commit 8fffa26
Showing
3 changed files
with
62 additions
and
43 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
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,56 @@ | ||
import { createElement, useContext, useMemo } from 'react' | ||
import { assoc, first, get, mapValues, kebabCase } from 'lodash/fp' | ||
import { pascalCase } from 'pascal-case' | ||
import marksy from 'marksy' | ||
|
||
import { useComponents } from '../hooks' | ||
import { doczState } from '../state' | ||
|
||
interface UseComponentPropsParams { | ||
componentName: string | ||
fileName: string | ||
} | ||
|
||
export const useComponentProps = ({ | ||
componentName, | ||
fileName, | ||
}: UseComponentPropsParams) => { | ||
const components = useComponents() | ||
const { props: stateProps } = useContext(doczState.context) | ||
|
||
const componentMatcher = (componentName: string, item: any) => { | ||
const matchingPatterns = [ | ||
fileName, | ||
`/${componentName}.`, | ||
`/${kebabCase(componentName)}.`, | ||
`/${pascalCase(componentName)}.`, | ||
] | ||
return !!matchingPatterns.find(pattern => item.key.includes(pattern)) | ||
} | ||
|
||
const found = | ||
stateProps && | ||
stateProps.length > 0 && | ||
stateProps.find(item => componentMatcher(componentName, item)) | ||
|
||
const value = get('value', found) || [] | ||
const firstDefinition = first(value) | ||
const definition = value.find((i: any) => i.displayName === componentName) | ||
|
||
const compile = useMemo( | ||
() => marksy({ createElement, elements: components }), | ||
[components] | ||
) | ||
|
||
const props = useMemo(() => { | ||
const props = get('props', definition || firstDefinition) | ||
const parseDescs = mapValues((prop: any) => { | ||
const desc = get('description', prop) | ||
return !desc ? prop : assoc('description', compile(desc).tree, prop) | ||
}) | ||
|
||
return parseDescs(props) | ||
}, [compile, definition || firstDefinition]) | ||
|
||
return props | ||
} |