Skip to content

Commit

Permalink
FIX tricky typing for extending framework supports
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo Y. Li committed Apr 21, 2019
1 parent 5100a4f commit 8caf900
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions addons/contexts/src/preview/frameworks/react.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';
import { createAddonDecorator, Renderer } from '../index';
import { createAddonDecorator, Render } from '../index';
import { addonContextsAPI } from '../api';

/**
* This is the framework specific bindings for React.
* '@storybook/react' expects the returning object from a decorator to be a 'React Element' (vNode).
*/
export const renderReact: Renderer = (contextNodes, propsMap, getStoryVNode) => {
export const renderReact: Render<React.ReactElement> = (contextNodes, propsMap, getStoryVNode) => {
const { getRendererFrom } = addonContextsAPI();
return getRendererFrom(React.createElement)(contextNodes, propsMap, getStoryVNode);
};
Expand Down
8 changes: 4 additions & 4 deletions addons/contexts/src/preview/frameworks/vue.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Vue, { Component } from 'vue';
import { createAddonDecorator, Renderer } from '../index';
import Vue from 'vue';
import { createAddonDecorator, Render } from '../index';
import { addonContextsAPI } from '../api';
import { ID } from '../../constants';

/**
* This is the framework specific bindings for Vue.
* '@storybook/vue' expects the returning object from a decorator to be a 'VueComponent'.
*/
export const renderVue: Renderer = (contextNodes, propsMap, getStoryVNode) => {
export const renderVue: Render<Vue.Component> = (contextNodes, propsMap, getStoryVNode) => {
const { getRendererFrom, updateReactiveSystem } = addonContextsAPI();
const reactiveProps = updateReactiveSystem(propsMap);
return Vue.extend({
Expand All @@ -16,7 +16,7 @@ export const renderVue: Renderer = (contextNodes, propsMap, getStoryVNode) => {
render: createElement =>
getRendererFrom((component, props, children) =>
createElement(component, { props }, [children])
)(contextNodes, reactiveProps, () => createElement(getStoryVNode() as Component)),
)(contextNodes, reactiveProps, () => createElement(getStoryVNode())),
});
};

Expand Down
6 changes: 3 additions & 3 deletions addons/contexts/src/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import { AddonSetting, AnyFunctionReturns, ContextNode, PropsMap } from '../type
* who is also knowing how to communicate with the Storybook manager (in React) via the Storybook
* event system.
*
* @param {Renderer} render - framework specific bindings
* @param {Render} render - framework specific bindings
*/
export type Renderer = (...args: [ContextNode[], PropsMap, AnyFunctionReturns<unknown>]) => unknown;
type CreateAddonDecorator = (renderer: Renderer) => (contexts: AddonSetting[]) => unknown;
export type Render<T> = (...args: [ContextNode[], PropsMap, AnyFunctionReturns<T>]) => T;
type CreateAddonDecorator = <T>(render: Render<T>) => (contexts: AddonSetting[]) => T;

export const createAddonDecorator: CreateAddonDecorator = render => {
const wrapper: StoryWrapper = (getStory, context, settings: any) => {
Expand Down
4 changes: 3 additions & 1 deletion addons/contexts/src/preview/libs/getContextNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ type GetContextNodes = (settings: WrapperSettings) => ContextNode[];
export const getContextNodes: GetContextNodes = ({ options, parameters }) => {
const titles = Array()
.concat(options, parameters)
.map(({ title } = {}) => title);
.filter(Boolean)
.map(({ title }) => title);

return Array.from(new Set(titles))
.filter(Boolean)
.map(title =>
Expand Down
2 changes: 1 addition & 1 deletion addons/contexts/src/preview/libs/getRendererFrom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const _getAggregatedWrap: GetAggregatedWrap = h => (components, props, op
*/
type GetRendererFrom = <T>(
h: AnyFunctionReturns<T>
) => (contextNodes: ContextNode[], propsMap: PropsMap, getStoryVNode: AnyFunctionReturns<any>) => T;
) => (contextNodes: ContextNode[], propsMap: PropsMap, getStoryVNode: AnyFunctionReturns<T>) => T;

export const getRendererFrom: GetRendererFrom = h => (contextNodes, propsMap, getStoryVNode) =>
contextNodes
Expand Down

0 comments on commit 8caf900

Please sign in to comment.