-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(documentation): add props component, fix documentations and versions #42
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,21 @@ name: Button | |
route: /button | ||
--- | ||
|
||
import { Playground, Props } from 'docz' | ||
import { Playground } from 'docz' | ||
import { Button } from '../'; | ||
import { Props } from './components/props'; | ||
|
||
# Button | ||
|
||
## Properties | ||
<Props of={Button} /> | ||
## Custom properties | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Теперь компоненты с большим числом пропов будут иметь сверху кастомный список пропов, потом примеры, потом весь список пропов |
||
|
||
<Props whiteList={[ | ||
'type', | ||
'size', | ||
'onClick', | ||
'extraClass', | ||
'htmlType', | ||
]} of={Button} /> | ||
|
||
## Primary small | ||
|
||
|
@@ -46,3 +54,8 @@ import { Button } from '../'; | |
<Playground> | ||
<Button type="secondary" size="large">Нажми на меня</Button> | ||
</Playground> | ||
|
||
|
||
## All properties | ||
|
||
<Props of={Button} /> |
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} />; | ||
}; |
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+)\)/; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Копипаста из библиотеки, надо попробовать сделать к ним ПР, чтобы они экспортировали нужную нам функцию |
||
|
||
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; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
На 2 ноутах у меня не собирается дока из мастера, потому пришлось понизить версию до "стабильной" (такой конфиг был тут пару месяцев назад), которая бы нормально собиралась и следовательно удалил лишние и неактуальные зависимости