[material-ui][Select] TS errors with the updated SelectProps
and when used in Storybook
#41410
Labels
component: select
This is the name of the generic UI component, not the React module!
package: material-ui
Specific to @mui/material
regression
A bug, but worse
Steps to reproduce
Reproduction steps
Select.tsx
andSelect.stories.tsx
to see the errors.Current behavior
Select.tsx
component. It has created multiple TS errors forSelect.tsx
andSelect.stories.tsx
.Additionally, the latest change breaks the
docgen
table on the docs in Storybook. The docs are autogenerated by thereact-docgen-typescript
from@storybook/addon-docs
.See the image below.
Expected behavior
No Typescript errors. It should work as before.
Context
The root cause of the issue is that MUI has adjusted the types for
Select
&SelectProps
type in a recent version.This effectively makes the second
Variant
generic param required for mui'sSelect
andSelectProps
; if omitted, the type ofvariant
prop effectively defaults to"outlined"
(see types here).Conceptually, MUI has combined multiple different components that have different props, HTML elements and styles into one
Select
component. The allowed props are different depending on thevariant
prop.It makes sense that storybook is unable to generate the correct props documentation and controls for the MUI
Select
, because it can only generate one set of props to statically display, but this component doesn't have just one set of props. It has one set per variant.Attempts at fixing
Fixing the types for
Select
It is possible to fix the types for our
Select
wrapper component to make the typescript compiler happy (e.g. accepting a second generic parameter for theVariant
on the wrapper component, passing it through toMuiSelect
, and doing a clever union on theprops
argument to makevariant
optional again).However we still have a dilemma with the stories, because of
variant
defaulting to outlined; typescript can't narrow the type forvariant
unless we explicitly provide the second generic argument.Making typescript compiler happy in
Select.stories.tsx
My next idea was to tweak the types in the stories file to make all of the errors go away; i.e. picking an arbitrary variant and supplying that as the second generic arg everywhere. The appearance of the component is correct and the variant can still be changed via controls; however, the prop table generated by
react-docgen-typescript
is incorrect and doesn't show accurate props for the selected variant.The table is generated at build time, so there's no way to update it as the user interacts with the
variant
control.Your environment
npx @mui/envinfo
Search keywords: Select, SelectProps
The text was updated successfully, but these errors were encountered: