Skip to content

Commit

Permalink
fix(ListItem): LastChild should be undefined in some case without crash
Browse files Browse the repository at this point in the history
  • Loading branch information
JF-Cozy committed Apr 27, 2023
1 parent 8a1725e commit 0686bb3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
10 changes: 6 additions & 4 deletions react/MuiCozyTheme/ListItem/index.js
Expand Up @@ -44,9 +44,9 @@ const useGutters = ({ gutters, ...props }) => {
const guttersValue = gutters === 'double' ? 32 : 16

const lastChild = getLastChild(props)
const isLastChildListItemIcon = getComponentName(lastChild).includes(
'ListItemIcon'
)
const isLastChildListItemIcon = lastChild
? getComponentName(lastChild).includes('ListItemIcon')
: false

const gutterRight = isLastChildListItemIcon ? guttersValue - 8 : guttersValue

Expand Down Expand Up @@ -106,10 +106,12 @@ ListItem.defaultProps = {
size: 'medium'
}

ListItem.propTypes = {
export const LitItemPropTypes = {
gutters: PropTypes.oneOf(['disabled', 'double', 'default']),
size: PropTypes.oneOf(['small', 'medium', 'large']),
ellipsis: PropTypes.bool
}

ListItem.propTypes = LitItemPropTypes

export default ListItem
8 changes: 6 additions & 2 deletions react/utils/react.js
Expand Up @@ -6,16 +6,20 @@ export const getComponentName = component => {
}

export const getChildren = props => {
return Children.toArray(props.children)
const children = Children.toArray(props.children)
return Children.count(children) > 0 ? children : null
}

export const getChildrenCount = props => {
const children = getChildren(props)
return children.length
return Children.count(children) || null
}

export const getLastChild = props => {
const children = getChildren(props)

if (!children) return null

const lastChild = children[children.length - 1]

return lastChild.type === Fragment
Expand Down

0 comments on commit 0686bb3

Please sign in to comment.