Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TS version of Button story with hooks snippet #22149

Merged
merged 2 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/snippets/react/button-story.with-hooks.js.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
```js
// Button.stories.js|ts|jsx|tsx
// Button.stories.js|jsx

import React, { useState } from 'react';

Expand Down
43 changes: 43 additions & 0 deletions docs/snippets/react/button-story.with-hooks.ts-4-9.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
```tsx
// Button.stories.ts|tsx

import React, { useState } from 'react';
import { Meta, StoryObj } from '@storybook/react';

import { Button } from './Button';

const meta = {
/* 👇 The title prop is optional.
* See https://storybook.js.org/docs/react/configure/overview#configure-story-loading
* to learn how to generate automatic titles
*/
title: 'Button',
component: Button,
} satisfies Meta<typeof Button>;
export default meta;

type Story = StoryObj<typeof meta>;

/*
* Example Button story with React Hooks.
* See note below related to this example.
*/
const ButtonWithHooks = () => {
// Sets the hooks for both the label and primary props
const [value, setValue] = useState('Secondary');
const [isPrimary, setIsPrimary] = useState(false);

// Sets a click handler to change the label's value
const handleOnChange = () => {
if (!isPrimary) {
setIsPrimary(true);
setValue('Primary');
}
};
return <Button primary={isPrimary} onClick={handleOnChange} label={value} />;
};

export const Primary = {
render: () => <ButtonWithHooks />,
} satisfies Story;
```
43 changes: 43 additions & 0 deletions docs/snippets/react/button-story.with-hooks.ts.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
```js
// Button.stories.ts|tsx

import React, { useState } from 'react';
import { Meta, StoryObj } from '@storybook/react';

import { Button } from './Button';

const meta: Meta<typeof Button> = {
/* 👇 The title prop is optional.
* See https://storybook.js.org/docs/react/configure/overview#configure-story-loading
* to learn how to generate automatic titles
*/
title: 'Button',
component: Button,
};
export default meta;

type Story = StoryObj<typeof Button>;

/*
* Example Button story with React Hooks.
* See note below related to this example.
*/
const ButtonWithHooks = () => {
// Sets the hooks for both the label and primary props
const [value, setValue] = useState('Secondary');
const [isPrimary, setIsPrimary] = useState(false);

// Sets a click handler to change the label's value
const handleOnChange = () => {
if (!isPrimary) {
setIsPrimary(true);
setValue('Primary');
}
};
return <Button primary={isPrimary} onClick={handleOnChange} label={value} />;
};

export const Primary: Story = {
render: () => <ButtonWithHooks />,
};
```
2 changes: 1 addition & 1 deletion docs/writing-stories/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Use the _named_ exports of a CSF file to define your component’s stories. We r
<CodeSnippets
paths={[
'react/button-story.with-hooks.js.mdx',
'react/button-story.with-hooks.ts.mdx',
]}
usesCsf3
csf2Path="writing-stories/introduction#snippet-button-story-with-hooks"
Expand Down Expand Up @@ -271,7 +272,6 @@ For instance, suppose you wanted to test your Button component against a differe

<!-- prettier-ignore-end -->


![Parameters background color](./parameters-background-colors.png)

This parameter would instruct the backgrounds addon to reconfigure itself whenever a Button story is selected. Most addons are configured via a parameter-based API and can be influenced at a [global](./parameters.md#global-parameters), [component](./parameters.md#component-parameters) and [story](./parameters.md#story-parameters) level.
Expand Down