-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
propsUtils.ts
38 lines (30 loc) · 1.23 KB
/
propsUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* eslint-disable no-underscore-dangle */
import { PropDef, PropsTableProps } from '@storybook/components';
import { Component } from '../blocks/shared';
export type PropsExtractor = (component: Component) => PropsTableProps | null;
export type PropDefGetter = (type: Component, section: string) => PropDef[] | null;
export const hasDocgen = (obj: any) => !!obj.__docgenInfo;
export const hasDocgenSection = (obj: any, section: string) =>
obj.__docgenInfo &&
obj.__docgenInfo[section] &&
Object.keys(obj.__docgenInfo[section]).length > 0;
export const propsFromDocgen: PropDefGetter = (type, section) => {
const props: Record<string, PropDef> = {};
const docgenInfoProps = type.__docgenInfo[section];
if (!docgenInfoProps) {
return null;
}
Object.keys(docgenInfoProps).forEach(property => {
const docgenInfoProp = docgenInfoProps[property];
const defaultValueDesc = docgenInfoProp.defaultValue || {};
const propType = docgenInfoProp.flowType || docgenInfoProp.type || 'other';
props[property] = {
name: property,
type: propType,
required: docgenInfoProp.required,
description: docgenInfoProp.description,
defaultValue: defaultValueDesc.value,
};
});
return Object.values(props);
};