-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (52 loc) · 1.69 KB
/
index.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
61
62
63
64
65
66
import React from 'react';
import { storiesOf } from '@storybook/react';
// import { action } from '@storybook/addon-actions';
// import { linkTo } from '@storybook/addon-links';
// import { Button, Welcome } from '@storybook/react/demo';
import { Machine } from 'xstate';
import { getShortestPaths } from 'xstate/lib/graph';
import { withStatechart } from 'react-automata/lib';
import '../index.css';
import items from '../images.json';
import { statechart, initialData, Gallery } from '../Gallery';
// WIP - based on react-automata/lib/testStatechart.js
const statechartStories = (stories, config, Component) => {
const paths = getShortestPaths(Machine(config.statechart));
Object.keys(paths).forEach(key => {
const initialData = config.fixtures ? config.fixtures.initialData : null;
const StateMachine = withStatechart(statechart, {
initialData,
devTools: true
})(Component);
class ComponentWrapper extends React.Component<*> {
instance: React$Element<*>;
componentDidMount() {
paths[key].forEach(({ event, state }) => {
const fixtures =
config.fixtures && config.fixtures[state]
? config.fixtures[state][event]
: null;
if (this.instance) this.instance.handleTransition(event, fixtures);
});
}
render() {
return <StateMachine ref={n => (this.instance = n)} />;
}
}
stories.add(key, () => <ComponentWrapper />);
});
};
export const fixtures = {
initialData,
gallery: {
SELECT_PHOTO: { photo: items[0] }
},
loading: {
SEARCH_SUCCESS: { items }
}
};
statechartStories(
storiesOf('Gallery', module),
{ statechart, fixtures },
Gallery
);