|
|
@ -1,6 +1,7 @@
|
|
|
|
import React from 'react'
|
|
|
|
import React from 'react'
|
|
|
|
import Downshift from 'downshift'
|
|
|
|
import Downshift from 'downshift'
|
|
|
|
import matchSorter from 'match-sorter'
|
|
|
|
import matchSorter from 'match-sorter'
|
|
|
|
|
|
|
|
import VisuallyHidden from '@reach/visually-hidden'
|
|
|
|
import { Down as ArrowDown } from './svg/Arrows'
|
|
|
|
import { Down as ArrowDown } from './svg/Arrows'
|
|
|
|
import CheckMark from './svg/Checkmark'
|
|
|
|
import CheckMark from './svg/Checkmark'
|
|
|
|
import { COLORS } from '../lib/constants'
|
|
|
|
import { COLORS } from '../lib/constants'
|
|
|
@ -56,37 +57,7 @@ class Dropdown extends React.PureComponent {
|
|
|
|
|
|
|
|
|
|
|
|
userInputtedValue = ''
|
|
|
|
userInputtedValue = ''
|
|
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
renderDropdown = ({
|
|
|
|
const { innerRef, color, selected, onChange, itemWrapper, icon, disableInput } = this.props
|
|
|
|
|
|
|
|
const { itemsToShow, inputValue } = this.state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const minWidth = calcMinWidth(itemsToShow)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
|
|
<Downshift
|
|
|
|
|
|
|
|
ref={innerRef}
|
|
|
|
|
|
|
|
inputValue={inputValue}
|
|
|
|
|
|
|
|
selectedItem={selected}
|
|
|
|
|
|
|
|
defaultHighlightedIndex={itemsToShow.findIndex(it => it === selected)}
|
|
|
|
|
|
|
|
itemToString={item => item.name}
|
|
|
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
|
|
|
onUserAction={this.onUserAction}
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
{renderDropdown({
|
|
|
|
|
|
|
|
color,
|
|
|
|
|
|
|
|
list: itemsToShow,
|
|
|
|
|
|
|
|
selected,
|
|
|
|
|
|
|
|
minWidth,
|
|
|
|
|
|
|
|
itemWrapper,
|
|
|
|
|
|
|
|
icon,
|
|
|
|
|
|
|
|
disableInput
|
|
|
|
|
|
|
|
})}
|
|
|
|
|
|
|
|
</Downshift>
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const renderDropdown = ({ color, list, minWidth, itemWrapper, icon, disableInput }) => ({
|
|
|
|
|
|
|
|
isOpen,
|
|
|
|
isOpen,
|
|
|
|
highlightedIndex,
|
|
|
|
highlightedIndex,
|
|
|
|
selectedItem,
|
|
|
|
selectedItem,
|
|
|
@ -94,9 +65,16 @@ const renderDropdown = ({ color, list, minWidth, itemWrapper, icon, disableInput
|
|
|
|
getToggleButtonProps,
|
|
|
|
getToggleButtonProps,
|
|
|
|
getInputProps,
|
|
|
|
getInputProps,
|
|
|
|
getItemProps
|
|
|
|
getItemProps
|
|
|
|
}) => {
|
|
|
|
}) => {
|
|
|
|
|
|
|
|
const { list, color, itemWrapper, icon, disableInput, title } = this.props
|
|
|
|
|
|
|
|
const { itemsToShow } = this.state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const minWidth = calcMinWidth(itemsToShow)
|
|
|
|
|
|
|
|
const labelId = title ? `${title.toLowerCase()}-dropdown` : undefined
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<DropdownContainer {...getRootProps({ refKey: 'innerRef' })} minWidth={minWidth}>
|
|
|
|
<DropdownContainer {...getRootProps({ refKey: 'innerRef' })} minWidth={minWidth}>
|
|
|
|
|
|
|
|
{title && <VisuallyHidden id={labelId}>{title}</VisuallyHidden>}
|
|
|
|
<DropdownIcon isOpen={isOpen}>{icon}</DropdownIcon>
|
|
|
|
<DropdownIcon isOpen={isOpen}>{icon}</DropdownIcon>
|
|
|
|
<SelectedItem
|
|
|
|
<SelectedItem
|
|
|
|
getToggleButtonProps={getToggleButtonProps}
|
|
|
|
getToggleButtonProps={getToggleButtonProps}
|
|
|
@ -109,7 +87,7 @@ const renderDropdown = ({ color, list, minWidth, itemWrapper, icon, disableInput
|
|
|
|
{selectedItem.name}
|
|
|
|
{selectedItem.name}
|
|
|
|
</SelectedItem>
|
|
|
|
</SelectedItem>
|
|
|
|
{isOpen ? (
|
|
|
|
{isOpen ? (
|
|
|
|
<ListItems color={color}>
|
|
|
|
<List color={color}>
|
|
|
|
{list.map((item, index) => (
|
|
|
|
{list.map((item, index) => (
|
|
|
|
<ListItem
|
|
|
|
<ListItem
|
|
|
|
key={index}
|
|
|
|
key={index}
|
|
|
@ -125,10 +103,33 @@ const renderDropdown = ({ color, list, minWidth, itemWrapper, icon, disableInput
|
|
|
|
{item.name}
|
|
|
|
{item.name}
|
|
|
|
</ListItem>
|
|
|
|
</ListItem>
|
|
|
|
))}
|
|
|
|
))}
|
|
|
|
</ListItems>
|
|
|
|
</List>
|
|
|
|
) : null}
|
|
|
|
) : null}
|
|
|
|
</DropdownContainer>
|
|
|
|
</DropdownContainer>
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
|
|
|
const { innerRef, selected, onChange, title } = this.props
|
|
|
|
|
|
|
|
const { itemsToShow, inputValue } = this.state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const labelId = title ? `${title.toLowerCase()}-dropdown` : undefined
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
|
|
<Downshift
|
|
|
|
|
|
|
|
ref={innerRef}
|
|
|
|
|
|
|
|
inputValue={inputValue}
|
|
|
|
|
|
|
|
selectedItem={selected}
|
|
|
|
|
|
|
|
defaultHighlightedIndex={itemsToShow.findIndex(it => it === selected)}
|
|
|
|
|
|
|
|
itemToString={item => item.name}
|
|
|
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
|
|
|
onUserAction={this.onUserAction}
|
|
|
|
|
|
|
|
labelId={labelId}
|
|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
{this.renderDropdown}
|
|
|
|
|
|
|
|
</Downshift>
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const DropdownContainer = ({ children, innerRef, minWidth, ...rest }) => {
|
|
|
|
const DropdownContainer = ({ children, innerRef, minWidth, ...rest }) => {
|
|
|
@ -173,9 +174,9 @@ const DropdownIcon = ({ children, isOpen }) => {
|
|
|
|
</style>
|
|
|
|
</style>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return null
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const SelectedItem = ({
|
|
|
|
const SelectedItem = ({
|
|
|
@ -245,7 +246,7 @@ const SelectedItem = ({
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const ListItems = ({ children, color }) => {
|
|
|
|
const List = ({ children, color }) => {
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<ul role="listbox" className="dropdown-list">
|
|
|
|
<ul role="listbox" className="dropdown-list">
|
|
|
|
{children}
|
|
|
|
{children}
|
|
|
|