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: Fix controls column display logic #10473

Merged
merged 3 commits into from
Apr 20, 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
8 changes: 7 additions & 1 deletion addons/docs/src/blocks/Props.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,13 @@ export const StoryTable: FC<StoryProps & { components: Record<string, Component>
storyArgTypes = data.parameters.argTypes;
}
storyArgTypes = filterArgTypes(storyArgTypes, include, exclude);
const [args, updateArgs] = useArgs(storyId, storyStore);

// eslint-disable-next-line prefer-const
let [args, updateArgs] = useArgs(storyId, storyStore);
if (!storyArgTypes || !Object.values(storyArgTypes).find((v) => !!v?.control)) {
updateArgs = null;
}

let tabs = { Story: { rows: storyArgTypes, args, updateArgs } } as Record<
string,
ArgsTableProps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ export const FooBar = ({ foo, bar } = {}) => (

<Preview>
<Story name="Component" argTypes={{ background: { control: { type: 'color' } } }}>
{(args) => <DocgenButton {...args} />}
{({ background, children }) => {
const contents = children && children.length > 0 ? children : <DocgenButton label="child" />;
return <ButtonGroup background={background}>{contents}</ButtonGroup>;
}}
</Story>
</Preview>

Expand Down
12 changes: 5 additions & 7 deletions lib/components/src/blocks/ArgsTable/ArgsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ export type ArgsTableProps = ArgsTableRowProps | ArgsTableErrorProps;
type RowProps = SectionRowProps | ArgRowProps;

const ArgsTableRow: FC<RowProps> = (props) => {
const { section, showControls } = props as SectionRowProps;
const { section, updateArgs } = props as SectionRowProps;
if (section) {
return <SectionRow {...{ section, showControls }} />;
return <SectionRow {...{ section, updateArgs }} />;
}
const { row, arg, updateArgs } = props as ArgRowProps;
const { row, arg } = props as ArgRowProps;
return <ArgRow {...{ row, arg, updateArgs }} />;
};

Expand Down Expand Up @@ -181,8 +181,6 @@ export const ArgsTable: FC<ArgsTableProps> = (props) => {
});
}

const showControls = args && Object.keys(args).length > 0;

const allRows: { key: string; value: any }[] = [];
Object.entries(ungroupedRows).forEach(([key, row]) => {
const arg = args && args[key];
Expand All @@ -194,7 +192,7 @@ export const ArgsTable: FC<ArgsTableProps> = (props) => {
Object.keys(categoryRows).forEach((category) => {
const catRows = categoryRows[category];
if (Object.keys(catRows).length > 0) {
allRows.push({ key: category, value: { section: category, showControls } });
allRows.push({ key: category, value: { section: category, updateArgs } });
Object.entries(catRows).forEach(([key, row]) => {
const arg = args && args[key];
allRows.push({
Expand All @@ -216,7 +214,7 @@ export const ArgsTable: FC<ArgsTableProps> = (props) => {
<th>Name</th>
<th>Description</th>
<th>Default</th>
{showControls ? <th>Control</th> : null}
{updateArgs ? <th>Control</th> : null}
</tr>
</thead>
<tbody className="docblock-propstable-body">
Expand Down
7 changes: 4 additions & 3 deletions lib/components/src/blocks/ArgsTable/SectionRow.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { FC } from 'react';
import { transparentize } from 'polished';
import { styled } from '@storybook/theming';
import { Args } from './types';

export interface SectionRowProps {
section: string;
showControls: boolean;
updateArgs?: (args: Args) => void;
}

const SectionTh = styled.th<{}>(({ theme }) => ({
Expand All @@ -20,8 +21,8 @@ const SectionTh = styled.th<{}>(({ theme }) => ({
background: `${theme.background.app} !important`,
}));

export const SectionRow: FC<SectionRowProps> = ({ section, showControls }) => (
export const SectionRow: FC<SectionRowProps> = ({ section, updateArgs }) => (
<tr>
<SectionTh colSpan={showControls ? 4 : 3}>{section}</SectionTh>
<SectionTh colSpan={updateArgs ? 4 : 3}>{section}</SectionTh>
</tr>
);