Add addParameterEnhancer export to client api

This commit is contained in:
Tom Coleman 2020-02-18 17:22:28 +11:00
parent 4135503de5
commit 95f713e849
5 changed files with 29 additions and 8 deletions

View File

@ -3,7 +3,13 @@ import { logger } from '@storybook/client-logger';
import { StoryFn, Parameters } from '@storybook/addons';
import { toId } from '@storybook/csf';
import { ClientApiParams, DecoratorFunction, ClientApiAddons, StoryApi } from './types';
import {
ClientApiParams,
DecoratorFunction,
ClientApiAddons,
StoryApi,
ParameterEnhancer,
} from './types';
import { applyHooks } from './hooks';
import StoryStore from './story_store';
import { defaultDecorateStory } from './decorators';
@ -25,6 +31,13 @@ export const addParameters = (parameters: Parameters) => {
singleton.addParameters(parameters);
};
export const addParameterEnhancer = (enhancer: ParameterEnhancer) => {
if (!singleton)
throw new Error(`Singleton client API not yet initialized, cannot call addParameterEnhancer`);
singleton.addParameterEnhancer(enhancer);
};
export default class ClientApi {
private _storyStore: StoryStore;
@ -83,6 +96,10 @@ export default class ClientApi {
this._storyStore.addGlobalMetadata({ decorators: [], parameters });
};
addParameterEnhancer = (enhancer: ParameterEnhancer) => {
this._storyStore.addParameterEnhancer(enhancer);
};
clearDecorators = () => {
this._storyStore.clearGlobalDecorators();
};

View File

@ -1,4 +1,4 @@
import ClientApi from './client_api';
import ClientApi, { addDecorator, addParameters, addParameterEnhancer } from './client_api';
import { defaultDecorateStory } from './decorators';
import StoryStore from './story_store';
import ConfigApi from './config_api';
@ -10,6 +10,9 @@ export * from './hooks';
export {
ClientApi,
addDecorator,
addParameters,
addParameterEnhancer,
StoryStore,
ConfigApi,
defaultDecorateStory,

View File

@ -8,16 +8,16 @@ import stable from 'stable';
import { Channel } from '@storybook/channels';
import Events from '@storybook/core-events';
import { logger } from '@storybook/client-logger';
import { Comparator, Parameters, StoryFn, StoryContext } from '@storybook/addons';
import { Comparator, Parameters, Args, StoryFn, StoryContext } from '@storybook/addons';
import {
DecoratorFunction,
StoryMetadata,
StoreData,
AddStoryArgs,
StoreItem,
StoryArgs,
ErrorLike,
GetStorybookKind,
ParameterEnhancer,
} from './types';
import { HooksContext } from './hooks';
import storySort from './storySort';
@ -34,8 +34,6 @@ interface StoryOptions {
type KindMetadata = StoryMetadata & { order: number };
type ParameterEnhancer = (context: StoryContext) => Parameters;
const isStoryDocsOnly = (parameters?: Parameters) => {
return parameters && parameters.docsOnly;
};

View File

@ -21,6 +21,8 @@ export interface StoryMetadata {
decorators: DecoratorFunction[];
}
export type ParameterEnhancer = (context: StoryContext) => Parameters;
export interface StoreItem extends StoryContext {
id: string;
kind: string;

View File

@ -46,12 +46,13 @@ export default ({
if (match) {
const configFilename = match[1];
virtualModuleMapping[entryFilename] = `
import { addDecorator, addParameters } from '@storybook/${framework}';
import { addDecorator, addParameters, addParameterEnhancer } from '@storybook/client-api';
const { decorators, parameters } = require('${configFilename}');
const { decorators, parameters, parameterEnhancers } = require('${configFilename}');
if (decorators) decorators.forEach(decorator => addDecorator(decorator));
if (parameters) addParameters(parameters);
if (parameterEnhancers) parameterEnhancers.forEach(enhancer => addParameterEnhancer(enhancer));
`;
}
});