From bbf676436b1611a6bd43fe723b3dc79fe5a7e30f Mon Sep 17 00:00:00 2001 From: "Leo Y. Li" Date: Thu, 4 Apr 2019 21:57:31 -0400 Subject: [PATCH] #14: rename file and move out memorization from React --- addons/addon-contexts/src/@types/index.ts | 2 +- .../{AddonWrapper.tsx => ReactWrapper.tsx} | 7 +++---- addons/addon-contexts/src/index.ts | 15 +++++++++------ 3 files changed, 13 insertions(+), 11 deletions(-) rename addons/addon-contexts/src/containers/{AddonWrapper.tsx => ReactWrapper.tsx} (79%) diff --git a/addons/addon-contexts/src/@types/index.ts b/addons/addon-contexts/src/@types/index.ts index f29e7a440865..c0d698ccb3b9 100644 --- a/addons/addon-contexts/src/@types/index.ts +++ b/addons/addon-contexts/src/@types/index.ts @@ -78,7 +78,7 @@ export declare type TAddonManager = FCNoChildren<{ }>; export declare type TAddonWrapper = FC<{ channel: Channel; - settings: WrapperSettings; + nodes: ContextNode[]; children: (ready: boolean) => ReactNode; }>; export declare type TMenuController = FCNoChildren< diff --git a/addons/addon-contexts/src/containers/AddonWrapper.tsx b/addons/addon-contexts/src/containers/ReactWrapper.tsx similarity index 79% rename from addons/addon-contexts/src/containers/AddonWrapper.tsx rename to addons/addon-contexts/src/containers/ReactWrapper.tsx index 9c65de5bb13c..442d2fbfc299 100644 --- a/addons/addon-contexts/src/containers/AddonWrapper.tsx +++ b/addons/addon-contexts/src/containers/ReactWrapper.tsx @@ -1,7 +1,7 @@ import React from 'react'; -import { useEffect, useMemo, useReducer, useState } from 'react'; +import { useEffect, useReducer, useState } from 'react'; import { useChannel } from '../libs/hooks'; -import { getNodes, renderAggregatedContexts } from '../libs/helpers'; +import { renderAggregatedContexts } from '../libs/helpers'; import { propsTreeReducer, propsTreeUpdater } from '../libs/ducks'; import { INIT_WRAPPER, UPDATE_MANAGER, UPDATE_WRAPPER } from '../libs/constants'; import { TAddonWrapper, StringObject, StringTuple } from '../@types'; @@ -9,8 +9,7 @@ import { TAddonWrapper, StringObject, StringTuple } from '../@types'; /** * Wrap story under addon-injected contexts */ -export const AddonWrapper: TAddonWrapper = ({ channel, settings, children }) => { - const nodes = useMemo(() => getNodes(settings), []); +export const ReactWrapper: TAddonWrapper = ({ channel, nodes, children }) => { const [propsMap, dispatch] = useReducer(propsTreeReducer, {}); const [ready, setReady] = useState(false); diff --git a/addons/addon-contexts/src/index.ts b/addons/addon-contexts/src/index.ts index 5ae541d7cb2b..be871b81a36b 100644 --- a/addons/addon-contexts/src/index.ts +++ b/addons/addon-contexts/src/index.ts @@ -1,15 +1,18 @@ -import { createElement as h, Fragment } from 'react'; +import { createElement as h } from 'react'; import addons, { makeDecorator } from '@storybook/addons'; -import { AddonWrapper } from './containers/AddonWrapper'; +import { ReactWrapper } from './containers/ReactWrapper'; import { ID, PARAM } from './libs/constants'; +import { getNodes } from './libs/helpers'; import { WithContexts, Wrapper } from './@types'; -const wrapper: Wrapper = (getStory, context, settings) => - h(AddonWrapper, { - settings, +const wrapper: Wrapper = (getStory, context, settings) => { + const nodes = getNodes(settings); + return h(ReactWrapper, { + nodes, channel: addons.getChannel(), - children: (ready: boolean) => () => (ready ? getStory(context) : h(Fragment)), + children: (ready: boolean) => () => (ready ? getStory(context) : h('div')), }); +}; export const withContexts: WithContexts = makeDecorator({ name: ID,