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

Typescript: Fix types of client-api & storystore #7337

Merged
merged 5 commits into from
Jul 15, 2019
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
17 changes: 11 additions & 6 deletions app/vue/src/client/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@ function decorateStory(getStory, decorators) {

const decoratedStory = decorator((p = {}) => {
story = decorated(
Object.assign(
context,
p,
{ parameters: Object.assign(context.parameters || {}, p.parameters) },
{ options: Object.assign(context.options || {}, p.options) }
Hypnosphi marked this conversation as resolved.
Show resolved Hide resolved
)
p
? {
...context,
...p,
parameters: {
...context.parameters,
...p.parameters,
},
}
: context
);

return story;
}, context);

Expand Down
13 changes: 8 additions & 5 deletions examples/official-storybook/stories/core/parameters.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/* eslint-disable react/prop-types */
import React from 'react';

// We would need to add this in config.js idomatically however that would make this file a bit confusing
// We would need to add this in config.js idiomatically however that would make this file a bit confusing
import { addParameters } from '@storybook/react';
import { addDecorator } from '@storybook/react/dist/client/preview';

addDecorator(fn => fn({ customStoryContext: 52, parameters: { customParameter: 42 } }));

addParameters({ globalParameter: 'globalParameter' });

Expand All @@ -15,10 +19,9 @@ export default {

// I'm not sure what we should recommend regarding propTypes? are they a good idea for examples?
// Given we sort of control the props, should we export a prop type?
export const passed = ({
// eslint-disable-next-line react/prop-types
parameters: { options, ...parameters },
}) => <pre>Parameters are {JSON.stringify(parameters, null, 2)}</pre>;
export const passed = ({ parameters: { options, ...parameters }, ...rest }) => (
<pre>StoryContext: {JSON.stringify({ ...rest, parameters }, null, 2)}</pre>
);
passed.story = {
name: 'passed to story',
parameters: { storyParameter: 'storyParameter' },
Expand Down
32 changes: 32 additions & 0 deletions examples/vue-kitchen-sink/__snapshots__/vueshots.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,38 @@ exports[`Storyshots Custom|Decorator for Vue template 1`] = `
</div>
`;

exports[`Storyshots Custom|Decorator for Vue withData 1`] = `
<div
style="border: medium solid blue;"
>
<div
style="border: medium solid red;"
>
<pre>
{
"id": "custom-decorator-for-vue--withdata",
"kind": "Custom|Decorator for Vue",
"name": "withData",
"story": "withData",
"customContext": 52,
"parameters": {
"options": {
"hierarchyRootSeparator": {},
"hierarchySeparator": {},
"docs": {
"iframeHeight": "60px"
}
},
"globalParameter": "globalParameter",
"framework": "vue",
"customParameter": 42
}
}
</pre>
</div>
</div>
`;

exports[`Storyshots Custom|Method for rendering Vue JSX 1`] = `
<button
class="button"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
decorators: [
storyFn => {
// Decorated with story-function
const WrapButton = storyFn();
const WrapButton = storyFn({ customContext: 52, parameters: { customParameter: 42 } });
return {
components: { WrapButton },
template: '<div :style="{ border: borderStyle }"><wrap-button/></div>',
Expand All @@ -30,6 +30,10 @@ export const template = () => ({
template: '<my-button>MyButton with template</my-button>',
});

export const withData = ({ parameters, ...rest }) => ({
template: `<pre>${JSON.stringify({ ...rest, parameters }, null, 2)}</pre>`,
});

export const render = () => ({
render(h) {
return h(MyButton, { props: { color: 'pink' } }, ['renders component: MyButton']);
Expand Down
42 changes: 20 additions & 22 deletions lib/client-api/src/client_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,29 @@ const merge = (a: any, b: any) =>
return undefined;
});

interface DecoratorPropData {
[key: string]: any;
}
const defaultContext: StoryContext = {
id: 'unspecified',
name: 'unspecified',
kind: 'unspecified',
parameters: {},
};

export const defaultDecorateStory = (storyFn: StoryFn, decorators: DecoratorFunction[]) =>
decorators.reduce(
(decorated, decorator) => (
context: StoryContext = {
id: 'unspecified',
name: 'unspecified',
kind: 'unspecified',
parameters: null,
}
) =>
decorator((p: DecoratorPropData = {}) => {
return decorated(
// MUTATION !
Object.assign(
context,
p,
{ parameters: Object.assign(context.parameters || {}, p.parameters) },
{ options: Object.assign(context.options || {}, p.options) }
)
);
}, context),
(decorated, decorator) => (context: StoryContext = defaultContext) =>
decorator(
p =>
decorated(
p
? {
...context,
...p,
parameters: { ...context.parameters, ...p.parameters },
}
: context
),
context
),
storyFn
);

Expand Down
10 changes: 6 additions & 4 deletions lib/client-api/src/story_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import {
const toKey = (input: string) =>
input.replace(/[^a-z0-9]+([a-z0-9])/gi, (...params) => params[1].toUpperCase());

const toChild = (it: {}) => ({ ...it });

let count = 0;

const getId = (): number => {
Expand Down Expand Up @@ -188,8 +186,12 @@ export default class StoryStore extends EventEmitter {
applyDecorators(getOriginal(), getDecorators())
);

const storyFn: StoryFn = (p: object) =>
getDecorated()({ ...identification, parameters: { ...parameters, ...p } });
const storyFn: StoryFn = p =>
getDecorated()({
...identification,
...p,
parameters: { ...parameters, ...(p && p.parameters) },
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
});

_data[id] = {
...identification,
Expand Down