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

Make addon-options work with story parameters #3958

Merged
merged 2 commits into from
Aug 6, 2018
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
25 changes: 25 additions & 0 deletions addons/options/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,29 @@ setOptions({

storybook.configure(() => require('./stories'), module);
```

### using withOptions options or parameters

The options-addon accepts story paramters to set options (as shown below).

```js
import { storiesOf } from '@storybook/marko';
import { withKnobs, text, number } from '@storybook/addon-knobs';
import { withOptions } from '@storybook/addon-options';
import Hello from '../components/hello/index.marko';

storiesOf('Addons|Knobs/Hello', module)
.addDecorator(withOptions)
.addDecorator(withKnobs)
.addParameters({ options: { addonPanelInRight: true } })
.add('Simple', () => {
const name = text('Name', 'John Doe');
const age = number('Age', 44);
return Hello.renderSync({
name,
age,
});
});
```

It is also possible to call `setOptions()` inside individual stories. Note that this will bring impact story render performance significantly.
2 changes: 2 additions & 0 deletions addons/options/preview.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const preview = require('./dist/preview');

exports.setOptions = preview.setOptions;
exports.withOptions = preview.withOptions;

preview.init();
31 changes: 30 additions & 1 deletion addons/options/src/preview/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import addons from '@storybook/addons';
import addons, { makeDecorator } from '@storybook/addons';
import { EVENT_ID } from '../shared';

// init function will be executed once when the storybook loads for the
Expand Down Expand Up @@ -41,3 +41,32 @@ export function setOptions(newOptions) {

channel.emit(EVENT_ID, { options });
}

export const withOptions = makeDecorator({
name: 'withOptions',
parameterName: 'options',
skipIfNoParametersOrOptions: false,
allowDeprecatedUsage: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nm123github -- was there a particular reason why you decided to allow deprecated usage for this decorator?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed bunch of other add-ons that had set it to true, so presumed this was the pattern to follow and maybe there was a plan to set them to false at a later date.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. That option is to allow add('story', withOptions(() => <Story />)) style usage (which is the old way addons did it pre-4.0). Given this API never existed for withOptions I don't think we need it.

wrapper: (getStory, context, { newOptions, parameters }) => {
const optionsIn = parameters || newOptions || {};

const channel = addons.getChannel();
if (!channel) {
throw new Error(
'Failed to find addon channel. This may be due to https://github.com/storybooks/storybook/issues/1192.'
);
}

// since 'undefined' and 'null' are the valid values we don't want to
// override the hierarchySeparator or hierarchyRootSeparator if the prop is missing
const options = {
...optionsIn,
...withRegexProp(optionsIn, 'hierarchySeparator'),
...withRegexProp(optionsIn, 'hierarchyRootSeparator'),
};

channel.emit(EVENT_ID, { options });

return getStory(context);
},
});
5 changes: 5 additions & 0 deletions examples/marko-cli/src/stories/addon-knobs.stories.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { storiesOf } from '@storybook/marko';
import { withKnobs, boolean, text, number } from '@storybook/addon-knobs';
import { withOptions } from '@storybook/addon-options';
import Hello from '../components/hello/index.marko';
import MarkoWidgetHello from '../components/marko-widgets/hello';
import MarkoWidgetButton from '../components/marko-widgets/button';

storiesOf('Addons|Knobs/Hello', module)
.addDecorator(withOptions)
.addDecorator(withKnobs)
.addParameters({ options: { addonPanelInRight: true } })
.add('Simple', () => {
const name = text('Name', 'John Doe');
const age = number('Age', 44);
Expand All @@ -16,7 +19,9 @@ storiesOf('Addons|Knobs/Hello', module)
});

storiesOf('Addons|Knobs/Hello (Marko Widget)', module)
.addDecorator(withOptions)
.addDecorator(withKnobs)
.addParameters({ options: { addonPanelInRight: false } })
.add('Simple', () => {
const name = text('Name', 'John Doe');
return MarkoWidgetHello.renderSync({
Expand Down
14 changes: 11 additions & 3 deletions examples/marko-cli/src/stories/index.stories.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { storiesOf } from '@storybook/marko';

import { withOptions } from '@storybook/addon-options';
import Hello from '../components/hello/index.marko';
import ClickCount from '../components/click-count/index.marko';
import StopWatch from '../components/stop-watch/index.marko';
Expand All @@ -8,9 +8,17 @@ import Welcome from '../components/welcome/index.marko';
storiesOf('Welcome', module).add('welcome', () => Welcome.renderSync({}));

storiesOf('Hello', module)
.addDecorator(withOptions)
.addParameters({ options: { addonPanelInRight: false } })
.add('Simple', () => Hello.renderSync({ name: 'abc', age: 20 }))
.add('with ERROR!', () => 'NOT A MARKO RENDER_RESULT');

storiesOf('ClickCount', module).add('Simple', () => ClickCount.renderSync({}));
storiesOf('ClickCount', module)
.addDecorator(withOptions)
.addParameters({ options: { addonPanelInRight: true } })
.add('Simple', () => ClickCount.renderSync({}), { hierarchyRootSeparator: /\|/ });

storiesOf('StopWatch', module).add('Simple', () => StopWatch.renderSync({}));
storiesOf('StopWatch', module)
.addDecorator(withOptions)
.addParameters({ options: { addonPanelInRight: false } })
.add('Simple', () => StopWatch.renderSync({}));