测试单击“ Web-Component”上的事件。故事书的按钮

发布于 2025-01-29 18:45:34 字数 1164 浏览 5 评论 0原文

我在互联网上寻找有关如何测试单击事件的示例在按钮上( Web组件/打字稿),但我找不到清晰的一。 您能建议我一些指南/文章或代码片段吗?

我已经创建了 story.ts 文件;这样的是:

import { html, TemplateResult } from 'lit';
import './index.ts';
import { Properties } from './model/button.interfaces';

export default {
  title: 'My button',
  component: 'my-button',
};

interface Story<T> {
  (args: T): TemplateResult;
  args?: Partial<T>;
  argTypes?: Record<string, unknown>;
  parameters?: any;
}

interface ArgTypes extends Properties{}

const Template: Story<ArgTypes> = (args: ArgTypes) => html`
      <my-button 
          ?disabled="${args.disabled}"
          color=${args.color}>
        ${args.text}
      </my-button>`

export const Button = Template.bind({});

Button.args = {
  label: "This is the text",
  color: "primary"
}

Button.argTypes = {
  color: {
    control: 'select',
    options: ['primary', 'secondary'],
    table: {
      category: 'Modifiers',
    },
  }, 
  disabled: {
    control: 'boolean',
    options: [true, false],
    table: {
      category: 'Modifiers',
    },
  }
}

谢谢

I'm looking on the Internet for an example about how to test click event on a button (web component/typescript) with Storybook, but I can't find a clear one.
Can you suggest me some guides/articles or code snippets?

I already created the stories.ts file; it's something like this:

import { html, TemplateResult } from 'lit';
import './index.ts';
import { Properties } from './model/button.interfaces';

export default {
  title: 'My button',
  component: 'my-button',
};

interface Story<T> {
  (args: T): TemplateResult;
  args?: Partial<T>;
  argTypes?: Record<string, unknown>;
  parameters?: any;
}

interface ArgTypes extends Properties{}

const Template: Story<ArgTypes> = (args: ArgTypes) => html`
      <my-button 
          ?disabled="${args.disabled}"
          color=${args.color}>
        ${args.text}
      </my-button>`

export const Button = Template.bind({});

Button.args = {
  label: "This is the text",
  color: "primary"
}

Button.argTypes = {
  color: {
    control: 'select',
    options: ['primary', 'secondary'],
    table: {
      category: 'Modifiers',
    },
  }, 
  disabled: {
    control: 'boolean',
    options: [true, false],
    table: {
      category: 'Modifiers',
    },
  }
}

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待天淡蓝洁白时 2025-02-05 18:45:34

由于Web组件可作为本机HTML元素起作用,因此您可以将onclick属性附加到&lt; my-button&gt;
尝试在组件上方添加脚本标签,在该组件上定义一个单击处理程序,然后将其添加到组件中。

const Template: Story<ArgTypes> = (args: ArgTypes) => html`
      <script>
          const clickHandler = () => console.log('clicked')
      </script>
      <my-button 
          ?disabled="${args.disabled}"
          color=${args.color}
          onclick="clickHandler()">
        ${args.text}
      </my-button>`

现在,每当单击按钮时,都应该看到日志。

Since web components work as native html elements, you can attach an onclick attribute to your <my-button>.
Try adding a script tag above your component in which you define an onclick handler and add it to your component.

const Template: Story<ArgTypes> = (args: ArgTypes) => html`
      <script>
          const clickHandler = () => console.log('clicked')
      </script>
      <my-button 
          ?disabled="${args.disabled}"
          color=${args.color}
          onclick="clickHandler()">
        ${args.text}
      </my-button>`

Now you should see the log whenever you click on the button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文