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

Feature: Extend setAddon API #1463

Closed
wants to merge 4 commits into from
Closed
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
26 changes: 17 additions & 9 deletions app/react/src/client/preview/client_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,6 @@ export default class ClientApi {
kind,
};

// apply addons
Object.keys(this._addons).forEach(name => {
const addon = this._addons[name];
api[name] = (...args) => {
addon.apply(api, args);
return api;
};
});

api.add = (storyName, getStory) => {
if (typeof storyName !== 'string') {
throw new Error(`Invalid or missing storyName provided for a "${kind}" story.`);
Expand Down Expand Up @@ -88,6 +79,23 @@ export default class ClientApi {
return api;
};

const initExp = /^_init/;

// apply addons
Object.keys(this._addons).filter(name => !name.match(initExp)).forEach(name => {
const addon = this._addons[name];
api[name] = (...args) => {
addon.apply(api, args);
return api;
};
});

// init addons
Object.keys(this._addons).filter(name => name.match(initExp)).forEach(name => {
const addon = this._addons[name];
addon.apply(api);
});

return api;
}

Expand Down
7 changes: 7 additions & 0 deletions examples/cra-kitchen-sink/.storybook/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
import { configure, setAddon } from '@storybook/react';
import { setOptions } from '@storybook/addon-options';
import infoAddon from '@storybook/addon-info';
Expand All @@ -16,6 +17,12 @@ setOptions({

setAddon(infoAddon);

setAddon({
_initBackground() {
this.add('Toc',() => <div> Info about <b>{this.kind}:</b> it's just a very simple example!</div>)
}
})

function loadStories() {
require('../src/stories/index');
require('../src/stories/storybook-components');
Expand Down