-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
decorateStory.ts
70 lines (57 loc) · 2.11 KB
/
decorateStory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { DecoratorFunction, LegacyStoryFn, StoryContext, StoryFn } from '@storybook/csf';
import { sanitizeStoryContextUpdate } from '@storybook/store';
import { computesTemplateFromComponent } from './angular-beta/ComputesTemplateFromComponent';
import { AngularFramework } from './types-6-0';
export default function decorateStory(
mainStoryFn: LegacyStoryFn<AngularFramework>,
decorators: DecoratorFunction<AngularFramework>[]
): LegacyStoryFn<AngularFramework> {
const returnDecorators = [cleanArgsDecorator, ...decorators].reduce(
(previousStoryFn: LegacyStoryFn<AngularFramework>, decorator) => (
context: StoryContext<AngularFramework>
) => {
const decoratedStory = decorator((update) => {
return previousStoryFn({
...context,
...sanitizeStoryContextUpdate(update),
});
}, context);
return decoratedStory;
},
(context) => prepareMain(mainStoryFn(context), context)
);
return returnDecorators;
}
const prepareMain = (
story: AngularFramework['storyResult'],
context: StoryContext<AngularFramework>
): AngularFramework['storyResult'] => {
let { template } = story;
const component = story.component ?? context.component;
const userDefinedTemplate = !hasNoTemplate(template);
if (!userDefinedTemplate && component) {
template = computesTemplateFromComponent(component, story.props, '');
}
return {
...story,
...(template ? { template, userDefinedTemplate } : {}),
};
};
function hasNoTemplate(template: string | null | undefined): template is undefined {
return template === null || template === undefined;
}
const cleanArgsDecorator: DecoratorFunction<AngularFramework> = (storyFn, context) => {
if (!context.argTypes || !context.args) {
return storyFn();
}
const argsToClean = context.args;
context.args = Object.entries(argsToClean).reduce((obj, [key, arg]) => {
const argType = context.argTypes[key];
// Only keeps args with a control or an action in argTypes
if (argType.action || argType.control) {
return { ...obj, [key]: arg };
}
return obj;
}, {});
return storyFn();
};