forked from storybookjs/storybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handleProp.ts
52 lines (44 loc) · 1.71 KB
/
handleProp.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { isNil } from 'lodash';
import { PropDef } from '@storybook/components';
import { ExtractedProp } from '../../../lib/docgen';
import { createType } from './createType';
import { createDefaultValue, createDefaultValueFromRawDefaultProp } from '../lib/defaultValues';
import { Component } from '../../../blocks/shared';
import { keepOriginalDefinitionOrder } from './sortProps';
import { rawDefaultPropTypeResolvers } from './rawDefaultPropResolvers';
export function enhancePropTypesProp(extractedProp: ExtractedProp, rawDefaultProp?: any): PropDef {
const { propDef } = extractedProp;
const newtype = createType(extractedProp);
if (!isNil(newtype)) {
propDef.type = newtype;
}
const { defaultValue } = extractedProp.docgenInfo;
if (!isNil(defaultValue) && !isNil(defaultValue.value)) {
const newDefaultValue = createDefaultValue(defaultValue.value);
if (!isNil(newDefaultValue)) {
propDef.defaultValue = newDefaultValue;
}
} else if (!isNil(rawDefaultProp)) {
const newDefaultValue = createDefaultValueFromRawDefaultProp(
rawDefaultProp,
propDef,
rawDefaultPropTypeResolvers
);
if (!isNil(newDefaultValue)) {
propDef.defaultValue = newDefaultValue;
}
}
// typescript information for types inheritance
propDef.parent = extractedProp.docgenInfo.parent;
return propDef;
}
export function enhancePropTypesProps(
extractedProps: ExtractedProp[],
component: Component
): PropDef[] {
const rawDefaultProps = !isNil(component.defaultProps) ? component.defaultProps : {};
const enhancedProps = extractedProps.map(x =>
enhancePropTypesProp(x, rawDefaultProps[x.propDef.name])
);
return keepOriginalDefinitionOrder(enhancedProps, component);
}