Skip to content

Commit

Permalink
Add param selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
usulpro committed Dec 21, 2019
1 parent 6b1962b commit 34398a6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true
}
14 changes: 10 additions & 4 deletions dev/withAdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ const DecoratorUI = ({ context, getStory, theme, info }) => (
</div>
);

export const withAdk = createDecorator({
theme: store => store.themes[store.currentTheme],
info: store => JSON.stringify(store, null, 2)
})(DecoratorUI, { isGlobal: true });
export const withAdk = createDecorator(
{
theme: store => store.themes[store.currentTheme],
info: store => JSON.stringify(store, null, 2),
},
{},
{
themeWithFn: (params, { theme }) => ({ fn: () => theme }),
}
)(DecoratorUI, { isGlobal: true });
export const adkParams = setParameters();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@storybook/addon-devkit",
"version": "1.2.4",
"version": "1.3.0",
"description": "Storybook Addon Development Kit",
"author": {
"name": "Oleg Proskurin",
Expand Down
47 changes: 40 additions & 7 deletions src/decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,47 @@ import withChannel from './withChannel';

import { getConfig } from './config';

const DecoratorHOC = ({ actions, selectors, Component, parameters, resetParameters, ...props }) => {
return <Component {...actions} {...selectors} {...props} />
const createHOC = paramSelectors => {
const DecoratorWrapper = ({
actions,
selectors,
Component,
parameters,
resetParameters,
...props
}) => {
let params = {};
if (paramSelectors) {
try {
const entries = Object.entries(paramSelectors);
const paramResults = entries
.map(([name, fn]) => {
try {
return { [name]: fn(parameters, selectors) };
} catch (err) {
console.error(err);
return null;
}
})
.filter(Boolean);
params = paramResults.reduce((obj, item) => ({ ...obj, ...item }), {});
} catch (err) {
console.error(err);
}
}
return <Component {...actions} {...selectors} {...params} {...props} />;
};
return DecoratorWrapper;
};

export const createDecorator = (storeSelectors, createActions) => (
Component,
{ isGlobal = true } = {}
) => initData => (getStory, context) => {
export const createDecorator = (
storeSelectors,
createActions,
paramSelectors
) => (Component, { isGlobal = true } = {}) => initData => (
getStory,
context
) => {
const {
ADDON_ID,
EVENT_ID_INIT,
Expand All @@ -33,7 +66,7 @@ export const createDecorator = (storeSelectors, createActions) => (
storyId,
storeSelectors,
createActions,
})(DecoratorHOC);
})(createHOC(paramSelectors));

return (
<WithChannel getStory={getStory} context={context} Component={Component} />
Expand Down

0 comments on commit 34398a6

Please sign in to comment.