Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ArgTypes depending on select args

Is there a way to dynamically change the ArgTypes when args of a components changes in Storybook?

For instance, suppose a strory with a component that has 2 controls: id and value. id can either be 1 or 2. If id == '1' then argTypes.value.control.min = 0, else argTypes.value.control.min = -100.

Example code:

export default {
  argTypes: {
    id: { control: { type: 'select', options: ['1', '2']}},
    value: { control: {type: 'range', min:0, max: 100, step:10}},
  }
};

const Template = (args) => <MyComponent {...args} />;

export const Example = Template.bind({});
Template.args = {
  id: '1',
  value: 0,
};
like image 686
MSIS Avatar asked Mar 02 '26 03:03

MSIS


1 Answers

You could do that using two different args and an if property.

...
argTypes: {
    id: { control: { type: 'select', options: ['1', '2']}},
    valueNatural: {
      control: {type: 'range', min:0, max: 100, step:10},
      if: {arg: 'id', eq: '1'}
    },
    valueInteger: {
      control: {type: 'range', min:-100, max: 100, step:10},
      if: {arg: 'id', eq: '2'}
    }
  }
like image 77
Guilherme Xavier Avatar answered Mar 03 '26 17:03

Guilherme Xavier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!