import clsx from 'clsx'; import React from 'react'; import { Link } from 'react-router-dom'; import { v4 as uuidv4 } from 'uuid'; import { SelectDropdown } from '../features/forms'; import { Icon, HStack, Select } from './ui'; interface IList { children: React.ReactNode } const List: React.FC = ({ children }) => (
{children}
); interface IListItem { label: React.ReactNode hint?: React.ReactNode to?: string onClick?(): void onSelect?(): void isSelected?: boolean children?: React.ReactNode } const ListItem: React.FC = ({ label, hint, children, to, onClick, onSelect, isSelected }) => { const id = uuidv4(); const domId = `list-group-${id}`; const onKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { onClick!(); } }; const LabelComp = to || onClick || onSelect ? 'span' : 'label'; 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, { // @ts-ignore id: domId, className: clsx({ 'w-auto': isSelect, }, child.props.className), }); } return null; }); }, [children, domId]); const className = clsx('flex items-center justify-between overflow-hidden bg-gradient-to-r from-gradient-start/20 to-gradient-end/20 px-4 py-2 first:rounded-t-lg last:rounded-b-lg dark:from-gradient-start/10 dark:to-gradient-end/10', { 'cursor-pointer hover:from-gradient-start/30 hover:to-gradient-end/30 dark:hover:from-gradient-start/5 dark:hover:to-gradient-end/5': typeof to !== 'undefined' || typeof onClick !== 'undefined' || typeof onSelect !== 'undefined', }); const body = ( <>
{label} {hint ? ( {hint} ) : null}
{(to || onClick) ? ( {children} ) : null} {onSelect ? (
{children}
) : null} {typeof to === 'undefined' && typeof onClick === 'undefined' && typeof onSelect === 'undefined' ? renderChildren() : null} ); if (to) return ( {body} ); const Comp = onClick ? 'a' : 'div'; const linkProps = onClick || onSelect ? { onClick: onClick || onSelect, onKeyDown, tabIndex: 0, role: 'link' } : {}; return ( {body} ); }; export { List as default, ListItem };