I didn't find any answer that resolve my problem. I'm building a React Storybook with Typescript. With optional props, Storybook addon add an "undefined" option. I put a defaultProps but it doesn't take it. Do you have any solution ?
Component :
type IconType = 'outline' | 'solid'
export interface IconProps {
name: keyof typeof icons
size?: number
type?: IconType
className?: string
fallback?:
| boolean
| React.ReactChild
| React.ReactFragment
| React.ReactPortal
| null
}
const Icon = ({
name,
type = 'solid',
className = '',
size = 4,
fallback = null,
}: IconProps): JSX.Element => {
if (!name) {
throw new Error("Can't call Icon component without name props")
}
const Icon = React.lazy(
() => import(`../${type}/${icons[name]}`)
)
return (
<React.Suspense fallback={fallback}>
<Icon className={className} style={{width: size, height: size}} />
</React.Suspense>
)
}
Icon.defaultProps = {
type: 'solid',
}
export default Icon
Story:
import React from 'react'
import { Story, Meta } from '@storybook/react'
import Icon, { IconProps } from '.'
export default {
title: 'Components/Icon',
component: Icon,
} as Meta
const Template: Story<IconProps> = (args) => <Icon {...args} />
export const Basic = Template.bind({})
Basic.args = {
name: 'plus',
}
And when I select undefined it doesn't take the defaultProps of Icon
