import { forwardRef, MutableRefObject, ReactNode, RefCallback } from 'react'; import styled from '@emotion/styled'; import { css } from '@emotion/react'; import { lighten } from 'polished'; import { mineShaft, nobel, primary, white } from 'styles/colors'; import { getThemeFont, fontSize } from 'styles/fonts'; const Content = styled.div<{ size?: string }>` flex: 1; font-family: ${getThemeFont}; font-size: ${fontSize.regular}; line-height: 20px; // For truncate text in flexbox: https://css-tricks.com/flexbox-truncated-text/ min-width: 0; ${({ size, theme: { rtl } }) => { switch (size) { case 'small': return css` padding: 6px 6px 6px 0; `; default: return css` padding: ${rtl ? '12px 0 12px 20px' : '12px 20px 12px 0;'}; `; } }}; `; export const boxShadow = css` box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.22); `; const UL = styled.ul>` list-style: none; padding: 0; margin: 0; ${({ isDropdownList }) => (isDropdownList ? '' : boxShadow)} display: inline-block; box-sizing: border-box; background-color: ${white}; color: ${mineShaft['900']}; ${({ size }) => { switch (size) { case 'small': return css` min-width: 12.5rem; `; default: return css` min-width: 16rem; `; } }}; `; const activeStyle = css` background-color: ${lighten(0.05, nobel['200'])}; border-left-color: ${primary.main}; border-right-color: ${primary.main}; `; const Wrapper = styled.li< Pick & { size?: string; isDropdownList?: boolean; active?: boolean; hasIcon?: boolean; } >` display: flex; align-items: top; direction: ${({ theme: { rtl } }) => rtl && 'rtl'}; ${({ isDropdownList }) => isDropdownList ? '' : css` position: relative; `} ${({ theme: { rtl } }) => css` padding-${rtl ? 'right' : 'left'}: 8px; border-${rtl ? 'right' : 'left'}: 2px solid transparent; `}; outline: 0; ${({ size }) => size === 'small' && css` padding-top: 8px; padding-bottom: 4px; &:not(:first-child) { padding-top: 4px; } &:last-child { padding-bottom: 8px; } `}; ${({ size, hasIcon }) => { const selector = hasIcon ? `&:not(:last-child) > div:nth-of-type(2)` : `&:not(:last-child) > div:first-of-type`; return css` ${selector} { ${(() => { switch (size) { case 'dropdown': case 'small': return css` border-bottom: 0; `; default: return css` border-bottom: 1px solid ${nobel['200']}; `; } })()} } `; }} ${({ hoverable, disabled }) => hoverable && !disabled && css` cursor: pointer; &:hover, &:focus { ${activeStyle}; } `}; ${({ active, disabled }) => active && !disabled && activeStyle}; ${({ disabled }) => disabled && css` opacity: 0.5; cursor: not-allowed; `}; `; const Icon = styled.div<{ size?: string }>` ${({ size, theme: { rtl } }) => { switch (size) { case 'small': return css` align-self: center; margin: ${rtl ? '0 0 0 6px' : '0 6px 0 0'}; `; default: return css` margin-top: 12px; margin-${rtl ? 'left' : 'right'}: 10px; `; } }}; `; interface ItemProps { icon?: ReactNode; size?: string; children?: ReactNode; options?: ReactNode; disabled?: boolean; [rest: string]: any; } const Item = ({ icon = null, size, children = null, options = null, disabled = false, ...rest }: ItemProps) => ( {icon && {icon}} {children} {options} ); const DropdownListItem = props => ; interface RenderFnProps { data: Record; // eslint-disable-next-line no-undef Item: (_itemProps: ItemProps) => JSX.Element; index: number; getProps: (_props?: Record) => Record; } interface Props { /** array of objects */ items: Record[]; /** enable item hover style */ hoverable?: boolean; /** used as `key` for list items */ unique?: string; size?: 'small' | 'default' | 'dropdown'; /** disable style if it is used by DropdownList */ isScrollableDropdownList?: boolean; /** * Render function * * @param {Object} data individual item in `items` * @param {Component} Item react component * @param {Function} getProps function to get props, pass the result to `Item`. * Add custom props by passing an object as argument. */ // eslint-disable-next-line no-undef render?: (_renderFnProps: RenderFnProps) => JSX.Element; /** @deprecated, use `render` */ // eslint-disable-next-line no-undef children?: (_renderFnProps: RenderFnProps) => JSX.Element; /** React ref */ ref?: RefCallback | MutableRefObject; /** Rest Props */ [rest: string]: any; } const List = forwardRef( ( { items, hoverable = false, unique = '', size = 'default', render, children, isScrollableDropdownList = false, ...rest }: Props, ref ) => { const renderFunc = render ?? children; return (
    {renderFunc ? items.map((data, index) => renderFunc({ data, Item: isScrollableDropdownList ? DropdownListItem : Item, index, getProps: props => ({ ...props, hoverable, size, key: unique ? data[unique] : index, tabIndex: 0, }), }) ) : null}
); } ); export default List; export type ListProps = Props;