Skip to content

Commit

Permalink
Add TS version of Button story with hooks snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
kylegach committed Apr 18, 2023
1 parent a547814 commit 987d41e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
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

0 comments on commit 987d41e

Please sign in to comment.