Skip to content
This repository was archived by the owner on Jan 31, 2024. It is now read-only.

Commit 01db6c5

Browse files
authored
Merge pull request #103 from TeaSeaLancs/csf3-meta-render
Allow render function to be sourced from story meta
2 parents 499a111 + da9f981 commit 01db6c5

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,29 @@ test('reuses args from composed story', () => {
148148

149149
### CSF3
150150

151-
Storybook 6.4 released a [new version of CSF](https://storybook.js.org/blog/component-story-format-3-0/), where the story can also be an object. This is supported in `@storybook/testing-react`, but you have to match the requisites:
151+
Storybook 6.4 released a [new version of CSF](https://storybook.js.org/blog/component-story-format-3-0/), where the story can also be an object. This is supported in `@storybook/testing-react`, but you have to match one of the requisites:
152152

153-
1 - Either your **story** has a `render` method or your **meta** contains a `component` property:
153+
1 - Your **story** has a `render` method
154+
2 - Or your **meta** has a `render` method
155+
3 - Or your **meta** contains a `component` property
154156

155157
```js
156158
// Example 1: Meta with component property
157159
export default {
158160
title: 'Button',
159-
component: Button // <-- This is strictly necessary
160-
}
161+
component: Button, // <-- This is strictly necessary
162+
};
163+
164+
// Example 2: Meta with render method:
165+
export default {
166+
title: 'Button',
167+
render: args => <Button {...args} />,
168+
};
161169

162-
// Example 2: Story with render method:
170+
// Example 3: Story with render method:
163171
export const Primary = {
164-
render: (args) => <Button {...args}>
165-
}
172+
render: args => <Button {...args} />,
173+
};
166174
```
167175

168176
#### Interactions with play function

example/src/components/AccountForm.stories.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export default {
1111
parameters: {
1212
layout: 'centered',
1313
},
14+
render: (args: AccountFormProps) => (<div>
15+
<p>This uses a custom render from meta</p>
16+
<AccountForm {...args} />
17+
</div>)
1418
} as ComponentMeta<typeof AccountForm>;
1519

1620
type Story = ComponentStoryObj<typeof AccountForm>
@@ -92,7 +96,7 @@ export const VerificationSuccess: Story = {
9296
export const StandardWithRenderFunction: Story = {
9397
...Standard,
9498
render: (args: AccountFormProps) => (<div>
95-
<p>This uses a custom render</p>
99+
<p>This uses a custom render from story</p>
96100
<AccountForm {...args} />
97101
</div>),
98102
};

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function composeStory<GenericArgs>(
8080
);
8181
}
8282

83-
const renderFn = typeof story === 'function' ? story : story.render ?? globalRender;
83+
const renderFn = typeof story === 'function' ? story : story.render ?? meta.render ?? globalRender;
8484
const finalStoryFn = (context: StoryContext<ReactFramework, GenericArgs>) => {
8585
const { passArgsFirst = true } = context.parameters;
8686
if (!passArgsFirst) {

0 commit comments

Comments
 (0)