Skip to content

Commit fa091d1

Browse files
committed
Wrap decorators into HOC
1 parent 1c1817c commit fa091d1

File tree

5 files changed

+35
-21
lines changed

5 files changed

+35
-21
lines changed

.storybook/stories.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ storiesOf('Storybook Addon Development Kit', module)
2828
</div>
2929
);
3030
},
31-
adkParams({ currentTheme: 32 })
31+
adkParams({ currentTheme: 1 })
3232
)
3333
.add(
3434
'Stories2',
@@ -40,7 +40,7 @@ storiesOf('Storybook Addon Development Kit', module)
4040
</div>
4141
);
4242
},
43-
adkParams({ currentTheme: 12 })
43+
adkParams({ currentTheme: 0 })
4444
);
4545
// .add('Details', () => (
4646
// <div>

dev/register.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ const AddonPanel = ({
1717
rect,
1818
Layout,
1919
Block,
20-
nextInd,
20+
indInc,
21+
indDec,
2122
update,
2223
}) => {
2324
return (
2425
<Layout style={{ padding: 0 }}>
2526
<Block style={{ ...blockStyle, minWidth: 50 }} size={200}>
2627
kind: {kind}
2728
<br />
28-
<button onClick={() => nextInd()}>setData</button>
29+
<button onClick={() => indInc()}> + </button>
30+
<button onClick={() => indDec()}> - </button>
2931
<br />
3032
<button onClick={() => update({ themes: ['T1', 'T2', 'T3'] })}>
3133
Update
@@ -46,11 +48,17 @@ register(
4648
{
4749
themeInd: store => store.currentTheme,
4850
themeList: store => store.themes,
51+
theme: store => store.themes[store.currentTheme]
4952
},
5053
({ global, local }) => ({
51-
nextInd: local(store => ({
54+
indInc: global(store => ({
55+
...store,
5256
currentTheme: store.currentTheme + 1,
5357
})),
58+
indDec: global(store => ({
59+
...store,
60+
currentTheme: store.currentTheme - 1,
61+
})),
5462
update: global(),
5563
})
5664
)(AddonPanel);

dev/withAdk.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,14 @@ import React from 'react';
22
import { createDecorator, setParameters } from '../src/decorator';
33
import './config'
44

5-
const DecoratorUI = ({ context, getStory, data, parameters }) => (
5+
const DecoratorUI = ({ context, getStory, data, parameters, theme }) => (
66
<div>
7-
ADK: <br />
8-
Data: <br />
9-
<p>{JSON.stringify(data, null, 2)}</p>
10-
context: <br />
11-
<p>
12-
<small>{JSON.stringify(context, null, 2)}</small>
13-
</p>
14-
Params: <br />
15-
<p>{JSON.stringify(parameters, null, 2)}</p>
7+
Theme: {theme} <br />
168
{getStory(context)}
179
</div>
1810
);
1911

20-
export const withAdk = createDecorator()(DecoratorUI, {isGlobal: false});
12+
export const withAdk = createDecorator({
13+
theme: store => store.themes[store.currentTheme]
14+
})(DecoratorUI, {isGlobal: true});
2115
export const adkParams = setParameters()

src/decorator.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ import withChannel from './withChannel';
33

44
import { getConfig } from './config';
55

6-
export const createDecorator = (selectors, createActions) => (Component, { isGlobal = true } = {}) => initData => (getStory, context) => {
6+
const DecoratorHOC = ({ actions, selectors, Component, ...props }) => (
7+
<Component {...actions} {...selectors} {...props} />
8+
);
9+
10+
export const createDecorator = (storeSelectors, createActions) => (
11+
Component,
12+
{ isGlobal = true } = {}
13+
) => initData => (getStory, context) => {
714
const {
815
ADDON_ID,
916
EVENT_ID_INIT,
@@ -24,10 +31,13 @@ export const createDecorator = (selectors, createActions) => (Component, { isGlo
2431
panel: false,
2532
parameters,
2633
storyId,
34+
storeSelectors,
2735
createActions,
28-
})(Component);
36+
})(DecoratorHOC);
2937

30-
return <WithChannel getStory={getStory} context={context} />;
38+
return (
39+
<WithChannel getStory={getStory} context={context} Component={Component} />
40+
);
3141
};
3242

3343
export const setParameters = () => {

src/withChannel.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ const withChannel = ({
3737
...parameters,
3838
};
3939

40+
const isReceived = false;
41+
4042
this.state = {
4143
data: initStateData,
42-
selectors: this.prepareSelectors(initStateData),
43-
isReceived: false,
44+
selectors: isReceived ? this.prepareSelectors(initStateData) : {},
45+
isReceived,
4446
};
4547

4648
this.store = new ChannelStore({

0 commit comments

Comments
 (0)