-
Notifications
You must be signed in to change notification settings - Fork 0
/
story.js
60 lines (49 loc) · 1.62 KB
/
story.js
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
import React from 'react';
import { storiesOf } from '@storybook/react';
import { Router, Route } from 'react-router';
import { createMemoryHistory } from 'history'
import components from './components';
const storybook = 'Titolo progetto';
const story = storiesOf(storybook, module)
.addDecorator(story => (
<Router history={createMemoryHistory('/')}>
<Route path="/" component={() => story()} />
</Router>
));
function findChild(name, props){
for (let i=0; i<components.length;i++) {
const element = components[i];
if (element.name === name) {
const Child = element.path;
return <Child {...props} />;
}
}
}
components.forEach(element => {
const Component = element.path;
const dataSource = element.data;
const dataStyle = element.style;
if (dataSource && Array.isArray(dataSource)) {
const _story = storiesOf(`${storybook}/${element.name}`, module);
dataSource.forEach(data => {
const props = data.data;
const name = data.name;
if (props.children) {
const _children = [];
props.children.forEach(element => {
_children.push(findChild(element.component, element.props));
});
props.children = _children;
}
_story.addDecorator(story => <div style={dataStyle}>{story()}</div>)
.addDecorator(story => (
<Router history={createMemoryHistory('/')}>
<Route path="/" component={() => story()} />
</Router>
))
.add(name, () => <Component {...props} />);
});
} else {
story.add(element.name, () => <Component />);
}
});