@ -1,30 +1,65 @@
import React from 'react'
import React , { Component } from 'react'
import Downshift from 'downshift'
import Downshift from 'downshift'
import matchSorter from 'match-sorter'
import ArrowDown from './svg/Arrowdown'
import ArrowDown from './svg/Arrowdown'
import CheckMark from './svg/Checkmark'
import CheckMark from './svg/Checkmark'
import { COLORS } from '../lib/constants'
import { COLORS } from '../lib/constants'
const Dropdown = ( { button , color , list , selected , onChange } ) => {
class Dropdown extends Component {
state = {
inputValue : this . props . selected . name ,
itemsToShow : this . props . list
}
userInputtedValue = ''
onUserAction = changes => {
this . setState ( ( { inputValue , itemsToShow } ) => {
const clearUserInput = changes . hasOwnProperty ( 'isOpen' )
if ( changes . hasOwnProperty ( 'inputValue' ) ) {
if ( changes . type === Downshift . stateChangeTypes . keyDownEscape ) {
inputValue = this . userInputtedValue
} else {
inputValue = changes . inputValue
this . userInputtedValue = changes . inputValue
}
}
itemsToShow = this . userInputtedValue
? matchSorter ( this . props . list , this . userInputtedValue , { keys : [ 'name' ] } )
: this . props . list
if (
changes . hasOwnProperty ( 'highlightedIndex' ) &&
( changes . type === Downshift . stateChangeTypes . keyDownArrowUp ||
changes . type === Downshift . stateChangeTypes . keyDownArrowDown )
) {
inputValue = itemsToShow [ changes . highlightedIndex ] . name
}
if ( clearUserInput ) {
this . userInputtedValue = ''
}
return { inputValue , itemsToShow }
} )
}
render ( ) {
const { button , color , list , selected , onChange } = this . props
return (
return (
< Downshift
< Downshift
render = { renderDropdown ( { button , color , list , selected } ) }
inputValue = { this . state . inputValue }
render = { renderDropdown ( { button , color , list : this . state . itemsToShow , selected } ) }
selectedItem = { selected }
selectedItem = { selected }
defaultHighlightedIndex = { list . findIndex ( it => it === selected ) }
defaultHighlightedIndex = { list . findIndex ( it => it === selected ) }
itemToString = { item => item . name }
itemToString = { item => item . name }
onChange = { onChange }
onChange = { onChange }
stateReducer = { reduceState ( list ) }
onUserAction = { this . onUserAction }
/ >
/ >
)
)
}
}
const reduceState = list => ( state , changes ) => {
switch ( changes . type ) {
case Downshift . stateChangeTypes . keyDownArrowUp :
case Downshift . stateChangeTypes . keyDownArrowDown :
return { ... changes , selectedItem : list [ changes . highlightedIndex ] }
default :
return changes
}
}
}
const renderDropdown = ( { button , color , list , selected } ) => ( {
const renderDropdown = ( { button , color , list , selected } ) => ( {
@ -43,7 +78,13 @@ const renderDropdown = ({ button, color, list, selected }) => ({
{ ... getRootProps ( { refKey : 'innerRef' } ) }
{ ... getRootProps ( { refKey : 'innerRef' } ) }
minWidth = { minWidth ( button , selected , list ) }
minWidth = { minWidth ( button , selected , list ) }
>
>
< SelectedItem { ... getButtonProps ( ) } { ... getInputProps ( ) } isOpen = { isOpen } color = { color } >
< SelectedItem
getButtonProps = { getButtonProps }
getInputProps = { getInputProps }
isOpen = { isOpen }
color = { color }
button = { button }
>
{ selectedItem . name }
{ selectedItem . name }
< / S e l e c t e d I t e m >
< / S e l e c t e d I t e m >
{ isOpen ? (
{ isOpen ? (
@ -82,12 +123,20 @@ const DropdownContainer = ({ children, innerRef, minWidth, ...rest }) => {
)
)
}
}
const SelectedItem = ( { children, isOpen , color , ... rest } ) => {
const SelectedItem = ( { getButtonProps, getInputProps , children, isOpen , color , button } ) => {
const itemColor = color || COLORS . SECONDARY
const itemColor = color || COLORS . SECONDARY
return (
return (
< span { ... rest } tabIndex = "0" className = { ` dropdown-display ${ isOpen ? 'is-open' : '' } ` } >
< span
{ ... getButtonProps ( ) }
tabIndex = "0"
className = { ` dropdown-display ${ isOpen ? 'is-open' : '' } ` }
>
{ button ? (
< span className = "dropdown-display-text" > { children } < / s p a n >
< span className = "dropdown-display-text" > { children } < / s p a n >
) : (
< input { ... getInputProps ( { placeholder : children } ) } className = "dropdown-display-text" / >
) }
< div role = "button" className = { ` dropdown-arrow ` } >
< div role = "button" className = { ` dropdown-arrow ` } >
< ArrowDown fill = { itemColor } / >
< ArrowDown fill = { itemColor } / >
< / d i v >
< / d i v >
@ -112,6 +161,11 @@ const SelectedItem = ({ children, isOpen, color, ...rest }) => {
. dropdown - display - text {
. dropdown - display - text {
flex - grow : 1 ;
flex - grow : 1 ;
color : $ { itemColor } ;
color : $ { itemColor } ;
background : transparent ;
border : none ;
outline : none ;
font - size : inherit ;
font - family : inherit ;
}
}
. is - open > . dropdown - arrow {
. is - open > . dropdown - arrow {
transform : rotate ( 180 deg ) ;
transform : rotate ( 180 deg ) ;