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

Addon-docs: Eval argTypes default value #10812

Merged
merged 3 commits into from
May 17, 2020
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
16 changes: 10 additions & 6 deletions addons/docs/src/frameworks/react/extractArgTypes.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import { PropDef, PropsTableRowsProps } from '@storybook/components';
import { ArgTypes } from '@storybook/api';
import { ArgTypesExtractor } from '../../lib/docgen';
import { trimQuotes } from '../../lib/sbtypes/utils';
import { extractProps } from './extractProps';

const trim = (val: any) => (val && typeof val === 'string' ? trimQuotes(val) : val);

export const extractArgTypes: ArgTypesExtractor = (component) => {
if (component) {
const props = extractProps(component);
const { rows } = props as PropsTableRowsProps;
if (rows) {
return rows.reduce((acc: ArgTypes, row: PropDef) => {
const { type, sbType, defaultValue, jsDocTags, required } = row;
const { type, sbType, defaultValue: defaultSummary, jsDocTags, required } = row;
let defaultValue = defaultSummary && (defaultSummary.detail || defaultSummary.summary);
try {
// eslint-disable-next-line no-eval
defaultValue = eval(defaultValue);
// eslint-disable-next-line no-empty
} catch {}

acc[row.name] = {
...row,
defaultValue: defaultValue && trim(defaultValue.detail || defaultValue.summary),
defaultValue,
type: { required, ...sbType },
table: {
type,
jsDocTags,
defaultValue,
defaultValue: defaultSummary,
},
};
return acc;
Expand Down
2 changes: 1 addition & 1 deletion examples/official-storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
// FIXME: Breaks e2e tests './intro.stories.mdx',
'../../lib/ui/src/**/*.stories.(js|tsx|mdx)',
'../../lib/components/src/**/*.stories.(js|tsx|mdx)',
'./stories/**/*.stories.(js|tsx|mdx)',
'./stories/**/*.stories.(js|ts|tsx|mdx)',
'./../../addons/docs/**/*.stories.tsx',
],
addons: [
Expand Down