|
|
|
@ -4,10 +4,10 @@ import ArrowDown from './svg/Arrowdown'
|
|
|
|
|
import CheckMark from './svg/Checkmark'
|
|
|
|
|
import { COLORS } from '../lib/constants'
|
|
|
|
|
|
|
|
|
|
const Dropdown = ({ list, selected, onChange }) => {
|
|
|
|
|
const Dropdown = ({ button, color, list, selected, onChange }) => {
|
|
|
|
|
return (
|
|
|
|
|
<Downshift
|
|
|
|
|
render={renderDropdown(list)}
|
|
|
|
|
render={renderDropdown({ button, color, list, selected })}
|
|
|
|
|
selectedItem={selected}
|
|
|
|
|
defaultHighlightedIndex={list.findIndex(it => it === selected)}
|
|
|
|
|
itemToString={item => item.name}
|
|
|
|
@ -27,7 +27,7 @@ const reduceState = list => (state, changes) => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const renderDropdown = list => ({
|
|
|
|
|
const renderDropdown = ({ button, color, list, selected }) => ({
|
|
|
|
|
isOpen,
|
|
|
|
|
highlightedIndex,
|
|
|
|
|
setHighlightedIndex,
|
|
|
|
@ -39,15 +39,19 @@ const renderDropdown = list => ({
|
|
|
|
|
getItemProps
|
|
|
|
|
}) => {
|
|
|
|
|
return (
|
|
|
|
|
<DropdownContainer {...getRootProps({ refKey: 'innerRef' })} minWidth={minWidth(list)}>
|
|
|
|
|
<SelectedItem {...getButtonProps()} {...getInputProps()} isOpen={isOpen}>
|
|
|
|
|
<DropdownContainer
|
|
|
|
|
{...getRootProps({ refKey: 'innerRef' })}
|
|
|
|
|
minWidth={minWidth(button, selected, list)}
|
|
|
|
|
>
|
|
|
|
|
<SelectedItem {...getButtonProps()} {...getInputProps()} isOpen={isOpen} color={color}>
|
|
|
|
|
{selectedItem.name}
|
|
|
|
|
</SelectedItem>
|
|
|
|
|
{isOpen ? (
|
|
|
|
|
<ListItems>
|
|
|
|
|
<ListItems color={color}>
|
|
|
|
|
{list.map((item, index) => (
|
|
|
|
|
<ListItem
|
|
|
|
|
key={index}
|
|
|
|
|
color={color}
|
|
|
|
|
{...getItemProps({
|
|
|
|
|
item,
|
|
|
|
|
isSelected: selectedItem === item,
|
|
|
|
@ -77,19 +81,21 @@ const DropdownContainer = ({ children, innerRef, minWidth, ...rest }) => {
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const SelectedItem = ({ children, isOpen, ...rest }) => {
|
|
|
|
|
const SelectedItem = ({ children, isOpen, color, ...rest }) => {
|
|
|
|
|
const itemColor = color || COLORS.SECONDARY
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<span {...rest} tabIndex="0" className="dropdown-display">
|
|
|
|
|
<span className="dropdown-display-text">{children}</span>
|
|
|
|
|
<div className={`dropdown-arrow ${isOpen ? 'is-reverse' : ''}`}>
|
|
|
|
|
<ArrowDown />
|
|
|
|
|
<ArrowDown fill={itemColor} />
|
|
|
|
|
</div>
|
|
|
|
|
<style jsx>{`
|
|
|
|
|
.dropdown-display {
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
height: 100%;
|
|
|
|
|
border: 1px solid ${COLORS.SECONDARY};
|
|
|
|
|
border: 1px solid ${itemColor};
|
|
|
|
|
border-radius: 3px;
|
|
|
|
|
padding: 8px 16px;
|
|
|
|
|
outline: none;
|
|
|
|
@ -99,6 +105,7 @@ const SelectedItem = ({ children, isOpen, ...rest }) => {
|
|
|
|
|
}
|
|
|
|
|
.dropdown-display-text {
|
|
|
|
|
flex-grow: 1;
|
|
|
|
|
color: ${itemColor};
|
|
|
|
|
}
|
|
|
|
|
.dropdown-arrow.is-reverse {
|
|
|
|
|
transform: rotate(180deg);
|
|
|
|
@ -108,14 +115,14 @@ const SelectedItem = ({ children, isOpen, ...rest }) => {
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ListItems = ({ children }) => {
|
|
|
|
|
const ListItems = ({ children, color }) => {
|
|
|
|
|
return (
|
|
|
|
|
<ul className="dropdown-list">
|
|
|
|
|
{children}
|
|
|
|
|
<style jsx>{`
|
|
|
|
|
.dropdown-list {
|
|
|
|
|
margin-top: -1px;
|
|
|
|
|
border: 1px solid ${COLORS.SECONDARY};
|
|
|
|
|
border: 1px solid ${color || COLORS.SECONDARY};
|
|
|
|
|
border-radius: 0 0 3px 3px;
|
|
|
|
|
max-height: 350px;
|
|
|
|
|
overflow-y: scroll;
|
|
|
|
@ -125,7 +132,9 @@ const ListItems = ({ children }) => {
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ListItem = ({ children, isHighlighted, isSelected, ...rest }) => {
|
|
|
|
|
const ListItem = ({ children, color, isHighlighted, isSelected, ...rest }) => {
|
|
|
|
|
const itemColor = color || COLORS.SECONDARY
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<li {...rest} className="dropdown-list-item">
|
|
|
|
|
<span className="dropdown-list-item-text">{children}</span>
|
|
|
|
@ -136,22 +145,25 @@ const ListItem = ({ children, isHighlighted, isSelected, ...rest }) => {
|
|
|
|
|
align-items: center;
|
|
|
|
|
background: ${isHighlighted ? COLORS.HOVER : COLORS.BLACK};
|
|
|
|
|
padding: 8px 16px;
|
|
|
|
|
border-bottom: 1px solid ${COLORS.SECONDARY};
|
|
|
|
|
border-bottom: 1px solid ${itemColor};
|
|
|
|
|
}
|
|
|
|
|
.dropdown-list-item:hover {
|
|
|
|
|
background: ${COLORS.HOVER};
|
|
|
|
|
}
|
|
|
|
|
.dropdown-list-item-text {
|
|
|
|
|
flex-grow: 1;
|
|
|
|
|
color: ${itemColor};
|
|
|
|
|
}
|
|
|
|
|
`}</style>
|
|
|
|
|
</li>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function minWidth(list) {
|
|
|
|
|
return list.reduce((max, { name }) => {
|
|
|
|
|
const wordSize = name.length * 12
|
|
|
|
|
function minWidth(isButton, selected, list) {
|
|
|
|
|
const items = isButton ? [...list, selected] : list
|
|
|
|
|
|
|
|
|
|
return items.reduce((max, { name }) => {
|
|
|
|
|
const wordSize = name.length * 10 + 32
|
|
|
|
|
return wordSize > max ? wordSize : max
|
|
|
|
|
}, 0)
|
|
|
|
|
}
|
|
|
|
|