fbredius/storybook

View on GitHub
docs/snippets/react/component-story-custom-args-complex.ts.mdx

Summary

Maintainability
Test Coverage
```ts
// YourComponent.stories.ts|tsx

import React from 'react';

import { ComponentStory, ComponentMeta } from '@storybook/react';

import { YourComponent } from './your-component';

//👇 Some function to demonstrate the behavior
const someFunction = (valuePropertyA, valuePropertyB) => {
  // Makes some computations and returns something
};

export default {
  /* 👇 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: 'YourComponent',
  //👇 Creates specific argTypes with options
  argTypes: {
    propertyA: {
      options: ['Item One', 'Item Two', 'Item Three'],
      control: { type: 'select' } // Automatically inferred when 'options' is defined
    },
    propertyB: {
      options: ['Another Item One', 'Another Item Two', 'Another Item Three'],
    },
  },
} as ComponentMeta<typeof YourComponent>;

const Template: ComponentStory<typeof YourComponent> = ({ propertyA, propertyB, ...rest }) => {
  //👇 Assigns the result from the function to a variable
  const someFunctionResult = someFunction(propertyA, propertyB);

  return <YourComponent someProperty={someFunctionResult} {...rest} />;
};

export const ExampleStory = Template.bind({});
ExampleStory.args= {
  propertyA: 'Item One',
  propertyB: 'Another Item One',
};
```