Skip to content

Commit

Permalink
#14: rename file and move out memorization from React
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo Y. Li committed Apr 17, 2019
1 parent d7bca4d commit 8e9c79c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion addons/addon-contexts/src/@types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
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';

/**
* 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);

Expand Down
15 changes: 9 additions & 6 deletions addons/addon-contexts/src/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 8e9c79c

Please sign in to comment.