[material-ui] Incorrect React component prop children
types for components like AvatarGroup
#38533
Open
2 tasks done
Labels
component: avatar
This is the name of the generic UI component, not the React module!
docs
Improvements or additions to the documentation
Duplicates
Latest version
Steps to reproduce 🕹
Many of the Material UI React components that expect particular React elements in the prop
children
currently have an incorrect TypeScript type for the prop ofReact.ReactNode
.In
demo.tsx
:Current behavior 😯
There will not be a TypeScript (or
prop-types
) error about using a React Fragment in theAvatarGroup
propchildren
, but if you render theDemo
component this will be logged as an error in the browser console:Expected behavior 🤔
For the current
AvatarGroup
API that intentionally forbids using a React Fragment in the propchildren
, there should be a TypeScript error that a React Fragment was used in theAvatarGroup
propchildren
, and there should also be aprop-types
warning in the browser console about the value used.Ideally the
AvatarGroup
API (and the API for other components with similar expectations about React Elements in children) would be changed to allow using aReact.ReactNode
type value in the propchildren
, including a React Fragment. This could be achieved by implementing something likereact-keyed-flatten-children
to convert children into a flattened and keyed array, accounting for React Fragments.Context 🔦
Here is an example where
AvatarGroup
has the wrong TypeScript type for the React component propchildren
, resulting also in the React componentpropTypes
being incorrect…Source for the type:
material-ui/packages/mui-material/src/AvatarGroup/AvatarGroup.d.ts
Lines 12 to 15 in 7770430
Published type:
https://unpkg.com/browse/@mui/[email protected]/AvatarGroup/AvatarGroup.d.ts
Resulting incorrect generated
propTypes
:material-ui/packages/mui-material/src/AvatarGroup/AvatarGroup.js
Lines 165 to 168 in 7770430
Incorrect API docs:
https://mui.com/material-ui/api/avatar-group/#AvatarGroup-prop-children
Some searches to try to help find other React components with an incorrect prop type for
children
:Note that the current limitation of the prop
children
not accepting a React component for React components likeAvatarGroup
is particularly annoying when writing Storybook stories, when you want to provide the children via the story configargs.children
. For example, inAvatarGroup.stories.tsx
:The first story
ChildrenFragment
at render results in this console error:While the second story
ChildrenArray
(which is what a user might attempt after following the instructions of the first console error to provide an array instead) at render results in this console error:The Material UI component docs for the
AvatarGroup
propchildren
didn't give any indication there would be render issues for stories written like this, there was no build time type safety, and it's not clear to a user after manually trying to render the components and reading the console errors the best way to resolve it.Your environment 🌎
npx @mui/envinfo
The text was updated successfully, but these errors were encountered: