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: Error handling for invalid Story id #7965

Merged
merged 13 commits into from
Sep 4, 2019
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import DocgenButton from '../../components/DocgenButton';

<Props of="." />

<Preview>
<Story id="nonexistent-story" />
</Preview>

## A random color ColorPalette

<ColorPalette>
Expand Down
10 changes: 10 additions & 0 deletions examples/official-storybook/stories/hooks.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ export const Input = () => {
const [text, setText] = useState('foo');
return <input value={text} onChange={e => setText(e.target.value)} />;
};

export const reactHookCheckbox = () => {
const [on, setOn] = React.useState(false);
return (
<label>
<input type="checkbox" checked={on} onChange={e => setOn(e.target.checked)} />
On
</label>
);
};
2 changes: 2 additions & 0 deletions examples/vue-kitchen-sink/src/stories/addon-docs.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ Just like in React, we can easily reference other stories in our docs:

<Story id="addon-knobs--all-knobs" height="400px" />

<Story id="nonexistent-story" />

## More info

For more info, check out the [Storybook Docs Technical Preview](https://docs.google.com/document/d/1un6YX7xDKEKl5-MVb-egnOYN8dynb5Hf7mq0hipk8JE/edit?usp=sharing).
Expand Down
14 changes: 8 additions & 6 deletions lib/components/src/blocks/Story.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';

import { IFrame } from './IFrame';
import { EmptyBlock } from './EmptyBlock';
import { ZoomContext } from './ZoomContext';
Expand All @@ -10,6 +9,12 @@ export enum StoryError {
NO_STORY = 'No component or story to display',
}

/** error message for Story with null storyFn
* if the story id exists, it must be pointing to a non-existing story
* if there is assigned story id, the story must be empty
*/
const MISSING_STORY = (id?: string) => (id ? `Story "${id}" doesn't exist.` : StoryError.NO_STORY);

interface CommonProps {
title: string;
height?: string;
Expand Down Expand Up @@ -48,15 +53,12 @@ const InlineZoomWrapper: React.FC<{ scale: number }> = ({ scale, children }) =>
);
};

const InlineStory: React.FunctionComponent<InlineStoryProps> = ({
storyFn: ReactStory,
height,
}) => (
const InlineStory: React.FunctionComponent<InlineStoryProps> = ({ storyFn, height, id }) => (
<div style={{ height }}>
<ZoomContext.Consumer>
{({ scale }) => (
<InlineZoomWrapper scale={scale}>
<ReactStory />
{storyFn ? React.createElement(storyFn) : <EmptyBlock>{MISSING_STORY(id)}</EmptyBlock>}
</InlineZoomWrapper>
)}
</ZoomContext.Consumer>
Expand Down