Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are the args for my storybook component showing as optional instead of required?

I have a simple loader component

import React from "react";
import { Backdrop, CircularProgress } from "@material-ui/core";
import { makeStyles } from "@material-ui/core/styles";

export interface LoaderProps {
  open: boolean;
}

const useStyles = makeStyles((theme) => ({
  backdrop: {
    zIndex: theme.zIndex.drawer + 1,
    color: "#fff",
  },
}));

const Loader: React.FC<LoaderProps> = ({ open }) => {
  const classes = useStyles();

  return (
    <Backdrop className={classes.backdrop} open={open}>
      <CircularProgress color="inherit" />
    </Backdrop>
  );
};

export default Loader;

Here is the story for it:

import React from "react";
import Loader, { LoaderProps } from "./Loader";
import { Story } from "@storybook/react/types-6-0";

export default {
  title: "Loader",
  component: Loader,
  excludeStories: /.*Data$/,
};

export const loader: Story<LoaderProps> = (args) => <Loader {...args} />;
loader.args = {
  open: true,
};

When I hover over open in the args it says the type could be boolean | undefined instead of just boolean? Why is this when the LoaderProps should enforce that is of type boolean?

like image 438
spamspark Avatar asked Jan 21 '21 15:01

spamspark


People also ask

How do you pass args in storybook?

In Storybook 6+, we pass the args as the first argument to the story function. The second argument is the “context”, which includes story parameters, globals, argTypes, and other information. In Storybook 5 and before we passed the context as the first argument.

What are storybook parameters?

Parameters are a set of static, named metadata about a story, typically used to control the behavior of Storybook features and addons.


1 Answers

Checking the Story type, it's defined as BaseStory<Args, StoryFnReactReturnType> & Annotations<Args, StoryFnReactReturnType>.

BaseStory isn't particularly interesting or relevant here, but Annotations has the property: args?: Partial<Args>.

In your code, Args is LoaderProps, so the Annotations object has an optional property args that itself is all of the LoaderProps properties made optional. That is, loader.args is of type Partial<LoaderProps> which is { open?: boolean }. The fact that it is optional is what allows it to also be undefined.

As for why storybook does this, I can only guess since the documentation in the types on Annotation.args in the type definitions has a dead link. If the properties on args were required, then you would have to set all of the properties in your Props definitions.

like image 88
Explosion Pills Avatar answered Nov 02 '22 23:11

Explosion Pills