I am currently using Storybook8
and have implemented a checkbox component
.
I am trying to pass setChecked
as a prop to see the checkbox in action when clicked, but I am finding it difficult to implement this even after consulting the Storybook 8
documentation.
How can I set up the component so that I can pass setChecked
and know when the checkbox
is checked?
CheckBoxComponent
interface checkboxLabelProps {
checked: boolean;
setChecked: () => void;
label?: string;
}
export const CheckboxLabel = ({ check, setChecked, label }: checkboxLabelProps) => {
return (
<CheckBoxContainer onClick={() => setChecked()}>
<CheckboxIcon checked={checked} />
{label && <CheckBoxLabel>{label}</CheckBoxLabel>}
</CheckBoxContainer>
);
};
and my checkbox.stories.tsx
const meta: Meta<typeof CheckboxLabel> = {
title: 'Input/checkbox',
component: CheckboxLabel,
tags: ['autodocs'],
parameters: {
layout: 'centered'
},
argTypes: {
label: { control: 'text' }
},
decorators: (Story, args) => {
const [checked, setChecked] = useState(true);
return <Story {...args} setChecked={() => setChecked(!checked)} />;
} // not working
} satisfies Meta<typeof CheckboxLabel>;
export default meta;
type Story = StoryObj<typeof meta>;
export const CheckIn: Story = {
args: {
checked: true,
label: 'Check'
}
};
When you click My Components in the Storybook
setChecked is not a function
An error appears.
How do I transfer the function, and can I induce a change in the state with clicks?