diff --git a/types/storybook__addon-info/index.d.ts b/types/storybook__addon-info/index.d.ts index 09c9a856ac..845feff65e 100644 --- a/types/storybook__addon-info/index.d.ts +++ b/types/storybook__addon-info/index.d.ts @@ -1,50 +1,70 @@ -// Type definitions for @storybook/addon-info 4.1 +// Type definitions for @storybook/addon-info 5.2 // Project: https://github.com/storybookjs/storybook, https://github.com/storybookjs/storybook/tree/master/addons/info // Definitions by: Mark Kornblum // Mattias Wikstrom // Kevin Lee // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.8 +// TypeScript Version: 3.1 -import * as React from 'react'; -import { RenderFunction, StoryDecorator } from '@storybook/react'; +import { ComponentType, ReactElement, ReactPortal } from 'react'; +import { DecoratorFunction, StoryFn, StoryContext, Parameters, StoryApi } from '@storybook/addons'; export interface WrapStoryProps { - storyFn?: RenderFunction; - context?: object; - options?: object; + storyFn?: StoryFn; + context?: object; + options?: object; +} + +export interface TableComponentOptionProps { + propDefinitions: Array<{ + property: string; + propType: object | string; // TODO: info about what this object is... + required: boolean; + description: string; + defaultValue: any; + }>; } export interface Options { - text?: string; - header?: boolean; - inline?: boolean; - source?: boolean; - propTables?: Array> | false; - propTablesExclude?: Array>; - styles?: object; - components?: { [key: string]: React.ComponentType }; - marksyConf?: object; - maxPropsIntoLine?: number; - maxPropObjectKeys?: number; - maxPropArrayLength?: number; - maxPropStringLength?: number; - TableComponent?: React.ComponentType<{ - propDefinitions: Array<{ - property: string; - propType: object | string; // TODO: info about what this object is... - required: boolean; - description: string; - defaultValue: any; - }> - }>; - excludedPropTypes?: string[]; + text?: string; + header?: boolean; + inline?: boolean; + source?: boolean; + propTables?: Array> | false; + propTablesExclude?: Array>; + styles?: object; + components?: { [key: string]: ComponentType }; + /** + * @deprecated "marksyConf" option has been renamed to "components" + */ + marksyConf?: object; + maxPropsIntoLine?: number; + maxPropObjectKeys?: number; + maxPropArrayLength?: number; + maxPropStringLength?: number; + TableComponent?: ComponentType; + excludedPropTypes?: string[]; } -// TODO: it would be better to use type inference for the parameters -// type DecoratorParams = StoryDecorator extends (...a: infer A) => any ? A: never; -export function withInfo(story: RenderFunction, context: { kind: string, story: string }): ReturnType; -// Legacy, but supported -export function withInfo(textOrOptions?: string | Options): (storyFn: RenderFunction) => (context?: object) => React.ReactElement; +export function withInfo(story: StoryFn, context: StoryContext): ReturnType>; +// Legacy, but supported +/** + * @deprecated withInfo wrapper is deprecated, use the info parameter globally or on each story + */ +export function withInfo( + textOrOptions?: string | Options, +): (storyFn: StoryFn) => (context?: object) => ReactElement; + +/** + * @deprecated setDefaults is deprecated. Instead, you can pass options into withInfo(options) directly, or use the info parameter. + */ export function setDefaults(newDefaults: Options): Options; + +declare module '@storybook/addons' { + interface ClientStoryApi { + storiesOf(kind: string, module: NodeModule): StoryApi; + addParameters(parameter: Parameters & { info: Options }): StoryApi; + addDecorator(decorator: DecoratorFunction): StoryApi; + } +} diff --git a/types/storybook__addon-info/package.json b/types/storybook__addon-info/package.json new file mode 100644 index 0000000000..0c9225e65d --- /dev/null +++ b/types/storybook__addon-info/package.json @@ -0,0 +1,7 @@ +{ + "private": true, + "dependencies": { + "@storybook/addons": "^5.2.0", + "@storybook/react": "^5.2.0" + } +} diff --git a/types/storybook__addon-info/storybook__addon-info-tests.tsx b/types/storybook__addon-info/storybook__addon-info-tests.tsx index 0e581c822d..13f83cc3fe 100644 --- a/types/storybook__addon-info/storybook__addon-info-tests.tsx +++ b/types/storybook__addon-info/storybook__addon-info-tests.tsx @@ -1,77 +1,119 @@ -/// +import React, { Component, FunctionComponent, HTMLAttributes } from 'react'; +import { addDecorator, addParameters, storiesOf } from '@storybook/react'; +import { setDefaults, withInfo, TableComponentOptionProps } from '@storybook/addon-info'; -import * as React from 'react'; -import { addDecorator, storiesOf } from '@storybook/react'; -import { setDefaults, withInfo } from '@storybook/addon-info'; - -const { Component } = React; - -const TableComponent = ({ propDefinitions }: { propDefinitions: Array<{ - property: string; - propType: { [key: string]: any} | string; - required: boolean; - description: string; - defaultValue: any; -}> }) => ( +const TableComponent: FunctionComponent = ({ propDefinitions }) => ( - - - - - - - + + + + + + + - {propDefinitions.map(row => ( - - - - - - - ))} + {propDefinitions.map(row => ( + + + + + + + ))}
propertypropTyperequireddefaultdescription
propertypropTyperequireddefaultdescription
{row.property}{row.required ? 'yes' : '-'} - {row.defaultValue === undefined ? '-' : row.defaultValue} - {row.description}
{row.property}{row.required ? 'yes' : '-'}{row.defaultValue === undefined ? '-' : row.defaultValue}{row.description}
); +const BaseButton: FunctionComponent & { label: string }> = ({ label }) => ( + +); + +const CustomButton: FunctionComponent & { label: string }> = ({ label, style }) => ( + +); + +// `withInfo` used as global decorator addDecorator(withInfo); +// define parameters of info addon as part of global parameters +addParameters({ + info: { + text: 'String or React Element with docs about my component', + inline: true, + header: true, + source: true, + propTables: [CustomButton], + propTablesExclude: [BaseButton], + styles: {}, + components: {}, + marksyConf: {}, + maxPropObjectKeys: 1, + maxPropArrayLength: 2, + maxPropsIntoLine: 3, + maxPropStringLength: 4, + TableComponent, + excludedPropTypes: [], + }, +}); + +// `withInfo` used as story decorator +storiesOf('Component', module).addDecorator(withInfo); + +// Set parameters for multiple config +storiesOf('Component', module) + .addParameters({ + info: { + header: '', + // Your settings + }, + }) + .add('with some emoji', () => ); + +storiesOf('Component', module) + .add( + 'with some emoji', + () => , + { info: { inline: true, header: false } }, // Make your component render inline with the additional info + ) + .add( + 'with no emoji', + () => , + { info: '☹️ no emojis' }, // Add additional info text directly + ); + +// Check deprecated option functions setDefaults({ inline: false, - propTables: false + propTables: false, }); storiesOf('Component', module) - .add('no info', - withInfo()(() => - Click the "?" mark at top-right to view the info. - ) - ) - .add('simple info', - withInfo('doc string about my component')(() => - Click the "?" mark at top-right to view the info. - ) - ) - .add('using an options object', - withInfo({ - text: 'String or React Element with docs about my component', - inline: true, - header: true, - source: true, - styles: {}, - components: {}, - marksyConf: {}, - maxPropObjectKeys: 1, - maxPropArrayLength: 2, - maxPropsIntoLine: 3, - maxPropStringLength: 4, - TableComponent, - excludedPropTypes: [], - })(() => - Click the "?" mark at top-right to view the info. - ) - ); + .add('no info', withInfo()(() => Click the "?" mark at top-right to view the info.)) + .add( + 'simple info', + withInfo('doc string about my component')(() => ( + Click the "?" mark at top-right to view the info. + )), + ) + .add( + 'using an options object', + withInfo({ + text: 'String or React Element with docs about my component', + inline: true, + header: true, + source: true, + styles: {}, + components: {}, + marksyConf: {}, + maxPropObjectKeys: 1, + maxPropArrayLength: 2, + maxPropsIntoLine: 3, + maxPropStringLength: 4, + TableComponent, + excludedPropTypes: [], + })(() => Click the "?" mark at top-right to view the info.), + ); diff --git a/types/storybook__addon-info/tsconfig.json b/types/storybook__addon-info/tsconfig.json index ad08c9163b..7fa9a9917a 100644 --- a/types/storybook__addon-info/tsconfig.json +++ b/types/storybook__addon-info/tsconfig.json @@ -1,32 +1,21 @@ { "compilerOptions": { "module": "commonjs", - "lib": [ - "es6" - ], + "lib": ["dom", "es6"], "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "jsx": "react", - "typeRoots": [ - "../" - ], + "typeRoots": ["../"], "paths": { - "@storybook/addon-info": [ - "storybook__addon-info" - ], - "@storybook/react": [ - "storybook__react" - ] + "@storybook/addon-info": ["storybook__addon-info"] }, "types": [], "noEmit": true, - "forceConsistentCasingInFileNames": true + "forceConsistentCasingInFileNames": true, + "esModuleInterop": true }, - "files": [ - "index.d.ts", - "storybook__addon-info-tests.tsx" - ] -} \ No newline at end of file + "files": ["index.d.ts", "storybook__addon-info-tests.tsx"] +}