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

feat: disable jsx per story #110

Merged
merged 3 commits into from
Jun 5, 2020
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,22 @@ storiesOf('test 2', module).addWithJSX(
// <div>Inner HTML</div>
```

### Disable JSX Addon

If enabled globally, the JSX addon can be disabled on individual stories:

```jsx
export const Simple = () => <div>Hello</div>;

Simple.story = {
parameters: {
jsx: {
disable: true
}
}
};
```

### Not JSX

If a Vue story defines its view with a template string then it will be displayed
Expand Down
60 changes: 32 additions & 28 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import React from 'react';
import {
addons,
makeDecorator,
StoryContext,
StoryFn,
StoryApi,
Expand Down Expand Up @@ -157,39 +158,42 @@ const defaultOpts = {
};

/** Extract components from story and emit them to the panel */
export const jsxDecorator = (
storyFn: StoryFn<React.ReactElement<any>>,
parameters?: StoryContext
) => {
const channel = addons.getChannel();
const story: ReturnType<typeof storyFn> & VueComponent = storyFn();
const options = {
...defaultOpts,
...((parameters && parameters.parameters.jsx) || {})
} as Required<JSXOptions>;

let components: ComponentMap = {};
let jsx = '';

if (story.template) {
if (options.enableBeautify) {
jsx = beautifyHTML(story.template, options);
export const jsxDecorator = makeDecorator({
name: 'jsx',
parameterName: 'jsx',
wrapper: (storyFn: any, parameters: StoryContext) => {
const story: ReturnType<typeof storyFn> & VueComponent = storyFn();

const channel = addons.getChannel();

const options = {
...defaultOpts,
...((parameters && parameters.parameters.jsx) || {})
} as Required<JSXOptions>;

let components: ComponentMap = {};
let jsx = '';

if (story.template) {
if (options.enableBeautify) {
jsx = beautifyHTML(story.template, options);
} else {
jsx = story.template;
}
} else {
jsx = story.template;
}
} else {
const rendered = renderJsx(story, options);
const rendered = renderJsx(story, options);

if (rendered) {
jsx = rendered;
components = getDocs(story);
if (rendered) {
jsx = rendered;
components = getDocs(story);
}
}
}

channel.emit(EVENTS.ADD_JSX, (parameters || {}).id, jsx, components);
channel.emit(EVENTS.ADD_JSX, (parameters || {}).id, jsx, components);

return story;
};
return story;
}
});

export default {
addWithJSX(this: StoryApi, kind: string, storyFn: StoryFn<any>) {
Expand Down