import clsx from 'clsx'; import React from 'react'; import { v4 as uuidv4 } from 'uuid'; import { SelectDropdown } from '../features/forms'; import Icon from './icon'; import { HStack, Select } from './ui'; interface IList { children: React.ReactNode } const List: React.FC = ({ children }) => (
{children}
); interface IListItem { label: React.ReactNode hint?: React.ReactNode onClick?(): void onSelect?(): void isSelected?: boolean children?: React.ReactNode } const ListItem: React.FC = ({ label, hint, children, onClick, onSelect, isSelected }) => { const id = uuidv4(); const domId = `list-group-${id}`; const onKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { onClick!(); } }; const Comp = onClick ? 'a' : 'div'; const LabelComp = onClick || onSelect ? 'span' : 'label'; const linkProps = onClick || onSelect ? { onClick: onClick || onSelect, onKeyDown, tabIndex: 0, role: 'link' } : {}; 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: clsx({ '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 };