forked from storybookjs/storybook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TS version of Button story with hooks snippet
- Loading branch information
Showing
4 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 />, | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters