Skip to content

Commit

Permalink
feat: add ability to have function as linkTo parameters (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthuret authored Apr 8, 2017
1 parent 90c96b2 commit 2ef8a6b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
13 changes: 13 additions & 0 deletions .storybook/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,17 @@ storiesOf('Button', module)
))
.add('Second Story', () => (
<button onClick={linkTo('Button', 'First Story')}>Go to "First Story"</button>
))
.add('Multiple Selection', () => (
<MultipleStories onClick={linkTo('Button', (filter) => {
return filter === 'First' ? 'First Story' : 'Second Story';
})}/>
));

const MultipleStories = ({onClick}) => {
return (
<ul>
<button onClick={onClick.bind(null, 'First')}>Go to "First Story"</button>
<button onClick={onClick.bind(null, 'Second')}>Go to "Second Story"</button>
</ul>);
}
6 changes: 5 additions & 1 deletion dist/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

function linkTo(kind, story) {
return function () {
var resolvedKind = typeof kind === 'function' ? kind.apply(undefined, arguments) : kind;
var resolvedStory = typeof story === 'function' ? story.apply(undefined, arguments) : story;

var channel = _storybookAddons2.default.getChannel();
channel.emit(_.EVENT_ID, { kind: kind, story: story });
console.log(resolvedKind);
channel.emit(_.EVENT_ID, { kind: resolvedKind, story: resolvedStory });
};
}
7 changes: 5 additions & 2 deletions src/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import addons from '@kadira/storybook-addons';
import { EVENT_ID } from './';

export function linkTo(kind, story) {
return function () {
return function (...args) {
const resolvedKind = typeof kind === 'function' ? kind(...args) : kind;
const resolvedStory = typeof story === 'function' ? story(...args) : story;

const channel = addons.getChannel();
channel.emit(EVENT_ID, {kind, story});
channel.emit(EVENT_ID, {kind: resolvedKind, story: resolvedStory});
};
}

0 comments on commit 2ef8a6b

Please sign in to comment.