feature: add dropdown component

This commit is contained in:
George Lima 2018-12-10 17:51:22 -03:00
parent 22b7a89fbe
commit f222fd1335
2 changed files with 114 additions and 0 deletions

View File

@ -0,0 +1,30 @@
---
name: DropDown
---
import { Playground, PropsTable } from 'docz'
import { Dropdown } from './dropdown.js'
import { DoczWrapper } from '../theme.js'
# DropDown
<PropsTable of={Dropdown} />
## Basic usage
<Playground>
<DoczWrapper>
{() => (
<div style={{ height: '500px' }}>
<Dropdown
options={[
{ label: 'asbh1yeasbdh23848asdasd', onClick: console.log },
{ label: 'urtyruhjr374hbfdjdhuh', onClick: console.log },
]}
renderTrigger={toggleVisibility => <button onClick={toggleVisibility}>Opa</button>}
/>
</div>
)}
</DoczWrapper>
</Playground>

View File

@ -0,0 +1,84 @@
// @flow
import React, { type Node, Component } from 'react';
import styled from 'styled-components';
/* eslint-disable import/no-extraneous-dependencies */
// $FlowFixMe
import { darken } from 'polished';
import Popover from 'react-popover';
import ClickOutside from 'react-click-outside';
import { TextComponent } from './text';
/* eslint-disable max-len */
const MenuWrapper = styled.div`
background-image: ${props => `linear-gradient(to right, ${darken(0.005, props.theme.colors.activeItem)}, ${props.theme.colors.activeItem})`};
padding: 10px;
border-radius: 10px;
position: absolute;
margin-left: -10px;
`;
const MenuItem = styled.button`
outline: none;
background-color: transparent;
border: none;
border-bottom-style: solid;
border-bottom-color: ${props => props.theme.colors.text};
border-bottom-width: 1px;
padding: 15px 0;
cursor: pointer;
width: 100%;
&:hover {
opacity: 0.9;
}
`;
const PopoverWithStyle = styled(Popover)`
& > .Popover-tip {
fill: ${props => props.theme.colors.activeItem};
}
`;
type Props = {
renderTrigger: (() => void) => Node,
options: Array<{ label: string, onClick: () => void }>,
};
type State = {
isOpen: boolean,
};
export class Dropdown extends Component<Props, State> {
state = {
isOpen: false,
};
toggleVisibility() {
this.setState(state => ({ isOpen: !state.isOpen }));
}
render() {
return (
<PopoverWithStyle
isOpen={this.state.isOpen}
preferPlace='below'
body={[
<ClickOutside onClickOutside={() => this.setState(() => ({ isOpen: false }))}>
<MenuWrapper>
<TextComponent value='All Addresses' isBold />
{this.props.options.map(({ label, onClick }) => (
<MenuItem onClick={onClick}>
<TextComponent value={label} />
</MenuItem>
))}
</MenuWrapper>
</ClickOutside>,
]}
tipSize={10}
>
{this.props.renderTrigger(() => this.toggleVisibility())}
</PopoverWithStyle>
);
}
}