-
Notifications
You must be signed in to change notification settings - Fork 19
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 #42 from Yandex-Practicum/fix_documentation
fix(documentation): add props component, fix documentations and versions
- Loading branch information
Showing
8 changed files
with
1,750 additions
and
1,274 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,24 @@ | ||
import React from 'react'; | ||
import { useComponentProps } from 'docz'; | ||
import components from 'gatsby-theme-docz/src/components'; | ||
import { ComponentWithDocGenInfo } from 'docz/dist/components/Props'; | ||
import { get } from 'lodash/fp'; | ||
import { getPropType, filterProps } from './props.utils'; | ||
|
||
const PropsBase = components.props; | ||
|
||
export const Props = ({ | ||
of: component, | ||
whiteList, | ||
}: { | ||
of: ComponentWithDocGenInfo; | ||
whiteList: string[]; | ||
}) => { | ||
const fileName = get('__filemeta.filename', component); | ||
const filemetaName = get('__filemeta.name', component); | ||
const componentName = filemetaName || get('displayName', component) || get('name', component); | ||
|
||
const props = useComponentProps({ componentName, fileName }); | ||
const filteredProps = filterProps(props, { whiteList }); | ||
return <PropsBase props={filteredProps} getPropType={getPropType} />; | ||
}; |
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,96 @@ | ||
import capitalize from 'capitalize'; | ||
import { get } from 'lodash/fp'; | ||
import { PropType, FlowType } from 'gatsby-theme-docz/src/components/Props'; | ||
|
||
/* | ||
Не типизирую, так как это копипаста из библиотеки, | ||
функция getPropType не экспортируется из нее, потому пришлось копировать :( | ||
*/ | ||
const RE_OBJECTOF = /(?:React\.)?(?:PropTypes\.)?objectOf\((?:React\.)?(?:PropTypes\.)?(\w+)\)/; | ||
|
||
const getTypeStr = (type: any): any => { | ||
switch (type.name.toLowerCase()) { | ||
case 'instanceof': | ||
return `Class(${type.value})`; | ||
case 'enum': | ||
if (type.computed) return type.value; | ||
return type.value ? type.value.map((v: any) => `${v.value}`).join(' │ ') : type.raw; | ||
case 'union': | ||
return type.value | ||
? type.value.map((t: any) => `${getTypeStr(t)}`).join(' │ ') | ||
: type.raw; | ||
case 'array': | ||
return type.raw; | ||
case 'arrayof': | ||
return `Array<${getTypeStr(type.value)}>`; | ||
case 'custom': | ||
if (type.raw.indexOf('function') !== -1 || type.raw.indexOf('=>') !== -1) | ||
return 'Custom(Function)'; | ||
else if (type.raw.toLowerCase().indexOf('objectof') !== -1) { | ||
const m = type.raw.match(RE_OBJECTOF); | ||
if (m && m[1]) return `ObjectOf(${capitalize(m[1])})`; | ||
return 'ObjectOf'; | ||
} | ||
return 'Custom'; | ||
case 'bool': | ||
return 'Boolean'; | ||
case 'func': | ||
return 'Function'; | ||
case 'shape': | ||
// eslint-disable-next-line no-case-declarations | ||
const shape = type.value; | ||
// eslint-disable-next-line no-case-declarations | ||
const rst: any = {}; | ||
Object.keys(shape).forEach((key) => { | ||
rst[key] = getTypeStr(shape[key]); | ||
}); | ||
|
||
return JSON.stringify(rst, null, 2); | ||
default: | ||
return type.name; | ||
} | ||
}; | ||
|
||
export const humanize = (type: any) => getTypeStr(type); | ||
|
||
export const getPropType = (prop: any) => { | ||
const propName = get('name', prop.flowType || prop.type); | ||
if (!propName) return null; | ||
|
||
const isEnum = propName.startsWith('"') || propName === 'enum'; | ||
const name = isEnum ? 'enum' : propName; | ||
const value = get('type.value', prop); | ||
if (!name) return null; | ||
|
||
if ( | ||
(isEnum && typeof value === 'string') || | ||
(!prop.flowType && !isEnum && !value) || | ||
(prop.flowType && !prop.flowType.elements) | ||
) { | ||
return name; | ||
} | ||
|
||
return prop.flowType ? humanize(prop.flowType) : humanize(prop.type); | ||
}; | ||
|
||
/* | ||
Далее идут реальные функции, которые написано под компонент | ||
*/ | ||
|
||
export const filterProps = ( | ||
componentProps: Record<string, PropType | FlowType>, | ||
{ whiteList }: { whiteList: string[] } | ||
): Record<string, PropType | FlowType> => { | ||
if (whiteList) { | ||
return whiteList.reduce( | ||
(acc: Record<typeof whiteList[number], PropType | FlowType>, key) => { | ||
if (componentProps[key]) { | ||
acc[key] = componentProps[key]; | ||
} | ||
return acc; | ||
}, | ||
{} | ||
); | ||
} | ||
return componentProps; | ||
}; |
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