Storybook onClick event is not logged unless i add it to argTypes

1.6k views Asked by At

For the onClick event to be logged in story book Actions tab I have to uncomment the argTypes: { onClick: {} } in the SimpleButton.stories.tsx file. My question is why do I have to do that? Can't it automatically infer that onClick is an action since in .storybook/preview.js file parameters includes the property actions: { argTypesRegex: "^on[A-Z].*" }?

export const parameters = {
  actions: { argTypesRegex: "^on[A-Z].*" },
};

SimpleButton.tsx

import { VFC } from "react";

interface IProps {
  /** Text to show */
  text: string;
  /** Function to run when button is clicked */
  onClick: () => void;
}

/** This is a very special button component */
const SimpleButton: VFC<IProps> = ({ text, onClick }) => {
  return (
    <button type="button" onClick={onClick}>
      {text}
    </button>
  );
};

export default SimpleButton;

SimpleButton.stories.tsx

import { Meta, Story } from "@storybook/react";
import { ComponentProps } from "react";
import SimpleButton from "../src/components/SimpleButton";

type Props = ComponentProps<typeof SimpleButton>;

const meta: Meta<Props> = {
  title: "SimpleButton",
  component: SimpleButton,
  // argTypes: { onClick: {} } // If I uncomment this onClick action will be logged
};
export default meta;

const Template: Story<Props> = args => <SimpleButton {...args} />;

export const Primary = Template.bind({});
Primary.args = { text: "Click me" };

export const Secondary = Template.bind({});
Secondary.args = { 
  text: "Click me 2",
  onClick: () => { console.log('btn click'); }
};

Regarding storybook versions I can see the following in package.json

"@storybook/addon-a11y": "^6.5.9",
"@storybook/addon-actions": "^6.5.9",
"@storybook/addon-essentials": "^6.5.9",
"@storybook/addon-links": "^6.5.9",
"@storybook/addons": "^6.5.9",
"@storybook/react": "^6.5.9",
"@storybook/theming": "^6.5.9",
1

There are 1 answers

1
dawnb On

I meet the same issue, then I terminate and restart storybook server with pnpm storybook, and now it's working.