110 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-07-06 18:56:28 -04:00
import React from 'react';
import PropTypes from 'prop-types';
2019-03-15 10:19:57 -04:00
import { css } from '@emotion/core';
import styled from '@emotion/styled';
2018-07-06 18:56:28 -04:00
import { Wrapper, Button as DropdownButton, Menu, MenuItem } from 'react-aria-menubutton';
2018-07-28 14:33:42 -06:00
import { buttons, components } from './styles';
2018-07-06 18:56:28 -04:00
import Icon from './Icon';
const StyledWrapper = styled(Wrapper)`
position: relative;
font-size: 14px;
user-select: none;
`;
2018-07-06 18:56:28 -04:00
const StyledDropdownButton = styled(DropdownButton)`
${buttons.button};
${buttons.default};
display: block;
padding-left: 20px;
padding-right: 40px;
&:after {
${components.caretDown};
content: '';
display: block;
position: absolute;
top: 16px;
right: 16px;
color: currentColor;
}
`;
2018-07-06 18:56:28 -04:00
const DropdownList = styled.ul`
2018-07-24 23:35:59 -04:00
${components.dropdownList};
2018-07-06 18:56:28 -04:00
margin: 0;
position: absolute;
top: 0;
left: 0;
min-width: 100%;
z-index: 2;
2018-07-06 18:56:28 -04:00
${props => css`
width: ${props.width};
top: ${props.top};
left: ${props.position === 'left' ? 0 : 'auto'};
right: ${props.position === 'right' ? 0 : 'auto'};
`};
`;
2018-07-06 18:56:28 -04:00
const StyledMenuItem = styled(MenuItem)`
2018-07-24 23:35:59 -04:00
${components.dropdownItem};
`;
2018-07-06 18:56:28 -04:00
const MenuItemIconContainer = styled.div`
flex: 1 0 32px;
text-align: right;
position: relative;
top: 2px;
`;
2018-07-06 18:56:28 -04:00
const Dropdown = ({
renderButton,
dropdownWidth = 'auto',
dropdownPosition = 'left',
dropdownTopOverlap = '0',
2018-07-17 11:37:17 -04:00
className,
2018-07-06 18:56:28 -04:00
children,
}) => {
return (
2018-07-17 11:37:17 -04:00
<StyledWrapper onSelection={handler => handler()} className={className}>
2018-07-06 18:56:28 -04:00
{renderButton()}
<Menu>
<DropdownList width={dropdownWidth} top={dropdownTopOverlap} position={dropdownPosition}>
{children}
</DropdownList>
</Menu>
</StyledWrapper>
);
};
Dropdown.propTypes = {
renderButton: PropTypes.func.isRequired,
dropdownWidth: PropTypes.string,
dropdownPosition: PropTypes.string,
dropdownTopOverlap: PropTypes.string,
className: PropTypes.string,
children: PropTypes.node,
};
2018-07-17 11:37:17 -04:00
const DropdownItem = ({ label, icon, iconDirection, onClick, className }) => (
<StyledMenuItem value={onClick} className={className}>
2018-07-06 18:56:28 -04:00
<span>{label}</span>
{icon ? (
<MenuItemIconContainer>
<Icon type={icon} direction={iconDirection} size="small" />
</MenuItemIconContainer>
) : null}
2018-07-06 18:56:28 -04:00
</StyledMenuItem>
);
DropdownItem.propTypes = {
label: PropTypes.string,
icon: PropTypes.string,
iconDirection: PropTypes.string,
onClick: PropTypes.func,
className: PropTypes.string,
};
2018-07-06 18:56:28 -04:00
export { Dropdown as default, DropdownItem, DropdownButton, StyledDropdownButton };