From 34398a65e0a51eee6def55f02620a9283e96657c Mon Sep 17 00:00:00 2001 From: Oleg Proskurin Date: Sat, 21 Dec 2019 22:52:42 +0300 Subject: [PATCH] Add param selectors --- .prettierrc | 6 ++++++ dev/withAdk.js | 14 ++++++++++---- package.json | 2 +- src/decorator.js | 47 ++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..0a72520 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,6 @@ +{ + "trailingComma": "es5", + "tabWidth": 2, + "semi": true, + "singleQuote": true +} diff --git a/dev/withAdk.js b/dev/withAdk.js index dec380e..600e49f 100644 --- a/dev/withAdk.js +++ b/dev/withAdk.js @@ -10,8 +10,14 @@ const DecoratorUI = ({ context, getStory, theme, info }) => ( ); -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(); diff --git a/package.json b/package.json index 8d73832..3a3dee2 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/decorator.js b/src/decorator.js index 947297c..7572da2 100644 --- a/src/decorator.js +++ b/src/decorator.js @@ -3,14 +3,47 @@ import withChannel from './withChannel'; import { getConfig } from './config'; -const DecoratorHOC = ({ actions, selectors, Component, parameters, resetParameters, ...props }) => { - return +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 ; + }; + 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, @@ -33,7 +66,7 @@ export const createDecorator = (storeSelectors, createActions) => ( storyId, storeSelectors, createActions, - })(DecoratorHOC); + })(createHOC(paramSelectors)); return (