import React, { FunctionComponent } from 'react'
import cc from 'classcat'
import { A11yProps, pickA11yProps } from '../_utils/interfaces'
import { ContentDivider } from '../divider/contentDivider'
import { ItemCheckboxProps } from '../itemCheckbox/ItemCheckbox'
import { ItemChoiceProps } from '../itemChoice'
import { ItemRadioProps } from '../itemRadio/ItemRadio'
import { StyledItemsList } from './ItemsList.style'
export const ItemsListDivider: FunctionComponent = () => null
// NOTE: react-hot-loader will update dynamically the type and break type comparisons.
// A pre-rendered type need to be used to fix it.
// See: https://github.com/gaearon/react-hot-loader#checking-element-types
const ItemsListDividerType = ().type
export type ItemsListChild =
| React.ReactElement
| React.ReactElement
| React.ReactElement
| null
type ChildrenType = ItemsListChild | Array
export type ItemsListProps = A11yProps &
Readonly<{
children: ChildrenType
withSeparators?: boolean
className?: string
keyGenerator?: (index: number) => string | number
}>
export const ItemsList = (props: ItemsListProps) => {
const {
children,
className = '',
withSeparators = false,
keyGenerator = (i: number) => i,
} = props
const a11yAttrs = pickA11yProps(props)
// Remove all falsy items.
// Falsy items can be added when using this pattern:
//
//
// {shouldShowListItem && } // shouldShowListItem value will be inserted in the list
//
//
const items = Array.isArray(children) ? children : [children]
const filteredChildren = items.filter(Boolean)
return (
{filteredChildren.map((item, index) => {
if (item.type === ItemsListDividerType || item.type === undefined) {
return null
}
const isLast = filteredChildren.length === index + 1
const hasSeparator =
!isLast && (filteredChildren[index + 1].type === ItemsListDividerType || withSeparators)
return (
{item}
{hasSeparator && }
)
})}
)
}