question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Addon-controls: Better handling of Typescript optional properties

See original GitHub issue

Is your feature request related to a problem? Please describe. I am not sure if this is a bug or not, but I put it as a feature request, since I didn’t find any documentation on this use case…

The Controls Addon can infer controls from my React component prop types. For example, having the following Typescript props for a component works well:

export interface TextProps {
  text: string
  loading: boolean
}

This correctly renders a text control for the text property and a boolean control for the loading property.

However, let’s say I make the props optional (as is the case of most props of React Components):

export interface TextProps {
  text?: string
  loading?: boolean
}

Both controls for both properties are now rendered as a raw text input (which strangely is not a control of type text). They should be rendered as in the first example.

Describe the solution you’d like Solution could be to handle optional properties as if they were not optionals if the component defines a default value for it.

For example, consider the following component:

export interface TextProps {
  text?: string
  loading?: boolean
}

export const TextProps: React.FC<TextProps> = ({ text = '', loading = false }) => {
  //...
}

Since default values are defined (and are already shown in props table), the controls should act as if undefined was not an actual value and display text and boolean controls for text and loading properties repectively.

Describe alternatives you’ve considered The only solution for me right now is to declare the Story argTypes manually for each, which is really not cool, since most Props of all my components are optionals, therefore requiring tedious manual setup in each of my stories.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:30 (18 by maintainers)

github_iconTop GitHub Comments

5reactions
milesjcommented, May 16, 2021

FWIW, on latest storybook (6.2 at time of writing), undefined is still coming through for optional props. Have done everything above.

Screen Shot 2021-05-16 at 2 47 13 PM

The shouldRemoveUndefinedFromOptional option (https://github.com/styleguidist/react-docgen-typescript#shouldremoveundefinedfromoptional-boolean) fixes it for booleans, but not unions.

Screen Shot 2021-05-16 at 2 52 38 PM

Related: https://github.com/styleguidist/react-docgen-typescript/issues/341

5reactions
inomdzhoncommented, Feb 9, 2021

I use storybook v6 version but problem still exist 😞

I create playground with my config here

Could you look it, please 🙏

Read more comments on GitHub >

github_iconTop Results From Across the Web

Controls - Storybook - JS.ORG
Essential addon Controls using a string. It works as long as you type a valid string into the auto-generated text control. Still, it's...
Read more >
@storybook/addon-controls | Yarn - Package Manager
Storybook Controls gives you a graphical UI to interact with a component's arguments dynamically, without needing to code. It creates an addon panel...
Read more >
Next-level component showcasing with Storybook controls
Learn about controls, a new Storybook addon that lets you dynamically interact with your React components for demo and testing purposes.
Read more >
Scaffolding a React Component Library with Storybook (Using ...
This repository contains a component library with customizable D3 visualizations written in React and TypeScript. Currently, this repository ...
Read more >
How do I get dropdown in controls addon for Storybook?
you should send an object to the control property. Like this: export default { title: "Components/Switch", argTypes: { color: { control: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found