Skip to content

Commit

Permalink
Addon-docs: Fix subcomponents display logic
Browse files Browse the repository at this point in the history
  • Loading branch information
shilman committed Jul 4, 2020
1 parent 8496d1f commit 0589e9f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
26 changes: 10 additions & 16 deletions addons/docs/src/blocks/Props.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type ComponentsProps = BaseProps & {

type StoryProps = BaseProps & {
story: '.' | string;
showComponents?: boolean;
showComponent?: boolean;
};

type PropsProps = BaseProps | OfProps | ComponentsProps | StoryProps;
Expand Down Expand Up @@ -134,7 +134,7 @@ export const StoryTable: FC<StoryProps & { components: Record<string, Component>
parameters: { argTypes },
storyStore,
} = context;
const { story, showComponents, components, include, exclude } = props;
const { story, components, include, exclude } = props;
let storyArgTypes;
try {
let storyId;
Expand All @@ -158,12 +158,6 @@ export const StoryTable: FC<StoryProps & { components: Record<string, Component>
}
storyArgTypes = filterArgTypes(storyArgTypes, include, exclude);

// This code handles three cases:
// 1. the story has args, in which case we want to show controls for the story
// 2. the story has args, and the user specifies showComponents, in which case
// we want to show controls for the primary component AND show props for each component
// 3. the story has NO args, in which case we want to show props for each component

// eslint-disable-next-line prefer-const
let [args, updateArgs] = useArgs(storyId, storyStore);
let tabs = { Story: { rows: storyArgTypes, args, updateArgs } } as Record<
Expand All @@ -180,10 +174,7 @@ export const StoryTable: FC<StoryProps & { components: Record<string, Component>
tabs = {};
}

if (showComponents || !storyHasArgsWithControls) {
tabs = addComponentTabs(tabs, components, context, include, exclude);
}

tabs = addComponentTabs(tabs, components, context, include, exclude);
return <TabbedArgsTable tabs={tabs} />;
} catch (err) {
return <ArgsTable error={err.message} />;
Expand All @@ -205,14 +196,17 @@ export const Props: FC<PropsProps> = (props) => {
} = context;

const { include, exclude, components } = props as ComponentsProps;
const { story } = props as StoryProps;
const { story, showComponent } = props as StoryProps;

let allComponents = components;
const main = getComponent(props, context);

if (!allComponents && main) {
const mainLabel = getComponentName(main);
allComponents = { [mainLabel]: main, ...subcomponents };
const mainLabel = getComponentName(main);
if (!allComponents) {
if (!story || showComponent) {
allComponents = { [mainLabel]: main };
}
allComponents = { ...allComponents, ...subcomponents };
}

if (story) {
Expand Down
14 changes: 11 additions & 3 deletions examples/official-storybook/stories/addon-docs/props.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { MemoButton } from '../../components/MemoButton';
<Meta
title="Addons/Docs/props"
component={ButtonGroup}
subcomponents={{ SubGroup }}
parameters={{ controls: { expanded: false } }}
/>

Expand Down Expand Up @@ -35,7 +36,12 @@ export const ArgsStory = (args = {}) => (
arrayArg: ['a', 'b'],
}}
argTypes={{
boolArg: { name: 'boolArg', type: { name: 'boolean' }, description: 'bool description' },
boolArg: {
name: 'boolArg',
type: { name: 'boolean' },
description: 'bool description',
control: 'boolean',
},
stringArg: {
name: 'stringArg',
type: { name: 'string' },
Expand All @@ -47,11 +53,13 @@ export const ArgsStory = (args = {}) => (
detail: 'some long bar default',
},
},
control: 'text',
},
arrayArg: {
name: 'arrayArg',
type: { name: 'array', value: { name: 'string' } },
description: 'baz description',
control: 'array',
},
selectArg: {
name: 'selectArg',
Expand All @@ -74,9 +82,9 @@ export const ArgsStory = (args = {}) => (

<Props story="ArgTypes" />

## ArgTypes w/ Components
## ArgTypes w/ Component

<Props story="ArgTypes" showComponents />
<Props story="ArgTypes" showComponent />

## Args

Expand Down

0 comments on commit 0589e9f

Please sign in to comment.