mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-08 03:41:54 +08:00
Add custom code to replace the custom string with the raw type when available
This commit is contained in:
parent
69a13d9127
commit
fbfaae82f8
@ -1,6 +1,10 @@
|
|||||||
export interface DocgenInfo {
|
export interface DocgenInfo {
|
||||||
type?: {
|
type?: {
|
||||||
name: string;
|
name: string;
|
||||||
|
value?: {
|
||||||
|
name?: string;
|
||||||
|
raw?: string;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
flowType?: any;
|
flowType?: any;
|
||||||
tsType?: any;
|
tsType?: any;
|
||||||
|
@ -42,7 +42,7 @@ function parse(content: string): Annotation {
|
|||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log(e);
|
console.error(e);
|
||||||
|
|
||||||
throw new Error('Cannot parse JSDoc tags.');
|
throw new Error('Cannot parse JSDoc tags.');
|
||||||
}
|
}
|
||||||
|
@ -737,6 +737,41 @@ describe('prop-types handler', () => {
|
|||||||
expect(propDef.description).toBe('onClick description');
|
expect(propDef.description).toBe('onClick description');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('when the prop type is arrayOf', () => {
|
||||||
|
it('should support array of primitives', () => {
|
||||||
|
const docgenInfo = createDocgenInfo({
|
||||||
|
type: {
|
||||||
|
name: 'arrayOf',
|
||||||
|
value: {
|
||||||
|
name: 'string',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const { propDef } = propTypesHandler(DEFAULT_PROP_NAME, docgenInfo);
|
||||||
|
|
||||||
|
expect(propDef.type.name).toBe('arrayOf');
|
||||||
|
expect(propDef.type.value.name).toBe('string');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should support array of custom shapes', () => {
|
||||||
|
const docgenInfo = createDocgenInfo({
|
||||||
|
type: {
|
||||||
|
name: 'arrayOf',
|
||||||
|
value: {
|
||||||
|
name: 'custom',
|
||||||
|
raw: 'Circle',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const { propDef } = propTypesHandler(DEFAULT_PROP_NAME, docgenInfo);
|
||||||
|
|
||||||
|
expect(propDef.type.name).toBe('arrayOf');
|
||||||
|
expect(propDef.type.value.name).toBe('Circle');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('ts handler', () => {
|
describe('ts handler', () => {
|
||||||
|
@ -132,6 +132,12 @@ export const propTypesHandler: TypeSystemHandler = (propName: string, docgenInfo
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (propDef.type.name === 'arrayOf') {
|
||||||
|
if (propDef.type.value.name === 'custom') {
|
||||||
|
if (!isNil(propDef.type.value.raw)) {
|
||||||
|
propDef.type.value.name = propDef.type.value.raw;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
/* eslint-disable react/no-unused-prop-types */
|
||||||
|
import React from 'react';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
|
||||||
|
const ITEM_SHAPE = {
|
||||||
|
text: PropTypes.string.isRequired,
|
||||||
|
value: PropTypes.string.isRequired,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const PropTypesProps = () => <div>PropTypes!</div>;
|
||||||
|
|
||||||
|
PropTypesProps.propTypes = {
|
||||||
|
arrayOfPrimitive: PropTypes.arrayOf(PropTypes.string),
|
||||||
|
arrayOfShape: PropTypes.arrayOf(ITEM_SHAPE),
|
||||||
|
};
|
||||||
|
|
||||||
|
PropTypesProps.defaultProps = {
|
||||||
|
arrayOfPrimitive: ['foo', 'bar'],
|
||||||
|
arrayOfShape: [{ text: 'foo', value: 'bar' }],
|
||||||
|
};
|
@ -0,0 +1,17 @@
|
|||||||
|
import React, { FC } from 'react';
|
||||||
|
|
||||||
|
interface ItemShape {
|
||||||
|
text: string;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TypeScriptPropsProps {
|
||||||
|
arrayOfPrimitive: string[];
|
||||||
|
arrayOfShape: ItemShape[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export const TypeScriptProps: FC<TypeScriptPropsProps> = () => <div>TypeScript!</div>;
|
||||||
|
TypeScriptProps.defaultProps = {
|
||||||
|
arrayOfPrimitive: ['foo', 'bar'],
|
||||||
|
arrayOfShape: [{ text: 'foo', value: 'bar' }],
|
||||||
|
};
|
@ -0,0 +1,13 @@
|
|||||||
|
import { Meta, Props } from '@storybook/addon-docs/blocks';
|
||||||
|
import { PropTypesProps } from "./prop-types";
|
||||||
|
import { TypeScriptProps } from "./ts-types";
|
||||||
|
|
||||||
|
<Meta title="Docgen|types" />
|
||||||
|
|
||||||
|
## Prop Types
|
||||||
|
|
||||||
|
<Props of={PropTypesProps} />
|
||||||
|
|
||||||
|
## TypeScript
|
||||||
|
|
||||||
|
<Props of={TypeScriptProps} />
|
@ -10,9 +10,7 @@ export interface PropDefJsDocTag {
|
|||||||
|
|
||||||
export interface PropDef {
|
export interface PropDef {
|
||||||
name: string;
|
name: string;
|
||||||
type: {
|
type: any;
|
||||||
name: string;
|
|
||||||
};
|
|
||||||
required: boolean;
|
required: boolean;
|
||||||
description?: string;
|
description?: string;
|
||||||
defaultValue?: string;
|
defaultValue?: string;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user