import classNames from 'clsx';
import * as React from 'react';
import { v4 as uuidv4 } from 'uuid';
import { SelectDropdown } from '../features/forms';
import Icon from './icon';
import { Select } from './ui';
const List: React.FC = ({ children }) => (
{children}
);
interface IListItem {
label: React.ReactNode,
hint?: React.ReactNode,
onClick?(): void,
onSelect?(): void
isSelected?: boolean
}
const ListItem: React.FC = ({ label, hint, children, onClick, onSelect, isSelected }) => {
const id = uuidv4();
const domId = `list-group-${id}`;
const Comp = onClick ? 'a' : 'div';
const LabelComp = onClick || onSelect ? 'span' : 'label';
const linkProps = onClick || onSelect ? { onClick: onClick || onSelect } : {};
const renderChildren = React.useCallback(() => {
return React.Children.map(children, (child) => {
if (React.isValidElement(child)) {
const isSelect = child.type === SelectDropdown || child.type === Select;
return React.cloneElement(child, {
id: domId,
className: classNames({
'w-auto': isSelect,
}, child.props.className),
});
}
return null;
});
}, [children, domId]);
return (
{label}
{hint ? (
{hint}
) : null}
{onClick ? (
{children}
) : null}
{onSelect ? (
{children}
{isSelected ? (
) : null}
) : null}
{typeof onClick === 'undefined' && typeof onSelect === 'undefined' ? renderChildren() : null}
);
};
export { List as default, ListItem };