import React from 'react'
import PropTypes from 'prop-types'
import { asField } from 'informed'
import { Input } from 'components/UI'
import styled, { withTheme } from 'styled-components'
import Downshift from 'downshift'
import { Box } from 'rebass'
import system from '@rebass/components'
const SelectOptionList = styled.ul`
padding: 0;
margin-top: 0;
position: absolute;
z-index: 2;
width: 100%;
max-height: 20rem;
overflow-y: auto;
overflow-x: hidden;
outline: 0;
transition: opacity 0.1s ease;
border: ${props => (props.isOpen ? null : 'none')};
background-color: ${props => props.theme.colors.lightestBackground};
`
const SelectOptionItem = styled(
system(
{
extend: Box,
as: 'li',
p: 3,
backgroundColor: 'lightestBackground'
},
'space',
'color'
)
)`
outline: none;
cursor: pointer;
`
const ControllerButton = styled('button')({
backgroundColor: 'transparent',
border: 'none',
position: 'absolute',
right: '5px',
top: 0,
cursor: 'pointer',
width: '47px',
display: 'flex',
flexDirection: 'column',
height: '50px',
justifyContent: 'center',
alignItems: 'center',
outline: 'none'
})
const ArrowIcon = ({ isOpen }) => (
)
ArrowIcon.propTypes = {
isOpen: PropTypes.bool
}
const itemToString = item => (item ? item.value : '')
/**
* @render react
* @name Select
*/
class Select extends React.PureComponent {
static displayName = 'Select'
static defaultProps = {
items: []
}
render() {
const { fieldApi, items, theme, ...rest } = this.props
return (
fieldApi.setValue(item.value)}
// If an invalid value has been typed into the input, set it back to the currently slected item.
onInputValueChange={(inputValue, stateAndHelpers) => {
if (inputValue && inputValue !== itemToString(stateAndHelpers.selectedItem)) {
fieldApi.setValue(itemToString(stateAndHelpers.selectedItem))
}
}}
>
{({
getInputProps,
getItemProps,
getMenuProps,
getToggleButtonProps,
isOpen,
highlightedIndex,
selectedItem,
openMenu,
closeMenu,
toggleMenu
}) => (
{isOpen
? items.map((item, index) => (
{item.label || item.value}
))
: null}
)}
)
}
}
export default withTheme(asField(Select))