Skip to content

Commit

Permalink
Merge pull request #19489 from storybookjs/deprecate/onBeforeRender
Browse files Browse the repository at this point in the history
remove onBeforeRender
  • Loading branch information
ndelangen authored Oct 14, 2022
2 parents 6429309 + 445fe03 commit 33c6f25
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 40 deletions.
16 changes: 0 additions & 16 deletions code/renderers/react/src/docs/jsxDecorator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,6 @@ describe('jsxDecorator', () => {
expect(mockChannel.emit).not.toHaveBeenCalled();
});

// This is deprecated, but still test it
it('allows the snippet output to be modified by onBeforeRender', async () => {
const storyFn = (args: any) => <div>args story</div>;
const onBeforeRender = (dom: string) => `<p>${dom}</p>`;
const jsx = { onBeforeRender };
const context = makeContext('args', { __isArgsStory: true, jsx }, {});
jsxDecorator(storyFn, context);
await new Promise((r) => setTimeout(r, 0));

expect(mockChannel.emit).toHaveBeenCalledWith(
SNIPPET_RENDERED,
'jsx-test--args',
'<p><div>\n args story\n</div></p>'
);
});

it('allows the snippet output to be modified by transformSource', async () => {
const storyFn = (args: any) => <div>args story</div>;
const transformSource = (dom: string) => `<p>${dom}</p>`;
Expand Down
26 changes: 2 additions & 24 deletions code/renderers/react/src/docs/jsxDecorator.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-disable no-underscore-dangle */
import React, { createElement, ReactElement } from 'react';
import reactElementToJSXString, { Options } from 'react-element-to-jsx-string';
import { dedent } from 'ts-dedent';
import deprecate from 'util-deprecate';

import { addons, useEffect } from '@storybook/addons';
import { StoryContext, ArgsStoryFn, PartialStoryFn } from '@storybook/csf';
Expand All @@ -22,30 +20,10 @@ type JSXOptions = Options & {
enableBeautify?: boolean;
/** Override the display name used for a component */
displayName?: string | Options['displayName'];
/** Deprecated: A function ran after the story is rendered */
onBeforeRender?(dom: string): string;
/** A function ran after a story is rendered (prefer this over `onBeforeRender`) */
/** A function ran after a story is rendered */
transformSource?(dom: string, context?: StoryContext<ReactFramework>): string;
};

/** Run the user supplied onBeforeRender function if it exists */
const applyBeforeRender = (domString: string, options: JSXOptions) => {
if (typeof options.onBeforeRender !== 'function') {
return domString;
}

const deprecatedOnBeforeRender = deprecate(
options.onBeforeRender,
dedent`
StoryFn.parameters.jsx.onBeforeRender was deprecated.
Prefer StoryFn.parameters.jsx.transformSource instead.
See https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-onbeforerender for details.
`
);

return deprecatedOnBeforeRender(domString);
};

/** Run the user supplied transformSource function if it exists */
const applyTransformSource = (
domString: string,
Expand Down Expand Up @@ -127,7 +105,7 @@ export const renderJsx = (code: React.ReactElement, options: JSXOptions) => {
? reactElementToJSXString
: // @ts-expect-error (Converted from ts-ignore)
reactElementToJSXString.default;
let string = applyBeforeRender(toJSXString(child, opts as Options), options);
let string: string = toJSXString(child, opts as Options);

if (string.indexOf('&quot;') > -1) {
const matches = string.match(/\S+=\\"([^"]*)\\"/g);
Expand Down

0 comments on commit 33c6f25

Please sign in to comment.