javascript - 如何从组件的操作中动态改变 Storybook v6 中的 "args"?

标签 javascript typescript vue.js storybook

让我们看看我们有一个简单的组件ToggleButton :

const ButtonComponent = Vue.component('ButtonComponent', {
  props: {
    value: Boolean
  },

  methods: {
    handleClick() {
      this.$emit('toggle');
    }
  },

  template: `
    <button 
      :class="value ? 'on' : 'off'"
      @click="handleClick"
    >
      Toggle
    </button>`
});
该组件的故事:
import ToggleButton from './ToggleButton.vue';

export default {
  title: 'ToggleButton',
  component: ToggleButton,

  argTypes: {
    onToggle: {
      action: 'toggle' // <-- instead of logging "toggle" I'd like to mutate `args.value` here
    }
  }
};

export const Default = (_args, { argTypes }) => ({
  components: { ToggleButton },
  props: Object.keys(argTypes),

  template: `
    <ToggleButton
      :value="value"
      :toggle="onToggle"
    />
  `
});

Default.args = {
  value: false
}
我要实现的是处理toggle剧情里面的 Action 和变化value我在 Default.args 中使用过的对象通过从 .off 更改类名来更改按钮样式至.on .

最佳答案

我遇到了同样的问题,并且一直在寻找几天,直到我偶然发现了这个 github 帖子:
https://github.com/storybookjs/storybook/issues/12006
目前在我的 React 中(我确信 vue 方法会类似),我执行以下操作:

import React from 'react';
import CheckboxGroupElement from '../CheckboxGroup';
import { STORYBOOK_CATEGORIES } from 'elements/storybook.categories';
import { useArgs } from '@storybook/client-api';

export default {
  component: CheckboxGroupElement,
  title: 'Components/CheckboxGroup',
  argTypes: {
    onChange: {
      control: 'func',
      table: {
        category: STORYBOOK_CATEGORIES.EVENTS,
      },
    },
  },
  parameters: { actions: { argTypesRegex: '^on.*' } },
};

const Template = (args) => {
  const [_, updateArgs] = useArgs();

  const handle = (e, f) => {
// inside this function I am updating arguments, but you can call it anywhere according to your demand, the key solution here is using `useArgs()`
// As you see I am updating list of options with new state here
    console.log(e, f);
    updateArgs({ ...args, options: e });
  };
  return <CheckboxGroupElement {...args} onChange={handle} />;
};

export const CheckboxGroup = Template.bind({});
CheckboxGroup.storyName = 'CheckboxGroup';
CheckboxGroup.args = {
//Here you define default args for your story (initial ones)
  controller: { label: 'Group controller' },
  options: [
    { label: 'option 1', checked: true },
    { label: 'option 2', checked: false },
    { label: 'option 3', checked: false },
  ],
  mode: 'nested',
};

关于javascript - 如何从组件的操作中动态改变 Storybook v6 中的 "args"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63708208/

相关文章:

javascript - 我可以通过 props.children React JS 将 props 传递给 children 吗

javascript - Angular Material - sidenav 组件无法正常工作

javascript - 将get查询中的参数传递给vue js?

jquery - 如何直接在浏览器中使用VueJS插件?

javascript - 为什么移动 div 会在手机上的 javascript 中留下痕迹?

javascript - 将 KML 作为文本字符串添加到 OpenLayers

javascript - 三.CubeTextureLoader 1px 边缘错误

javascript - 我的 React Typescript 获胜百分比计数器中出现逻辑 NaN 错误

javascript - 字符串转 UTF8 转 SHA256 转 BASE64

javascript - Summernote 不会编辑其内容