Skip to content

Commit 34398a6

Browse files
committed
Add param selectors
1 parent 6b1962b commit 34398a6

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"trailingComma": "es5",
3+
"tabWidth": 2,
4+
"semi": true,
5+
"singleQuote": true
6+
}

dev/withAdk.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ const DecoratorUI = ({ context, getStory, theme, info }) => (
1010
</div>
1111
);
1212

13-
export const withAdk = createDecorator({
14-
theme: store => store.themes[store.currentTheme],
15-
info: store => JSON.stringify(store, null, 2)
16-
})(DecoratorUI, { isGlobal: true });
13+
export const withAdk = createDecorator(
14+
{
15+
theme: store => store.themes[store.currentTheme],
16+
info: store => JSON.stringify(store, null, 2),
17+
},
18+
{},
19+
{
20+
themeWithFn: (params, { theme }) => ({ fn: () => theme }),
21+
}
22+
)(DecoratorUI, { isGlobal: true });
1723
export const adkParams = setParameters();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@storybook/addon-devkit",
3-
"version": "1.2.4",
3+
"version": "1.3.0",
44
"description": "Storybook Addon Development Kit",
55
"author": {
66
"name": "Oleg Proskurin",

src/decorator.js

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

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

6-
const DecoratorHOC = ({ actions, selectors, Component, parameters, resetParameters, ...props }) => {
7-
return <Component {...actions} {...selectors} {...props} />
6+
const createHOC = paramSelectors => {
7+
const DecoratorWrapper = ({
8+
actions,
9+
selectors,
10+
Component,
11+
parameters,
12+
resetParameters,
13+
...props
14+
}) => {
15+
let params = {};
16+
if (paramSelectors) {
17+
try {
18+
const entries = Object.entries(paramSelectors);
19+
const paramResults = entries
20+
.map(([name, fn]) => {
21+
try {
22+
return { [name]: fn(parameters, selectors) };
23+
} catch (err) {
24+
console.error(err);
25+
return null;
26+
}
27+
})
28+
.filter(Boolean);
29+
params = paramResults.reduce((obj, item) => ({ ...obj, ...item }), {});
30+
} catch (err) {
31+
console.error(err);
32+
}
33+
}
34+
return <Component {...actions} {...selectors} {...params} {...props} />;
35+
};
36+
return DecoratorWrapper;
837
};
938

10-
export const createDecorator = (storeSelectors, createActions) => (
11-
Component,
12-
{ isGlobal = true } = {}
13-
) => initData => (getStory, context) => {
39+
export const createDecorator = (
40+
storeSelectors,
41+
createActions,
42+
paramSelectors
43+
) => (Component, { isGlobal = true } = {}) => initData => (
44+
getStory,
45+
context
46+
) => {
1447
const {
1548
ADDON_ID,
1649
EVENT_ID_INIT,
@@ -33,7 +66,7 @@ export const createDecorator = (storeSelectors, createActions) => (
3366
storyId,
3467
storeSelectors,
3568
createActions,
36-
})(DecoratorHOC);
69+
})(createHOC(paramSelectors));
3770

3871
return (
3972
<WithChannel getStory={getStory} context={context} Component={Component} />

0 commit comments

Comments
 (0)