-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Got "test-components" concept working
- Loading branch information
Showing
5 changed files
with
66 additions
and
16 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
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
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,7 @@ | ||
import React, { FC } from 'react'; | ||
|
||
export const Button: FC<{ onClick: () => void }> = ({ onClick, children }) => ( | ||
<button type="button" onClick={onClick}> | ||
{children} | ||
</button> | ||
); |
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,32 @@ | ||
import React, { ComponentType, ReactElement } from 'react'; | ||
import { ArgsStoryFn } from '@storybook/csf'; | ||
|
||
import { Button } from './Button'; | ||
|
||
const Components = { Button }; | ||
|
||
type ReactFrameworkOrString = { | ||
component: ComponentType | keyof typeof Components; | ||
storyResult: ReactElement<unknown>; | ||
}; | ||
|
||
export const render: ArgsStoryFn<ReactFrameworkOrString> = (args, context) => { | ||
const { id, component } = context; | ||
if (!component) { | ||
throw new Error( | ||
`Unable to render story ${id} as the component annotation is missing from the default export` | ||
); | ||
} | ||
|
||
let Component; | ||
if (typeof component === 'string') { | ||
if (!Object.keys(Components).includes(component)) { | ||
throw new Error(`Unable to render story ${id}: unknown component name ${component}`); | ||
} | ||
Component = Components[component]; | ||
} else { | ||
Component = component; | ||
} | ||
|
||
return <Component {...(args as any)} />; | ||
}; |
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