Skip to content

Instantly share code, notes, and snippets.

@oreqizer
Created April 28, 2017 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oreqizer/5b71ae3ca980f09d59966eb80ff4765f to your computer and use it in GitHub Desktop.
Save oreqizer/5b71ae3ca980f09d59966eb80ff4765f to your computer and use it in GitHub Desktop.
Storybook groups brainstorm
import React from 'react';
import { storiesOf, action, linkTo, getStorybook } from '@kadira/storybook';
import Button from './Button';
import Welcome from './Welcome';
import { WithNotes } from '../notes-addon';
// import { addGroups, Group } from 'storybook-groups';
const Group = ({ children }) => React.Children.only(children);
const css = {
padding: 10,
border: '2px solid black',
};
function addGroups(storiesOf, m) {
const stories = getStorybook();
// Object<string, Object<string, React$Component[]>>
const kinds = stories.reduce((acc, next) => Object.assign({}, acc, {
[next.kind]: next.stories
.map(story => story.render())
.filter(node => node.type === Group)
.reduce((acc, node) => {
// side effects
if (acc[node.props.name]) {
acc[node.props.name].push(node.props.children);
return acc;
}
acc[node.props.name] = [node.props.children];
return acc;
}, {}),
}), {});
Object
.keys(kinds)
.forEach(kind => {
storiesOf(kind, m)
.add('All', () => (
<div>
{Object.keys(kinds[kind]).map(name => (
<div style={css} key={name}>
<h3>{name}</h3>
{kinds[kind][name]}
</div>
))}
</div>
));
});
}
storiesOf('Welcome', module)
.add('to Storybook', () => (
<Welcome showApp={linkTo('Button')} />
));
storiesOf('Button', module)
.add('with text', () => (
<Group name="buttons">
<Button onClick={action('clicked')}>Hello Button</Button>
</Group>
))
.add('with some emoji', () => (
<Group name="buttons">
<Button onClick={action('clicked')}>😀 😎 👍 💯</Button>
</Group>
));
addGroups(storiesOf, module);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment