import icons from '@repay/cactus-icons' import { border } from '@repay/cactus-theme' import PropTypes from 'prop-types' import React from 'react' import styled from 'styled-components' import { compose, margin, MarginProps } from 'styled-system' import Flex from '../Flex/Flex' import { flexItem, FlexItemProps } from '../helpers/styled' import Text, { TextProps } from '../Text/Text' interface ListProps extends MarginProps, FlexItemProps, React.HTMLAttributes { dividers?: boolean } interface ListItemProps extends React.HTMLAttributes { icon?: keyof typeof icons } interface ItemHeaderProps extends Omit { children?: React.ReactNode icon?: keyof typeof icons as?: React.ElementType } // Workaround for a styled-components bug when using `&` in a nested style. const nested = (): string => ` ${UL} { margin-top: 8px; margin-bottom: 8px; margin-left: 24px; } ` const UL = styled.ul<{ $dividers: boolean }>` padding: 0; margin: 0; list-style-type: none; ${nested} && { ${compose(flexItem, margin)} } ${(p) => p.$dividers && ` li { border-top: ${border(p, 'lightContrast')}; } li:first-of-type { border-top: none; } `} .clickable { cursor: pointer; } ` export const List = React.forwardRef( ({ children, dividers = false, ...props }, ref) => (
    {children}
) ) const ListItem = React.forwardRef( ({ icon, children, ...props }, ref) => { const Icon = icon && icons[icon] const iconElement = Icon ? : null return (
  • {iconElement} {children}
  • ) } ) const ItemHeader: React.FC = ({ icon, ...props }) => { const Icon = icon && icons[icon] const iconElement = Icon ? : null return ( {iconElement} ) } List.displayName = 'List' ListItem.displayName = 'List.Item' ItemHeader.displayName = 'List.ItemHeader' List.propTypes = { dividers: PropTypes.bool, } ListItem.propTypes = { icon: PropTypes.oneOf(Object.keys(icons) as (keyof typeof icons)[]), } type ListType = typeof List & { Item: typeof ListItem; ItemHeader: typeof ItemHeader } const TypedList = List as ListType TypedList.Item = ListItem TypedList.ItemHeader = ItemHeader export { ListItem, ItemHeader } export default TypedList