-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Addon-controls: Better handling of Typescript optional properties #11146
Comments
@alexbchr do you have a public repro of this? Also make sure you are on the latest beta. i tried to repro it in our monorepo, but it behaves the way I'd expect it. Can you take a quick look and let me know if you are seeing something different than this? Input: https://github.com/storybookjs/storybook/pull/11149/files#diff-7b136f51af3f83ecf482139c745e467d |
I am effectively on the latest Beta (26). Here is my import path from 'path'
import type { StorybookConfig } from '@storybook/core/types'
const tsconfig = path.resolve(__dirname, '../../tsconfig.json')
const storybookConfig: StorybookConfig = {
stories: ['../../src/components/**/*.stories.tsx'],
addons: [
'@storybook/addon-actions/register',
'@storybook/addon-toolbars',
'@storybook/addon-docs',
'@storybook/addon-controls',
],
typescript: {
check: true,
checkOptions: { tsconfig },
reactDocgen: 'react-docgen-typescript',
reactDocgenTypescriptOptions: {
shouldExtractLiteralValuesFromEnum: true,
tsconfigPath: tsconfig,
propFilter: prop => !/^(testID)$/.test(prop.name),
},
},
webpackFinal: async config => {
if (!config?.resolve) {
return config
}
config.resolve.alias = {
...(config.resolve.alias || {}),
// Replace react-native dependencies with react-native-web
'react-native$': 'react-native-web',
// Replace @storybook/react-native with @storybook/react
'@storybook/react-native': '@storybook/react',
// Make react-native-svg work
'react-native-svg': 'react-native-svg/lib/commonjs/ReactNativeSVG.web',
}
return config
},
}
export default storybookConfig Here is my component: import React from 'react'
import { ButtonBasePublicProps } from '../ButtonBase/ButtonBase'
import { SecondaryButton } from './SecondaryButton/SecondaryButton'
import { PrimaryButton } from './PrimaryButton/PrimaryButton'
import { DestructiveButton } from './DestructiveButton/DestructiveButton'
import { BorderlessButton } from './BorderlessButton/BorderlessButton'
export interface ButtonProps extends ButtonBasePublicProps {
/**
* Set to true to have a primary button.
*/
primary?: boolean
/**
* Set to true to have a destructive button.
*/
destructive?: boolean
/**
* Set to true to have a borderless button.
*/
borderless?: boolean
}
/**
* Button triggering certain user actions.
*/
export const Button: React.FC<ButtonProps> = ({
primary = false,
destructive = false,
borderless = false,
...props
}) => {
if (destructive) {
return <DestructiveButton {...props} />
} else if (primary) {
return <PrimaryButton {...props} />
} else if (borderless) {
return <BorderlessButton {...props} />
} else {
// Secondary
return <SecondaryButton {...props} />
}
} |
@alexbchr Do you have a repo i can look at? |
Hey @shilman, I believe I'm having this same issue! On top of that, the zero-config Typescript setup is not working for me, as types are not being inferred at all. I've never done a repro repo before, so I don't know if it's good enough, but I gave it a shot: Let me know if I can help in some other way. PS: I've been in love with SB 6.0, keep up your awesome work! |
@abbudao Thanks so much for the repro, really appreciate it. @alexbchr are you also using CRA? Here's what I'm seeing. You're using CRA, so Storybook's "zero-config" settings are being replaced by
@mrmckeb LMK if you can take this or we can pair on it? |
@shilman Sorry for the delay for the repro, but here it is: Note that I am not using CRA, but a React Native + Expo setup. I reproduced the issue with Betas 26 and 28. Also, I referenced most packages I use in my original repo in the Also, Storybook CLI wasn't able to parse my
|
@alexbchr I get the following error trying to install:
That said, I'm not surprised RN is failing--it's not supported by |
That's weird, this package directly comes from Expo and should be available since it is hosted on GitHub... Everything works fine for me when installing packages using Yarn. When I access I understand that RN is not supported. This is why I use React Native Web to render components in a webpage and why |
That’s weird. I’ll clear the cache and try again tomorrow. |
@abbudao I released |
Is this behavior exhibited in the example in the repo? |
@hipstersmoothie Yes. Here is the repro:
|
This is happening because of #11140. If the project doesn't have In the reproduction provided the project doesn't have typescript installed, so th plugin does nothing |
@hipstersmoothie the project does have |
whoops didn't see that |
I'm also seeing similar behavior in @alexbchr's repo, which is a completely different (non-CRA) setup. https://github.com/alexbchr/upgraded-giggle |
Ok this is probably because we need to provide some default compiler options. When you remove the Adding the following fixes the loading |
I'll get a PR up that uses the root tsconfig if no tsconfigPath or compilerOptions is provided |
@hipstersmoothie I just updated my repo and added the following compiler options in the compilerOptions: {
allowSyntheticDefaultImports: true,
esModuleInterop: true
} The behavior is still the same for me. The props which can be undefined doesn't appear well. |
¡Ay Caramba!! I just released https://github.com/storybookjs/storybook/releases/tag/v6.0.0-beta.31 containing PR #11184 that references this issue. Upgrade today to try it out! You can find this prerelease on the Closing this issue. Please re-open if you think there's still more to do. |
@shilman and @hipstersmoothie, thank you for your hard work! 💪 The default Typescript preset is working automagically now! 🥳 Optional fields are still not hiding their Let me know if I can be of any help. |
For me it is working now! Note however that if I didn't put the following compilerOptions under compilerOptions: {
allowSyntheticDefaultImports: true,
esModuleInterop: true,
} Thanks a lot for the quick fix, this is gonna make Stories Implementation so much simpler and clearer! 🎉 |
this one was all @hipstersmoothie -- bravo!!! 👏👏👏 |
Olé!! I just released https://github.com/storybookjs/storybook/releases/tag/v6.0.0-rc.19 containing PR #11149 that references this issue. Upgrade today to try it out! You can find this prerelease on the |
I use storybook v6 version but problem still exist 😞 I create playground with my config here Could you look it, please 🙏 |
FWIW, on latest storybook (6.2 at time of writing), undefined is still coming through for optional props. Have done everything above. The |
I just tested setting |
@victor-grajski should we make that part of the default settings in storybook? can you open a PR for that? |
Currently dealing with the same problem. However, in my project, most proptypes are nullable types, because they have default props configured. Can we make these types non nullable too besides making them required? |
@shilman sorry i missed your mention! I see Or did you mean open a PR in @storybook/react-docgen/typescript? I don't see a reference to |
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:
This correctly renders a
text
control for thetext
property and aboolean
control for theloading
property.However, let's say I make the props optional (as is the case of most props of React Components):
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:
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 displaytext
andboolean
controls fortext
andloading
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.The text was updated successfully, but these errors were encountered: