Skip to content
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

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,821 changes: 1,563 additions & 1,258 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"@babel/preset-react": "^7.13.13",
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"@reach/router": "1.3.4",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
Expand All @@ -60,11 +59,9 @@
"babel-plugin-transform-object-assign": "^6.22.0",
"babel-plugin-typescript-to-proptypes": "^1.0.0",
"cross-env": "^7.0.3",
"docz": "^2.4.0",
"docz": "^2.3.1",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

На 2 ноутах у меня не собирается дока из мастера, потому пришлось понизить версию до "стабильной" (такой конфиг был тут пару месяцев назад), которая бы нормально собиралась и следовательно удалил лишние и неактуальные зависимости

"dot-json": "^1.2.0",
"eslint": "^7.11.0",
"gatsby-link": "4.1.0",
"gatsby-react-router-scroll": "5.12.1",
"husky": "^4.2.5",
"npx": "^10.2.2",
"prettier": "^2.2.1",
Expand Down
19 changes: 16 additions & 3 deletions src/__docz__/button.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Теперь компоненты с большим числом пропов будут иметь сверху кастомный список пропов, потом примеры, потом весь список пропов


<Props whiteList={[
'type',
'size',
'onClick',
'extraClass',
'htmlType',
]} of={Button} />

## Primary small

Expand Down Expand Up @@ -46,3 +54,8 @@ import { Button } from '../';
<Playground>
<Button type="secondary" size="large">Нажми на меня</Button>
</Playground>


## All properties

<Props of={Button} />
24 changes: 24 additions & 0 deletions src/__docz__/components/props.tsx
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} />;
};
96 changes: 96 additions & 0 deletions src/__docz__/components/props.utils.tsx
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+)\)/;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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;
};
17 changes: 14 additions & 3 deletions src/__docz__/email-input.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@
name: EmailInput
route: /email-input
---
import { Playground, Props } from 'docz'
import { Playground } from 'docz'
import { EmailInput } from '../';
import { Props } from './components/props';


# EmailInput

## Properties
## Custom properties

<Props of={EmailInput} />
<Props whiteList={[
'value',
'size',
'placeholder',
'isIcon',
'extraClass',
'onChange',
]} of={EmailInput} />

## Basic usage

Expand All @@ -31,3 +39,6 @@ import { EmailInput } from '../';



## All properties

<Props of={EmailInput} />
25 changes: 22 additions & 3 deletions src/__docz__/input.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@
name: Input
route: /input
---
import { Playground, Props } from 'docz'
import { Playground } from 'docz'
import { Input } from '../';
import { Props } from './components/props';


# Input

## Properties
## Custom properties

<Props of={Input} />
<Props whiteList={[
'value',
'type',
'placeholder',
'success',
'error',
'disabled',
'icon',
'errorText',
'size',
'extraClass',
'onChange',
'onIconClick',
'onBlur',
'onFocus',
]} of={Input} />

## Basic usage

Expand Down Expand Up @@ -43,4 +59,7 @@ import { Input } from '../';



## All properties

<Props of={Input} />

17 changes: 14 additions & 3 deletions src/__docz__/password-input.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@
name: PasswordInput
route: /password-input
---
import { Playground, Props } from 'docz'
import { Playground } from 'docz'
import { PasswordInput } from '../';
import { Props } from './components/props';


# PasswordInput

## Properties
## Custom properties

<Props of={PasswordInput} />
<Props whiteList={[
'value',
'placeholder',
'size',
'icon',
'extraClass',
'onChange',
]} of={PasswordInput} />

## Basic usage

Expand Down Expand Up @@ -40,6 +48,9 @@ import { PasswordInput } from '../';
</Playground>


## All properties

<Props of={PasswordInput} />



Expand Down